Skip to content

Instantly share code, notes, and snippets.

@loikein
Last active June 30, 2024 23:16
Show Gist options
  • Save loikein/9dfda2435a73da9f0304f755d9e11206 to your computer and use it in GitHub Desktop.
Save loikein/9dfda2435a73da9f0304f755d9e11206 to your computer and use it in GitHub Desktop.
Time zone conversion shortcode for Hugo
Params:
TimeFormat: "15:04"
DateFormat: "2006-01-02"
FullTimeFormat: "2006-01-02 15:04"
MIT License
Copyright (c) 2024 loikein
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
{{- /*
Manually transform time stamps to any given time zone
loikein 2024-06-10T17:39:14Z
Usage
-----
Call with %% delimiters if using in header.
(ref: https://github.com/gohugoio/hugo/issues/6081#issuecomment-554601922)
No need to set TZ in site-wide hugo.toml.
Args
----
(0) $t: timestamp, any time zone, required
Eg:
"2024-06-10T18:27:12Z"
"2018-01-29 00:00:00 +0000 UTC"
(1) $d: time zone offset, omittable if not using $format
Default: "0h"
Eg:
"+1h" = "1h"
"-1h"
"+4h30m"
(2) $format: time format, omittable
Possible values:
"time" => .Site.Params.TimeFormat (default)
"full" => .Site.Params.FullTimeFormat
"date" => .Site.Params.DateFormat
*/ -}}
{{- $t0 := .Get 0 -}}
{{- $t := time.AsTime $t0 -}}
{{- $d := .Get 1 | default "0h" -}}
{{- $format := .Get 2 | default "time" -}}
{{- /* 1. get timezone offset */ -}}
{{- $d = strings.TrimPrefix "+" $d -}}{{- /* will do nothing if no + or has - */ -}}
{{- $d := time.ParseDuration $d -}}
{{- /* 2. convert to UTC, then add time zone offset */ -}}
{{- $t = $t.UTC.Add $d -}}
{{- /* 3. decide time format */ -}}
{{- if eq $format "time" -}}
{{- $t = $t.Format .Site.Params.TimeFormat -}}
{{- else if eq $format "full" -}}
{{- $t = $t.Format .Site.Params.FullTimeFormat -}}
{{- else if eq $format "date" -}}
{{- $t = $t.Format .Site.Params.DateFormat -}}
{{- end -}}
{{- /* 4. print time */ -}}
{{- $t -}}
@loikein
Copy link
Author

loikein commented Jun 10, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment