github.com/damirazo/docker@v1.9.0/pkg/timeutils/utils.go (about)

     1  package timeutils
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  // GetTimestamp tries to parse given string as golang duration,
    10  // then RFC3339 time and finally as a Unix timestamp. If
    11  // any of these were successful, it returns a Unix timestamp
    12  // as string otherwise returns the given value back.
    13  // In case of duration input, the returned timestamp is computed
    14  // as the given reference time minus the amount of the duration.
    15  func GetTimestamp(value string, reference time.Time) string {
    16  	if d, err := time.ParseDuration(value); value != "0" && err == nil {
    17  		return strconv.FormatInt(reference.Add(-d).Unix(), 10)
    18  	}
    19  
    20  	var format string
    21  	if strings.Contains(value, ".") {
    22  		format = time.RFC3339Nano
    23  	} else {
    24  		format = time.RFC3339
    25  	}
    26  
    27  	loc := time.FixedZone(time.Now().Zone())
    28  	if len(value) < len(format) {
    29  		format = format[:len(value)]
    30  	}
    31  	t, err := time.ParseInLocation(format, value, loc)
    32  	if err != nil {
    33  		return value
    34  	}
    35  	return strconv.FormatInt(t.Unix(), 10)
    36  }