-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathLocalDateTimeJvm.kt
148 lines (125 loc) · 6.13 KB
/
LocalDateTimeJvm.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/
@file:JvmName("LocalDateTimeKt")
@file:JvmMultifileClass
package kotlinx.datetime
import kotlinx.datetime.format.*
import kotlinx.datetime.internal.removeLeadingZerosFromLongYearFormLocalDateTime
import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer
import kotlinx.serialization.Serializable
import java.time.DateTimeException
import java.time.format.DateTimeParseException
import java.time.LocalDateTime as jtLocalDateTime
@Serializable(with = LocalDateTimeIso8601Serializer::class)
public actual class LocalDateTime internal constructor(
internal val value: jtLocalDateTime
) : Comparable<LocalDateTime>, java.io.Serializable {
public actual constructor(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
this(try {
jtLocalDateTime.of(year, month, day, hour, minute, second, nanosecond)
} catch (e: DateTimeException) {
throw IllegalArgumentException(e)
})
public actual constructor(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
this(year, month.number, day, hour, minute, second, nanosecond)
public actual constructor(date: LocalDate, time: LocalTime) :
this(jtLocalDateTime.of(date.value, time.value))
@Deprecated(
"Use kotlinx.datetime.Month",
ReplaceWith("LocalDateTime(year, month.toKotlinMonth(), dayOfMonth, hour, minute, second, nanosecond)")
)
public constructor(
year: Int,
month: java.time.Month,
dayOfMonth: Int,
hour: Int,
minute: Int,
second: Int = 0,
nanosecond: Int = 0
) : this(
year,
month.toKotlinMonth(),
dayOfMonth,
hour,
minute,
second,
nanosecond
)
public actual val year: Int get() = value.year
@Deprecated("Use the 'month' property instead", ReplaceWith("this.month.number"), level = DeprecationLevel.WARNING)
public actual val monthNumber: Int get() = value.monthValue
public actual val month: Month get() = value.month.toKotlinMonth()
@PublishedApi internal fun getMonth(): java.time.Month = value.month
@Deprecated("Use the 'day' property instead", ReplaceWith("this.day"), level = DeprecationLevel.WARNING)
public actual val dayOfMonth: Int get() = value.dayOfMonth
public actual val day: Int get() = value.dayOfMonth
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek.toKotlinDayOfWeek()
@PublishedApi internal fun getDayOfWeek(): java.time.DayOfWeek = value.dayOfWeek
public actual val dayOfYear: Int get() = value.dayOfYear
public actual val hour: Int get() = value.hour
public actual val minute: Int get() = value.minute
public actual val second: Int get() = value.second
public actual val nanosecond: Int get() = value.nano
public actual val date: LocalDate get() = LocalDate(value.toLocalDate()) // cache?
public actual val time: LocalTime get() = LocalTime(value.toLocalTime())
override fun equals(other: Any?): Boolean =
(this === other) || (other is LocalDateTime && this.value == other.value)
override fun hashCode(): Int = value.hashCode()
actual override fun toString(): String = value.toString()
actual override fun compareTo(other: LocalDateTime): Int = this.value.compareTo(other.value)
public actual companion object {
public actual fun parse(input: CharSequence, format: DateTimeFormat<LocalDateTime>): LocalDateTime =
if (format === Formats.ISO) {
try {
val sanitizedInput = removeLeadingZerosFromLongYearFormLocalDateTime(input.toString())
jtLocalDateTime.parse(sanitizedInput).let(::LocalDateTime)
} catch (e: DateTimeParseException) {
throw DateTimeFormatException(e)
}
} else {
format.parse(input)
}
@Deprecated("This overload is only kept for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun parse(isoString: String): LocalDateTime = parse(input = isoString)
internal actual val MIN: LocalDateTime = LocalDateTime(jtLocalDateTime.MIN)
internal actual val MAX: LocalDateTime = LocalDateTime(jtLocalDateTime.MAX)
@Suppress("FunctionName")
public actual fun Format(builder: DateTimeFormatBuilder.WithDateTime.() -> Unit): DateTimeFormat<LocalDateTime> =
LocalDateTimeFormat.build(builder)
// even though this class uses writeReplace (so serialVersionUID is not needed for a stable serialized form), a
// stable serialVersionUID means exceptions caused by deserialization of malicious streams will be consistent
// (InvalidClassException vs. InvalidObjectException, see MaliciousJvmSerializationTest)
private const val serialVersionUID: Long = -4261744960416354711L
}
public actual object Formats {
public actual val ISO: DateTimeFormat<LocalDateTime> = ISO_DATETIME
}
private fun readObject(ois: java.io.ObjectInputStream): Unit = throw java.io.InvalidObjectException(
"kotlinx.datetime.LocalDateTime must be deserialized via kotlinx.datetime.Ser"
)
private fun writeReplace(): Any = Ser(Ser.DATE_TIME_TAG, this)
}
/**
* @suppress
*/
@Deprecated(
"Use the constructor that accepts a 'day'",
ReplaceWith("LocalDateTime(year = year, month = month.toKotlinMonth(), day = dayOfMonth, hour = hour, minute = minute, second = second, nanosecond = nanosecond)"),
level = DeprecationLevel.WARNING
)
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.internal.LowPriorityInOverloadResolution
public fun LocalDateTime(
year: Int,
month: java.time.Month,
dayOfMonth: Int,
hour: Int,
minute: Int,
second: Int = 0,
nanosecond: Int = 0,
): LocalDateTime = LocalDateTime(
year = year, month = month.toKotlinMonth(), day = dayOfMonth,
hour = hour, minute = minute, second = second, nanosecond = nanosecond
)