github.com/wangyougui/gf/v2@v2.6.5/os/gtime/gtime_z_example_time_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/wangyougui/gf. 6 7 package gtime_test 8 9 import ( 10 "encoding/json" 11 "fmt" 12 "reflect" 13 "time" 14 15 "github.com/wangyougui/gf/v2/os/gtime" 16 ) 17 18 func ExampleNew_Basic() { 19 curTime := "2018-08-08 08:08:08" 20 timer, _ := time.Parse("2006-01-02 15:04:05", curTime) 21 t1 := gtime.New(&timer) 22 t2 := gtime.New(curTime) 23 t3 := gtime.New(curTime, "Y-m-d H:i:s") 24 t4 := gtime.New(curTime) 25 t5 := gtime.New(1533686888) 26 27 fmt.Println(t1) 28 fmt.Println(t2) 29 fmt.Println(t3) 30 fmt.Println(t4) 31 fmt.Println(t5) 32 33 // Output: 34 // 2018-08-08 08:08:08 35 // 2018-08-08 08:08:08 36 // 2018-08-08 08:08:08 37 // 2018-08-08 08:08:08 38 // 2018-08-08 08:08:08 39 } 40 41 func ExampleNew_WithFormat() { 42 fmt.Println(gtime.New("20220629133225", "YmdHis").Format("Y-m-d H:i:s")) 43 44 // Output: 45 // 2022-06-29 13:32:25 46 } 47 48 // Now creates and returns a time object of now. 49 func ExampleNow() { 50 t := gtime.Now() 51 fmt.Println(t) 52 53 // May Output: 54 // 2021-11-06 13:41:08 55 } 56 57 // NewFromTime creates and returns a Time object with given time.Time object. 58 func ExampleNewFromTime() { 59 timer, _ := time.Parse("2006-01-02 15:04:05", "2018-08-08 08:08:08") 60 nTime := gtime.NewFromTime(timer) 61 62 fmt.Println(nTime) 63 64 // Output: 65 // 2018-08-08 08:08:08 66 } 67 68 // NewFromStr creates and returns a Time object with given string. 69 // Note that it returns nil if there's error occurs. 70 func ExampleNewFromStr() { 71 t := gtime.NewFromStr("2018-08-08 08:08:08") 72 73 fmt.Println(t) 74 75 // Output: 76 // 2018-08-08 08:08:08 77 } 78 79 // NewFromStrFormat creates and returns a Time object with given string and 80 // custom format like: Y-m-d H:i:s. 81 // Note that it returns nil if there's error occurs. 82 func ExampleNewFromStrFormat() { 83 t := gtime.NewFromStrFormat("2018-08-08 08:08:08", "Y-m-d H:i:s") 84 fmt.Println(t) 85 86 // Output: 87 // 2018-08-08 08:08:08 88 } 89 90 // NewFromStrLayout creates and returns a Time object with given string and 91 // stdlib layout like: 2006-01-02 15:04:05. 92 // Note that it returns nil if there's error occurs. 93 func ExampleNewFromStrLayout() { 94 t := gtime.NewFromStrLayout("2018-08-08 08:08:08", "2006-01-02 15:04:05") 95 fmt.Println(t) 96 97 // Output: 98 // 2018-08-08 08:08:08 99 } 100 101 // NewFromTimeStamp creates and returns a Time object with given timestamp, 102 // which can be in seconds to nanoseconds. 103 // Eg: 1600443866 and 1600443866199266000 are both considered as valid timestamp number. 104 func ExampleNewFromTimeStamp() { 105 t1 := gtime.NewFromTimeStamp(1533686888) 106 t2 := gtime.NewFromTimeStamp(1533686888000) 107 108 fmt.Println(t1.String() == t2.String()) 109 fmt.Println(t1) 110 111 // Output: 112 // true 113 // 2018-08-08 08:08:08 114 } 115 116 // Timestamp returns the timestamp in seconds. 117 func ExampleTime_Timestamp() { 118 t := gtime.Timestamp() 119 120 fmt.Println(t) 121 122 // May output: 123 // 1533686888 124 } 125 126 // Timestamp returns the timestamp in milliseconds. 127 func ExampleTime_TimestampMilli() { 128 t := gtime.TimestampMilli() 129 130 fmt.Println(t) 131 132 // May output: 133 // 1533686888000 134 } 135 136 // Timestamp returns the timestamp in microseconds. 137 func ExampleTime_TimestampMicro() { 138 t := gtime.TimestampMicro() 139 140 fmt.Println(t) 141 142 // May output: 143 // 1533686888000000 144 } 145 146 // Timestamp returns the timestamp in nanoseconds. 147 func ExampleTime_TimestampNano() { 148 t := gtime.TimestampNano() 149 150 fmt.Println(t) 151 152 // May output: 153 // 1533686888000000 154 } 155 156 // TimestampStr is a convenience method which retrieves and returns 157 // the timestamp in seconds as string. 158 func ExampleTime_TimestampStr() { 159 t := gtime.TimestampStr() 160 161 fmt.Println(reflect.TypeOf(t)) 162 163 // Output: 164 // string 165 } 166 167 // Month returns the month of the year specified by t. 168 func ExampleTime_Month() { 169 gt := gtime.New("2018-08-08 08:08:08") 170 t1 := gt.Month() 171 172 fmt.Println(t1) 173 174 // Output: 175 // 8 176 } 177 178 // Second returns the second offset within the minute specified by t, 179 // in the range [0, 59]. 180 func ExampleTime_Second() { 181 gt := gtime.New("2018-08-08 08:08:08") 182 t1 := gt.Second() 183 184 fmt.Println(t1) 185 186 // Output: 187 // 8 188 } 189 190 // String returns current time object as string. 191 func ExampleTime_String() { 192 gt := gtime.New("2018-08-08 08:08:08") 193 t1 := gt.String() 194 195 fmt.Println(t1) 196 fmt.Println(reflect.TypeOf(t1)) 197 198 // Output: 199 // 2018-08-08 08:08:08 200 // string 201 } 202 203 // IsZero reports whether t represents the zero time instant, 204 // January 1, year 1, 00:00:00 UTC. 205 func ExampleTime_IsZero() { 206 gt := gtime.New("2018-08-08 08:08:08") 207 208 fmt.Println(gt.IsZero()) 209 210 // Output: 211 // false 212 } 213 214 // Add adds the duration to current time. 215 func ExampleTime_Add() { 216 gt := gtime.New("2018-08-08 08:08:08") 217 gt1 := gt.Add(time.Duration(10) * time.Second) 218 219 fmt.Println(gt1) 220 221 // Output: 222 // 2018-08-08 08:08:18 223 } 224 225 // AddStr parses the given duration as string and adds it to current time. 226 // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". 227 func ExampleTime_AddStr() { 228 gt := gtime.New("2018-08-08 08:08:08") 229 gt1, _ := gt.AddStr("10s") 230 231 fmt.Println(gt1) 232 233 // Output: 234 // 2018-08-08 08:08:18 235 } 236 237 // AddDate adds year, month and day to the time. 238 func ExampleTime_AddDate() { 239 var ( 240 year = 1 241 month = 2 242 day = 3 243 ) 244 gt := gtime.New("2018-08-08 08:08:08") 245 gt = gt.AddDate(year, month, day) 246 247 fmt.Println(gt) 248 249 // Output: 250 // 2019-10-11 08:08:08 251 } 252 253 // Round returns the result of rounding t to the nearest multiple of d (since the zero time). 254 // The rounding behavior for halfway values is to round up. 255 // If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged. 256 // 257 // Round operates on the time as an absolute duration since the 258 // zero time; it does not operate on the presentation form of the 259 // time. Thus, Round(Hour) may return a time with a non-zero 260 // minute, depending on the time's Location. 261 func ExampleTime_Round() { 262 gt := gtime.New("2018-08-08 08:08:08") 263 t := gt.Round(time.Duration(10) * time.Second) 264 265 fmt.Println(t) 266 267 // Output: 268 // 2018-08-08 08:08:10 269 } 270 271 // Truncate returns the result of rounding t down to a multiple of d (since the zero time). 272 // If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged. 273 // 274 // Truncate operates on the time as an absolute duration since the 275 // zero time; it does not operate on the presentation form of the 276 // time. Thus, Truncate(Hour) may return a time with a non-zero 277 // minute, depending on the time's Location. 278 func ExampleTime_Truncate() { 279 gt := gtime.New("2018-08-08 08:08:08") 280 t := gt.Truncate(time.Duration(10) * time.Second) 281 282 fmt.Println(t) 283 284 // Output: 285 // 2018-08-08 08:08:00 286 } 287 288 // Equal reports whether t and u represent the same time instant. 289 // Two times can be equal even if they are in different locations. 290 // For example, 6:00 +0200 CEST and 4:00 UTC are Equal. 291 // See the documentation on the Time type for the pitfalls of using == with 292 // Time values; most code should use Equal instead. 293 func ExampleTime_Equal() { 294 gt1 := gtime.New("2018-08-08 08:08:08") 295 gt2 := gtime.New("2018-08-08 08:08:08") 296 297 fmt.Println(gt1.Equal(gt2)) 298 299 // Output: 300 // true 301 } 302 303 // Before reports whether the time instant t is before u. 304 func ExampleTime_Before() { 305 gt1 := gtime.New("2018-08-07") 306 gt2 := gtime.New("2018-08-08") 307 308 fmt.Println(gt1.Before(gt2)) 309 310 // Output: 311 // true 312 } 313 314 // After reports whether the time instant t is after u. 315 func ExampleTime_After() { 316 gt1 := gtime.New("2018-08-07") 317 gt2 := gtime.New("2018-08-08") 318 319 fmt.Println(gt1.After(gt2)) 320 321 // Output: 322 // false 323 } 324 325 // Sub returns the duration t-u. If the result exceeds the maximum (or minimum) 326 // value that can be stored in a Duration, the maximum (or minimum) duration 327 // will be returned. 328 // To compute t-d for a duration d, use t.Add(-d). 329 func ExampleTime_Sub() { 330 gt1 := gtime.New("2018-08-08 08:08:08") 331 gt2 := gtime.New("2018-08-08 08:08:10") 332 333 fmt.Println(gt2.Sub(gt1)) 334 335 // Output: 336 // 2s 337 } 338 339 // StartOfMinute clones and returns a new time of which the seconds is set to 0. 340 func ExampleTime_StartOfMinute() { 341 gt1 := gtime.New("2018-08-08 08:08:08") 342 343 fmt.Println(gt1.StartOfMinute()) 344 345 // Output: 346 // 2018-08-08 08:08:00 347 } 348 349 func ExampleTime_StartOfHour() { 350 gt1 := gtime.New("2018-08-08 08:08:08") 351 352 fmt.Println(gt1.StartOfHour()) 353 354 // Output: 355 // 2018-08-08 08:00:00 356 } 357 358 func ExampleTime_StartOfDay() { 359 gt1 := gtime.New("2018-08-08 08:08:08") 360 361 fmt.Println(gt1.StartOfDay()) 362 363 // Output: 364 // 2018-08-08 00:00:00 365 } 366 367 func ExampleTime_StartOfWeek() { 368 gt1 := gtime.New("2018-08-08 08:08:08") 369 370 fmt.Println(gt1.StartOfWeek()) 371 372 // Output: 373 // 2018-08-05 00:00:00 374 } 375 376 func ExampleTime_StartOfQuarter() { 377 gt1 := gtime.New("2018-08-08 08:08:08") 378 379 fmt.Println(gt1.StartOfQuarter()) 380 381 // Output: 382 // 2018-07-01 00:00:00 383 } 384 385 func ExampleTime_StartOfHalf() { 386 gt1 := gtime.New("2018-08-08 08:08:08") 387 388 fmt.Println(gt1.StartOfHalf()) 389 390 // Output: 391 // 2018-07-01 00:00:00 392 } 393 394 func ExampleTime_StartOfYear() { 395 gt1 := gtime.New("2018-08-08 08:08:08") 396 397 fmt.Println(gt1.StartOfYear()) 398 399 // Output: 400 // 2018-01-01 00:00:00 401 } 402 403 func ExampleTime_EndOfMinute() { 404 gt1 := gtime.New("2018-08-08 08:08:08") 405 406 fmt.Println(gt1.EndOfMinute()) 407 408 // Output: 409 // 2018-08-08 08:08:59 410 } 411 412 func ExampleTime_EndOfHour() { 413 gt1 := gtime.New("2018-08-08 08:08:08") 414 415 fmt.Println(gt1.EndOfHour()) 416 417 // Output: 418 // 2018-08-08 08:59:59 419 } 420 421 func ExampleTime_EndOfDay() { 422 gt1 := gtime.New("2018-08-08 08:08:08") 423 424 fmt.Println(gt1.EndOfDay()) 425 426 // Output: 427 // 2018-08-08 23:59:59 428 } 429 430 func ExampleTime_EndOfWeek() { 431 gt1 := gtime.New("2018-08-08 08:08:08") 432 433 fmt.Println(gt1.EndOfWeek()) 434 435 // Output: 436 // 2018-08-11 23:59:59 437 } 438 439 func ExampleTime_EndOfMonth() { 440 gt1 := gtime.New("2018-08-08 08:08:08") 441 442 fmt.Println(gt1.EndOfMonth()) 443 444 // Output: 445 // 2018-08-31 23:59:59 446 } 447 448 func ExampleTime_EndOfQuarter() { 449 gt1 := gtime.New("2018-08-08 08:08:08") 450 451 fmt.Println(gt1.EndOfQuarter()) 452 453 // Output: 454 // 2018-09-30 23:59:59 455 } 456 457 func ExampleTime_EndOfHalf() { 458 gt1 := gtime.New("2018-08-08 08:08:08") 459 460 fmt.Println(gt1.EndOfHalf()) 461 462 // Output: 463 // 2018-12-31 23:59:59 464 } 465 466 func ExampleTime_EndOfYear() { 467 gt1 := gtime.New("2018-08-08 08:08:08") 468 469 fmt.Println(gt1.EndOfYear()) 470 471 // Output: 472 // 2018-12-31 23:59:59 473 } 474 475 func ExampleTime_MarshalJSON() { 476 type Person struct { 477 Name string `json:"name"` 478 Birthday *gtime.Time `json:"birthday"` 479 } 480 p := new(Person) 481 p.Name = "goframe" 482 p.Birthday = gtime.New("2018-08-08 08:08:08") 483 j, _ := json.Marshal(p) 484 fmt.Println(string(j)) 485 486 // Output: 487 // {"name":"goframe","birthday":"2018-08-08 08:08:08"} 488 } 489 490 func ExampleTime_UnmarshalJSON() { 491 type Person struct { 492 Name string `json:"name"` 493 Birthday *gtime.Time `json:"birthday"` 494 } 495 p := new(Person) 496 src := `{"name":"goframe","birthday":"2018-08-08 08:08:08"}` 497 json.Unmarshal([]byte(src), p) 498 499 fmt.Println(p) 500 501 // Output 502 // &{goframe 2018-08-08 08:08:08} 503 }