Hello, how can I have the epoch millisecond of my time zone and not UTC with kotlinx-datetime ?
✅ 1
m
Michael Paus
08/12/2024, 12:48 PM
There is no such thing as “the epoch millisecond of my time zone and not UTC”. By definition the epoch milliseconds are always based on UTC time but you can derive your local date/time from that.
https://en.wikipedia.org/wiki/Unix_time
👀 1
c
CLOVIS
08/12/2024, 1:06 PM
Please describe what you want to achieve exactly, #C01923PC6A0
r
Raphael TEYSSANDIER
08/12/2024, 2:11 PM
I managed to succeed with
Copy code
fun Clock.System.nowTimeZone(
timeZone: TimeZone = TimeZone.currentSystemDefault()
): Long {
val now = now()
val offset = now.offsetIn(timeZone)
return now.toEpochMilliseconds() + offset.totalSeconds.times(1000)
}
c
CLOVIS
08/12/2024, 2:13 PM
I don't know what you want to do, but that's not equivalent to the number of seconds that happened since Jan 1st 1970 in your timezone. The offset is not constant over time; maybe that place has changed timezones since 1970 (happened to surprisingly more places than you'd think). Maybe you're currently in the summer time.
It depends what you want to use that for. But if you have a counter that has counted the number of seconds, this is certainly not the value you'd expect.
CLOVIS
08/12/2024, 2:14 PM
Also, extension functions should be on
Clock
(not
Clock.System
) to make them easier to test.
thank you color 1
r
Raphael TEYSSANDIER
08/12/2024, 2:16 PM
I want a timestamp of my zone. I know it's wrong. I'll changed soon to use ISO8601