github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/tag/date_time.go (about) 1 /* 2 Copyright 2019 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tag 18 19 import ( 20 "context" 21 "fmt" 22 "time" 23 24 "4d63.com/tz" 25 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 27 ) 28 29 const tagTime = "2006-01-02_15-04-05.999_MST" 30 31 // dateTimeTagger tags an image by the timestamp of the built image 32 // dateTimeTagger implements Tagger 33 type dateTimeTagger struct { 34 Format string 35 TimeZone string 36 timeFn func() time.Time 37 } 38 39 // NewDateTimeTagger creates a tagger from a date format and timezone. 40 func NewDateTimeTagger(format, timezone string) Tagger { 41 return &dateTimeTagger{ 42 Format: format, 43 TimeZone: timezone, 44 timeFn: time.Now, 45 } 46 } 47 48 // GenerateTag generates a tag using the current timestamp. 49 func (t *dateTimeTagger) GenerateTag(ctx context.Context, image latest.Artifact) (string, error) { 50 format := tagTime 51 if len(t.Format) > 0 { 52 format = t.Format 53 } 54 55 timezone := "Local" 56 if len(t.TimeZone) > 0 { 57 timezone = t.TimeZone 58 } 59 60 loc, err := tz.LoadLocation(timezone) 61 if err != nil { 62 return "", fmt.Errorf("bad timezone provided: %q, error: %s", timezone, err) 63 } 64 65 return t.timeFn().In(loc).Format(format), nil 66 }