github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/example_local_date_time_test.go (about) 1 package chrono_test 2 3 import ( 4 "fmt" 5 6 "github.com/go-chrono/chrono" 7 ) 8 9 func ExampleLocalDateTimeOf() { 10 dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0) 11 12 fmt.Println(dt) 13 // Output: 2007-05-20 12:30:15 14 } 15 16 func ExampleOfLocalDateTime() { 17 d := chrono.LocalDateOf(2007, chrono.May, 20) 18 t := chrono.LocalTimeOf(12, 30, 15, 0) 19 20 dt := chrono.OfLocalDateTime(d, t) 21 22 fmt.Println(dt) 23 // Output: 2007-05-20 12:30:15 24 } 25 26 func ExampleLocalDateTime_Split() { 27 dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0) 28 d, t := dt.Split() 29 30 fmt.Printf("date = %s, time = %s", d, t) 31 // Output: date = 2007-05-20, time = 12:30:15 32 } 33 34 func ExampleLocalDateTime_Compare() { 35 dt1 := chrono.LocalDateTimeOf(2007, chrono.May, 26, 12, 30, 15, 0) 36 dt2 := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0) 37 38 if dt2.Compare(dt1) == -1 { 39 fmt.Println(dt2, "is before", dt1) 40 } 41 // Output: 2007-05-20 12:30:15 is before 2007-05-26 12:30:15 42 } 43 44 func ExampleLocalDateTime_Add() { 45 dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0) 46 47 fmt.Println(dt.Add(chrono.DurationOf(26 * chrono.Hour))) 48 // Output: 2007-05-21 14:30:15 49 } 50 51 func ExampleLocalDateTime_AddDate() { 52 dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0) 53 54 fmt.Println(dt.AddDate(2, 6, 8)) 55 // Output: 2009-11-28 12:30:15 56 } 57 58 func ExampleLocalDateTime_Format() { 59 dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0) 60 61 fmt.Println(dt.Format(chrono.ISO8601DateTimeExtended)) 62 // Output: 2007-05-20T12:30:15 63 } 64 65 func ExampleLocalDateTime_Parse() { 66 var dt chrono.LocalDateTime 67 _ = dt.Parse(chrono.ISO8601DateTimeExtended, "2007-05-20T12:30:15") 68 69 fmt.Println(dt) 70 // Output: 2007-05-20 12:30:15 71 }