File size: 6,810 Bytes
fea99b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | package datetime
import (
"encoding/json"
"time"
"github.com/govalues/decimal"
"github.com/rickb777/period"
"github.com/samber/lo"
)
// ISODuration represents ISO 8601 duration.
// It is mostly a wrapper around github.com/rickb777/period
type ISODuration struct {
period.Period
}
func NewISODuration(years, months, weeks, days, hours, minutes, seconds int) ISODuration {
return ISODuration{
period.New(years, months, weeks, days, hours, minutes, seconds),
}
}
// MarshalJSON marshals the Duration to a JSON string.
func (d ISODuration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Period)
}
// UnmarshalJSON unmarshals the Duration from a JSON string.
func (d *ISODuration) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &d.Period)
}
// ISOString() returns the ISO8601 string representation of the period
func (p ISODuration) ISOString() ISODurationString {
return ISODurationString(p.Period.String())
}
// ISOStringPtrOrNil() returns the ISO8601 string representation of the period or if Period is nil, returns nil
func (d *ISODuration) ISOStringPtrOrNil() *ISODurationString {
if d == nil {
return nil
}
return lo.ToPtr(d.ISOString())
}
// Equal returns true if the two periods are equal
func (p *ISODuration) Equal(v *ISODuration) bool {
if p == nil && v == nil {
return true
}
if p == nil || v == nil {
return false
}
return p.String() == v.String()
}
func (p ISODuration) Normalise(exact bool) ISODuration {
return ISODuration{p.Period.Normalise(exact)}
}
func (p ISODuration) Simplify(exact bool) ISODuration {
return ISODuration{p.Period.Simplify(exact)}
}
func (p ISODuration) Negate() ISODuration {
return ISODuration{p.Period.Negate()}
}
func (p ISODuration) Add(p2 ISODuration) (ISODuration, error) {
s2 := period.ISOString(p2.String())
per2, err := period.Parse(string(s2))
if err != nil {
return ISODuration{}, err
}
p3, err := p.Period.Add(per2)
if err != nil {
return ISODuration{}, NewDurationArithmeticError(p.String(), err)
}
return ISODuration{p3}, nil
}
func (p ISODuration) Subtract(p2 ISODuration) (ISODuration, error) {
s2 := period.ISOString(p2.String())
per2, err := period.Parse(string(s2))
if err != nil {
return ISODuration{}, err
}
p3, err := p.Period.Subtract(per2)
if err != nil {
return ISODuration{}, NewDurationArithmeticError(p.String(), err)
}
return ISODuration{p3}, nil
}
// AddTo adds the duration to the time.Time and returns the result and a boolean indicating if the conversion was precise.
// The conversion is always precise but the signature is kept for backwards compatibility.
func (p ISODuration) AddTo(t time.Time) (time.Time, bool) {
// Use our custom date arithmetic to handle month/year overflow correctly
result := NewDateTime(t).Add(p).AsTime()
return result, true
}
func ISODurationBetween(start time.Time, end time.Time) ISODuration {
per := period.Between(start, end)
return ISODuration{per}
}
// ISODurationFromDuration creates an IMPRECISE Period from a time.Duration
func ISODurationFromDuration(d time.Duration) ISODuration {
return ISODuration{period.NewOf(d).Normalise(false).Simplify(false)}
}
// DivisibleBy returns true if the duration is divisible by the smaller duration.
func (d ISODuration) DivisibleBy(smaller ISODuration) (bool, error) {
l := d.Simplify(true)
s := smaller.Simplify(true)
if l.IsZero() {
return false, nil
}
if s.IsZero() {
return false, nil
}
// Test with different days-per-month and hours-per-day scenarios
testDaysInMonth := []int{28, 29, 30, 31}
testHoursInDays := []int{25, 24, 23}
for _, daysInMonth := range testDaysInMonth {
for _, hoursInDays := range testHoursInDays {
largerSeconds, err := convertPeriodToSeconds(l.Period, daysInMonth, hoursInDays)
if err != nil {
return false, err
}
smallerSeconds, err := convertPeriodToSeconds(s.Period, daysInMonth, hoursInDays)
if err != nil {
return false, err
}
if smallerSeconds.IsZero() {
return false, nil
}
quotient, remainder, err := largerSeconds.QuoRem(smallerSeconds)
if err != nil {
return false, err
}
if !remainder.IsZero() {
return false, nil
}
if quotient.Sign() <= 0 || !quotient.IsInt() {
return false, nil
}
}
}
return true, nil
}
func (p ISODuration) Mul(n int) (ISODuration, error) {
nDecimal, err := decimal.NewFromInt64(int64(n), 0, 0)
if err != nil {
return ISODuration{}, err
}
per, err := p.Period.Mul(nDecimal)
if err != nil {
return ISODuration{}, err
}
return ISODuration{per}, nil
}
// convertPeriodToSeconds converts a period to total seconds using decimal precision
func convertPeriodToSeconds(p period.Period, daysInMonth int, hoursInDays int) (decimal.Decimal, error) {
zero := decimal.Zero
yearsMul, err := decimal.New(int64(daysInMonth*12*hoursInDays*3600), 0)
if err != nil {
return zero, err
}
// Convert years to seconds: years * (daysInMonth * 12) * hoursInDays * 3600
years, err := p.YearsDecimal().Mul(yearsMul)
if err != nil {
return zero, err
}
monthsMul, err := decimal.New(int64(daysInMonth*hoursInDays*3600), 0)
if err != nil {
return zero, err
}
// Convert months to seconds: months * daysInMonth * hoursInDays * 3600
months, err := p.MonthsDecimal().Mul(monthsMul)
if err != nil {
return zero, err
}
weeksMul, err := decimal.New(int64(7*hoursInDays*3600), 0)
if err != nil {
return zero, err
}
// Convert weeks to seconds: weeks * 7 * hoursInDays * 3600
weeks, err := p.WeeksDecimal().Mul(weeksMul)
if err != nil {
return zero, err
}
// Convert days to seconds: days * hoursInDays * 3600
daysMul, err := decimal.New(int64(hoursInDays*3600), 0)
if err != nil {
return zero, err
}
days, err := p.DaysDecimal().Mul(daysMul)
if err != nil {
return zero, err
}
// Convert hours to seconds: hours * 3600
hoursMul, err := decimal.New(int64(3600), 0)
if err != nil {
return zero, err
}
hours, err := p.HoursDecimal().Mul(hoursMul)
if err != nil {
return zero, err
}
// Convert minutes to seconds: minutes * 60
minutesMul, err := decimal.New(int64(60), 0)
if err != nil {
return zero, err
}
minutes, err := p.MinutesDecimal().Mul(minutesMul)
if err != nil {
return zero, err
}
// Seconds are already in the right unit
seconds := p.SecondsDecimal()
// Sum all components
result, err := years.Add(months)
if err != nil {
return zero, err
}
result, err = result.Add(weeks)
if err != nil {
return zero, err
}
result, err = result.Add(days)
if err != nil {
return zero, err
}
result, err = result.Add(hours)
if err != nil {
return zero, err
}
result, err = result.Add(minutes)
if err != nil {
return zero, err
}
result, err = result.Add(seconds)
if err != nil {
return zero, err
}
return result, nil
}
|