github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/internal_helpers.go (about) 1 // Copyright © 2020. All rights reserved. 2 // Author: Ilya Stroy. 3 // Contacts: iyuryevich@pm.me, https://github.com/qioalice 4 // License: https://opensource.org/licenses/MIT 5 6 package ekatime 7 8 import ( 9 "time" 10 ) 11 12 // getBeginningOfYear returns a unix timestamp of the 13 // 00:00:01 (12:00:00 AM) time of the January 1st in the 'y' year. 14 func getBeginningOfYear(y Year) Timestamp { 15 if y >= 1970 && y <= _Table5UpperBound { 16 return _Table5[y-1970][0] 17 } 18 return Timestamp(time.Date(int(y), time.January, 1, 0, 0, 0, 0, time.UTC).Unix()) 19 } 20 21 // getBeginningOfMonth returns a unix timestamp of the 22 // 00:00:01 (12:00:00 AM) time for the 1st day of 'm' month in the 'y' year. 23 func getBeginningOfMonth(y Year, m Month) Timestamp { 24 if y >= 1970 && y <= _Table5UpperBound && m >= MONTH_JANUARY && m <= MONTH_DECEMBER { 25 return _Table5[y-1970][m-1] 26 } 27 return Timestamp(time.Date(int(y), time.Month(m), 1, 0, 0, 0, 0, time.UTC).Unix()) 28 } 29 30 // dateFromUnix returns an Year, Month and Day from the current timestamp 'ts'. 31 func dateFromUnix(t Timestamp) (y Year, m Month, d Day) { 32 ty, tm, td := time.Unix(t.I64(), 0).UTC().Date() 33 if ty > 4095 { 34 ty = 4095 35 } 36 return Year(ty), Month(tm), Day(td) 37 } 38 39 // timeFromUnix returns an Hour, Minute and Second from the current timestamp 'ts'. 40 func timeFromUnix(t Timestamp) (h Hour, m Minute, s Second) { 41 th, tm, ts := time.Unix(t.I64(), 0).UTC().Clock() 42 return Hour(th), Minute(tm), Second(ts) 43 }