-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Description
This has come up at least a couple of times:
- https://discourse.gohugo.io/t/localize-utc-time-with-specific-timezone/54173
- https://discourse.gohugo.io/t/present-a-date-object-in-different-timezones/41863
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:
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
}LGro
