Skip to content

Posix TZ string #140

@hartmamt

Description

@hartmamt

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>"
		},
	)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions