1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. 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 time provides functionality for measuring and displaying time.
|
6 |
|
|
//
|
7 |
|
|
// The calendrical calculations always assume a Gregorian calendar.
|
8 |
|
|
package time
|
9 |
|
|
|
10 |
|
|
import "errors"
|
11 |
|
|
|
12 |
|
|
// A Time represents an instant in time with nanosecond precision.
|
13 |
|
|
//
|
14 |
|
|
// Programs using times should typically store and pass them as values,
|
15 |
|
|
// not pointers. That is, time variables and struct fields should be of
|
16 |
|
|
// type time.Time, not *time.Time.
|
17 |
|
|
//
|
18 |
|
|
// Time instants can be compared using the Before, After, and Equal methods.
|
19 |
|
|
// The Sub method subtracts two instants, producing a Duration.
|
20 |
|
|
// The Add method adds a Time and a Duration, producing a Time.
|
21 |
|
|
//
|
22 |
|
|
// The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC.
|
23 |
|
|
// As this time is unlikely to come up in practice, the IsZero method gives
|
24 |
|
|
// a simple way of detecting a time that has not been initialized explicitly.
|
25 |
|
|
//
|
26 |
|
|
// Each Time has associated with it a Location, consulted when computing the
|
27 |
|
|
// presentation form of the time, such as in the Format, Hour, and Year methods.
|
28 |
|
|
// The methods Local, UTC, and In return a Time with a specific location.
|
29 |
|
|
// Changing the location in this way changes only the presentation; it does not
|
30 |
|
|
// change the instant in time being denoted and therefore does not affect the
|
31 |
|
|
// computations described in earlier paragraphs.
|
32 |
|
|
//
|
33 |
|
|
type Time struct {
|
34 |
|
|
// sec gives the number of seconds elapsed since
|
35 |
|
|
// January 1, year 1 00:00:00 UTC.
|
36 |
|
|
sec int64
|
37 |
|
|
|
38 |
|
|
// nsec specifies a non-negative nanosecond
|
39 |
|
|
// offset within the second named by Seconds.
|
40 |
|
|
// It must be in the range [0, 999999999].
|
41 |
|
|
nsec int32
|
42 |
|
|
|
43 |
|
|
// loc specifies the Location that should be used to
|
44 |
|
|
// determine the minute, hour, month, day, and year
|
45 |
|
|
// that correspond to this Time.
|
46 |
|
|
// Only the zero Time has a nil Location.
|
47 |
|
|
// In that case it is interpreted to mean UTC.
|
48 |
|
|
loc *Location
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
// After reports whether the time instant t is after u.
|
52 |
|
|
func (t Time) After(u Time) bool {
|
53 |
|
|
return t.sec > u.sec || t.sec == u.sec && t.nsec > u.nsec
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
// Before reports whether the time instant t is before u.
|
57 |
|
|
func (t Time) Before(u Time) bool {
|
58 |
|
|
return t.sec < u.sec || t.sec == u.sec && t.nsec < u.nsec
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
// Equal reports whether t and u represent the same time instant.
|
62 |
|
|
// Two times can be equal even if they are in different locations.
|
63 |
|
|
// For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
|
64 |
|
|
// This comparison is different from using t == u, which also compares
|
65 |
|
|
// the locations.
|
66 |
|
|
func (t Time) Equal(u Time) bool {
|
67 |
|
|
return t.sec == u.sec && t.nsec == u.nsec
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
// A Month specifies a month of the year (January = 1, ...).
|
71 |
|
|
type Month int
|
72 |
|
|
|
73 |
|
|
const (
|
74 |
|
|
January Month = 1 + iota
|
75 |
|
|
February
|
76 |
|
|
March
|
77 |
|
|
April
|
78 |
|
|
May
|
79 |
|
|
June
|
80 |
|
|
July
|
81 |
|
|
August
|
82 |
|
|
September
|
83 |
|
|
October
|
84 |
|
|
November
|
85 |
|
|
December
|
86 |
|
|
)
|
87 |
|
|
|
88 |
|
|
var months = [...]string{
|
89 |
|
|
"January",
|
90 |
|
|
"February",
|
91 |
|
|
"March",
|
92 |
|
|
"April",
|
93 |
|
|
"May",
|
94 |
|
|
"June",
|
95 |
|
|
"July",
|
96 |
|
|
"August",
|
97 |
|
|
"September",
|
98 |
|
|
"October",
|
99 |
|
|
"November",
|
100 |
|
|
"December",
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
// String returns the English name of the month ("January", "February", ...).
|
104 |
|
|
func (m Month) String() string { return months[m-1] }
|
105 |
|
|
|
106 |
|
|
// A Weekday specifies a day of the week (Sunday = 0, ...).
|
107 |
|
|
type Weekday int
|
108 |
|
|
|
109 |
|
|
const (
|
110 |
|
|
Sunday Weekday = iota
|
111 |
|
|
Monday
|
112 |
|
|
Tuesday
|
113 |
|
|
Wednesday
|
114 |
|
|
Thursday
|
115 |
|
|
Friday
|
116 |
|
|
Saturday
|
117 |
|
|
)
|
118 |
|
|
|
119 |
|
|
var days = [...]string{
|
120 |
|
|
"Sunday",
|
121 |
|
|
"Monday",
|
122 |
|
|
"Tuesday",
|
123 |
|
|
"Wednesday",
|
124 |
|
|
"Thursday",
|
125 |
|
|
"Friday",
|
126 |
|
|
"Saturday",
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
// String returns the English name of the day ("Sunday", "Monday", ...).
|
130 |
|
|
func (d Weekday) String() string { return days[d] }
|
131 |
|
|
|
132 |
|
|
// Computations on time.
|
133 |
|
|
//
|
134 |
|
|
// The zero value for a Time is defined to be
|
135 |
|
|
// January 1, year 1, 00:00:00.000000000 UTC
|
136 |
|
|
// which (1) looks like a zero, or as close as you can get in a date
|
137 |
|
|
// (1-1-1 00:00:00 UTC), (2) is unlikely enough to arise in practice to
|
138 |
|
|
// be a suitable "not set" sentinel, unlike Jan 1 1970, and (3) has a
|
139 |
|
|
// non-negative year even in time zones west of UTC, unlike 1-1-0
|
140 |
|
|
// 00:00:00 UTC, which would be 12-31-(-1) 19:00:00 in New York.
|
141 |
|
|
//
|
142 |
|
|
// The zero Time value does not force a specific epoch for the time
|
143 |
|
|
// representation. For example, to use the Unix epoch internally, we
|
144 |
|
|
// could define that to distinguish a zero value from Jan 1 1970, that
|
145 |
|
|
// time would be represented by sec=-1, nsec=1e9. However, it does
|
146 |
|
|
// suggest a representation, namely using 1-1-1 00:00:00 UTC as the
|
147 |
|
|
// epoch, and that's what we do.
|
148 |
|
|
//
|
149 |
|
|
// The Add and Sub computations are oblivious to the choice of epoch.
|
150 |
|
|
//
|
151 |
|
|
// The presentation computations - year, month, minute, and so on - all
|
152 |
|
|
// rely heavily on division and modulus by positive constants. For
|
153 |
|
|
// calendrical calculations we want these divisions to round down, even
|
154 |
|
|
// for negative values, so that the remainder is always positive, but
|
155 |
|
|
// Go's division (like most hardware divison instructions) rounds to
|
156 |
|
|
// zero. We can still do those computations and then adjust the result
|
157 |
|
|
// for a negative numerator, but it's annoying to write the adjustment
|
158 |
|
|
// over and over. Instead, we can change to a different epoch so long
|
159 |
|
|
// ago that all the times we care about will be positive, and then round
|
160 |
|
|
// to zero and round down coincide. These presentation routines already
|
161 |
|
|
// have to add the zone offset, so adding the translation to the
|
162 |
|
|
// alternate epoch is cheap. For example, having a non-negative time t
|
163 |
|
|
// means that we can write
|
164 |
|
|
//
|
165 |
|
|
// sec = t % 60
|
166 |
|
|
//
|
167 |
|
|
// instead of
|
168 |
|
|
//
|
169 |
|
|
// sec = t % 60
|
170 |
|
|
// if sec < 0 {
|
171 |
|
|
// sec += 60
|
172 |
|
|
// }
|
173 |
|
|
//
|
174 |
|
|
// everywhere.
|
175 |
|
|
//
|
176 |
|
|
// The calendar runs on an exact 400 year cycle: a 400-year calendar
|
177 |
|
|
// printed for 1970-2469 will apply as well to 2470-2869. Even the days
|
178 |
|
|
// of the week match up. It simplifies the computations to choose the
|
179 |
|
|
// cycle boundaries so that the exceptional years are always delayed as
|
180 |
|
|
// long as possible. That means choosing a year equal to 1 mod 400, so
|
181 |
|
|
// that the first leap year is the 4th year, the first missed leap year
|
182 |
|
|
// is the 100th year, and the missed missed leap year is the 400th year.
|
183 |
|
|
// So we'd prefer instead to print a calendar for 2001-2400 and reuse it
|
184 |
|
|
// for 2401-2800.
|
185 |
|
|
//
|
186 |
|
|
// Finally, it's convenient if the delta between the Unix epoch and
|
187 |
|
|
// long-ago epoch is representable by an int64 constant.
|
188 |
|
|
//
|
189 |
|
|
// These three considerations—choose an epoch as early as possible, that
|
190 |
|
|
// uses a year equal to 1 mod 400, and that is no more than 2⁶³ seconds
|
191 |
|
|
// earlier than 1970—bring us to the year -292277022399. We refer to
|
192 |
|
|
// this year as the absolute zero year, and to times measured as a uint64
|
193 |
|
|
// seconds since this year as absolute times.
|
194 |
|
|
//
|
195 |
|
|
// Times measured as an int64 seconds since the year 1—the representation
|
196 |
|
|
// used for Time's sec field—are called internal times.
|
197 |
|
|
//
|
198 |
|
|
// Times measured as an int64 seconds since the year 1970 are called Unix
|
199 |
|
|
// times.
|
200 |
|
|
//
|
201 |
|
|
// It is tempting to just use the year 1 as the absolute epoch, defining
|
202 |
|
|
// that the routines are only valid for years >= 1. However, the
|
203 |
|
|
// routines would then be invalid when displaying the epoch in time zones
|
204 |
|
|
// west of UTC, since it is year 0. It doesn't seem tenable to say that
|
205 |
|
|
// printing the zero time correctly isn't supported in half the time
|
206 |
|
|
// zones. By comparison, it's reasonable to mishandle some times in
|
207 |
|
|
// the year -292277022399.
|
208 |
|
|
//
|
209 |
|
|
// All this is opaque to clients of the API and can be changed if a
|
210 |
|
|
// better implementation presents itself.
|
211 |
|
|
|
212 |
|
|
const (
|
213 |
|
|
// The unsigned zero year for internal calculations.
|
214 |
|
|
// Must be 1 mod 400, and times before it will not compute correctly,
|
215 |
|
|
// but otherwise can be changed at will.
|
216 |
|
|
absoluteZeroYear = -292277022399
|
217 |
|
|
|
218 |
|
|
// The year of the zero Time.
|
219 |
|
|
// Assumed by the unixToInternal computation below.
|
220 |
|
|
internalYear = 1
|
221 |
|
|
|
222 |
|
|
// The year of the zero Unix time.
|
223 |
|
|
unixYear = 1970
|
224 |
|
|
|
225 |
|
|
// Offsets to convert between internal and absolute or Unix times.
|
226 |
|
|
absoluteToInternal int64 = (absoluteZeroYear - internalYear) * 365.2425 * secondsPerDay
|
227 |
|
|
internalToAbsolute = -absoluteToInternal
|
228 |
|
|
|
229 |
|
|
unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * secondsPerDay
|
230 |
|
|
internalToUnix int64 = -unixToInternal
|
231 |
|
|
)
|
232 |
|
|
|
233 |
|
|
// IsZero reports whether t represents the zero time instant,
|
234 |
|
|
// January 1, year 1, 00:00:00 UTC.
|
235 |
|
|
func (t Time) IsZero() bool {
|
236 |
|
|
return t.sec == 0 && t.nsec == 0
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
// abs returns the time t as an absolute time, adjusted by the zone offset.
|
240 |
|
|
// It is called when computing a presentation property like Month or Hour.
|
241 |
|
|
func (t Time) abs() uint64 {
|
242 |
|
|
l := t.loc
|
243 |
|
|
if l == nil {
|
244 |
|
|
l = &utcLoc
|
245 |
|
|
}
|
246 |
|
|
// Avoid function call if we hit the local time cache.
|
247 |
|
|
sec := t.sec + internalToUnix
|
248 |
|
|
if l != &utcLoc {
|
249 |
|
|
if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
|
250 |
|
|
sec += int64(l.cacheZone.offset)
|
251 |
|
|
} else {
|
252 |
|
|
_, offset, _, _, _ := l.lookup(sec)
|
253 |
|
|
sec += int64(offset)
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
return uint64(sec + (unixToInternal + internalToAbsolute))
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
// Date returns the year, month, and day in which t occurs.
|
260 |
|
|
func (t Time) Date() (year int, month Month, day int) {
|
261 |
|
|
year, month, day, _ = t.date(true)
|
262 |
|
|
return
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
// Year returns the year in which t occurs.
|
266 |
|
|
func (t Time) Year() int {
|
267 |
|
|
year, _, _, _ := t.date(false)
|
268 |
|
|
return year
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
// Month returns the month of the year specified by t.
|
272 |
|
|
func (t Time) Month() Month {
|
273 |
|
|
_, month, _, _ := t.date(true)
|
274 |
|
|
return month
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
// Day returns the day of the month specified by t.
|
278 |
|
|
func (t Time) Day() int {
|
279 |
|
|
_, _, day, _ := t.date(true)
|
280 |
|
|
return day
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
// Weekday returns the day of the week specified by t.
|
284 |
|
|
func (t Time) Weekday() Weekday {
|
285 |
|
|
// January 1 of the absolute year, like January 1 of 2001, was a Monday.
|
286 |
|
|
sec := (t.abs() + uint64(Monday)*secondsPerDay) % secondsPerWeek
|
287 |
|
|
return Weekday(int(sec) / secondsPerDay)
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
// ISOWeek returns the ISO 8601 year and week number in which t occurs.
|
291 |
|
|
// Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to
|
292 |
|
|
// week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1
|
293 |
|
|
// of year n+1.
|
294 |
|
|
func (t Time) ISOWeek() (year, week int) {
|
295 |
|
|
year, month, day, yday := t.date(true)
|
296 |
|
|
wday := int(t.Weekday()+6) % 7 // weekday but Monday = 0.
|
297 |
|
|
const (
|
298 |
|
|
Mon int = iota
|
299 |
|
|
Tue
|
300 |
|
|
Wed
|
301 |
|
|
Thu
|
302 |
|
|
Fri
|
303 |
|
|
Sat
|
304 |
|
|
Sun
|
305 |
|
|
)
|
306 |
|
|
|
307 |
|
|
// Calculate week as number of Mondays in year up to
|
308 |
|
|
// and including today, plus 1 because the first week is week 0.
|
309 |
|
|
// Putting the + 1 inside the numerator as a + 7 keeps the
|
310 |
|
|
// numerator from being negative, which would cause it to
|
311 |
|
|
// round incorrectly.
|
312 |
|
|
week = (yday - wday + 7) / 7
|
313 |
|
|
|
314 |
|
|
// The week number is now correct under the assumption
|
315 |
|
|
// that the first Monday of the year is in week 1.
|
316 |
|
|
// If Jan 1 is a Tuesday, Wednesday, or Thursday, the first Monday
|
317 |
|
|
// is actually in week 2.
|
318 |
|
|
jan1wday := (wday - yday + 7*53) % 7
|
319 |
|
|
if Tue <= jan1wday && jan1wday <= Thu {
|
320 |
|
|
week++
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
// If the week number is still 0, we're in early January but in
|
324 |
|
|
// the last week of last year.
|
325 |
|
|
if week == 0 {
|
326 |
|
|
year--
|
327 |
|
|
week = 52
|
328 |
|
|
// A year has 53 weeks when Jan 1 or Dec 31 is a Thursday,
|
329 |
|
|
// meaning Jan 1 of the next year is a Friday
|
330 |
|
|
// or it was a leap year and Jan 1 of the next year is a Saturday.
|
331 |
|
|
if jan1wday == Fri || (jan1wday == Sat && isLeap(year)) {
|
332 |
|
|
week++
|
333 |
|
|
}
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
// December 29 to 31 are in week 1 of next year if
|
337 |
|
|
// they are after the last Thursday of the year and
|
338 |
|
|
// December 31 is a Monday, Tuesday, or Wednesday.
|
339 |
|
|
if month == December && day >= 29 && wday < Thu {
|
340 |
|
|
if dec31wday := (wday + 31 - day) % 7; Mon <= dec31wday && dec31wday <= Wed {
|
341 |
|
|
year++
|
342 |
|
|
week = 1
|
343 |
|
|
}
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
return
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
// Clock returns the hour, minute, and second within the day specified by t.
|
350 |
|
|
func (t Time) Clock() (hour, min, sec int) {
|
351 |
|
|
sec = int(t.abs() % secondsPerDay)
|
352 |
|
|
hour = sec / secondsPerHour
|
353 |
|
|
sec -= hour * secondsPerHour
|
354 |
|
|
min = sec / secondsPerMinute
|
355 |
|
|
sec -= min * secondsPerMinute
|
356 |
|
|
return
|
357 |
|
|
}
|
358 |
|
|
|
359 |
|
|
// Hour returns the hour within the day specified by t, in the range [0, 23].
|
360 |
|
|
func (t Time) Hour() int {
|
361 |
|
|
return int(t.abs()%secondsPerDay) / secondsPerHour
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
// Minute returns the minute offset within the hour specified by t, in the range [0, 59].
|
365 |
|
|
func (t Time) Minute() int {
|
366 |
|
|
return int(t.abs()%secondsPerHour) / secondsPerMinute
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
// Second returns the second offset within the minute specified by t, in the range [0, 59].
|
370 |
|
|
func (t Time) Second() int {
|
371 |
|
|
return int(t.abs() % secondsPerMinute)
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
// Nanosecond returns the nanosecond offset within the second specified by t,
|
375 |
|
|
// in the range [0, 999999999].
|
376 |
|
|
func (t Time) Nanosecond() int {
|
377 |
|
|
return int(t.nsec)
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
// A Duration represents the elapsed time between two instants
|
381 |
|
|
// as an int64 nanosecond count. The representation limits the
|
382 |
|
|
// largest representable duration to approximately 290 years.
|
383 |
|
|
type Duration int64
|
384 |
|
|
|
385 |
|
|
// Common durations. There is no definition for units of Day or larger
|
386 |
|
|
// to avoid confusion across daylight savings time zone transitions.
|
387 |
|
|
const (
|
388 |
|
|
Nanosecond Duration = 1
|
389 |
|
|
Microsecond = 1000 * Nanosecond
|
390 |
|
|
Millisecond = 1000 * Microsecond
|
391 |
|
|
Second = 1000 * Millisecond
|
392 |
|
|
Minute = 60 * Second
|
393 |
|
|
Hour = 60 * Minute
|
394 |
|
|
)
|
395 |
|
|
|
396 |
|
|
// Duration returns a string representing the duration in the form "72h3m0.5s".
|
397 |
|
|
// Leading zero units are omitted. As a special case, durations less than one
|
398 |
|
|
// second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
|
399 |
|
|
// that the leading digit is non-zero. The zero duration formats as 0,
|
400 |
|
|
// with no unit.
|
401 |
|
|
func (d Duration) String() string {
|
402 |
|
|
// Largest time is 2540400h10m10.000000000s
|
403 |
|
|
var buf [32]byte
|
404 |
|
|
w := len(buf)
|
405 |
|
|
|
406 |
|
|
u := uint64(d)
|
407 |
|
|
neg := d < 0
|
408 |
|
|
if neg {
|
409 |
|
|
u = -u
|
410 |
|
|
}
|
411 |
|
|
|
412 |
|
|
if u < uint64(Second) {
|
413 |
|
|
// Special case: if duration is smaller than a second,
|
414 |
|
|
// use smaller units, like 1.2ms
|
415 |
|
|
var (
|
416 |
|
|
prec int
|
417 |
|
|
unit byte
|
418 |
|
|
)
|
419 |
|
|
switch {
|
420 |
|
|
case u == 0:
|
421 |
|
|
return "0"
|
422 |
|
|
case u < uint64(Microsecond):
|
423 |
|
|
// print nanoseconds
|
424 |
|
|
prec = 0
|
425 |
|
|
unit = 'n'
|
426 |
|
|
case u < uint64(Millisecond):
|
427 |
|
|
// print microseconds
|
428 |
|
|
prec = 3
|
429 |
|
|
unit = 'u'
|
430 |
|
|
default:
|
431 |
|
|
// print milliseconds
|
432 |
|
|
prec = 6
|
433 |
|
|
unit = 'm'
|
434 |
|
|
}
|
435 |
|
|
w -= 2
|
436 |
|
|
buf[w] = unit
|
437 |
|
|
buf[w+1] = 's'
|
438 |
|
|
w, u = fmtFrac(buf[:w], u, prec)
|
439 |
|
|
w = fmtInt(buf[:w], u)
|
440 |
|
|
} else {
|
441 |
|
|
w--
|
442 |
|
|
buf[w] = 's'
|
443 |
|
|
|
444 |
|
|
w, u = fmtFrac(buf[:w], u, 9)
|
445 |
|
|
|
446 |
|
|
// u is now integer seconds
|
447 |
|
|
w = fmtInt(buf[:w], u%60)
|
448 |
|
|
u /= 60
|
449 |
|
|
|
450 |
|
|
// u is now integer minutes
|
451 |
|
|
if u > 0 {
|
452 |
|
|
w--
|
453 |
|
|
buf[w] = 'm'
|
454 |
|
|
w = fmtInt(buf[:w], u%60)
|
455 |
|
|
u /= 60
|
456 |
|
|
|
457 |
|
|
// u is now integer hours
|
458 |
|
|
// Stop at hours because days can be different lengths.
|
459 |
|
|
if u > 0 {
|
460 |
|
|
w--
|
461 |
|
|
buf[w] = 'h'
|
462 |
|
|
w = fmtInt(buf[:w], u)
|
463 |
|
|
}
|
464 |
|
|
}
|
465 |
|
|
}
|
466 |
|
|
|
467 |
|
|
if neg {
|
468 |
|
|
w--
|
469 |
|
|
buf[w] = '-'
|
470 |
|
|
}
|
471 |
|
|
|
472 |
|
|
return string(buf[w:])
|
473 |
|
|
}
|
474 |
|
|
|
475 |
|
|
// fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the
|
476 |
|
|
// tail of buf, omitting trailing zeros. it omits the decimal
|
477 |
|
|
// point too when the fraction is 0. It returns the index where the
|
478 |
|
|
// output bytes begin and the value v/10**prec.
|
479 |
|
|
func fmtFrac(buf []byte, v uint64, prec int) (nw int, nv uint64) {
|
480 |
|
|
// Omit trailing zeros up to and including decimal point.
|
481 |
|
|
w := len(buf)
|
482 |
|
|
print := false
|
483 |
|
|
for i := 0; i < prec; i++ {
|
484 |
|
|
digit := v % 10
|
485 |
|
|
print = print || digit != 0
|
486 |
|
|
if print {
|
487 |
|
|
w--
|
488 |
|
|
buf[w] = byte(digit) + '0'
|
489 |
|
|
}
|
490 |
|
|
v /= 10
|
491 |
|
|
}
|
492 |
|
|
if print {
|
493 |
|
|
w--
|
494 |
|
|
buf[w] = '.'
|
495 |
|
|
}
|
496 |
|
|
return w, v
|
497 |
|
|
}
|
498 |
|
|
|
499 |
|
|
// fmtInt formats v into the tail of buf.
|
500 |
|
|
// It returns the index where the output begins.
|
501 |
|
|
func fmtInt(buf []byte, v uint64) int {
|
502 |
|
|
w := len(buf)
|
503 |
|
|
if v == 0 {
|
504 |
|
|
w--
|
505 |
|
|
buf[w] = '0'
|
506 |
|
|
} else {
|
507 |
|
|
for v > 0 {
|
508 |
|
|
w--
|
509 |
|
|
buf[w] = byte(v%10) + '0'
|
510 |
|
|
v /= 10
|
511 |
|
|
}
|
512 |
|
|
}
|
513 |
|
|
return w
|
514 |
|
|
}
|
515 |
|
|
|
516 |
|
|
// Nanoseconds returns the duration as an integer nanosecond count.
|
517 |
|
|
func (d Duration) Nanoseconds() int64 { return int64(d) }
|
518 |
|
|
|
519 |
|
|
// These methods return float64 because the dominant
|
520 |
|
|
// use case is for printing a floating point number like 1.5s, and
|
521 |
|
|
// a truncation to integer would make them not useful in those cases.
|
522 |
|
|
// Splitting the integer and fraction ourselves guarantees that
|
523 |
|
|
// converting the returned float64 to an integer rounds the same
|
524 |
|
|
// way that a pure integer conversion would have, even in cases
|
525 |
|
|
// where, say, float64(d.Nanoseconds())/1e9 would have rounded
|
526 |
|
|
// differently.
|
527 |
|
|
|
528 |
|
|
// Seconds returns the duration as a floating point number of seconds.
|
529 |
|
|
func (d Duration) Seconds() float64 {
|
530 |
|
|
sec := d / Second
|
531 |
|
|
nsec := d % Second
|
532 |
|
|
return float64(sec) + float64(nsec)*1e-9
|
533 |
|
|
}
|
534 |
|
|
|
535 |
|
|
// Minutes returns the duration as a floating point number of minutes.
|
536 |
|
|
func (d Duration) Minutes() float64 {
|
537 |
|
|
min := d / Minute
|
538 |
|
|
nsec := d % Minute
|
539 |
|
|
return float64(min) + float64(nsec)*(1e-9/60)
|
540 |
|
|
}
|
541 |
|
|
|
542 |
|
|
// Hours returns the duration as a floating point number of hours.
|
543 |
|
|
func (d Duration) Hours() float64 {
|
544 |
|
|
hour := d / Hour
|
545 |
|
|
nsec := d % Hour
|
546 |
|
|
return float64(hour) + float64(nsec)*(1e-9/60/60)
|
547 |
|
|
}
|
548 |
|
|
|
549 |
|
|
// Add returns the time t+d.
|
550 |
|
|
func (t Time) Add(d Duration) Time {
|
551 |
|
|
t.sec += int64(d / 1e9)
|
552 |
|
|
t.nsec += int32(d % 1e9)
|
553 |
|
|
if t.nsec >= 1e9 {
|
554 |
|
|
t.sec++
|
555 |
|
|
t.nsec -= 1e9
|
556 |
|
|
} else if t.nsec < 0 {
|
557 |
|
|
t.sec--
|
558 |
|
|
t.nsec += 1e9
|
559 |
|
|
}
|
560 |
|
|
return t
|
561 |
|
|
}
|
562 |
|
|
|
563 |
|
|
// Sub returns the duration t-u.
|
564 |
|
|
// To compute t-d for a duration d, use t.Add(-d).
|
565 |
|
|
func (t Time) Sub(u Time) Duration {
|
566 |
|
|
return Duration(t.sec-u.sec)*Second + Duration(t.nsec-u.nsec)
|
567 |
|
|
}
|
568 |
|
|
|
569 |
|
|
// Since returns the time elapsed since t.
|
570 |
|
|
// It is shorthand for time.Now().Sub(t).
|
571 |
|
|
func Since(t Time) Duration {
|
572 |
|
|
return Now().Sub(t)
|
573 |
|
|
}
|
574 |
|
|
|
575 |
|
|
// AddDate returns the time corresponding to adding the
|
576 |
|
|
// given number of years, months, and days to t.
|
577 |
|
|
// For example, AddDate(-1, 2, 3) applied to January 1, 2011
|
578 |
|
|
// returns March 4, 2010.
|
579 |
|
|
//
|
580 |
|
|
// AddDate normalizes its result in the same way that Date does,
|
581 |
|
|
// so, for example, adding one month to October 31 yields
|
582 |
|
|
// December 1, the normalized form for November 31.
|
583 |
|
|
func (t Time) AddDate(years int, months int, days int) Time {
|
584 |
|
|
year, month, day := t.Date()
|
585 |
|
|
hour, min, sec := t.Clock()
|
586 |
|
|
return Date(year+years, month+Month(months), day+days, hour, min, sec, int(t.nsec), t.loc)
|
587 |
|
|
}
|
588 |
|
|
|
589 |
|
|
const (
|
590 |
|
|
secondsPerMinute = 60
|
591 |
|
|
secondsPerHour = 60 * 60
|
592 |
|
|
secondsPerDay = 24 * secondsPerHour
|
593 |
|
|
secondsPerWeek = 7 * secondsPerDay
|
594 |
|
|
daysPer400Years = 365*400 + 97
|
595 |
|
|
daysPer100Years = 365*100 + 24
|
596 |
|
|
daysPer4Years = 365*4 + 1
|
597 |
|
|
days1970To2001 = 31*365 + 8
|
598 |
|
|
)
|
599 |
|
|
|
600 |
|
|
// date computes the year and, only when full=true,
|
601 |
|
|
// the month and day in which t occurs.
|
602 |
|
|
func (t Time) date(full bool) (year int, month Month, day int, yday int) {
|
603 |
|
|
// Split into time and day.
|
604 |
|
|
d := t.abs() / secondsPerDay
|
605 |
|
|
|
606 |
|
|
// Account for 400 year cycles.
|
607 |
|
|
n := d / daysPer400Years
|
608 |
|
|
y := 400 * n
|
609 |
|
|
d -= daysPer400Years * n
|
610 |
|
|
|
611 |
|
|
// Cut off 100-year cycles.
|
612 |
|
|
// The last cycle has one extra leap year, so on the last day
|
613 |
|
|
// of that year, day / daysPer100Years will be 4 instead of 3.
|
614 |
|
|
// Cut it back down to 3 by subtracting n>>2.
|
615 |
|
|
n = d / daysPer100Years
|
616 |
|
|
n -= n >> 2
|
617 |
|
|
y += 100 * n
|
618 |
|
|
d -= daysPer100Years * n
|
619 |
|
|
|
620 |
|
|
// Cut off 4-year cycles.
|
621 |
|
|
// The last cycle has a missing leap year, which does not
|
622 |
|
|
// affect the computation.
|
623 |
|
|
n = d / daysPer4Years
|
624 |
|
|
y += 4 * n
|
625 |
|
|
d -= daysPer4Years * n
|
626 |
|
|
|
627 |
|
|
// Cut off years within a 4-year cycle.
|
628 |
|
|
// The last year is a leap year, so on the last day of that year,
|
629 |
|
|
// day / 365 will be 4 instead of 3. Cut it back down to 3
|
630 |
|
|
// by subtracting n>>2.
|
631 |
|
|
n = d / 365
|
632 |
|
|
n -= n >> 2
|
633 |
|
|
y += n
|
634 |
|
|
d -= 365 * n
|
635 |
|
|
|
636 |
|
|
year = int(int64(y) + absoluteZeroYear)
|
637 |
|
|
yday = int(d)
|
638 |
|
|
|
639 |
|
|
if !full {
|
640 |
|
|
return
|
641 |
|
|
}
|
642 |
|
|
|
643 |
|
|
day = yday
|
644 |
|
|
if isLeap(year) {
|
645 |
|
|
// Leap year
|
646 |
|
|
switch {
|
647 |
|
|
case day > 31+29-1:
|
648 |
|
|
// After leap day; pretend it wasn't there.
|
649 |
|
|
day--
|
650 |
|
|
case day == 31+29-1:
|
651 |
|
|
// Leap day.
|
652 |
|
|
month = February
|
653 |
|
|
day = 29
|
654 |
|
|
return
|
655 |
|
|
}
|
656 |
|
|
}
|
657 |
|
|
|
658 |
|
|
// Estimate month on assumption that every month has 31 days.
|
659 |
|
|
// The estimate may be too low by at most one month, so adjust.
|
660 |
|
|
month = Month(day / 31)
|
661 |
|
|
end := int(daysBefore[month+1])
|
662 |
|
|
var begin int
|
663 |
|
|
if day >= end {
|
664 |
|
|
month++
|
665 |
|
|
begin = end
|
666 |
|
|
} else {
|
667 |
|
|
begin = int(daysBefore[month])
|
668 |
|
|
}
|
669 |
|
|
|
670 |
|
|
month++ // because January is 1
|
671 |
|
|
day = day - begin + 1
|
672 |
|
|
return
|
673 |
|
|
}
|
674 |
|
|
|
675 |
|
|
// daysBefore[m] counts the number of days in a non-leap year
|
676 |
|
|
// before month m begins. There is an entry for m=12, counting
|
677 |
|
|
// the number of days before January of next year (365).
|
678 |
|
|
var daysBefore = [...]int32{
|
679 |
|
|
0,
|
680 |
|
|
31,
|
681 |
|
|
31 + 28,
|
682 |
|
|
31 + 28 + 31,
|
683 |
|
|
31 + 28 + 31 + 30,
|
684 |
|
|
31 + 28 + 31 + 30 + 31,
|
685 |
|
|
31 + 28 + 31 + 30 + 31 + 30,
|
686 |
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31,
|
687 |
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
|
688 |
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
|
689 |
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
|
690 |
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
|
691 |
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
|
692 |
|
|
}
|
693 |
|
|
|
694 |
|
|
func daysIn(m Month, year int) int {
|
695 |
|
|
if m == February && isLeap(year) {
|
696 |
|
|
return 29
|
697 |
|
|
}
|
698 |
|
|
return int(daysBefore[m] - daysBefore[m-1])
|
699 |
|
|
}
|
700 |
|
|
|
701 |
|
|
// Provided by package runtime.
|
702 |
|
|
func now() (sec int64, nsec int32)
|
703 |
|
|
|
704 |
|
|
// Now returns the current local time.
|
705 |
|
|
func Now() Time {
|
706 |
|
|
sec, nsec := now()
|
707 |
|
|
return Time{sec + unixToInternal, nsec, Local}
|
708 |
|
|
}
|
709 |
|
|
|
710 |
|
|
// UTC returns t with the location set to UTC.
|
711 |
|
|
func (t Time) UTC() Time {
|
712 |
|
|
t.loc = UTC
|
713 |
|
|
return t
|
714 |
|
|
}
|
715 |
|
|
|
716 |
|
|
// Local returns t with the location set to local time.
|
717 |
|
|
func (t Time) Local() Time {
|
718 |
|
|
t.loc = Local
|
719 |
|
|
return t
|
720 |
|
|
}
|
721 |
|
|
|
722 |
|
|
// In returns t with the location information set to loc.
|
723 |
|
|
//
|
724 |
|
|
// In panics if loc is nil.
|
725 |
|
|
func (t Time) In(loc *Location) Time {
|
726 |
|
|
if loc == nil {
|
727 |
|
|
panic("time: missing Location in call to Time.In")
|
728 |
|
|
}
|
729 |
|
|
t.loc = loc
|
730 |
|
|
return t
|
731 |
|
|
}
|
732 |
|
|
|
733 |
|
|
// Location returns the time zone information associated with t.
|
734 |
|
|
func (t Time) Location() *Location {
|
735 |
|
|
l := t.loc
|
736 |
|
|
if l == nil {
|
737 |
|
|
l = UTC
|
738 |
|
|
}
|
739 |
|
|
return l
|
740 |
|
|
}
|
741 |
|
|
|
742 |
|
|
// Zone computes the time zone in effect at time t, returning the abbreviated
|
743 |
|
|
// name of the zone (such as "CET") and its offset in seconds east of UTC.
|
744 |
|
|
func (t Time) Zone() (name string, offset int) {
|
745 |
|
|
name, offset, _, _, _ = t.loc.lookup(t.sec + internalToUnix)
|
746 |
|
|
return
|
747 |
|
|
}
|
748 |
|
|
|
749 |
|
|
// Unix returns the Unix time, the number of seconds elapsed
|
750 |
|
|
// since January 1, 1970 UTC.
|
751 |
|
|
func (t Time) Unix() int64 {
|
752 |
|
|
return t.sec + internalToUnix
|
753 |
|
|
}
|
754 |
|
|
|
755 |
|
|
// UnixNano returns the Unix time, the number of nanoseconds elapsed
|
756 |
|
|
// since January 1, 1970 UTC.
|
757 |
|
|
func (t Time) UnixNano() int64 {
|
758 |
|
|
return (t.sec+internalToUnix)*1e9 + int64(t.nsec)
|
759 |
|
|
}
|
760 |
|
|
|
761 |
|
|
type gobError string
|
762 |
|
|
|
763 |
|
|
func (g gobError) Error() string { return string(g) }
|
764 |
|
|
|
765 |
|
|
const timeGobVersion byte = 1
|
766 |
|
|
|
767 |
|
|
// GobEncode implements the gob.GobEncoder interface.
|
768 |
|
|
func (t Time) GobEncode() ([]byte, error) {
|
769 |
|
|
var offsetMin int16 // minutes east of UTC. -1 is UTC.
|
770 |
|
|
|
771 |
|
|
if t.Location() == &utcLoc {
|
772 |
|
|
offsetMin = -1
|
773 |
|
|
} else {
|
774 |
|
|
_, offset := t.Zone()
|
775 |
|
|
if offset%60 != 0 {
|
776 |
|
|
return nil, errors.New("Time.GobEncode: zone offset has fractional minute")
|
777 |
|
|
}
|
778 |
|
|
offset /= 60
|
779 |
|
|
if offset < -32768 || offset == -1 || offset > 32767 {
|
780 |
|
|
return nil, errors.New("Time.GobEncode: unexpected zone offset")
|
781 |
|
|
}
|
782 |
|
|
offsetMin = int16(offset)
|
783 |
|
|
}
|
784 |
|
|
|
785 |
|
|
enc := []byte{
|
786 |
|
|
timeGobVersion, // byte 0 : version
|
787 |
|
|
byte(t.sec >> 56), // bytes 1-8: seconds
|
788 |
|
|
byte(t.sec >> 48),
|
789 |
|
|
byte(t.sec >> 40),
|
790 |
|
|
byte(t.sec >> 32),
|
791 |
|
|
byte(t.sec >> 24),
|
792 |
|
|
byte(t.sec >> 16),
|
793 |
|
|
byte(t.sec >> 8),
|
794 |
|
|
byte(t.sec),
|
795 |
|
|
byte(t.nsec >> 24), // bytes 9-12: nanoseconds
|
796 |
|
|
byte(t.nsec >> 16),
|
797 |
|
|
byte(t.nsec >> 8),
|
798 |
|
|
byte(t.nsec),
|
799 |
|
|
byte(offsetMin >> 8), // bytes 13-14: zone offset in minutes
|
800 |
|
|
byte(offsetMin),
|
801 |
|
|
}
|
802 |
|
|
|
803 |
|
|
return enc, nil
|
804 |
|
|
}
|
805 |
|
|
|
806 |
|
|
// GobDecode implements the gob.GobDecoder interface.
|
807 |
|
|
func (t *Time) GobDecode(buf []byte) error {
|
808 |
|
|
if len(buf) == 0 {
|
809 |
|
|
return errors.New("Time.GobDecode: no data")
|
810 |
|
|
}
|
811 |
|
|
|
812 |
|
|
if buf[0] != timeGobVersion {
|
813 |
|
|
return errors.New("Time.GobDecode: unsupported version")
|
814 |
|
|
}
|
815 |
|
|
|
816 |
|
|
if len(buf) != /*version*/ 1+ /*sec*/ 8+ /*nsec*/ 4+ /*zone offset*/ 2 {
|
817 |
|
|
return errors.New("Time.GobDecode: invalid length")
|
818 |
|
|
}
|
819 |
|
|
|
820 |
|
|
buf = buf[1:]
|
821 |
|
|
t.sec = int64(buf[7]) | int64(buf[6])<<8 | int64(buf[5])<<16 | int64(buf[4])<<24 |
|
822 |
|
|
int64(buf[3])<<32 | int64(buf[2])<<40 | int64(buf[1])<<48 | int64(buf[0])<<56
|
823 |
|
|
|
824 |
|
|
buf = buf[8:]
|
825 |
|
|
t.nsec = int32(buf[3]) | int32(buf[2])<<8 | int32(buf[1])<<16 | int32(buf[0])<<24
|
826 |
|
|
|
827 |
|
|
buf = buf[4:]
|
828 |
|
|
offset := int(int16(buf[1])|int16(buf[0])<<8) * 60
|
829 |
|
|
|
830 |
|
|
if offset == -1*60 {
|
831 |
|
|
t.loc = &utcLoc
|
832 |
|
|
} else if _, localoff, _, _, _ := Local.lookup(t.sec + internalToUnix); offset == localoff {
|
833 |
|
|
t.loc = Local
|
834 |
|
|
} else {
|
835 |
|
|
t.loc = FixedZone("", offset)
|
836 |
|
|
}
|
837 |
|
|
|
838 |
|
|
return nil
|
839 |
|
|
}
|
840 |
|
|
|
841 |
|
|
// MarshalJSON implements the json.Marshaler interface.
|
842 |
|
|
// Time is formatted as RFC3339.
|
843 |
|
|
func (t Time) MarshalJSON() ([]byte, error) {
|
844 |
|
|
yearInt := t.Year()
|
845 |
|
|
if yearInt < 0 || yearInt > 9999 {
|
846 |
|
|
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
|
847 |
|
|
}
|
848 |
|
|
|
849 |
|
|
// We need a four-digit year, but Format produces variable-width years.
|
850 |
|
|
year := itoa(yearInt)
|
851 |
|
|
year = "0000"[:4-len(year)] + year
|
852 |
|
|
|
853 |
|
|
var formattedTime string
|
854 |
|
|
if t.nsec == 0 {
|
855 |
|
|
// RFC3339, no fractional second
|
856 |
|
|
formattedTime = t.Format("-01-02T15:04:05Z07:00")
|
857 |
|
|
} else {
|
858 |
|
|
// RFC3339 with fractional second
|
859 |
|
|
formattedTime = t.Format("-01-02T15:04:05.000000000Z07:00")
|
860 |
|
|
|
861 |
|
|
// Trim trailing zeroes from fractional second.
|
862 |
|
|
const nanoEnd = 24 // Index of last digit of fractional second
|
863 |
|
|
var i int
|
864 |
|
|
for i = nanoEnd; formattedTime[i] == '0'; i-- {
|
865 |
|
|
// Seek backwards until first significant digit is found.
|
866 |
|
|
}
|
867 |
|
|
|
868 |
|
|
formattedTime = formattedTime[:i+1] + formattedTime[nanoEnd+1:]
|
869 |
|
|
}
|
870 |
|
|
|
871 |
|
|
buf := make([]byte, 0, 1+len(year)+len(formattedTime)+1)
|
872 |
|
|
buf = append(buf, '"')
|
873 |
|
|
buf = append(buf, year...)
|
874 |
|
|
buf = append(buf, formattedTime...)
|
875 |
|
|
buf = append(buf, '"')
|
876 |
|
|
return buf, nil
|
877 |
|
|
}
|
878 |
|
|
|
879 |
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
880 |
|
|
// Time is expected in RFC3339 format.
|
881 |
|
|
func (t *Time) UnmarshalJSON(data []byte) (err error) {
|
882 |
|
|
*t, err = Parse("\""+RFC3339+"\"", string(data))
|
883 |
|
|
// Fractional seconds are handled implicitly by Parse.
|
884 |
|
|
return
|
885 |
|
|
}
|
886 |
|
|
|
887 |
|
|
// Unix returns the local Time corresponding to the given Unix time,
|
888 |
|
|
// sec seconds and nsec nanoseconds since January 1, 1970 UTC.
|
889 |
|
|
// It is valid to pass nsec outside the range [0, 999999999].
|
890 |
|
|
func Unix(sec int64, nsec int64) Time {
|
891 |
|
|
if nsec < 0 || nsec >= 1e9 {
|
892 |
|
|
n := nsec / 1e9
|
893 |
|
|
sec += n
|
894 |
|
|
nsec -= n * 1e9
|
895 |
|
|
if nsec < 0 {
|
896 |
|
|
nsec += 1e9
|
897 |
|
|
sec--
|
898 |
|
|
}
|
899 |
|
|
}
|
900 |
|
|
return Time{sec + unixToInternal, int32(nsec), Local}
|
901 |
|
|
}
|
902 |
|
|
|
903 |
|
|
func isLeap(year int) bool {
|
904 |
|
|
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
905 |
|
|
}
|
906 |
|
|
|
907 |
|
|
// norm returns nhi, nlo such that
|
908 |
|
|
// hi * base + lo == nhi * base + nlo
|
909 |
|
|
// 0 <= nlo < base
|
910 |
|
|
func norm(hi, lo, base int) (nhi, nlo int) {
|
911 |
|
|
if lo < 0 {
|
912 |
|
|
n := (-lo-1)/base + 1
|
913 |
|
|
hi -= n
|
914 |
|
|
lo += n * base
|
915 |
|
|
}
|
916 |
|
|
if lo >= base {
|
917 |
|
|
n := lo / base
|
918 |
|
|
hi += n
|
919 |
|
|
lo -= n * base
|
920 |
|
|
}
|
921 |
|
|
return hi, lo
|
922 |
|
|
}
|
923 |
|
|
|
924 |
|
|
// Date returns the Time corresponding to
|
925 |
|
|
// yyyy-mm-dd hh:mm:ss + nsec nanoseconds
|
926 |
|
|
// in the appropriate zone for that time in the given location.
|
927 |
|
|
//
|
928 |
|
|
// The month, day, hour, min, sec, and nsec values may be outside
|
929 |
|
|
// their usual ranges and will be normalized during the conversion.
|
930 |
|
|
// For example, October 32 converts to November 1.
|
931 |
|
|
//
|
932 |
|
|
// A daylight savings time transition skips or repeats times.
|
933 |
|
|
// For example, in the United States, March 13, 2011 2:15am never occurred,
|
934 |
|
|
// while November 6, 2011 1:15am occurred twice. In such cases, the
|
935 |
|
|
// choice of time zone, and therefore the time, is not well-defined.
|
936 |
|
|
// Date returns a time that is correct in one of the two zones involved
|
937 |
|
|
// in the transition, but it does not guarantee which.
|
938 |
|
|
//
|
939 |
|
|
// Date panics if loc is nil.
|
940 |
|
|
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {
|
941 |
|
|
if loc == nil {
|
942 |
|
|
panic("time: missing Location in call to Date")
|
943 |
|
|
}
|
944 |
|
|
|
945 |
|
|
// Normalize month, overflowing into year.
|
946 |
|
|
m := int(month) - 1
|
947 |
|
|
year, m = norm(year, m, 12)
|
948 |
|
|
month = Month(m) + 1
|
949 |
|
|
|
950 |
|
|
// Normalize nsec, sec, min, hour, overflowing into day.
|
951 |
|
|
sec, nsec = norm(sec, nsec, 1e9)
|
952 |
|
|
min, sec = norm(min, sec, 60)
|
953 |
|
|
hour, min = norm(hour, min, 60)
|
954 |
|
|
day, hour = norm(day, hour, 24)
|
955 |
|
|
|
956 |
|
|
y := uint64(int64(year) - absoluteZeroYear)
|
957 |
|
|
|
958 |
|
|
// Compute days since the absolute epoch.
|
959 |
|
|
|
960 |
|
|
// Add in days from 400-year cycles.
|
961 |
|
|
n := y / 400
|
962 |
|
|
y -= 400 * n
|
963 |
|
|
d := daysPer400Years * n
|
964 |
|
|
|
965 |
|
|
// Add in 100-year cycles.
|
966 |
|
|
n = y / 100
|
967 |
|
|
y -= 100 * n
|
968 |
|
|
d += daysPer100Years * n
|
969 |
|
|
|
970 |
|
|
// Add in 4-year cycles.
|
971 |
|
|
n = y / 4
|
972 |
|
|
y -= 4 * n
|
973 |
|
|
d += daysPer4Years * n
|
974 |
|
|
|
975 |
|
|
// Add in non-leap years.
|
976 |
|
|
n = y
|
977 |
|
|
d += 365 * n
|
978 |
|
|
|
979 |
|
|
// Add in days before this month.
|
980 |
|
|
d += uint64(daysBefore[month-1])
|
981 |
|
|
if isLeap(year) && month >= March {
|
982 |
|
|
d++ // February 29
|
983 |
|
|
}
|
984 |
|
|
|
985 |
|
|
// Add in days before today.
|
986 |
|
|
d += uint64(day - 1)
|
987 |
|
|
|
988 |
|
|
// Add in time elapsed today.
|
989 |
|
|
abs := d * secondsPerDay
|
990 |
|
|
abs += uint64(hour*secondsPerHour + min*secondsPerMinute + sec)
|
991 |
|
|
|
992 |
|
|
unix := int64(abs) + (absoluteToInternal + internalToUnix)
|
993 |
|
|
|
994 |
|
|
// Look for zone offset for t, so we can adjust to UTC.
|
995 |
|
|
// The lookup function expects UTC, so we pass t in the
|
996 |
|
|
// hope that it will not be too close to a zone transition,
|
997 |
|
|
// and then adjust if it is.
|
998 |
|
|
_, offset, _, start, end := loc.lookup(unix)
|
999 |
|
|
if offset != 0 {
|
1000 |
|
|
switch utc := unix - int64(offset); {
|
1001 |
|
|
case utc < start:
|
1002 |
|
|
_, offset, _, _, _ = loc.lookup(start - 1)
|
1003 |
|
|
case utc >= end:
|
1004 |
|
|
_, offset, _, _, _ = loc.lookup(end)
|
1005 |
|
|
}
|
1006 |
|
|
unix -= int64(offset)
|
1007 |
|
|
}
|
1008 |
|
|
|
1009 |
|
|
return Time{unix + unixToInternal, int32(nsec), loc}
|
1010 |
|
|
}
|