pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/timeutil/example_test.go (about) 1 package timeutil 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 "time" 13 ) 14 15 // ////////////////////////////////////////////////////////////////////////////////// // 16 17 func ExamplePrettyDuration() { 18 // you can use int ... 19 fmt.Println(PrettyDuration(300)) 20 21 // ... and time.Duration types 22 fmt.Println(PrettyDuration(123456 * time.Second)) 23 24 // Output: 25 // 5 minutes 26 // 1 day 10 hours 17 minutes and 36 seconds 27 } 28 29 func ExamplePrettyDurationInDays() { 30 // you can use int ... 31 fmt.Println(PrettyDurationInDays(650)) 32 33 // ... and time.Duration types 34 fmt.Println(PrettyDurationInDays(168 * time.Hour)) 35 36 // Output: 37 // today 38 // 7 days 39 } 40 41 func ExampleParseDuration() { 42 d, _ := ParseDuration("2w3d10h20m35s") 43 44 fmt.Println(d) 45 fmt.Println(PrettyDuration(d)) 46 47 // Output: 48 // 1506035 49 // 2 weeks 3 days 10 hours 20 minutes and 35 seconds 50 } 51 52 func ExampleFormat() { 53 date := time.Date(2010, 6, 15, 15, 30, 45, 1234, time.Local) 54 55 fmt.Println(Format(date, "%A %d/%b/%Y %H:%M:%S.%N")) 56 57 // Output: 58 // Tuesday 15/Jun/2010 15:30:45.000001234 59 } 60 61 func ExampleShortDuration() { 62 fmt.Println(ShortDuration(time.Second * 85)) 63 fmt.Println(ShortDuration(3215*time.Millisecond, true)) 64 65 // Output: 66 // 1:25 67 // 0:03.215 68 } 69 70 func ExampleDurationToSeconds() { 71 fmt.Println(DurationToSeconds(2500 * time.Millisecond)) 72 73 // Output: 74 // 2.5 75 } 76 77 func ExampleSecondsToDuration() { 78 fmt.Println(SecondsToDuration(3600)) 79 80 // Output: 81 // 1h0m0s 82 } 83 84 func ExampleDate() { 85 StartDate = 1577836800 86 87 t := int64(1583020800) 88 d := TinyDate(t) 89 90 fmt.Println(t) 91 fmt.Println(d) 92 93 // Output: 94 // 1583020800 95 // 5184000 96 } 97 98 func ExamplePrevDay() { 99 d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) 100 101 fmt.Println(PrevDay(d)) 102 // Output: 103 // 2021-05-31 12:30:15 +0000 UTC 104 } 105 106 func ExamplePrevMonth() { 107 d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) 108 109 fmt.Println(PrevMonth(d)) 110 // Output: 111 // 2021-05-01 12:30:15 +0000 UTC 112 } 113 114 func ExamplePrevYear() { 115 d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) 116 117 fmt.Println(PrevYear(d)) 118 // Output: 119 // 2020-06-01 12:30:15 +0000 UTC 120 } 121 122 func ExampleNextDay() { 123 d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) 124 125 fmt.Println(NextDay(d)) 126 // Output: 127 // 2021-06-02 12:30:15 +0000 UTC 128 } 129 130 func ExampleNextMonth() { 131 d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) 132 133 fmt.Println(NextMonth(d)) 134 // Output: 135 // 2021-07-01 12:30:15 +0000 UTC 136 } 137 138 func ExampleNextYear() { 139 d := time.Date(2021, 6, 1, 12, 30, 15, 0, time.UTC) 140 141 fmt.Println(NextYear(d)) 142 // Output: 143 // 2022-06-01 12:30:15 +0000 UTC 144 } 145 146 func ExamplePrevWorkday() { 147 d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) 148 149 fmt.Println(PrevWorkday(d)) 150 // Output: 151 // 2021-06-04 12:30:15 +0000 UTC 152 } 153 154 func ExamplePrevWeekend() { 155 d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) 156 157 fmt.Println(PrevWeekend(d)) 158 // Output: 159 // 2021-06-05 12:30:15 +0000 UTC 160 } 161 162 func ExampleNextWorkday() { 163 d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) 164 165 fmt.Println(NextWorkday(d)) 166 // Output: 167 // 2021-06-07 12:30:15 +0000 UTC 168 } 169 170 func ExampleNextWeekend() { 171 d := time.Date(2021, 6, 6, 12, 30, 15, 0, time.UTC) 172 173 fmt.Println(NextWeekend(d)) 174 // Output: 175 // 2021-06-12 12:30:15 +0000 UTC 176 } 177 178 func ExampleDate_Unix() { 179 StartDate = 1577836800 180 181 d := TinyDate(1583020800) 182 183 fmt.Println(d.Unix()) 184 185 // Output: 186 // 1583020800 187 } 188 189 func ExampleDate_Time() { 190 StartDate = 1577836800 191 192 d := TinyDate(1583020800) 193 194 fmt.Println(d.Time().In(time.UTC)) 195 196 // Output: 197 // 2020-03-01 00:00:00 +0000 UTC 198 }