let’s say I want the value of “yesterday”, I can d...
# kotlinx-datetime
j
let’s say I want the value of “yesterday”, I can do something like this :
Copy code
Clock.System.now()
    .minus(1, DateTimeUnit.DAY, TimeZone.of(timeZoneId))
I just realized that
now
is also relative to my personal timezone, isn’t? I could ask for the now value of japan for example, which is way different that mine. So why doesn’t
now
take a Timezone parameter? Or did I miss something?
d
now
returns an
Instant
, which is timezone-independent and is the same for you and Japan.
j
why does
minus
et
plus
cares about the time zone then since it also returns an instant? It’s weird to me that I need to specify the timezone on the operation but not on the starting point
Is it not possible to add or substract a duration from an instant without a timezone?
d
Due to daylight savings etc, 1 day later at the same time may not always be 24 hours.
If you convert the Instant to a LocalDateTime, then you can subtract one day without specifying a timezone
d
We don't provide
LocalDateTime
arithmetic (exactly because it would be possible to use it without a time zone).
Regarding ±1 day not always being the same as 24 hours, yes, that's the reason. The semantics for ±1 day is "the moment at the next/previous day with the same time of day".
d
Oops I meant
LocalDate
🙂
d
Yeah, that's true:
Clock.System.now().toLocalDateTime(timeZone).date.minus(1, DateTimeUnit.DAY)
also allows knowing what day it was yesterday.
i
If you're interested only in the date component,
Clock.System.todayAt(timeZone)
might be of use.
j
I just realized that the time zone parameter is not required in
plus
and
minus
which make much more sense to me now hehe. I did not see this was available
Instant.minus(value: Long, unit: DateTimeUnit.TimeBased)
Thanks for the clarifications 🙂
d
Note that that function is only available for time-based units (hours, minutes, etc). If you want to subtract a day then you'll still need to specify the timezone.
j
True, I did not see that. Then I’ll just specify it 🤷