CLI date/time related options - allow different time formats (#21867)

This commit is contained in:
Winfried Plappert authored and GitHub committed 2026-11-22 18:12:38 +00:00
1 parent 1d80020e28
commit 8f4873d5d7
7 files changed
+154 -30

No files matched your search

+29
View File
@@ -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
+1 -1
View File
@@ -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)
}
+2 -26
View File
@@ -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
}
}
+1 -1
View File
@@ -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)
}
+1 -1
View File
@@ -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)
}
+32 -1
View File
@@ -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)
}
+88
View File
@@ -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)