| 2674 | } |
| 2675 | |
| 2676 | func TestTimestampFlagApply_ShortenedLayouts(t *testing.T) { |
| 2677 | now := time.Now().UTC() |
| 2678 | |
| 2679 | shortenedLayoutsPrecisions := map[string]time.Duration{ |
| 2680 | time.Kitchen: time.Minute, |
| 2681 | time.Stamp: time.Second, |
| 2682 | time.StampMilli: time.Millisecond, |
| 2683 | time.StampMicro: time.Microsecond, |
| 2684 | time.StampNano: time.Nanosecond, |
| 2685 | time.TimeOnly: time.Second, |
| 2686 | "15:04": time.Minute, |
| 2687 | } |
| 2688 | |
| 2689 | // TODO: replace with maps.Keys() (go >= ), lo.Keys() if acceptable |
| 2690 | getKeys := func(m map[string]time.Duration) []string { |
| 2691 | if m == nil { |
| 2692 | return nil |
| 2693 | } |
| 2694 | |
| 2695 | keys := make([]string, 0, len(m)) |
| 2696 | for k := range m { |
| 2697 | keys = append(keys, k) |
| 2698 | } |
| 2699 | return keys |
| 2700 | } |
| 2701 | |
| 2702 | fl := TimestampFlag{ |
| 2703 | Name: "time", |
| 2704 | Config: TimestampConfig{ |
| 2705 | Layouts: getKeys(shortenedLayoutsPrecisions), |
| 2706 | }, |
| 2707 | } |
| 2708 | |
| 2709 | for layout, prec := range shortenedLayoutsPrecisions { |
| 2710 | err := fl.Set(fl.Name, now.Format(layout)) |
| 2711 | assert.NoError(t, err) |
| 2712 | assert.Equal(t, now.Truncate(prec), fl.value.Get()) |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | func TestTimestampFlagApplyValue(t *testing.T) { |
| 2717 | expectedResult, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") |