Source File
time.go
Belonging Package
time
package time
import (
_ // for go:linkname
)
loc *Location
}
const (
hasMonotonic = 1 << 63
maxWall = wallToInternal + (1<<33 - 1) // year 2157
minWall = wallToInternal // year 1885
nsecMask = 1<<30 - 1
nsecShift = 30
)
func ( *Time) () int64 {
if .wall&hasMonotonic != 0 {
return wallToInternal + int64(.wall<<1>>(nsecShift+1))
}
return .ext
}
func ( *Time) () int64 { return .sec() + internalToUnix }
.stripMono()
}
.ext +=
}
func ( *Time) () int64 {
if .wall&hasMonotonic == 0 {
return 0
}
return .ext
}
absoluteZeroYear = -292277022399
internalYear = 1
absoluteToInternal int64 = (absoluteZeroYear - internalYear) * 365.2425 * secondsPerDay
internalToAbsolute = -absoluteToInternal
unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * secondsPerDay
internalToUnix int64 = -unixToInternal
wallToInternal int64 = (1884*365 + 1884/4 - 1884/100 + 1884/400) * secondsPerDay
internalToWall int64 = -wallToInternal
)
:= .unixSec()
if != &utcLoc {
if .cacheZone != nil && .cacheStart <= && < .cacheEnd {
= .cacheZone.name
= .cacheZone.offset
} else {
, , _, _ = .lookup()
}
+= int64()
} else {
= "UTC"
}
= uint64( + (unixToInternal + internalToAbsolute))
return
}
func ( Time) () Weekday {
return absWeekday(.abs())
}
:= ( + uint64(Monday)*secondsPerDay) % secondsPerWeek
return Weekday(int() / secondsPerDay)
}
:= .abs()
if == 4 {
= -3
+= uint64() * secondsPerDay
, , , := absDate(, false)
return , /7 + 1
}
func ( uint64) (, , int) {
= int( % secondsPerDay)
= / secondsPerHour
-= * secondsPerHour
= / secondsPerMinute
-= * secondsPerMinute
return
}
func ( Time) () int {
return int(.abs()%secondsPerDay) / secondsPerHour
}
func ( Time) () int {
return int(.abs()%secondsPerHour) / secondsPerMinute
}
func ( Time) () int {
return int(.abs() % secondsPerMinute)
}
type Duration int64
const (
minDuration Duration = -1 << 63
maxDuration Duration = 1<<63 - 1
)
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
var int
--
[] = 's'
--
switch {
case == 0:
return "0s"
= 0
[] = 'n'
-- // Need room for two bytes.
copy([:], "µ")
= fmtInt([:], %60)
/= 60
if > 0 {
--
[] = 'm'
= fmtInt([:], %60)
/= 60
func ( Duration) ( Duration) Duration {
if <= 0 {
return
}
:= %
if < 0 {
= -
if lessThanHalf(, ) {
return +
}
if := - + ; < {
return
}
return minDuration // overflow
}
if lessThanHalf(, ) {
return -
}
if := + - ; > {
return
}
return maxDuration // overflow
}
func ( Time) ( Time) Duration {
if .wall&.wall&hasMonotonic != 0 {
:= .ext
:= .ext
:= Duration( - )
if < 0 && > {
return maxDuration // t - u is positive out of range
}
if > 0 && < {
return minDuration // t - u is negative out of range
}
return
}
switch {
case .Add().Equal():
return // d is correct
case .Before():
return minDuration // t - u is negative out of range
default:
return maxDuration // t - u is positive out of range
}
}
= Time{hasMonotonic, runtimeNano() - startNano, nil}
} else {
= Now()
}
return .Sub()
}
= Time{hasMonotonic, runtimeNano() - startNano, nil}
} else {
= Now()
}
return .Sub()
}
func ( Time) ( int, int, int) Time {
, , := .Date()
, , := .Clock()
return Date(+, +Month(), +, , , , int(.nsec()), .Location())
}
const (
secondsPerMinute = 60
secondsPerHour = 60 * secondsPerMinute
secondsPerDay = 24 * secondsPerHour
secondsPerWeek = 7 * secondsPerDay
daysPer400Years = 365*400 + 97
daysPer100Years = 365*100 + 24
daysPer4Years = 365*4 + 1
)
:= / secondsPerDay
:= / daysPer400Years
:= 400 *
-= daysPer400Years *
= / daysPer100Years
-= >> 2
+= 100 *
-= daysPer100Years *
= / daysPer4Years
+= 4 *
-= daysPer4Years *
= / 365
-= >> 2
+=
-= 365 *
= int(int64() + absoluteZeroYear)
= int()
if ! {
return
}
=
switch {
--
= February
= 29
return
}
}
= Month( / 31)
:= int(daysBefore[+1])
var int
if >= {
++
=
} else {
= int(daysBefore[])
}
++ // because January is 1
= - + 1
return
}
var daysBefore = [...]int32{
0,
31,
31 + 28,
31 + 28 + 31,
31 + 28 + 31 + 30,
31 + 28 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
}
func ( Month, int) int {
if == February && isLeap() {
return 29
}
return int(daysBefore[] - daysBefore[-1])
}
func ( int) uint64 {
:= uint64(int64() - absoluteZeroYear)
:= / 400
-= 400 *
:= daysPer400Years *
= / 100
-= 100 *
+= daysPer100Years *
= / 4
-= 4 *
+= daysPer4Years *
=
+= 365 *
return
}
func () int64
var startNano int64 = runtimeNano() - 1
func ( Time) () ([]byte, error) {
var int16 // minutes east of UTC. -1 is UTC.
if .Location() == UTC {
= -1
} else {
, := .Zone()
if %60 != 0 {
return nil, errors.New("Time.MarshalBinary: zone offset has fractional minute")
}
/= 60
if < -32768 || == -1 || > 32767 {
return nil, errors.New("Time.MarshalBinary: unexpected zone offset")
}
= int16()
}
:= .sec()
:= .nsec()
:= []byte{
timeBinaryVersion, // byte 0 : version
byte( >> 56), // bytes 1-8: seconds
byte( >> 48),
byte( >> 40),
byte( >> 32),
byte( >> 24),
byte( >> 16),
byte( >> 8),
byte(),
byte( >> 24), // bytes 9-12: nanoseconds
byte( >> 16),
byte( >> 8),
byte(),
byte( >> 8), // bytes 13-14: zone offset in minutes
byte(),
}
return , nil
}
func ( *Time) ( []byte) error {
:=
if len() == 0 {
return errors.New("Time.UnmarshalBinary: no data")
}
if [0] != timeBinaryVersion {
return errors.New("Time.UnmarshalBinary: unsupported version")
}
if len() != /*version*/ 1+ /*sec*/ 8+ /*nsec*/ 4+ /*zone offset*/ 2 {
return errors.New("Time.UnmarshalBinary: invalid length")
}
= [1:]
:= int64([7]) | int64([6])<<8 | int64([5])<<16 | int64([4])<<24 |
int64([3])<<32 | int64([2])<<40 | int64([1])<<48 | int64([0])<<56
= [8:]
:= int32([3]) | int32([2])<<8 | int32([1])<<16 | int32([0])<<24
= [4:]
:= int(int16([1])|int16([0])<<8) * 60
* = Time{}
.wall = uint64()
.ext =
if == -1*60 {
.setLoc(&utcLoc)
} else if , , , := Local.lookup(.unixSec()); == {
.setLoc(Local)
} else {
.setLoc(FixedZone("", ))
}
return nil
}
func ( Time) () ([]byte, error) {
return .MarshalBinary()
}
func ( *Time) ( []byte) error {
return .UnmarshalBinary()
}
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
}
:= make([]byte, 0, len(RFC3339Nano)+2)
= append(, '"')
= .AppendFormat(, RFC3339Nano)
= append(, '"')
return , nil
}
func ( Time) () ([]byte, error) {
if := .Year(); < 0 || >= 10000 {
return nil, errors.New("Time.MarshalText: year outside of range [0,9999]")
}
:= make([]byte, 0, len(RFC3339Nano))
return .AppendFormat(, RFC3339Nano), nil
}
:= daysSinceEpoch()
+= uint64(daysBefore[-1])
if isLeap() && >= March {
++ // February 29
}
+= uint64( - 1)
:= * secondsPerDay
+= uint64(*secondsPerHour + *secondsPerMinute + )
:= int64() + (absoluteToInternal + internalToUnix)
= true
= -
= -
if < 0 {
+= 1e9
-- // sec >= 1 before the -- so safe
}
}
^= 1
= -
}
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |