From 8f4873d5d799e704dcf6ab0e178862c4d7970a92 Mon Sep 17 00:00:00 2001 From: Winfried Plappert <18740761+wplapper@users.noreply.github.com> Date: Sun, 22 Nov 2026 18:12:38 +0000 Subject: [PATCH] CLI date/time related options - allow different time formats (#21867) --- changelog/unreleased/issue-2065 | 29 +++++++ cmd/restic/cmd_backup.go | 2 +- cmd/restic/cmd_find.go | 28 +------ cmd/restic/cmd_rewrite.go | 2 +- cmd/restic/cmd_rewrite_integration_test.go | 2 +- internal/ui/format.go | 33 +++++++- internal/ui/format_test.go | 88 ++++++++++++++++++++++ 7 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 changelog/unreleased/issue-2065 diff --git a/changelog/unreleased/issue-2065 b/changelog/unreleased/issue-2065 new file mode 100644 index 000000000..8ecadfec9 --- /dev/null +++ b/changelog/unreleased/issue-2065 @@ -0,0 +1,29 @@ +Enhancement: More flexible formats for backup and rewrite time related options + +Restic backup --time allowed in the past only the format "2006-01-02 15:04:05". + +`backup --time` and `rewrite --new-time` can now use the following range of date/time +formats: + +``` +y-m-d +y-m-d HH:MM +y-m-d HH:MM:SS + +d.m.y +d.m.y HH:MM +d.m.y HH:MM:SS +``` + +With additional modifiers for timezone name and timezone offset: + +``` +y-m-d HH:MM:SS MST +y-m-d HH:MM:SS -0700 + +d.m.y HH:MM:SS MST +d.m.y HH:MM:SS -0700 +``` + +https://github.com/restic/restic/issues/2065 +https://github.com/restic/restic/pull/21867 diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 63814fb93..2231ea995 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -528,7 +528,7 @@ func runBackup(ctx context.Context, opts BackupOptions, gopts global.Options, te timeStamp := time.Now() backupStart := timeStamp if opts.TimeStamp != "" { - timeStamp, err = time.ParseInLocation(global.TimeFormat, opts.TimeStamp, time.Local) + timeStamp, err = ui.ParseTime(opts.TimeStamp) if err != nil { return errors.Fatalf("error in time option: %v", err) } diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index e3e5aa631..8df46cfbe 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -103,30 +103,6 @@ type findPattern struct { ignoreCase bool } -var timeFormats = []string{ - "2006-01-02", - "2006-01-02 15:04", - "2006-01-02 15:04:05", - "2006-01-02 15:04:05 -0700", - "2006-01-02 15:04:05 MST", - "02.01.2006", - "02.01.2006 15:04", - "02.01.2006 15:04:05", - "02.01.2006 15:04:05 -0700", - "02.01.2006 15:04:05 MST", - "Mon Jan 2 15:04:05 -0700 MST 2006", -} - -func parseTime(str string) (time.Time, error) { - for _, fmt := range timeFormats { - if t, err := time.ParseInLocation(fmt, str, time.Local); err == nil { - return t, nil - } - } - - return time.Time{}, errors.Fatalf("unable to parse time: %q", str) -} - type statefulOutput struct { ListLong bool HumanReadable bool @@ -610,13 +586,13 @@ func runFind(ctx context.Context, opts FindOptions, gopts global.Options, args [ } if opts.Oldest != "" { - if pat.oldest, err = parseTime(opts.Oldest); err != nil { + if pat.oldest, err = ui.ParseTime(opts.Oldest); err != nil { return err } } if opts.Newest != "" { - if pat.newest, err = parseTime(opts.Newest); err != nil { + if pat.newest, err = ui.ParseTime(opts.Newest); err != nil { return err } } diff --git a/cmd/restic/cmd_rewrite.go b/cmd/restic/cmd_rewrite.go index 56cf3c21d..b70570d0d 100644 --- a/cmd/restic/cmd_rewrite.go +++ b/cmd/restic/cmd_rewrite.go @@ -92,7 +92,7 @@ func (sma snapshotMetadataArgs) convert() (*snapshotMetadata, error) { var timeStamp *time.Time if sma.Time != "" { - t, err := time.ParseInLocation(global.TimeFormat, sma.Time, time.Local) + t, err := ui.ParseTime(sma.Time) if err != nil { return nil, errors.Fatalf("error in time option: %v", err) } diff --git a/cmd/restic/cmd_rewrite_integration_test.go b/cmd/restic/cmd_rewrite_integration_test.go index 3e6521841..5f7103dc0 100644 --- a/cmd/restic/cmd_rewrite_integration_test.go +++ b/cmd/restic/cmd_rewrite_integration_test.go @@ -372,7 +372,7 @@ func TestRewriteWrongMetadata(t *testing.T) { }, } err := testRunRewriteOutput(t, opts, env.gopts, []string{}) - rtest.Assert(t, err != nil && strings.Contains(err.Error(), "error in time option: parsing time"), + rtest.Assert(t, err != nil && strings.Contains(err.Error(), "unable to parse time:"), "expected time string related errors, got %v", err) } diff --git a/internal/ui/format.go b/internal/ui/format.go index bc39b4ae0..63c74b893 100644 --- a/internal/ui/format.go +++ b/internal/ui/format.go @@ -3,7 +3,6 @@ package ui import ( "bytes" "encoding/json" - "errors" "fmt" "math/bits" "strconv" @@ -11,6 +10,8 @@ import ( "unicode" "golang.org/x/text/width" + + "github.com/restic/restic/internal/errors" ) func FormatBytes(c uint64) string { @@ -185,3 +186,33 @@ func wideRune(s string) (wide bool, utfsize uint) { wide = kind != width.Neutral && kind != width.EastAsianNarrow return wide, uint(size) } + +// allowed formats for ParseTime +var timeFormats = []string{ + // y-m-d variations + "2006-1-2", + "2006-1-2 15:04", + "2006-1-2 15:04:05", + "2006-1-2 15:04:05 -0700", + "2006-1-2 15:04:05 MST", + + // d.m.y variations + "2.1.2006", + "2.1.2006 15:04", + "2.1.2006 15:04:05", + "2.1.2006 15:04:05 -0700", + "2.1.2006 15:04:05 MST", + + // *nix style date/time + "Mon Jan 2 15:04:05 -0700 MST 2006", +} + +func ParseTime(str string) (time.Time, error) { + for _, fmt := range timeFormats { + if t, err := time.ParseInLocation(fmt, str, time.Local); err == nil { + return t, nil + } + } + + return time.Time{}, errors.Fatalf("unable to parse time: %q", str) +} diff --git a/internal/ui/format_test.go b/internal/ui/format_test.go index d3c4bc277..e359501ac 100644 --- a/internal/ui/format_test.go +++ b/internal/ui/format_test.go @@ -162,6 +162,94 @@ func TestTruncate(t *testing.T) { } } +func testParseTimeMayFail(t *testing.T, s string, expectError bool) error { + t.Helper() + _, err := ParseTime(s) + if expectError == (err != nil) { + return nil + } + + return err +} + +func TestParseTime(t *testing.T) { + tests := []struct { + time string + err bool + }{ + { + time: "2026-11-1", + }, + { + time: "2026-11-11 25:00:00", + err: true, + }, + { + time: "2026-11-11 23:00:00 GMT", + }, + { + time: "2026-11-11 23:00:00 UTC", + }, + { + time: "2026-11-11 23:00:00 +0200", + }, + { + time: "2026-13-1", + err: true, + }, + { + time: "2026-11-31", + err: true, + }, + { + time: "1.3.2026", + }, + { + time: "32-3-2026", + err: true, + }, + { + time: "1.3.2026 01:02", + }, + { + time: "1.3.2026 11:22:33", + }, + { + time: "1.3.2026 11:22:333", + err: true, + }, + { + time: "01.3.2026 01:02:03 GMT", + }, + { + time: "01.7.2026 01:02:03 IST", + }, + { + time: "01.3.2026 01:02:03 CET", + }, + { + time: "01.3.2026 01:02:03 CST", + err: true, + }, + { + time: "01.08.2026 12:00:01 +0200", + }, + { + time: "29.02.2027", + err: true, + }, + { + time: "29.02.2028", + }, + } + + for _, tt := range tests { + t.Run(tt.time, func(t *testing.T) { + rtest.OK(t, testParseTimeMayFail(t, tt.time, tt.err)) + }) + } +} + func benchmarkTruncate(b *testing.B, s string, w int) { for i := 0; i < b.N; i++ { Truncate(s, w)