github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/value/time.go (about) 1 package value 2 3 import ( 4 "fmt" 5 "math" 6 "strings" 7 "time" 8 9 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 10 ) 11 12 const InfiniteDuration = time.Duration(math.MaxInt64) 13 14 const ( 15 secondsPerMinute uint64 = 60 16 secondsPerHour = 60 * secondsPerMinute 17 secondsPerDay = 24 * secondsPerHour 18 microsecondsPerSecond = 1e6 19 nanosecondsPerMicrosecond = 1000 20 ) 21 22 // Date format layouts described in time.Format and time.ANSIC docs. 23 const ( 24 LayoutDate = "2006-01-02" 25 LayoutDatetime = "2006-01-02T15:04:05Z" 26 LayoutTimestamp = "2006-01-02T15:04:05.000000Z" 27 LayoutTzDatetime = "2006-01-02T15:04:05" 28 LayoutTzTimestamp = "2006-01-02T15:04:05.000000" 29 ) 30 31 var epoch = time.Unix(0, 0) 32 33 // IntervalToDuration returns time.Duration from given microseconds 34 func IntervalToDuration(n int64) time.Duration { 35 return time.Duration(n) * time.Microsecond 36 } 37 38 // durationToMicroseconds returns microseconds from given time.Duration 39 func durationToMicroseconds(d time.Duration) int64 { 40 return int64(d / time.Microsecond) 41 } 42 43 // DateToTime up to 11761191-01-20 00:00:00 +0000 UTC. 44 func DateToTime(n uint32) time.Time { 45 return time.Unix(0, 0).Add(time.Hour * 24 * time.Duration(n)) 46 } 47 48 // DatetimeToTime converts seconds to time.Time 49 // Up to 2106-02-07 06:28:15 +0000 UTC. 50 func DatetimeToTime(n uint32) time.Time { 51 return time.Unix(int64(n), 0) 52 } 53 54 // TimestampToTime converts given microseconds to time.Time 55 // Up to 586524-01-19 08:01:49.000551615 +0000 UTC. 56 func TimestampToTime(n uint64) time.Time { 57 sec := n / microsecondsPerSecond 58 nsec := (n - (sec * microsecondsPerSecond)) * nanosecondsPerMicrosecond 59 60 return time.Unix(int64(sec), int64(nsec)) 61 } 62 63 func TzDateToTime(s string) (t time.Time, err error) { 64 ss := strings.Split(s, ",") 65 if len(ss) != 2 { //nolint:gomnd 66 return t, xerrors.WithStackTrace(fmt.Errorf("not found timezone location in '%s'", s)) 67 } 68 location, err := time.LoadLocation(ss[1]) 69 if err != nil { 70 return t, xerrors.WithStackTrace(err) 71 } 72 t, err = time.ParseInLocation(LayoutDate, ss[0], location) 73 if err != nil { 74 return t, xerrors.WithStackTrace(fmt.Errorf("parse '%s' failed: %w", s, err)) 75 } 76 77 return t, nil 78 } 79 80 func TzDatetimeToTime(s string) (t time.Time, err error) { 81 ss := strings.Split(s, ",") 82 if len(ss) != 2 { //nolint:gomnd 83 return t, xerrors.WithStackTrace(fmt.Errorf("not found timezone location in '%s'", s)) 84 } 85 location, err := time.LoadLocation(ss[1]) 86 if err != nil { 87 return t, xerrors.WithStackTrace(err) 88 } 89 t, err = time.ParseInLocation(LayoutTzDatetime, ss[0], location) 90 if err != nil { 91 return t, xerrors.WithStackTrace(fmt.Errorf("parse '%s' failed: %w", s, err)) 92 } 93 94 return t, nil 95 } 96 97 func TzTimestampToTime(s string) (t time.Time, err error) { 98 ss := strings.Split(s, ",") 99 if len(ss) != 2 { //nolint:gomnd 100 return t, xerrors.WithStackTrace(fmt.Errorf("not found timezone location in '%s'", s)) 101 } 102 location, err := time.LoadLocation(ss[1]) 103 if err != nil { 104 return t, xerrors.WithStackTrace(err) 105 } 106 layout := LayoutTzTimestamp 107 if strings.IndexByte(ss[0], '.') < 0 { 108 layout = LayoutTzDatetime 109 } 110 t, err = time.ParseInLocation(layout, ss[0], location) 111 if err != nil { 112 return t, xerrors.WithStackTrace(fmt.Errorf("parse '%s' failed: %w", s, err)) 113 } 114 115 return t, nil 116 }