github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/example_offset_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 ExampleOffsetDateTimeOf() { 10 dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30) 11 12 fmt.Println(dt) 13 // Output: 2007-05-20 12:30:15+02:30 14 } 15 16 func ExampleOfLocalDateTimeOffset() { 17 d := chrono.LocalDateOf(2007, chrono.May, 20) 18 t := chrono.LocalTimeOf(12, 30, 15, 0) 19 20 dt := chrono.OfLocalDateTimeOffset(d, t, 2*chrono.Hour+30*chrono.Minute) 21 22 fmt.Println(dt) 23 // Output: 2007-05-20 12:30:15+02:30 24 } 25 26 func ExampleOffsetDateTime_Split() { 27 dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30) 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+02:30 32 } 33 34 func ExampleOffsetDateTime_Compare() { 35 dt1 := chrono.OffsetDateTimeOf(2007, chrono.May, 26, 12, 30, 15, 0, 2, 30) 36 dt2 := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30) 37 38 if dt2.Compare(dt1) == -1 { 39 fmt.Println(dt2, "is before", dt1) 40 } 41 // Output: 2007-05-20 12:30:15+02:30 is before 2007-05-26 12:30:15+02:30 42 } 43 44 func ExampleOffsetDateTime_Add() { 45 dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30) 46 47 fmt.Println(dt.Add(chrono.DurationOf(26 * chrono.Hour))) 48 // Output: 2007-05-21 14:30:15+02:30 49 } 50 51 func ExampleOffsetDateTime_AddDate() { 52 dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30) 53 54 fmt.Println(dt.AddDate(2, 6, 8)) 55 // Output: 2009-11-28 12:30:15+02:30 56 } 57 58 func ExampleOffsetDateTime_Format() { 59 dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30) 60 61 fmt.Println(dt.Format(chrono.ISO8601DateTimeExtended)) 62 // Output: 2007-05-20T12:30:15+02:30 63 } 64 65 func ExampleOffsetDateTime_Parse() { 66 var dt chrono.OffsetDateTime 67 _ = dt.Parse(chrono.ISO8601DateTimeExtended, "2007-05-20T12:30:15+02:30") 68 69 fmt.Println(dt) 70 // Output: 2007-05-20 12:30:15+02:30 71 }