Howdy Kotlin friends :wave: I've been recently wo...
# feed
s
Howdy Kotlin friends πŸ‘‹ I've been recently working on Kotlinx Datetime Fun, a library aiming to remove the need in ever write your own datetime util method. Project is now currently used production and at a point worth sharing with you all. Feedback & contributions is most welcome! Let's go team Kotlin!
h
Sorry to say it but your LocalDate arithmetic is very naive. The reason to not include it in the official library is to handle all the edge cases, and there are many edge cases, the most common is day light saving. On this day, the day does not have 86.000 seconds. So you need to always pass a time zone and don't handle the calculation by ourself but delegate it to the system/database.
s
Thanks for the feedback @hfhbd, mind specifying which method you're referring to exactly?
h
c
@hfhbd AFAIK leap seconds are not implemented by kotlinx-datetime
But I agree,
LocalTime.plusHours()
& co are dangerous
@Sami Eljabali as an example; What's the time 2h after 2am? β€’ Usually, it's 4am β€’ In most of Europe, on March 29th, it's 3am That's why KotlinX.Datetime doesn't have the method, it's too easy to forget these cases
s
But local time on its own has no association with timezone and date. I'm looking at this file in particular that Philip mentioned.
Meaning 2AM + 2 hours is always 4AM right?
c
As I just said, in two weeks, it won't be
☝️ 1
s
Maybe in a developer's mind they're thinking of a zoned time
But I think I'm seeing what you're getting at
One can't help but think of local times mentally without thinking of a timezone related to it
c
All times are zoned if humans read them. If you display that information to a user, it's going to be wrong for them
πŸ‘ 1
s
Right, got you
c
That's why you can't compare two times either, your code can't know if they are from the same timezone
βž• 3
s
So I see why kotlinx datetime made everything local to avoid this
But as a result, personally, when thinking of LocalTime that's devoid of Zones, think of it abstractly
As in we're talking about time in a void sort to speak
There and then in saying 2AM + 2 hours is 4AM is perfectly fine
c
Yes, but none of the programs I write are in that imaginary world
πŸ‘ 1
If you're making a game, then probably yeah it doesn't matter
πŸ‘ 1
s
Got it, thanks for the immediate feedback
It's most appreciated πŸ™‚
@hfhbd which of the methods are offending in LocalDateTimeMutatingExtensions?
Nevermind, I think I see it
Copy code
fun LocalDateTime.minusYears(years: Int, timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    LocalDateTime(this.toLocalDate().plusYears(-years), this.toLocalTime())
fun LocalDateTime.plusYears(years: Int, timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    LocalDateTime(this.toLocalDate().plusYears(years), this.toLocalTime())

fun LocalDateTime.minusMonths(months: Int, timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    LocalDateTime(this.toLocalDate().plusMonths(-months), this.toLocalTime())
fun LocalDateTime.plusMonths(months: Int, timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    LocalDateTime(this.toLocalDate().plusMonths(months), this.toLocalTime())

fun LocalDateTime.minusDays(days: Int, timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    LocalDateTime(this.toLocalDate().plusDays(-days), this.toLocalTime())
fun LocalDateTime.plusDays(days: Int, timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    LocalDateTime(this.toLocalDate().plusDays(days), this.toLocalTime())
I take in timezone but don't actually take it into account
Where as I do with the other mutating methods
Such as:
Copy code
fun LocalDateTime.plusHours(hours: Int, timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    this.toInstant(timeZone)
        .plus(hours.hours)
        .toLocalDateTime(timeZone)
πŸ‘Œ 1
h
Yeah, please don't take the feedback negative. LocalDate, Timezones etc. are for humans only. And it is incredibly easy to shoot yourself into the foot, because it is crazy (or humans in general). It really depends on your use-case, but you always need to include the timezone if you want to interact with humans or calculate anything.
βž• 1
s
> Yeah, please don't take the feedback negative. Not at all! I signed up for this πŸ˜„ I also want to get it right
I'm a bit torn on removing local time mutations entirely, it's going to take some time for me to ingest Ivan's point and come full circle.
The fact that time-of-day calculations are time, location dependent and that rules change over location and time (updated local laws), and that certain locations do or don't take scientific updates to time-of-day (eg leap seconds and UTC, vs GMT, vs TAI and UT1 timezones). Even big companies whose bread and butter is to get these calculations right have sometimes been wrong, bugs in their software.
s
Even Sun/Oracle wasn't able to get it right, so they basically adopted the de facto standard Joda Time, which (more or less) became the new Java Time API.
πŸ‘ 2
s
Couple of releases later and made changes per everyone's feedback. Great Kotlinx DateTime learning experience that helped me too better appreciate its design. Thanks all!
πŸ™Œ 1