@@ -66,44 +66,51 @@ public sealed class DateTimeUnit {
6666
6767 @Serializable(with = DateBasedDateTimeUnitSerializer ::class )
6868 public sealed class DateBased : DateTimeUnit () {
69- // TODO: investigate how to move subclasses up to DateTimeUnit scope
70- @Serializable(with = DayBasedDateTimeUnitSerializer ::class )
71- public class DayBased (public val days : Int ) : DateBased() {
72- init {
73- require(days > 0 ) { " Unit duration must be positive, but was $days days." }
74- }
69+ @Suppress(" TOPLEVEL_TYPEALIASES_ONLY" )
70+ @Deprecated(" Use DateTimeUnit.DayBased" , ReplaceWith (" DateTimeUnit.DayBased" , " kotlinx.datetime.DateTimeUnit" ))
71+ public typealias DayBased = DateTimeUnit .DayBased
72+ @Suppress(" TOPLEVEL_TYPEALIASES_ONLY" )
73+ @Deprecated(" Use DateTimeUnit.MonthBased" , ReplaceWith (" DateTimeUnit.MonthBased" , " kotlinx.datetime.DateTimeUnit" ))
74+ public typealias MonthBased = DateTimeUnit .MonthBased
75+ }
76+
77+ @Serializable(with = DayBasedDateTimeUnitSerializer ::class )
78+ public class DayBased (public val days : Int ) : DateBased() {
79+ init {
80+ require(days > 0 ) { " Unit duration must be positive, but was $days days." }
81+ }
82+
83+ override fun times (scalar : Int ): DateTimeUnit .DayBased = DateTimeUnit .DayBased (safeMultiply(days, scalar))
7584
76- override fun times (scalar : Int ): DayBased = DayBased (safeMultiply(days, scalar))
85+ override fun equals (other : Any? ): Boolean =
86+ this == = other || (other is DateTimeUnit .DayBased && this .days == other.days)
7787
78- override fun equals (other : Any? ): Boolean =
79- this == = other || (other is DayBased && this .days == other.days)
88+ override fun hashCode (): Int = days xor 0x10000
8089
81- override fun hashCode (): Int = days xor 0x10000
90+ override fun toString (): String = if (days % 7 == 0 )
91+ formatToString(days / 7 , " WEEK" )
92+ else
93+ formatToString(days, " DAY" )
94+ }
8295
83- override fun toString (): String = if (days % 7 == 0 )
84- formatToString(days / 7 , " WEEK " )
85- else
86- formatToString(days, " DAY " )
96+ @Serializable(with = MonthBasedDateTimeUnitSerializer :: class )
97+ public class MonthBased ( public val months : Int ) : DateBased() {
98+ init {
99+ require(months > 0 ) { " Unit duration must be positive, but was $months months. " }
87100 }
88- @Serializable(with = MonthBasedDateTimeUnitSerializer ::class )
89- public class MonthBased (public val months : Int ) : DateBased() {
90- init {
91- require(months > 0 ) { " Unit duration must be positive, but was $months months." }
92- }
93101
94- override fun times (scalar : Int ): MonthBased = MonthBased (safeMultiply(months, scalar))
102+ override fun times (scalar : Int ): DateTimeUnit . MonthBased = DateTimeUnit . MonthBased (safeMultiply(months, scalar))
95103
96- override fun equals (other : Any? ): Boolean =
97- this == = other || (other is MonthBased && this .months == other.months)
104+ override fun equals (other : Any? ): Boolean =
105+ this == = other || (other is DateTimeUnit . MonthBased && this .months == other.months)
98106
99- override fun hashCode (): Int = months xor 0x20000
107+ override fun hashCode (): Int = months xor 0x20000
100108
101- override fun toString (): String = when {
102- months % 12_00 == 0 -> formatToString(months / 12_00 , " CENTURY" )
103- months % 12 == 0 -> formatToString(months / 12 , " YEAR" )
104- months % 3 == 0 -> formatToString(months / 3 , " QUARTER" )
105- else -> formatToString(months, " MONTH" )
106- }
109+ override fun toString (): String = when {
110+ months % 12_00 == 0 -> formatToString(months / 12_00 , " CENTURY" )
111+ months % 12 == 0 -> formatToString(months / 12 , " YEAR" )
112+ months % 3 == 0 -> formatToString(months / 3 , " QUARTER" )
113+ else -> formatToString(months, " MONTH" )
107114 }
108115 }
109116
@@ -117,11 +124,11 @@ public sealed class DateTimeUnit {
117124 public val SECOND : TimeBased = MILLISECOND * 1000
118125 public val MINUTE : TimeBased = SECOND * 60
119126 public val HOUR : TimeBased = MINUTE * 60
120- public val DAY : DateBased . DayBased = DateBased . DayBased (days = 1 )
121- public val WEEK : DateBased . DayBased = DAY * 7
122- public val MONTH : DateBased . MonthBased = DateBased . MonthBased (months = 1 )
123- public val QUARTER : DateBased . MonthBased = MONTH * 3
124- public val YEAR : DateBased . MonthBased = MONTH * 12
125- public val CENTURY : DateBased . MonthBased = YEAR * 100
127+ public val DAY : DayBased = DayBased (days = 1 )
128+ public val WEEK : DayBased = DAY * 7
129+ public val MONTH : MonthBased = MonthBased (months = 1 )
130+ public val QUARTER : MonthBased = MONTH * 3
131+ public val YEAR : MonthBased = MONTH * 12
132+ public val CENTURY : MonthBased = YEAR * 100
126133 }
127134}
0 commit comments