Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package time provides functionality for measuring and displaying time. | 5 // Package time provides functionality for measuring and displaying time. |
6 // | 6 // |
7 // The calendrical calculations always assume a Gregorian calendar. | 7 // The calendrical calculations always assume a Gregorian calendar. |
8 package time | 8 package time |
9 | 9 |
10 // A Time represents an instant in time with nanosecond precision. | 10 // A Time represents an instant in time with nanosecond precision. |
11 // | 11 // |
12 // Programs using times should typically store and pass them as values, | 12 // Programs using times should typically store and pass them as values, |
13 // not pointers. That is, time variables and struct fields should be of | 13 // not pointers. That is, time variables and struct fields should be of |
14 // type time.Time, not *time.Time. | 14 // type time.Time, not *time.Time. |
15 // | 15 // |
16 // Time instants can be compared using the Before, After, and Equal methods. | 16 // Time instants can be compared using the Before, After, and Equal methods. |
17 // The Sub method subtracts two instants, producing a Duration. | 17 // The Sub method subtracts two instants, producing a Duration. |
18 // The Add method adds a Time and a Duration, producing a Time. | 18 // The Add method adds a Time and a Duration, producing a Time. |
19 // | 19 // |
20 // The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. | 20 // The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. |
21 // As this time is unlikely to come up in practice, the IsZero method | 21 // As this time is unlikely to come up in practice, the IsZero method gives |
22 // gives a simple way of detecting an uninitialized time. | 22 // a simple way of detecting a time that has not been initialized explicitly. |
23 // | 23 // |
24 // Each Time has associated with it a Location, consulted when computing the | 24 // Each Time has associated with it a Location, consulted when computing the |
25 // presentation form of the time, such as in the Format, Hour, and Year methods. | 25 // presentation form of the time, such as in the Format, Hour, and Year methods. |
26 // The methods Local, UTC, and In return a Time with a specific zone. | 26 // The methods Local, UTC, and In return a Time with a specific location. |
27 // Changing the zone in this way changes only the presentation; it does not | 27 // Changing the location in this way changes only the presentation; it does not |
28 // change the instant in time being denoted and therefore does not affect the | 28 // change the instant in time being denoted and therefore does not affect the |
29 // computations described in earlier paragraphs. | 29 // computations described in earlier paragraphs. |
30 // | 30 // |
31 type Time struct { | 31 type Time struct { |
32 // sec gives the number of seconds elapsed since | 32 // sec gives the number of seconds elapsed since |
33 // January 1, year 1 00:00:00 UTC. | 33 // January 1, year 1 00:00:00 UTC. |
34 sec int64 | 34 sec int64 |
35 | 35 |
36 // nsec specifies a non-negative nanosecond | 36 // nsec specifies a non-negative nanosecond |
37 // offset within the second named by Seconds. | 37 // offset within the second named by Seconds. |
(...skipping 12 matching lines...) Expand all Loading... | |
50 func (t Time) After(u Time) bool { | 50 func (t Time) After(u Time) bool { |
51 return t.sec > u.sec || t.sec == u.sec && t.nsec > u.nsec | 51 return t.sec > u.sec || t.sec == u.sec && t.nsec > u.nsec |
52 } | 52 } |
53 | 53 |
54 // Before reports whether the time instant t is before u. | 54 // Before reports whether the time instant t is before u. |
55 func (t Time) Before(u Time) bool { | 55 func (t Time) Before(u Time) bool { |
56 return t.sec < u.sec || t.sec == u.sec && t.nsec < u.nsec | 56 return t.sec < u.sec || t.sec == u.sec && t.nsec < u.nsec |
57 } | 57 } |
58 | 58 |
59 // Equal reports whether t and u represent the same time instant. | 59 // Equal reports whether t and u represent the same time instant. |
60 // Two times can be equal even if they are in different time zones. | 60 // Two times can be equal even if they are in different locations. |
61 // For example, 1:00 +0200 CEST and 2:00 +0100 CET are Equal. | 61 // For example, 6:00 +0200 CEST and 4:00 UTC are Equal. |
62 // This comparison is different from using t == u, which also compares | 62 // This comparison is different from using t == u, which also compares |
63 // the locations. | 63 // the locations. |
64 func (t Time) Equal(u Time) bool { | 64 func (t Time) Equal(u Time) bool { |
65 return t.sec == u.sec && t.nsec == u.nsec | 65 return t.sec == u.sec && t.nsec == u.nsec |
66 } | 66 } |
67 | 67 |
68 // A Month specifies a month of the year (January = 1, ...). | 68 // A Month specifies a month of the year (January = 1, ...). |
69 type Month int | 69 type Month int |
70 | 70 |
71 const ( | 71 const ( |
72 January Month = 1 + iota | 72 January Month = 1 + iota |
73 February | 73 February |
74 March | 74 March |
75 April | 75 April |
76 May | 76 May |
77 June | 77 June |
78 July | 78 July |
79 August | 79 August |
80 September | 80 September |
81 October | 81 October |
82 November | 82 November |
83 December | 83 December |
84 ) | 84 ) |
85 | 85 |
86 var months = []string{ | 86 var months = [...]string{ |
87 "January", | 87 "January", |
88 "February", | 88 "February", |
89 "March", | 89 "March", |
90 "April", | 90 "April", |
91 "May", | 91 "May", |
92 "June", | 92 "June", |
93 "July", | 93 "July", |
94 "August", | 94 "August", |
95 "September", | 95 "September", |
96 "October", | 96 "October", |
(...skipping 10 matching lines...) Expand all Loading... | |
107 const ( | 107 const ( |
108 Sunday Weekday = iota | 108 Sunday Weekday = iota |
109 Monday | 109 Monday |
110 Tuesday | 110 Tuesday |
111 Wednesday | 111 Wednesday |
112 Thursday | 112 Thursday |
113 Friday | 113 Friday |
114 Saturday | 114 Saturday |
115 ) | 115 ) |
116 | 116 |
117 var days = []string{ | 117 var days = [...]string{ |
118 "Sunday", | 118 "Sunday", |
119 "Monday", | 119 "Monday", |
120 "Tuesday", | 120 "Tuesday", |
121 "Wednesday", | 121 "Wednesday", |
122 "Thursday", | 122 "Thursday", |
123 "Friday", | 123 "Friday", |
124 "Saturday", | 124 "Saturday", |
125 } | 125 } |
126 | 126 |
127 // String returns the English name of the day ("Sunday", "Monday", ...). | 127 // String returns the English name of the day ("Sunday", "Monday", ...). |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 l := t.loc | 240 l := t.loc |
241 if l == nil { | 241 if l == nil { |
242 l = &utcLoc | 242 l = &utcLoc |
243 } | 243 } |
244 // Avoid function call if we hit the local time cache. | 244 // Avoid function call if we hit the local time cache. |
245 sec := t.sec + internalToUnix | 245 sec := t.sec + internalToUnix |
246 if l != &utcLoc { | 246 if l != &utcLoc { |
247 if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd { | 247 if l.cacheZone != nil && l.cacheStart <= sec && sec < l.cacheEnd { |
248 sec += int64(l.cacheZone.offset) | 248 sec += int64(l.cacheZone.offset) |
249 } else { | 249 } else { |
250 » » » _, offset, _, _, _ := l.Lookup(sec) | 250 » » » _, offset, _, _, _ := l.lookup(sec) |
251 sec += int64(offset) | 251 sec += int64(offset) |
252 } | 252 } |
253 } | 253 } |
254 return uint64(sec + (unixToInternal + internalToAbsolute)) | 254 return uint64(sec + (unixToInternal + internalToAbsolute)) |
255 } | 255 } |
256 | 256 |
257 // Date returns the year, month, and day in which t occurs. | 257 // Date returns the year, month, and day in which t occurs. |
258 func (t Time) Date() (year int, month Month, day int) { | 258 func (t Time) Date() (year int, month Month, day int) { |
259 year, month, day, _ = t.date(true) | 259 year, month, day, _ = t.date(true) |
260 return | 260 return |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 // in the range [0, 999999999]. | 373 // in the range [0, 999999999]. |
374 func (t Time) Nanosecond() int { | 374 func (t Time) Nanosecond() int { |
375 return int(t.nsec) | 375 return int(t.nsec) |
376 } | 376 } |
377 | 377 |
378 // A Duration represents the elapsed time between two instants | 378 // A Duration represents the elapsed time between two instants |
379 // as an int64 nanosecond count. The representation limits the | 379 // as an int64 nanosecond count. The representation limits the |
380 // largest representable duration to approximately 290 years. | 380 // largest representable duration to approximately 290 years. |
381 type Duration int64 | 381 type Duration int64 |
382 | 382 |
383 // Common durations. There is no definition for units of Day or larger | |
384 // to avoid confusion across daylight savings time zone transitions. | |
383 const ( | 385 const ( |
384 Nanosecond Duration = 1 | 386 Nanosecond Duration = 1 |
385 Microsecond = 1000 * Nanosecond | 387 Microsecond = 1000 * Nanosecond |
386 Millisecond = 1000 * Microsecond | 388 Millisecond = 1000 * Microsecond |
387 Second = 1000 * Millisecond | 389 Second = 1000 * Millisecond |
388 Minute = 60 * Second | 390 Minute = 60 * Second |
389 Hour = 60 * Minute | 391 Hour = 60 * Minute |
390 ) | 392 ) |
391 | 393 |
392 // Duration returns a string representing the duration in the form "72h3m0.5s". | 394 // Duration returns a string representing the duration in the form "72h3m0.5s". |
393 // Leading zero units are omitted. As a special case, durations less than one | 395 // Leading zero units are omitted. As a special case, durations less than one |
394 // second format use a smaller unit—milli-, micro-, or nanoseconds—to ensure | 396 // second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure |
395 // that the leading digit is non-zero. The zero duration formats as 0, | 397 // that the leading digit is non-zero. The zero duration formats as 0, |
396 // with no unit. | 398 // with no unit. |
397 func (d Duration) String() string { | 399 func (d Duration) String() string { |
398 // Largest time is 2540400h10m10.000000000s | 400 // Largest time is 2540400h10m10.000000000s |
399 var buf [32]byte | 401 var buf [32]byte |
400 w := len(buf) | 402 w := len(buf) |
401 | 403 |
402 u := uint64(d) | 404 u := uint64(d) |
403 neg := d < 0 | 405 neg := d < 0 |
404 if neg { | 406 if neg { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
485 } | 487 } |
486 v /= 10 | 488 v /= 10 |
487 } | 489 } |
488 if print { | 490 if print { |
489 w-- | 491 w-- |
490 buf[w] = '.' | 492 buf[w] = '.' |
491 } | 493 } |
492 return w, v | 494 return w, v |
493 } | 495 } |
494 | 496 |
495 // fmtInt formats v into the tail of buf[:w]. | 497 // fmtInt formats v into the tail of buf. |
496 // It returns the index where the output begins. | 498 // It returns the index where the output begins. |
497 func fmtInt(buf []byte, v uint64) int { | 499 func fmtInt(buf []byte, v uint64) int { |
498 w := len(buf) | 500 w := len(buf) |
499 if v == 0 { | 501 if v == 0 { |
500 w-- | 502 w-- |
501 buf[w] = '0' | 503 buf[w] = '0' |
502 } else { | 504 } else { |
503 for v > 0 { | 505 for v > 0 { |
504 w-- | 506 w-- |
505 buf[w] = byte(v%10) + '0' | 507 buf[w] = byte(v%10) + '0' |
506 v /= 10 | 508 v /= 10 |
507 } | 509 } |
508 } | 510 } |
509 return w | 511 return w |
510 } | 512 } |
511 | 513 |
512 // Nanoseconds returns the duration as an integer nanosecond count. | 514 // Nanoseconds returns the duration as an integer nanosecond count. |
513 func (d Duration) Nanoseconds() int64 { return int64(d) } | 515 func (d Duration) Nanoseconds() int64 { return int64(d) } |
514 | 516 |
515 // REVIEW NOTE: The use of float64 for these methods was not | 517 // These methods return float64 because the dominant |
516 // my first choice, but in practice everyone who would have called | 518 // use case is for printing a floating point number like 1.5s, and |
517 // Seconds was instead writing float64(d.Nanoseconds())/1e9 in | 519 // a truncation to integer would make them not useful in those cases. |
518 // order to have sub-second precision. | 520 // Splitting the integer and fraction ourselves guarantees that |
r
2011/11/18 17:37:37
delete at least the phrase "REVIEW NOTE" and remov
rsc
2011/11/18 18:21:36
Done:
// These methods return float64 because the
| |
521 // converting the returned float64 to an integer rounds the same | |
522 // way that a pure integer conversion would have, even in cases | |
523 // where, say, float64(d.Nanoseconds())/1e9 would have rounded | |
524 // differently. | |
519 | 525 |
520 // Seconds returns the duration as a floating point number of seconds. | 526 // Seconds returns the duration as a floating point number of seconds. |
521 func (d Duration) Seconds() float64 { | 527 func (d Duration) Seconds() float64 { |
522 // Splitting the second vs nsec ourselves guarantees | |
523 // that int64(d.Seconds()) == d.Nanoseconds()p/1e9 | |
r
2011/11/18 17:37:37
s/p//
rsc
2011/11/18 18:21:36
Done.
| |
524 // even in cases where float64(d) / 1e9 would have | |
r
2011/11/18 17:37:37
s; / ;/;
rsc
2011/11/18 18:21:36
Done.
| |
525 // rounded differently. | |
526 sec := d / Second | 528 sec := d / Second |
527 nsec := d % Second | 529 nsec := d % Second |
528 return float64(sec) + float64(nsec)*1e-9 | 530 return float64(sec) + float64(nsec)*1e-9 |
529 } | 531 } |
530 | 532 |
531 // REVIEW NOTE: There are no uses of Minutes or Hours in the tree. | |
532 // Are these worth having? Days and Years are out because those | |
533 // units do not have fixed lengths. | |
r
2011/11/18 17:37:37
delete
rsc
2011/11/18 18:21:36
Done.
| |
534 | |
535 // Minutes returns the duration as a floating point number of minutes. | 533 // Minutes returns the duration as a floating point number of minutes. |
536 func (d Duration) Minutes() float64 { return float64(d) / float64(Minute) } | 534 func (d Duration) Minutes() float64 { |
535 » min := d / Minute | |
536 » nsec := d % Minute | |
537 » return float64(min) + float64(nsec)*(1e-9/60) | |
538 } | |
537 | 539 |
538 // Hours returns the duration as a floating point number of hours. | 540 // Hours returns the duration as a floating point number of hours. |
539 func (d Duration) Hours() float64 { return float64(d) / float64(Hour) } | 541 func (d Duration) Hours() float64 { |
542 » hour := d / Hour | |
543 » nsec := d % Hour | |
544 » return float64(hour) + float64(nsec)*(1e-9/60/60) | |
545 } | |
540 | 546 |
541 // Add returns the time t+d. | 547 // Add returns the time t+d. |
542 func (t Time) Add(d Duration) Time { | 548 func (t Time) Add(d Duration) Time { |
543 t.sec += int64(d / 1e9) | 549 t.sec += int64(d / 1e9) |
544 t.nsec += int32(d % 1e9) | 550 t.nsec += int32(d % 1e9) |
545 if t.nsec > 1e9 { | 551 if t.nsec > 1e9 { |
546 t.sec++ | 552 t.sec++ |
547 t.nsec -= 1e9 | 553 t.nsec -= 1e9 |
548 } else if t.nsec < 0 { | 554 } else if t.nsec < 0 { |
549 t.sec-- | 555 t.sec-- |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
653 31 + 28, | 659 31 + 28, |
654 31 + 28 + 31, | 660 31 + 28 + 31, |
655 31 + 28 + 31 + 30, | 661 31 + 28 + 31 + 30, |
656 31 + 28 + 31 + 30 + 31, | 662 31 + 28 + 31 + 30 + 31, |
657 31 + 28 + 31 + 30 + 31 + 30, | 663 31 + 28 + 31 + 30 + 31 + 30, |
658 31 + 28 + 31 + 30 + 31 + 30 + 31, | 664 31 + 28 + 31 + 30 + 31 + 30 + 31, |
659 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, | 665 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, |
660 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, | 666 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, |
661 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, | 667 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, |
662 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, | 668 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, |
663 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31, | 669 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31, |
r
2011/11/18 17:37:37
there's an optical illusion here, making this line
| |
664 } | 670 } |
665 | 671 |
672 func daysIn(m Month, year int) int { | |
673 if m == February && isLeap(year) { | |
674 return 29 | |
675 } | |
676 return int(daysBefore[m+1] - daysBefore[m]) | |
677 } | |
678 | |
666 // Provided by package runtime. | 679 // Provided by package runtime. |
667 func now() (sec int64, nsec int32) | 680 func now() (sec int64, nsec int32) |
668 | 681 |
669 // Now returns the current local time. | 682 // Now returns the current local time. |
670 func Now() Time { | 683 func Now() Time { |
671 sec, nsec := now() | 684 sec, nsec := now() |
672 return Time{sec + unixToInternal, nsec, Local} | 685 return Time{sec + unixToInternal, nsec, Local} |
673 } | 686 } |
674 | 687 |
675 // UTC returns t with the location set to UTC. | 688 // UTC returns t with the location set to UTC. |
676 func (t Time) UTC() Time { | 689 func (t Time) UTC() Time { |
677 t.loc = UTC | 690 t.loc = UTC |
678 return t | 691 return t |
679 } | 692 } |
680 | 693 |
681 // Local returns t with the location set to local time. | 694 // Local returns t with the location set to local time. |
682 func (t Time) Local() Time { | 695 func (t Time) Local() Time { |
683 t.loc = Local | 696 t.loc = Local |
684 return t | 697 return t |
685 } | 698 } |
686 | 699 |
687 // In returns t with the location information set to loc. | 700 // In returns t with the location information set to loc. |
r
2011/11/18 17:37:37
// It panics if loc is nil.
(or you could say nil
rsc
2011/11/18 18:21:36
I added the comment. I have been trying to force
| |
701 // | |
702 // In panics if loc is nil. | |
688 func (t Time) In(loc *Location) Time { | 703 func (t Time) In(loc *Location) Time { |
689 if loc == nil { | 704 if loc == nil { |
690 panic("time: missing Location in call to Time.In") | 705 panic("time: missing Location in call to Time.In") |
691 } | 706 } |
692 t.loc = loc | 707 t.loc = loc |
693 return t | 708 return t |
694 } | 709 } |
695 | 710 |
696 // Location returns the time zone information associated with t. | 711 // Location returns the time zone information associated with t. |
697 func (t Time) Location() *Location { | 712 func (t Time) Location() *Location { |
698 l := t.loc | 713 l := t.loc |
699 if l == nil { | 714 if l == nil { |
700 l = UTC | 715 l = UTC |
701 } | 716 } |
702 return l | 717 return l |
703 } | 718 } |
704 | 719 |
705 // Zone computes the time zone in effect at time t, returning the abbreviated | 720 // Zone computes the time zone in effect at time t, returning the abbreviated |
706 // name of the zone (such as "CET") and its offset in seconds east of UTC. | 721 // name of the zone (such as "CET") and its offset in seconds east of UTC. |
707 func (t Time) Zone() (name string, offset int) { | 722 func (t Time) Zone() (name string, offset int) { |
708 » name, offset, _, _, _ = t.loc.Lookup(t.sec + internalToUnix) | 723 » name, offset, _, _, _ = t.loc.lookup(t.sec + internalToUnix) |
709 return | 724 return |
710 } | 725 } |
711 | 726 |
712 // Unix returns the Unix time, the number of seconds since January 1, 1970 UTC. | 727 // Unix returns the Unix time, the number of seconds elapsed |
728 // since January 1, 1970 UTC. | |
713 func (t Time) Unix() int64 { | 729 func (t Time) Unix() int64 { |
714 return t.sec + internalToUnix | 730 return t.sec + internalToUnix |
715 } | 731 } |
716 | 732 |
717 // UnixNano returns the Unix time, the number of nanoseconds since January 1, 19 70 UTC. | 733 // UnixNano returns the Unix time, the number of nanoseconds elapsed |
734 // since January 1, 1970 UTC. | |
718 func (t Time) UnixNano() int64 { | 735 func (t Time) UnixNano() int64 { |
719 return (t.sec+internalToUnix)*1e9 + int64(t.nsec) | 736 return (t.sec+internalToUnix)*1e9 + int64(t.nsec) |
720 } | 737 } |
721 | 738 |
722 // Unix returns the local Time corresponding to the given Unix time, | 739 // Unix returns the local Time corresponding to the given Unix time, |
723 // sec seconds and nsec nanoseconds since January 1, 1970 UTC. | 740 // sec seconds and nsec nanoseconds since January 1, 1970 UTC. |
724 // It is valid to pass nsec outside the range [0, 999999999]. | 741 // It is valid to pass nsec outside the range [0, 999999999]. |
725 func Unix(sec int64, nsec int64) Time { | 742 func Unix(sec int64, nsec int64) Time { |
726 if nsec < 0 || nsec >= 1e9 { | 743 if nsec < 0 || nsec >= 1e9 { |
727 n := nsec / 1e9 | 744 n := nsec / 1e9 |
(...skipping 27 matching lines...) Expand all Loading... | |
755 } | 772 } |
756 return hi, lo | 773 return hi, lo |
757 } | 774 } |
758 | 775 |
759 // Date returns the Time corresponding to | 776 // Date returns the Time corresponding to |
760 // yyyy-mm-dd hh:mm:ss + nsec nanoseconds | 777 // yyyy-mm-dd hh:mm:ss + nsec nanoseconds |
761 // in the appropriate zone for that time in the given location. | 778 // in the appropriate zone for that time in the given location. |
762 // | 779 // |
763 // The month, day, hour, min, sec, and nsec values may be outside | 780 // The month, day, hour, min, sec, and nsec values may be outside |
764 // their usual ranges and will be normalized during the conversion. | 781 // their usual ranges and will be normalized during the conversion. |
765 // For example, October 42 converts to November 11. | 782 // For example, October 32 converts to November 1. |
r
2011/11/18 17:37:37
easier to understand
For example, October 32 is No
rsc
2011/11/18 18:21:36
Done.
| |
766 // | 783 // |
767 // A daylight savings time transition skips or repeats times. | 784 // A daylight savings time transition skips or repeats times. |
768 // For example, in the United States, March 13, 2011 2:15am never occurred, | 785 // For example, in the United States, March 13, 2011 2:15am never occurred, |
769 // while November 6, 2011 1:15am occurred twice. In such cases, the | 786 // while November 6, 2011 1:15am occurred twice. In such cases, the |
770 // choice of time zone, and therefore the time, is not well-defined. | 787 // choice of time zone, and therefore the time, is not well-defined. |
771 // Date guarantees to return a time correct in one of the two zones | 788 // Date returns a time that is correct in one of the two zones involved |
772 // involved in the transition, but it does not guarantee which. | 789 // in the transition, but it does not guarantee which. |
790 // | |
791 // Date panics if loc is nil. | |
773 func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) T ime { | 792 func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) T ime { |
774 if loc == nil { | 793 if loc == nil { |
775 panic("time: missing Location in call to Date") | 794 panic("time: missing Location in call to Date") |
r
2011/11/18 17:37:37
utc?
rsc
2011/11/18 18:21:36
Added panic to doc comment.
| |
776 } | 795 } |
777 | 796 |
778 // Normalize month, overflowing into year. | 797 // Normalize month, overflowing into year. |
779 m := int(month) - 1 | 798 m := int(month) - 1 |
780 year, m = norm(year, m, 12) | 799 year, m = norm(year, m, 12) |
781 month = Month(m) + 1 | 800 month = Month(m) + 1 |
782 | 801 |
783 // Normalize nsec, sec, min, hour, overflowing into day. | 802 // Normalize nsec, sec, min, hour, overflowing into day. |
784 sec, nsec = norm(sec, nsec, 1e9) | 803 sec, nsec = norm(sec, nsec, 1e9) |
785 min, sec = norm(min, sec, 60) | 804 min, sec = norm(min, sec, 60) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
818 // Add in days before today. | 837 // Add in days before today. |
819 d += uint64(day - 1) | 838 d += uint64(day - 1) |
820 | 839 |
821 // Add in time elapsed today. | 840 // Add in time elapsed today. |
822 abs := d * secondsPerDay | 841 abs := d * secondsPerDay |
823 abs += uint64(hour*secondsPerHour + min*secondsPerMinute + sec) | 842 abs += uint64(hour*secondsPerHour + min*secondsPerMinute + sec) |
824 | 843 |
825 unix := int64(abs) + (absoluteToInternal + internalToUnix) | 844 unix := int64(abs) + (absoluteToInternal + internalToUnix) |
826 | 845 |
827 // Look for zone offset for t, so we can adjust to UTC. | 846 // Look for zone offset for t, so we can adjust to UTC. |
828 » // The Lookup function expects UTC, so we pass t in the | 847 » // The lookup function expects UTC, so we pass t in the |
829 // hope that it will not be too close to a zone transition, | 848 // hope that it will not be too close to a zone transition, |
830 // and then adjust if it is. | 849 // and then adjust if it is. |
831 » _, offset, _, start, end := loc.Lookup(unix) | 850 » _, offset, _, start, end := loc.lookup(unix) |
832 if offset != 0 { | 851 if offset != 0 { |
833 switch utc := unix - int64(offset); { | 852 switch utc := unix - int64(offset); { |
834 case utc < start: | 853 case utc < start: |
835 » » » _, offset, _, _, _ = loc.Lookup(start - 1) | 854 » » » _, offset, _, _, _ = loc.lookup(start - 1) |
836 case utc >= end: | 855 case utc >= end: |
837 » » » _, offset, _, _, _ = loc.Lookup(end) | 856 » » » _, offset, _, _, _ = loc.lookup(end) |
838 } | 857 } |
839 unix -= int64(offset) | 858 unix -= int64(offset) |
840 } | 859 } |
841 | 860 |
842 return Time{unix + unixToInternal, int32(nsec), loc} | 861 return Time{unix + unixToInternal, int32(nsec), loc} |
843 } | 862 } |
LEFT | RIGHT |