Debian Codegraph @

timespan/timespan.go

raw symbols SCIP
1 // Copyright 2015 Rick Beton. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package timespanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/"
9 "stringsscip-go gomod github.com/golang/go/src go1.17 strings/"
10 "timescip-go gomod github.com/golang/go/src go1.17 time/"
11
12 "github.com/rickb777/datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/"
13 "github.com/rickb777/date/periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/"
14 )
15
16 // TimestampFormat is a simple format for date & time, "2006-01-02 15:04:05".
17 const TimestampFormatscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimestampFormat. = "2006-01-02 15:04:05"
18
19 //const ISOFormat = "2006-01-02T15:04:05"
20
21 // TimeSpan holds a span of time between two instants with a 1 nanosecond resolution.
22 // It is implemented using a time.Duration, therefore is limited to a maximum span of 290 years.
23 type TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# struct {
24 markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark. timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#
25 durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#
26 }
27
28 // ZeroTimeSpan creates a new zero-duration time span at a specified time.
29 func ZeroTimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/ZeroTimeSpan().(start timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
30 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{start, 0}
31 }
32
33 // TimeSpanOf creates a new time span at a specified time and duration.
34 func TimeSpanOfscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpanOf().(start timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#, d timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
35 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{start, d}
36 }
37
38 // NewTimeSpan creates a new time span from two times. The start and end can be in either
39 // order; the result will be normalised. The inputs are half-open: the start is included and
40 // the end is excluded.
41 func NewTimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewTimeSpan().(t1, t2 timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
42 if t2.Beforescip-go gomod github.com/golang/go/src go1.17 time/Time#Before().(t1) {
43 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{t2, t1.Subscip-go gomod github.com/golang/go/src go1.17 time/Time#Sub().(t2)}
44 }
45 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{t1, t2.Subscip-go gomod github.com/golang/go/src go1.17 time/Time#Sub().(t1)}
46 }
47
48 // Start gets the end time of the time span.
49 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Start().() timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time# {
50 if ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. < 0 {
51 return ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Addscip-go gomod github.com/golang/go/src go1.17 time/Time#Add().(ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration.)
52 }
53 return ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark.
54 }
55
56 // End gets the end time of the time span. Strictly, this is one nanosecond after the
57 // range of time included in the time span; this implements the half-open model.
58 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().() timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time# {
59 if ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. < 0 {
60 return ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark.
61 }
62 return ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Addscip-go gomod github.com/golang/go/src go1.17 time/Time#Add().(ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration.)
63 }
64
65 // Duration gets the duration of the time span.
66 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Duration().() timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# {
67 return ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration.
68 }
69
70 // IsEmpty returns true if this is an empty time span (zero duration).
71 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) IsEmptyscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#IsEmpty().() bool {
72 return ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. == 0
73 }
74
75 // Normalise ensures that the mark time is at the start time and the duration is positive.
76 // The normalised time span is returned.
77 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Normalisescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Normalise().() TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
78 if ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. < 0 {
79 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Addscip-go gomod github.com/golang/go/src go1.17 time/Time#Add().(ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration.), -ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration.}
80 }
81 return ts
82 }
83
84 // ShiftBy moves the time span by moving both the start and end times similarly.
85 // A negative parameter is allowed.
86 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) ShiftByscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#ShiftBy().(d timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
87 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Addscip-go gomod github.com/golang/go/src go1.17 time/Time#Add().(d), ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration.}
88 }
89
90 // ExtendBy lengthens the time span by a specified amount. The parameter may be negative,
91 // in which case it is possible that the end of the time span will appear to be before the
92 // start. However, the result is normalised so that the resulting start is the lesser value.
93 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) ExtendByscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#ExtendBy().(d timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
94 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark., ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. + d}.Normalisescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Normalise().()
95 }
96
97 // ExtendWithoutWrapping lengthens the time span by a specified amount. The parameter may be
98 // negative, but if its magnitude is large than the time span's duration, it will be truncated
99 // so that the result has zero duration in that case. The start time is never altered.
100 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) ExtendWithoutWrappingscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#ExtendWithoutWrapping().(d timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
101 tsn := ts.Normalisescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Normalise().()
102 if d < 0 && -d > tsn.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. {
103 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{tsn.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark., 0}
104 }
105 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{tsn.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark., tsn.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration. + d}
106 }
107
108 // String produces a human-readable description of a time span.
109 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Stringscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#String().() string {
110 return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%s from %s to %s", ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration., ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Formatscip-go gomod github.com/golang/go/src go1.17 time/Time#Format().(TimestampFormatscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimestampFormat.), ts.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().().Formatscip-go gomod github.com/golang/go/src go1.17 time/Time#Format().(TimestampFormatscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimestampFormat.))
111 }
112
113 // In returns a TimeSpan adjusted from its current location to a new location. Because
114 // location is considered to be a presentational attribute, the actual time itself is not
115 // altered by this function. This matches the behaviour of time.Time.In(loc).
116 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Inscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#In().(loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
117 t := ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Inscip-go gomod github.com/golang/go/src go1.17 time/Time#In().(loc)
118 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{t, ts.durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#duration.}
119 }
120
121 // DateRangeIn obtains the date range corresponding to the time span in a specified location.
122 // The result is normalised.
123 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) DateRangeInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#DateRangeIn().(loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# {
124 no := ts.Normalisescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Normalise().()
125 startDate := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.NewAtscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/NewAt().(no.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Inscip-go gomod github.com/golang/go/src go1.17 time/Time#In().(loc))
126 endDate := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.NewAtscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/NewAt().(no.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().().Inscip-go gomod github.com/golang/go/src go1.17 time/Time#In().(loc))
127 return NewDateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewDateRange().(startDate, endDate)
128 }
129
130 // Contains tests whether a given moment of time is enclosed within the time span. The
131 // start time is inclusive; the end time is exclusive.
132 // If t has a different locality to the time-span, it is adjusted accordingly.
133 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Containsscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Contains().(t timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#) bool {
134 tl := t.Inscip-go gomod github.com/golang/go/src go1.17 time/Time#In().(ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Locationscip-go gomod github.com/golang/go/src go1.17 time/Time#Location().())
135 return ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Equalscip-go gomod github.com/golang/go/src go1.17 time/Time#Equal().(tl) || ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Beforescip-go gomod github.com/golang/go/src go1.17 time/Time#Before().(tl) && ts.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().().Afterscip-go gomod github.com/golang/go/src go1.17 time/Time#After().(tl)
136 }
137
138 // Merge combines two time spans by calculating a time span that just encompasses them both.
139 // As a special case, if one span is entirely contained within the other span, the larger of
140 // the two is returned. Otherwise, the result is the start of the earlier one to the end of the
141 // later one, even if the two spans don't overlap.
142 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Mergescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Merge().(other TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# {
143 if ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Afterscip-go gomod github.com/golang/go/src go1.17 time/Time#After().(other.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark.) {
144 // swap the ranges to simplify the logic
145 return other.Mergescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Merge().(ts)
146
147 } else if ts.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().().Afterscip-go gomod github.com/golang/go/src go1.17 time/Time#After().(other.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().()) {
148 // other is a proper subrange of ts
149 return ts
150
151 } else {
152 return NewTimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewTimeSpan().(ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark., other.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().())
153 }
154 }
155
156 // RFC5545DateTimeLayout is the format string used by iCalendar (RFC5545). Note
157 // that "Z" is to be appended when the time is UTC.
158 const RFC5545DateTimeLayoutscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeLayout. = "20060102T150405"
159
160 // RFC5545DateTimeZulu is the UTC format string used by iCalendar (RFC5545). Note
161 // that this cannot be used for parsing with time.Parse.
162 const RFC5545DateTimeZuluscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeZulu. = RFC5545DateTimeLayoutscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeLayout. + "Z"
163
164 func layoutHasTimezonescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/layoutHasTimezone().(layout string) bool {
165 return stringsscip-go gomod github.com/golang/go/src go1.17 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.17 strings/IndexByte().(layout, 'Z') >= 0 || stringsscip-go gomod github.com/golang/go/src go1.17 strings/.Containsscip-go gomod github.com/golang/go/src go1.17 strings/Contains().(layout, "-07")
166 }
167
168 // Equal reports whether ts and us represent the same time start and duration.
169 // Two times can be equal even if they are in different locations.
170 // For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
171 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Equalscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Equal().(us TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) bool {
172 return ts.Durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Duration().() == us.Durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Duration().() && ts.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Start().().Equalscip-go gomod github.com/golang/go/src go1.17 time/Time#Equal().(us.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Start().())
173 }
174
175 // Format returns a textual representation of the time value formatted according to layout.
176 // It produces a string containing the start and end time. Or, if useDuration is true,
177 // it returns a string containing the start time and the duration.
178 //
179 // The layout string is as specified for time.Format. If it doesn't have a timezone element
180 // ("07" or "Z") and the times in the timespan are UTC, the "Z" zulu indicator is added.
181 // This is as required by iCalendar (RFC5545).
182 //
183 // Also, if the layout is blank, it defaults to RFC5545DateTimeLayout.
184 //
185 // The separator between the two parts of the result would be "/" for RFC5545, but can be
186 // anything.
187 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) Formatscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Format().(layout, separator string, useDuration bool) string {
188 if layout == "" {
189 layout = RFC5545DateTimeLayoutscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeLayout.
190 }
191
192 // if the time is UTC and the format doesn't contain zulu field ("Z") or timezone field ("07")
193 if ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Locationscip-go gomod github.com/golang/go/src go1.17 time/Time#Location().().Stringscip-go gomod github.com/golang/go/src go1.17 time/Location#String().() == "UTC" && !layoutHasTimezonescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/layoutHasTimezone().(layout) {
194 layout = RFC5545DateTimeZuluscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeZulu.
195 }
196
197 s := ts.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Start().()
198 e := ts.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#End().()
199
200 if useDuration {
201 p := periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/.Betweenscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Between().(s, e)
202 return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%s%s%s", s.Formatscip-go gomod github.com/golang/go/src go1.17 time/Time#Format().(layout), separator, p)
203 }
204
205 return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%s%s%s", s.Formatscip-go gomod github.com/golang/go/src go1.17 time/Time#Format().(layout), separator, e.Formatscip-go gomod github.com/golang/go/src go1.17 time/Time#Format().(layout))
206 }
207
208 // FormatRFC5545 formats the timespan as a string containing the start time and end time, or the
209 // start time and duration, if useDuration is true. The two parts are separated by slash.
210 // The time(s) is expressed as UTC zulu.
211 // This is as required by iCalendar (RFC5545).
212 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) FormatRFC5545scip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#FormatRFC5545().(useDuration bool) string {
213 return ts.Formatscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Format().(RFC5545DateTimeZuluscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeZulu., "/", useDuration)
214 }
215
216 // MarshalText formats the timespan as a string using, using RFC5545 layout.
217 // This implements the encoding.TextMarshaler interface.
218 func (ts TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) MarshalTextscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#MarshalText().() (text []byte, err error) {
219 s := ts.Formatscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#Format().(RFC5545DateTimeZuluscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeZulu., "/", true)
220 return []byte(s), nil
221 }
222
223 // ParseRFC5545InLocation parses a string as a timespan. The string must contain either of
224 //
225 // time "/" time
226 // time "/" period
227 //
228 // If the input time(s) ends in "Z", the location is UTC (as per RFC5545). Otherwise, the
229 // specified location will be used for the resulting times; this behaves the same as
230 // time.ParseInLocation.
231 func ParseRFC5545InLocationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/ParseRFC5545InLocation().(text string, loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) (TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#, error) {
232 slash := stringsscip-go gomod github.com/golang/go/src go1.17 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.17 strings/IndexByte().(text, '/')
233 if slash < 0 {
234 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{}, fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("cannot parse %q because there is no separator '/'", text)
235 }
236
237 start := text[:slash]
238 rest := text[slash+1:]
239
240 st, err := parseTimeInLocationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/parseTimeInLocation().(start, loc)
241 if err != nil {
242 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{}, fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("cannot parse start time in %q: %s", text, err.Error())
243 }
244
245 //fmt.Printf("got %20s %s\n", st.Location(), st.Format(RFC5545DateTimeLayout))
246
247 if rest == "" {
248 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{}, fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("cannot parse %q because there is end time or duration", text)
249 }
250
251 if rest[0] == 'P' {
252 pe, e2 := periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/.Parsescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Parse().(rest)
253 if e2 != nil {
254 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{}, fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("cannot parse period in %q: %s", text, e2.Error())
255 }
256
257 du, precise := pe.Durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#Duration().()
258 if precise {
259 return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{st, du}, nil
260 }
261
262 et := st.AddDatescip-go gomod github.com/golang/go/src go1.17 time/Time#AddDate().(pe.Yearsscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#Years().(), pe.Monthsscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#Months().(), pe.Daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#Days().())
263 return NewTimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewTimeSpan().(st, et), nil
264 }
265
266 et, err := parseTimeInLocationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/parseTimeInLocation().(rest, loc)
267 return NewTimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewTimeSpan().(st, et), err
268 }
269
270 func parseTimeInLocationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/parseTimeInLocation().(text string, loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) (timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#, error) {
271 if stringsscip-go gomod github.com/golang/go/src go1.17 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.17 strings/HasSuffix().(text, "Z") {
272 text = text[:len(text)-1]
273 return timescip-go gomod github.com/golang/go/src go1.17 time/.ParseInLocationscip-go gomod github.com/golang/go/src go1.17 time/ParseInLocation().(RFC5545DateTimeLayoutscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeLayout., text, timescip-go gomod github.com/golang/go/src go1.17 time/.UTCscip-go gomod github.com/golang/go/src go1.17 time/UTC.)
274 }
275 return timescip-go gomod github.com/golang/go/src go1.17 time/.ParseInLocationscip-go gomod github.com/golang/go/src go1.17 time/ParseInLocation().(RFC5545DateTimeLayoutscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/RFC5545DateTimeLayout., text, loc)
276 }
277
278 // UnmarshalText parses a string as a timespan. It expects RFC5545 layout.
279 //
280 // If the receiver timespan is non-nil and has a time with a location,
281 // this location is used for parsing. Otherwise time.Local is used.
282 //
283 // This implements the encoding.TextUnmarshaler interface.
284 func (ts *TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#) UnmarshalTextscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#UnmarshalText().(text []byte) (err error) {
285 loc := timescip-go gomod github.com/golang/go/src go1.17 time/.Localscip-go gomod github.com/golang/go/src go1.17 time/Local.
286 if ts != nil {
287 loc = ts.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#mark..Locationscip-go gomod github.com/golang/go/src go1.17 time/Time#Location().()
288 }
289 *ts, err = ParseRFC5545InLocationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/ParseRFC5545InLocation().(string(text), loc)
290 return
291 }