From c429517c6bdb25c19e040a43238b3904847efcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Sun, 23 Jan 2022 14:46:02 +0100 Subject: [PATCH] Fix incorrect timezone for natural dates (#156) --- CHANGELOG.md | 1 + internal/util/date/date.go | 24 +++++++++++++++++++----- tests/cmd-new.tesh | 16 +++++++++++++++- tests/fixtures/new/.zk/config.toml | 4 ++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 826b6f0..af61761 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. ### Fixed * [#126](https://github.com/mickael-menu/zk/issues/126) Embedded image links shown as not found. +* [#152](https://github.com/mickael-menu/zk/issues/152) Incorrect timezone for natural dates. ## 0.9.0 diff --git a/internal/util/date/date.go b/internal/util/date/date.go index ec9cbc2..d9ccd7d 100644 --- a/internal/util/date/date.go +++ b/internal/util/date/date.go @@ -1,7 +1,6 @@ package date import ( - "strconv" "time" "github.com/tj/go-naturaldate" @@ -41,11 +40,26 @@ func TimeFromNatural(date string) (time.Time, error) { if date == "" { return time.Now(), nil } - if i, err := strconv.ParseInt(date, 10, 0); err == nil && i >= 1000 && i < 5000 { - return time.Date(int(i), time.January, 0, 0, 0, 0, 0, time.UTC), nil - } if t, err := time.Parse(time.RFC3339, date); err == nil { return t, nil } - return naturaldate.Parse(date, time.Now().UTC(), naturaldate.WithDirection(naturaldate.Past)) + if t, err := time.ParseInLocation("2006-01-02T15:04:05", date, time.Local); err == nil { + return t, nil + } + if t, err := time.ParseInLocation("2006-01-02T15:04", date, time.Local); err == nil { + return t, nil + } + if t, err := time.ParseInLocation("2006-01-02", date, time.Local); err == nil { + return t, nil + } + if t, err := time.ParseInLocation("2006-01", date, time.Local); err == nil { + return t, nil + } + if t, err := time.ParseInLocation("2006", date, time.Local); err == nil { + return t, nil + } + if t, err := time.ParseInLocation("15:04", date, time.Local); err == nil { + return t, nil + } + return naturaldate.Parse(date, time.Now(), naturaldate.WithDirection(naturaldate.Past)) } diff --git a/tests/cmd-new.tesh b/tests/cmd-new.tesh index c4db404..04fa258 100644 --- a/tests/cmd-new.tesh +++ b/tests/cmd-new.tesh @@ -60,12 +60,26 @@ $ EDITOR="echo 'edit'" zk new --title "Edit" $ EDITOR="echo 'edit'" zk new --title "Print path" --print-path >{{working-dir}}/print-path.md -# Set explicitely today's date. +# Set explicitely today's date (natural dates). $ zk new --group date --date "January 2nd" --dry-run 2>{{working-dir}}/02-01.md $ zk new --group date --date "December 24th" --dry-run 2>{{working-dir}}/24-12.md +# Set explicitely today's date (RFC 3339) +$ zk new --group date-raw --date "2022-01-23T13:55:48+01:00" --dry-run +2>{{working-dir}}/2022-01-23 13:55:48 +0100 {{match ".+"}}.md +$ zk new --group date-raw --date "2022-02-17T17:53:12" --dry-run +2>{{working-dir}}/2022-02-17 17:53:12 {{match ".+"}}.md +$ zk new --group date-raw --date "2022-02-17T17:53" --dry-run +2>{{working-dir}}/2022-02-17 17:53:00 {{match ".+"}}.md +$ zk new --group date-raw --date "2022-02-17" --dry-run +2>{{working-dir}}/2022-02-17 00:00:00 {{match ".+"}}.md +$ zk new --group date-raw --date "2022-02" --dry-run +2>{{working-dir}}/2022-02-01 00:00:00 {{match ".+"}}.md +$ zk new --group date-raw --date "2022" --dry-run +2>{{working-dir}}/2022-01-01 00:00:00 {{match ".+"}}.md + # Dry run doesn't write the note. $ zk new --dry-run --title "Dry run" ># Dry run diff --git a/tests/fixtures/new/.zk/config.toml b/tests/fixtures/new/.zk/config.toml index 1fdafe3..60b1892 100644 --- a/tests/fixtures/new/.zk/config.toml +++ b/tests/fixtures/new/.zk/config.toml @@ -6,6 +6,10 @@ filename = "{{slug title}}" template = "empty.md" filename = "{{date now '%d-%m'}}" +[group.date-raw.note] +template = "empty.md" +filename = "{{now}}" + [group.handlebars.note] template = "handlebars.md"