chronotope / chrono-tz

TimeZone implementations for rust-chrono from the IANA database
Other
239 stars 57 forks source link

Posix TZ string #140

Open hartmamt opened 1 year ago

hartmamt commented 1 year ago

Is there any way to pass in a time zone location and get back the posix tz string? For example:

Africa/El_Aaiun returns <GMT>0?

I've got Go code that will do this but it requires reflection.

// getFieldString uses to get the unexported value of the extends field
func getFieldString(e *time.Location, field string) string {
    r := reflect.ValueOf(e)
    f := reflect.Indirect(r).FieldByName(field)
    return f.String()
}

// ConvertFormat formats the tz string so it is readable by ESP32 devices
func convertFormat(t *time.Location) string {
    r := regexp.MustCompile(`<.*?>`)
    var count int
    return r.ReplaceAllStringFunc(
        getFieldString(t, "extend"),
        func(m string) string {
            count++
            if count == 1 {
                return "<GMT>"
            }
            return "<DST>"
        },
    )
}
djc commented 1 year ago

I don't think we currently support that. I'm not very fluent in Go code, so not sure how it synthesizes this data from what's in the TZDB.

pitdicker commented 7 months ago

This crate compiles the time zone data into a large table of transitions for a fixed set of years. The data doesn't include a TZ string like TZif files have.