github.com/gogf/gf/v2@v2.7.4/os/gtime/gtime_z_example_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gtime_test 8 9 import ( 10 "fmt" 11 12 "github.com/gogf/gf/v2/os/gtime" 13 ) 14 15 // New creates and returns a Time object with given parameter. 16 // The optional parameter can be type of: time.Time/*time.Time, string or integer. 17 func ExampleSetTimeZone() { 18 gtime.SetTimeZone("Asia/Shanghai") 19 fmt.Println(gtime.Datetime()) 20 // May Output: 21 // 2018-08-08 08:08:08 22 } 23 24 func ExampleTimestamp() { 25 fmt.Println(gtime.Timestamp()) 26 27 // May Output: 28 // 1636359252 29 } 30 31 func ExampleTimestampMilli() { 32 fmt.Println(gtime.TimestampMilli()) 33 34 // May Output: 35 // 1636359252000 36 } 37 38 func ExampleTimestampMicro() { 39 fmt.Println(gtime.TimestampMicro()) 40 41 // May Output: 42 // 1636359252000000 43 } 44 45 func ExampleTimestampNano() { 46 fmt.Println(gtime.TimestampNano()) 47 48 // May Output: 49 // 1636359252000000000 50 } 51 52 func ExampleTimestampStr() { 53 fmt.Println(gtime.TimestampStr()) 54 55 // May Output: 56 // 1636359252 57 } 58 59 func ExampleDate() { 60 fmt.Println(gtime.Date()) 61 62 // May Output: 63 // 2006-01-02 64 } 65 66 func ExampleDatetime() { 67 fmt.Println(gtime.Datetime()) 68 69 // May Output: 70 // 2006-01-02 15:04:05 71 } 72 73 func ExampleISO8601() { 74 fmt.Println(gtime.ISO8601()) 75 76 // May Output: 77 // 2006-01-02T15:04:05-07:00 78 } 79 80 func ExampleRFC822() { 81 fmt.Println(gtime.RFC822()) 82 83 // May Output: 84 // Mon, 02 Jan 06 15:04 MST 85 } 86 87 func ExampleStrToTime() { 88 res, _ := gtime.StrToTime("2006-01-02T15:04:05-07:00", "Y-m-d H:i:s") 89 fmt.Println(res) 90 91 // May Output: 92 // 2006-01-02 15:04:05 93 } 94 95 func ExampleConvertZone() { 96 res, _ := gtime.ConvertZone("2006-01-02 15:04:05", "Asia/Tokyo", "Asia/Shanghai") 97 fmt.Println(res) 98 99 // Output: 100 // 2006-01-02 16:04:05 101 } 102 103 func ExampleStrToTimeFormat() { 104 res, _ := gtime.StrToTimeFormat("2006-01-02 15:04:05", "Y-m-d H:i:s") 105 fmt.Println(res) 106 107 // Output: 108 // 2006-01-02 15:04:05 109 } 110 111 func ExampleStrToTimeLayout() { 112 res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02") 113 fmt.Println(res) 114 115 // Output: 116 // 2018-08-08 00:00:00 117 } 118 119 // ParseDuration parses a duration string. 120 // A duration string is a possibly signed sequence of 121 // decimal numbers, each with optional fraction and a unit suffix, 122 // such as "300ms", "-1.5h", "1d" or "2h45m". 123 // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d". 124 // 125 // Very note that it supports unit "d" more than function time.ParseDuration. 126 func ExampleParseDuration() { 127 res, _ := gtime.ParseDuration("+10h") 128 fmt.Println(res) 129 130 // Output: 131 // 10h0m0s 132 } 133 134 func ExampleTime_Format() { 135 gt1 := gtime.New("2018-08-08 08:08:08") 136 137 fmt.Println(gt1.Format("Y-m-d")) 138 fmt.Println(gt1.Format("l")) 139 fmt.Println(gt1.Format("F j, Y, g:i a")) 140 fmt.Println(gt1.Format("j, n, Y")) 141 fmt.Println(gt1.Format("h-i-s, j-m-y, it is w Day z")) 142 fmt.Println(gt1.Format("D M j G:i:s T Y")) 143 144 // Output: 145 // 2018-08-08 146 // Wednesday 147 // August 8, 2018, 8:08 am 148 // 8, 8, 2018 149 // 08-08-08, 8-08-18, 0831 0808 3 Wedam18 219 150 // Wed Aug 8 8:08:08 CST 2018 151 } 152 153 func ExampleTime_FormatNew() { 154 gt1 := gtime.New("2018-08-08 08:08:08") 155 156 fmt.Println(gt1.FormatNew("Y-m-d")) 157 fmt.Println(gt1.FormatNew("Y-m-d H:i")) 158 159 // Output: 160 // 2018-08-08 00:00:00 161 // 2018-08-08 08:08:00 162 } 163 164 func ExampleTime_FormatTo() { 165 gt1 := gtime.New("2018-08-08 08:08:08") 166 167 fmt.Println(gt1.FormatTo("Y-m-d")) 168 169 // Output: 170 // 2018-08-08 00:00:00 171 } 172 173 func ExampleTime_Layout() { 174 gt1 := gtime.New("2018-08-08 08:08:08") 175 176 fmt.Println(gt1.Layout("2006-01-02")) 177 178 // Output: 179 // 2018-08-08 180 } 181 182 func ExampleTime_LayoutNew() { 183 gt1 := gtime.New("2018-08-08 08:08:08") 184 185 fmt.Println(gt1.LayoutNew("2006-01-02")) 186 187 // Output: 188 // 2018-08-08 00:00:00 189 } 190 191 func ExampleTime_LayoutTo() { 192 gt1 := gtime.New("2018-08-08 08:08:08") 193 194 fmt.Println(gt1.LayoutTo("2006-01-02")) 195 196 // Output: 197 // 2018-08-08 00:00:00 198 } 199 200 func ExampleTime_IsLeapYear() { 201 gt1 := gtime.New("2018-08-08 08:08:08") 202 203 fmt.Println(gt1.IsLeapYear()) 204 205 // Output: 206 // false 207 } 208 209 func ExampleTime_DayOfYear() { 210 gt1 := gtime.New("2018-01-08 08:08:08") 211 212 fmt.Println(gt1.DayOfYear()) 213 214 // Output: 215 // 7 216 } 217 218 // DaysInMonth returns the day count of current month. 219 func ExampleTime_DaysInMonth() { 220 gt1 := gtime.New("2018-08-08 08:08:08") 221 222 fmt.Println(gt1.DaysInMonth()) 223 224 // Output: 225 // 31 226 } 227 228 // WeeksOfYear returns the point of current week for the year. 229 func ExampleTime_WeeksOfYear() { 230 gt1 := gtime.New("2018-01-08 08:08:08") 231 232 fmt.Println(gt1.WeeksOfYear()) 233 234 // Output: 235 // 2 236 } 237 238 func ExampleTime_ToZone() { 239 gt1 := gtime.Now() 240 gt2, _ := gt1.ToZone("Asia/Shanghai") 241 gt3, _ := gt1.ToZone("Asia/Tokyo") 242 243 fmt.Println(gt2) 244 fmt.Println(gt3) 245 246 // May Output: 247 // 2021-11-11 17:10:10 248 // 2021-11-11 18:10:10 249 }