Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type for Instant in time zone #217

Closed
Paxa opened this issue Jun 21, 2022 · 1 comment
Closed

Type for Instant in time zone #217

Paxa opened this issue Jun 21, 2022 · 1 comment

Comments

@Paxa
Copy link

Paxa commented Jun 21, 2022

Would be nice to have type that keep exact point in time and timezone that we use for formatting it, will be helpful to calculate date in readable way

something like:

val tz = TimeZone.of("Asia/Jakarta")
val today = Clock.System.todayAt(tz).toZonedDateTime(tz) // beginning of today in my time zone

today.toString()                       // => 2022-06-21T00:00:00+07:00 - (ISO 8601 formatted)

val meetTime = today + 12.hours        // => 2022-06-21T10:00:00+07:00

// other user may have different time zone settings
meetTime.toString(TimeZone.of("Europe/Moscow")) // => 2022-06-21T08:00:00+03:00

db.saveTime(meetTime.toSqlTimestamp()) // => 2022-06-21T05:00:00Z

May be there is already something like that, which I missed?

@dkhalanskyjb
Copy link
Collaborator

There's no such functionality built in at the moment, but for formatting an Instant with a chosen offset, you can do something like this:

fun Instant.toString(timeZone: TimeZone) =
    "${toLocalDateTime(timeZone)}${offsetIn(timeZone)}"

If you need the type for something other than formatting, please share it here: #163

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants