github.com/kaydxh/golang@v0.0.131/go/time/time.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package time 23 24 import "time" 25 26 const ( 27 DayFormat = "20060102" 28 TimeMillFormat = "20060102150405.000" 29 ShortTimeFormat = "20060102150405" 30 ShortDashTimeFormat = "2006-01-02-15:04:05" 31 DefaultTimeFormat = "2006-01-02 15:04:05" 32 DefaultTimeMillFormat = "2006-01-02 15:04:05.000" 33 ) 34 35 func Now() time.Time { 36 now := time.Now() 37 return now 38 } 39 40 func NowString(layout string) string { 41 tm := Now() 42 if layout == "" { 43 layout = DefaultTimeFormat 44 } 45 46 return tm.Format(layout) 47 } 48 49 func BeginningOfDay(days int) time.Time { 50 now := time.Now() 51 theDay := now.AddDate(0, 0, days) 52 y, m, d := theDay.Date() 53 return time.Date(y, m, d, 0, 0, 0, 0, theDay.Location()) 54 } 55 56 func BeginningOfDayString(days int, layout string) string { 57 now := time.Now() 58 theDay := now.AddDate(0, 0, days) 59 y, m, d := theDay.Date() 60 61 if layout == "" { 62 layout = DefaultTimeFormat 63 } 64 return time.Date(y, m, d, 0, 0, 0, 0, theDay.Location()).Format(layout) 65 } 66 67 func EndOfDay(days int) time.Time { 68 now := time.Now() 69 theDay := now.AddDate(0, 0, days) 70 y, m, d := theDay.Date() 71 return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), theDay.Location()) 72 } 73 74 func EndOfDayString(days int, layout string) string { 75 now := time.Now() 76 theDay := now.AddDate(0, 0, days) 77 y, m, d := theDay.Date() 78 79 if layout == "" { 80 layout = DefaultTimeFormat 81 } 82 return time.Date( 83 y, 84 m, 85 d, 86 23, 87 59, 88 59, 89 int(time.Second-time.Nanosecond), 90 theDay.Location(), 91 ).Format(layout) 92 } 93 94 // Truncate only happens in UTC semantics, apparently. 95 // observed values for truncating given time with 86400 secs: 96 // 97 // before truncation: 2018/06/01 03:54:54 2018-06-01T03:18:00+09:00 98 // after truncation: 2018/06/01 03:54:54 2018-05-31T09:00:00+09:00 99 // 100 // This is really annoying when we want to truncate in local time 101 // so we hack: we take the apparent local time in the local zone, 102 // and pretend that it's in UTC. do our math, and put it back to 103 // the local zone 104 func TruncateToUTC(t time.Time, d time.Duration) time.Time { 105 if t.Location() == time.UTC { 106 return t.Truncate(d) 107 } 108 109 base := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC) 110 base = base.Truncate(d) 111 112 return time.Date( 113 base.Year(), 114 base.Month(), 115 base.Day(), 116 base.Hour(), 117 base.Minute(), 118 base.Second(), 119 base.Nanosecond(), 120 base.Location(), 121 ) 122 } 123 124 func TruncateToUTCString(t time.Time, d time.Duration, layout string) string { 125 utc := TruncateToUTC(t, d) 126 127 if layout == "" { 128 layout = DefaultTimeFormat 129 } 130 131 return utc.Format(layout) 132 }