github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/local_date_time.go (about) 1 package chrono 2 3 import ( 4 "math/big" 5 ) 6 7 // LocalDateTime is a date and time without a time zone or time component. 8 // This is a combination of a LocalDate and LocalTime. 9 type LocalDateTime struct { 10 v big.Int 11 } 12 13 // LocalDateTimeOf returns the LocalDateTime that stores the specified year, month, day, 14 // hour, minute, second, and nanosecond offset within the specified second. 15 // The same range of values as supported by OfLocalDate and OfLocalTime are allowed here. 16 func LocalDateTimeOf(year int, month Month, day, hour, min, sec, nsec int) LocalDateTime { 17 date, err := makeDate(year, int(month), day) 18 if err != nil { 19 panic(err.Error()) 20 } 21 22 time, err := makeTime(hour, min, sec, nsec) 23 if err != nil { 24 panic(err.Error()) 25 } 26 27 return LocalDateTime{v: makeDateTime(date, time)} 28 } 29 30 // OfLocalDateTime combines the supplied LocalDate and LocalTime into a single LocalDateTime. 31 func OfLocalDateTime(date LocalDate, time LocalTime) LocalDateTime { 32 return LocalDateTime{v: makeDateTime(int64(date), time.v)} 33 } 34 35 // Compare compares d with d2. If d is before d2, it returns -1; 36 // if d is after d2, it returns 1; if they're the same, it returns 0. 37 func (d LocalDateTime) Compare(d2 LocalDateTime) int { 38 return d.v.Cmp(&d2.v) 39 } 40 41 // Split returns separate a LocalDate and LocalTime that together represent d. 42 func (d LocalDateTime) Split() (LocalDate, LocalTime) { 43 date, time := splitDateAndTime(d.v) 44 return LocalDate(date), LocalTime{v: time} 45 } 46 47 // In returns the OffsetDateTime represeting d with the specified offset. 48 func (d LocalDateTime) In(offset Offset) OffsetDateTime { 49 return OffsetDateTime{v: d.v, o: int64(offset)} 50 } 51 52 // UTC returns the OffsetDateTime represeting d at the UTC offset. 53 func (d LocalDateTime) UTC() OffsetDateTime { 54 return OffsetDateTime{v: d.v} 55 } 56 57 // Add returns the datetime d+v. 58 // This function panics if the resulting datetime would fall outside of the allowed range. 59 func (d LocalDateTime) Add(v Duration) LocalDateTime { 60 out, err := addDurationToBigDate(d.v, v) 61 if err != nil { 62 panic(err.Error()) 63 } 64 return LocalDateTime{v: out} 65 } 66 67 // CanAdd returns false if Add would panic if passed the same arguments. 68 func (d LocalDateTime) CanAdd(v Duration) bool { 69 _, err := addDurationToBigDate(d.v, v) 70 return err == nil 71 } 72 73 // AddDate returns the datetime corresponding to adding the given number of years, months, and days to d. 74 // This function panic if the resulting datetime would fall outside of the allowed date range. 75 func (d LocalDateTime) AddDate(years, months, days int) LocalDateTime { 76 out, err := addDateToBigDate(d.v, years, months, days) 77 if err != nil { 78 panic(err.Error()) 79 } 80 return LocalDateTime{v: out} 81 } 82 83 // CanAddDate returns false if AddDate would panic if passed the same arguments. 84 func (d LocalDateTime) CanAddDate(years, months, days int) bool { 85 _, err := addDateToBigDate(d.v, years, months, days) 86 return err == nil 87 } 88 89 // Sub returns the duration d-u. 90 func (d LocalDateTime) Sub(u LocalDateTime) Duration { 91 out := new(big.Int).Set(&d.v) 92 out.Sub(out, &u.v) 93 return Duration{v: *out} 94 } 95 96 func (d LocalDateTime) String() string { 97 date, time := splitDateAndTime(d.v) 98 hour, min, sec, nsec := fromTime(time) 99 year, month, day, err := fromDate(date) 100 if err != nil { 101 panic(err.Error()) 102 } 103 return simpleDateStr(year, month, day) + " " + simpleTimeStr(hour, min, sec, nsec, nil) 104 } 105 106 // Format returns a textual representation of the date-time value formatted according to the layout defined by the argument. 107 // See the constants section of the documentation to see how to represent the layout format. 108 func (d LocalDateTime) Format(layout string) string { 109 date, time := d.Split() 110 out, err := formatDateTimeOffset(layout, (*int32)(&date), &time.v, nil) 111 if err != nil { 112 panic(err.Error()) 113 } 114 return out 115 } 116 117 // Parse a formatted string and store the value it represents in d. 118 // See the constants section of the documentation to see how to represent the layout format. 119 func (d *LocalDateTime) Parse(layout, value string) error { 120 dv, tv := splitDateAndTime(d.v) 121 if err := parseDateAndTime(layout, value, &dv, &tv, nil); err != nil { 122 return err 123 } 124 125 d.v = makeDateTime(dv, tv) 126 return nil 127 } 128 129 // MinLocalDateTime returns the earliest supported datetime. 130 func MinLocalDateTime() LocalDateTime { 131 return minLocalDateTime 132 } 133 134 // MaxLocalDateTime returns the latest supported datetime. 135 func MaxLocalDateTime() LocalDateTime { 136 return maxLocalDateTime 137 }