Skip to content

tpl/time: Add time.In function #13548

@jmooring

Description

@jmooring

This has come up at least a couple of times:

Example usage (contrived):

{{ $eventDate := time.AsTime "2025-03-30T11:20:00-04:00" }}
{{ $timeZoneNames := slice "America/Denver" "Australia/Adelaide" "Europe/Oslo" }}

<ul>
  {{ range $timeZoneNames }}
    <li>{{ $eventDate | time.In . | time.Format "2 Jan 2006 3:04:05 PM" }} ({{ . }})</li>
  {{ end }}
</ul>

Rendered:

Image

Proposed function:

// In returns the time t in the IANA time zone specified by timeZoneName.
// If timeZoneName is "" or "UTC", the time is returned in UTC.
// If timeZoneName is "Local", the time is returned in the system's local time zone.
// Otherwise, timeZoneName must be a valid IANA location name (e.g., "Europe/Oslo").
func (ns *Namespace) In(timeZoneName string, t time.Time) (time.Time, error) {
	location, err := time.LoadLocation(timeZoneName)
	if err != nil {
		return time.Time{}, err
	}

	return t.In(location), nil
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions