MCPcopy
hub / github.com/kopia/kopia / computeMaxTime

Function computeMaxTime

cli/command_restore.go:551–637  ·  view source on GitHub ↗

computeMaxTime returns the first time after the max allowed.

(timespec string)

Source from the content-addressed store, hash-verified

549
550// computeMaxTime returns the first time after the max allowed.
551func computeMaxTime(timespec string) (time.Time, error) {
552 now := clock.Now()
553
554 if timespec == "yesterday" {
555 return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local), nil
556 }
557
558 if timespec == "last-month" {
559 return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local), nil
560 }
561
562 if timespec == "last-year" {
563 return time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local), nil
564 }
565
566 if strings.HasSuffix(timespec, "-ago") {
567 ymd := timeAgoRE.FindStringSubmatch(timespec)
568 if ymd != nil {
569 t := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
570
571 years, _ := strconv.Atoi(ymd[1])
572 months, _ := strconv.Atoi(ymd[2])
573 days, _ := strconv.Atoi(ymd[3])
574
575 // +1 to compute end time of current day
576 return t.AddDate(-years, -months, -days+1), nil
577 }
578 }
579
580 // Just used as markers, the value does not really matter
581 day := 24 * time.Hour //nolint:mnd
582 month := 30 * day //nolint:mnd
583 year := 12 * month //nolint:mnd
584
585 formats := []struct {
586 format string
587 precision time.Duration
588 }{
589 // Used by kopia output
590 {"2006-01-02 15:04:05 MST", time.Second},
591 {"2006-01-02 15:04:05.000 MST", time.Millisecond},
592
593 // Others
594 {"2006-1-2T15:04:05Z07:00", time.Second},
595 {"2006-1-2T15:04:05Z0700", time.Second},
596 {"2006-1-2T15:04:05Z07", time.Second},
597 {"2006-1-2T15:04:05", time.Second},
598 {"2006-1-2T15:04Z07", time.Minute},
599 {"2006-1-2T15:04", time.Minute},
600 {"2006-1-2T15", time.Hour},
601 {"2006-1-2 15:04:05Z0700", time.Second},
602 {"2006-1-2 15:04:05Z07", time.Second},
603 {"2006-1-2 15:04:05", time.Second},
604 {"2006-1-2 15:04Z07", time.Minute},
605 {"2006-1-2 15:04", time.Minute},
606 {"2006-1-2 15", time.Hour},
607 {"2006-1-2", day},
608 {"2006-1", month},

Callers 2

createSnapshotTimeFilterFunction · 0.85

Calls 5

NowFunction · 0.92
ParseMethod · 0.80
ErrorfMethod · 0.80
ContainsMethod · 0.45
AddMethod · 0.45

Tested by 1