From 5f8b4ebdba40f67ef50e5f76bc4e688c6d5e9788 Mon Sep 17 00:00:00 2001 From: nitram509 Date: Tue, 13 Sep 2022 23:23:19 +0200 Subject: [PATCH] use int64 type to prevent year 2038 problem on 32bit machines also being compatible with golang standard runtime using int64 as well --- duration.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/duration.go b/duration.go index 7163e3d..10bb1df 100644 --- a/duration.go +++ b/duration.go @@ -15,14 +15,20 @@ import ( // Duration represents an ISO8601 Duration // https://en.wikipedia.org/wiki/ISO_8601#Durations type Duration struct { - Y int - M int - W int - D int - // Time Component - TH int - TM int - TS int + // Y is the year designator that follows the value for the number of calendar years. + Y int64 + // M is the month designator that follows the value for the number of calendar months. + M int64 + // W is the week designator that follows the value for the number of weeks. + W int64 + // D is the day designator that follows the value for the number of calendar days. + D int64 + // TH is the hour designator that follows the value for the number of hours. + TH int64 + // TM is the minute designator that follows the value for the number of minutes. + TM int64 + // TS is the second designator that follows the value for the number of seconds. + TS int64 } var pattern = regexp.MustCompile(`^P((?P\d+)Y)?((?P\d+)M)?((?P\d+)W)?((?P\d+)D)?(T((?P\d+)H)?((?P\d+)M)?((?P\d+)S)?)?$`) @@ -44,7 +50,7 @@ func ParseISO8601(from string) (Duration, error) { continue } - val, err := strconv.Atoi(part) + val, err := strconv.ParseInt(part, 10, 64) if err != nil { return d, err } @@ -92,7 +98,7 @@ func (d Duration) HasTimePart() bool { func (d Duration) Shift(t time.Time) time.Time { if d.Y != 0 || d.M != 0 || d.W != 0 || d.D != 0 { days := d.W*7 + d.D - t = t.AddDate(d.Y, d.M, days) + t = t.AddDate(int(d.Y), int(d.M), int(days)) } t = t.Add(d.timeDuration()) return t