Skip to content

Commit

Permalink
fix missing return in zoneddatetime parse. remove default timezoning
Browse files Browse the repository at this point in the history
  • Loading branch information
seljabali committed Sep 5, 2021
1 parent ee53dff commit 2618843
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -24,9 +24,9 @@ val result = "06/07/2021".parseLocalDate(format = "MM/dd/yyyy")
val result = "2021-10-04T10:10:00+0000".parseZonedDateTime()

// Maintain original time zone
val result = "2021-10-04T10:10:00+0000".parseZonedDateTime(convertToDefaultTimeZone = false)
val result = "2021-10-04T10:10:00+0000".parseZonedDateTime(useSystemTimeZone = false)

// Parse LocalDate, ZonedDateTime conversion, system time zone conversion
// Parse LocalDate, convert to ZonedDateTime, using system time zone
val result = "2021-06-07".parseZonedDateTime()
```
#### 2. Creation
Expand All @@ -40,6 +40,7 @@ val result = ZonedDateTimeUtil.new(1325134800000)
```kotlin
// Day
val result = dateA.compareDay(dateB)
val result = dateA.getDayDifference(dateB)
val result = dateA.isAfterEqualDay(dateB)

// Year
Expand All @@ -48,11 +49,12 @@ val result = dateA.isBeforeYear(dateB)

// Month
val result = dateA.compareMonth(dateB)
val result = dateA.isEqualMonth(dateB)
val result = dateA.getMonthDifference(dateB)
val result = dateA.isEqualMonth(dateB)

// Time
val result = dateA.compareTime(dateB)
val result = dateA.getMinuteDifference(dateB)
val result = dateA.isAfterEqualTime(dateB)
```

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/localtime/LocalTimes.kt
Expand Up @@ -3,6 +3,7 @@ package localtime
import java.time.LocalTime

object LocalTimes {
val startOfDay: LocalTime get() = LocalTime.MIN
val endOfDay: LocalTime get() = LocalTime.MAX
val startOfDay: LocalTime = LocalTime.MIN
val noon: LocalTime = LocalTime.NOON
val endOfDay: LocalTime = LocalTime.MAX
}
17 changes: 3 additions & 14 deletions src/main/kotlin/zoneddatetime/ZonedDateTimeUtil.kt
@@ -1,24 +1,13 @@
package zoneddatetime

import java.time.DateTimeException
import java.time.Instant
import java.time.LocalDateTime
import java.time.Month
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.zone.ZoneRulesException

object ZonedDateTimeUtil {

fun getDefaultZoneId(): ZoneId =
try {
ZoneId.systemDefault()
} catch (e: DateTimeException) {
null
} catch (e: ZoneRulesException) {
null
} ?: ZoneId.of("America/Montreal")

fun new(
year: Int,
month: Int,
Expand All @@ -37,7 +26,7 @@ object ZonedDateTimeUtil {
second,
nano
)
return ZonedDateTime.of(localDateTime, getDefaultZoneId())
return ZonedDateTime.of(localDateTime, ZoneId.systemDefault())
}

fun new(
Expand All @@ -59,10 +48,10 @@ object ZonedDateTimeUtil {
second,
nano
)
return ZonedDateTime.of(localDateTime, getDefaultZoneId())
return ZonedDateTime.of(localDateTime, ZoneId.systemDefault())
}

fun new(millis: Long): ZonedDateTime = Instant.ofEpochMilli(millis).atZone(getDefaultZoneId())
fun new(millis: Long): ZonedDateTime = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault())

fun isLeapYear(year: Int): Boolean =
when {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/zoneddatetime/ZonedDateTimes.kt
Expand Up @@ -10,10 +10,11 @@ import java.time.DayOfWeek.SUNDAY
import java.time.DayOfWeek.THURSDAY
import java.time.DayOfWeek.TUESDAY
import java.time.DayOfWeek.WEDNESDAY
import java.time.ZoneId
import java.time.ZonedDateTime

object ZonedDateTimes {
val now: ZonedDateTime get() = ZonedDateTime.now(ZonedDateTimeUtil.getDefaultZoneId())
val now: ZonedDateTime get() = ZonedDateTime.now(ZoneId.systemDefault())
val today: ZonedDateTime get() = now.atStartOfDay()
val yesterday: ZonedDateTime get() = today.minusDays(1)
val tomorrow: ZonedDateTime get() = today.plusDays(1)
Expand Down
Expand Up @@ -2,24 +2,25 @@ package zoneddatetime.extensions

import localdatetime.extensions.parseLocalDateTime
import zoneddatetime.ZonedDateTimeUtil
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException

fun String.parseZonedDateTime(
format: String? = null,
convertToDefaultTimeZone: Boolean = true
useSystemTimeZone: Boolean = true
): ZonedDateTime? {
val zonedDateTime = parseZonedDateTimeHelper(this, format)
if (zonedDateTime != null) {
if (convertToDefaultTimeZone) {
zonedDateTime.withZoneSameInstant(ZonedDateTimeUtil.getDefaultZoneId())
if (useSystemTimeZone) {
return zonedDateTime.withZoneSameInstant(ZoneId.systemDefault())
}
return zonedDateTime
}
val localDateTime = this.parseLocalDateTime(format)
if (localDateTime != null) {
return ZonedDateTime.of(localDateTime, ZonedDateTimeUtil.getDefaultZoneId())
return ZonedDateTime.of(localDateTime, ZoneId.systemDefault())
}
return null
}
Expand Down

0 comments on commit 2618843

Please sign in to comment.