Source File
format.go
Belonging Package
time
package time
import
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
const (
_ = iota
stdLongMonth = iota + stdNeedDate // "January"
stdMonth // "Jan"
stdNumMonth // "1"
stdZeroMonth // "01"
stdLongWeekDay // "Monday"
stdWeekDay // "Mon"
stdDay // "2"
stdUnderDay // "_2"
stdZeroDay // "02"
stdUnderYearDay // "__2"
stdZeroYearDay // "002"
stdHour = iota + stdNeedClock // "15"
stdHour12 // "3"
stdZeroHour12 // "03"
stdMinute // "4"
stdZeroMinute // "04"
stdSecond // "5"
stdZeroSecond // "05"
stdLongYear = iota + stdNeedDate // "2006"
stdYear // "06"
stdPM = iota + stdNeedClock // "PM"
stdpm // "pm"
stdTZ = iota // "MST"
stdISO8601TZ // "Z0700" // prints Z for UTC
stdISO8601SecondsTZ // "Z070000"
stdISO8601ShortTZ // "Z07"
stdISO8601ColonTZ // "Z07:00" // prints Z for UTC
stdISO8601ColonSecondsTZ // "Z07:00:00"
stdNumTZ // "-0700" // always numeric
stdNumSecondsTz // "-070000"
stdNumShortTZ // "-07" // always numeric
stdNumColonTZ // "-07:00" // always numeric
stdNumColonSecondsTZ // "-07:00:00"
stdFracSecond0 // ".0", ".00", ... , trailing zeros included
stdFracSecond9 // ".9", ".99", ..., trailing zeros omitted
stdNeedDate = 1 << 8 // need month, day, year
stdNeedClock = 2 << 8 // need hour, minute, second
stdArgShift = 16 // extra argument in high bits, above low stdArgShift
stdMask = 1<<stdArgShift - 1 // mask out argument
)
var std0x = [...]int{stdZeroMonth, stdZeroDay, stdZeroHour12, stdZeroMinute, stdZeroSecond, stdYear}
func ( string) ( string, int, string) {
for := 0; < len(); ++ {
switch := int([]); {
case 'J': // January, Jan
if len() >= +3 && [:+3] == "Jan" {
if len() >= +7 && [:+7] == "January" {
return [0:], stdLongMonth, [+7:]
}
if !startsWithLowerCase([+3:]) {
return [0:], stdMonth, [+3:]
}
}
case 'M': // Monday, Mon, MST
if len() >= +3 {
if [:+3] == "Mon" {
if len() >= +6 && [:+6] == "Monday" {
return [0:], stdLongWeekDay, [+6:]
}
if !startsWithLowerCase([+3:]) {
return [0:], stdWeekDay, [+3:]
}
}
if [:+3] == "MST" {
return [0:], stdTZ, [+3:]
}
}
case '0': // 01, 02, 03, 04, 05, 06, 002
if len() >= +2 && '1' <= [+1] && [+1] <= '6' {
return [0:], std0x[[+1]-'1'], [+2:]
}
if len() >= +3 && [+1] == '0' && [+2] == '2' {
return [0:], stdZeroYearDay, [+3:]
}
case '1': // 15, 1
if len() >= +2 && [+1] == '5' {
return [0:], stdHour, [+2:]
}
return [0:], stdNumMonth, [+1:]
case '2': // 2006, 2
if len() >= +4 && [:+4] == "2006" {
return [0:], stdLongYear, [+4:]
}
return [0:], stdDay, [+1:]
case '_': // _2, _2006, __2
if len() >= +5 && [+1:+5] == "2006" {
return [0 : +1], stdLongYear, [+5:]
}
return [0:], stdUnderDay, [+2:]
}
if len() >= +3 && [+1] == '_' && [+2] == '2' {
return [0:], stdUnderYearDay, [+3:]
}
case '3':
return [0:], stdHour12, [+1:]
case '4':
return [0:], stdMinute, [+1:]
case '5':
return [0:], stdSecond, [+1:]
case 'P': // PM
if len() >= +2 && [+1] == 'M' {
return [0:], stdPM, [+2:]
}
case 'p': // pm
if len() >= +2 && [+1] == 'm' {
return [0:], stdpm, [+2:]
}
case '-': // -070000, -07:00:00, -0700, -07:00, -07
if len() >= +7 && [:+7] == "-070000" {
return [0:], stdNumSecondsTz, [+7:]
}
if len() >= +9 && [:+9] == "-07:00:00" {
return [0:], stdNumColonSecondsTZ, [+9:]
}
if len() >= +5 && [:+5] == "-0700" {
return [0:], stdNumTZ, [+5:]
}
if len() >= +6 && [:+6] == "-07:00" {
return [0:], stdNumColonTZ, [+6:]
}
if len() >= +3 && [:+3] == "-07" {
return [0:], stdNumShortTZ, [+3:]
}
case 'Z': // Z070000, Z07:00:00, Z0700, Z07:00,
if len() >= +7 && [:+7] == "Z070000" {
return [0:], stdISO8601SecondsTZ, [+7:]
}
if len() >= +9 && [:+9] == "Z07:00:00" {
return [0:], stdISO8601ColonSecondsTZ, [+9:]
}
if len() >= +5 && [:+5] == "Z0700" {
return [0:], stdISO8601TZ, [+5:]
}
if len() >= +6 && [:+6] == "Z07:00" {
return [0:], stdISO8601ColonTZ, [+6:]
}
if len() >= +3 && [:+3] == "Z07" {
return [0:], stdISO8601ShortTZ, [+3:]
}
case '.': // .000 or .999 - repeated digits for fractional seconds.
if +1 < len() && ([+1] == '0' || [+1] == '9') {
:= [+1]
:= + 1
for < len() && [] == {
++
if !isDigit(, ) {
:= stdFracSecond0
if [+1] == '9' {
= stdFracSecond9
}
|= ( - ( + 1)) << stdArgShift
return [0:], , [:]
}
}
}
}
return , 0, ""
}
var longDayNames = []string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
}
var shortDayNames = []string{
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
}
var shortMonthNames = []string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}
var longMonthNames = []string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
for != "" {
, , := nextStdChunk()
if != "" {
= append(, ...)
}
if == 0 {
break
}
=
if < 0 && &stdNeedDate != 0 {
, , , = absDate(, true)
++
}
if < 0 && &stdNeedClock != 0 {
, , = absClock()
}
switch & stdMask {
case stdYear:
:=
if < 0 {
= -
}
= appendInt(, %100, 2)
case stdLongYear:
= appendInt(, , 4)
case stdMonth:
= append(, .String()[:3]...)
case stdLongMonth:
:= .String()
= append(, ...)
case stdNumMonth:
= appendInt(, int(), 0)
case stdZeroMonth:
= appendInt(, int(), 2)
case stdWeekDay:
= append(, absWeekday().String()[:3]...)
case stdLongWeekDay:
:= absWeekday().String()
= append(, ...)
case stdDay:
= appendInt(, , 0)
case stdUnderDay:
if < 10 {
= append(, ' ')
}
= appendInt(, , 0)
case stdZeroDay:
= appendInt(, , 2)
case stdUnderYearDay:
if < 100 {
= append(, ' ')
if < 10 {
= append(, ' ')
}
}
= appendInt(, , 0)
case stdZeroYearDay:
= appendInt(, , 3)
case stdHour:
= appendInt(, , 2)
:= % 12
if == 0 {
= 12
}
= appendInt(, , 0)
:= % 12
if == 0 {
= 12
}
= appendInt(, , 2)
case stdMinute:
= appendInt(, , 0)
case stdZeroMinute:
= appendInt(, , 2)
case stdSecond:
= appendInt(, , 0)
case stdZeroSecond:
= appendInt(, , 2)
case stdPM:
if >= 12 {
= append(, "PM"...)
} else {
= append(, "AM"...)
}
case stdpm:
if >= 12 {
= append(, "pm"...)
} else {
= append(, "am"...)
}
if == 0 && ( == stdISO8601TZ || == stdISO8601ColonTZ || == stdISO8601SecondsTZ || == stdISO8601ShortTZ || == stdISO8601ColonSecondsTZ) {
= append(, 'Z')
break
}
:= / 60 // convert to minutes
:=
if < 0 {
= append(, '-')
= -
= -
} else {
= append(, '+')
}
= appendInt(, /60, 2)
if == stdISO8601ColonTZ || == stdNumColonTZ || == stdISO8601ColonSecondsTZ || == stdNumColonSecondsTZ {
= append(, ':')
}
if != stdNumShortTZ && != stdISO8601ShortTZ {
= appendInt(, %60, 2)
}
if == stdISO8601SecondsTZ || == stdNumSecondsTz || == stdNumColonSecondsTZ || == stdISO8601ColonSecondsTZ {
if == stdNumColonSecondsTZ || == stdISO8601ColonSecondsTZ {
= append(, ':')
}
= appendInt(, %60, 2)
}
case stdTZ:
if != "" {
= append(, ...)
break
:= / 60 // convert to minutes
if < 0 {
= append(, '-')
= -
} else {
= append(, '+')
}
= appendInt(, /60, 2)
= appendInt(, %60, 2)
case stdFracSecond0, stdFracSecond9:
= formatNano(, uint(.Nanosecond()), >>stdArgShift, &stdMask == stdFracSecond9)
}
}
return
}
var errBad = errors.New("bad value for field") // placeholder not passed to user
for {
var error
, , := nextStdChunk()
:= [len() : len()-len()]
, = skip(, )
if != nil {
return Time{}, &ParseError{, , , , ""}
}
if == 0 {
if len() != 0 {
return Time{}, &ParseError{, , "", , ": extra text: " + quote()}
}
break
}
=
var string
switch & stdMask {
case stdYear:
if len() < 2 {
= errBad
break
}
:=
, = [0:2], [2:]
, = atoi()
if != nil {
=
} else if >= 69 { // Unix time starts Dec 31 1969 in some time zones
+= 1900
} else {
+= 2000
}
case stdLongYear:
if len() < 4 || !isDigit(, 0) {
= errBad
break
}
, = [0:4], [4:]
, = atoi()
case stdMonth:
, , = lookup(shortMonthNames, )
++
case stdLongMonth:
, , = lookup(longMonthNames, )
++
case stdNumMonth, stdZeroMonth:
, , = getnum(, == stdZeroMonth)
if == nil && ( <= 0 || 12 < ) {
= "month"
}
_, , = lookup(shortDayNames, )
case stdLongWeekDay:
_, , = lookup(longDayNames, )
case stdDay, stdUnderDay, stdZeroDay:
if == stdUnderDay && len() > 0 && [0] == ' ' {
= [1:]
}
case stdUnderYearDay, stdZeroYearDay:
for := 0; < 2; ++ {
if == stdUnderYearDay && len() > 0 && [0] == ' ' {
= [1:]
}
}
case stdHour:
, , = getnum(, false)
if < 0 || 24 <= {
= "hour"
}
case stdHour12, stdZeroHour12:
, , = getnum(, == stdZeroHour12)
if < 0 || 12 < {
= "hour"
}
case stdMinute, stdZeroMinute:
, , = getnum(, == stdZeroMinute)
if < 0 || 60 <= {
= "minute"
}
case stdSecond, stdZeroSecond:
, , = getnum(, == stdZeroSecond)
if < 0 || 60 <= {
= "second"
break
if len() >= 2 && [0] == '.' && isDigit(, 1) {
_, , _ = nextStdChunk()
&= stdMask
break
:= 2
for ; < len() && isDigit(, ); ++ {
}
, , = parseNanoseconds(, )
= [:]
}
case stdPM:
if len() < 2 {
= errBad
break
}
, = [0:2], [2:]
switch {
case "PM":
= true
case "AM":
= true
default:
= errBad
}
case stdpm:
if len() < 2 {
= errBad
break
}
, = [0:2], [2:]
switch {
case "pm":
= true
case "am":
= true
default:
= errBad
}
case stdISO8601TZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ShortTZ, stdISO8601ColonSecondsTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ, stdNumSecondsTz, stdNumColonSecondsTZ:
if ( == stdISO8601TZ || == stdISO8601ShortTZ || == stdISO8601ColonTZ) && len() >= 1 && [0] == 'Z' {
= [1:]
= UTC
break
}
var , , , string
if == stdISO8601ColonTZ || == stdNumColonTZ {
if len() < 6 {
= errBad
break
}
if [3] != ':' {
= errBad
break
}
, , , , = [0:1], [1:3], [4:6], "00", [6:]
} else if == stdNumShortTZ || == stdISO8601ShortTZ {
if len() < 3 {
= errBad
break
}
, , , , = [0:1], [1:3], "00", "00", [3:]
} else if == stdISO8601ColonSecondsTZ || == stdNumColonSecondsTZ {
if len() < 9 {
= errBad
break
}
if [3] != ':' || [6] != ':' {
= errBad
break
}
, , , , = [0:1], [1:3], [4:6], [7:9], [9:]
} else if == stdISO8601SecondsTZ || == stdNumSecondsTz {
if len() < 7 {
= errBad
break
}
, , , , = [0:1], [1:3], [3:5], [5:7], [7:]
} else {
if len() < 5 {
= errBad
break
}
, , , , = [0:1], [1:3], [3:5], "00", [5:]
}
var , , int
, = atoi()
if == nil {
, = atoi()
}
if == nil {
, = atoi()
}
= (*60+)*60 + // offset is in seconds
switch [0] {
case '+':
case '-':
= -
default:
= errBad
}
if len() >= 3 && [0:3] == "UTC" {
= UTC
= [3:]
break
}
, := parseTimeZone()
if ! {
= errBad
break
}
, = [:], [:]
:= 1 + ( >> stdArgShift)
if len() < {
= errBad
break
}
, , = parseNanoseconds(, )
= [:]
case stdFracSecond9:
break
:= 0
for < 9 && +1 < len() && '0' <= [+1] && [+1] <= '9' {
++
}
, , = parseNanoseconds(, 1+)
= [1+:]
}
if != "" {
return Time{}, &ParseError{, , , , ": " + + " out of range"}
}
if != nil {
return Time{}, &ParseError{, , , , ""}
}
}
if && < 12 {
+= 12
} else if && == 12 {
= 0
}
if >= 0 {
var int
var int
if isLeap() {
if == 31+29 {
= int(February)
= 29
} else if > 31+29 {
--
}
}
if < 1 || > 365 {
return Time{}, &ParseError{, , "", , ": day-of-year out of range"}
}
if == 0 {
= (-1)/31 + 1
if int(daysBefore[]) < {
++
}
= - int(daysBefore[-1])
if >= 0 && != {
return Time{}, &ParseError{, , "", , ": day-of-year does not match month"}
}
=
if >= 0 && != {
return Time{}, &ParseError{, , "", , ": day-of-year does not match day"}
}
=
} else {
if < 0 {
= int(January)
}
if < 0 {
= 1
}
}
if [0] == '+' || [0] == '-' {
= parseSignedOffset()
:= > 0 // parseSignedOffset returns 0 in case of bad input
return ,
func ( string) int {
= [3:]
if len() == 0 {
return 3
}
return 3 + parseSignedOffset()
}
func ( string) int {
:= [0]
if != '-' && != '+' {
return 0
}
, , := leadingInt([1:])
:= 10 -
for := 0; < ; ++ {
*= 10
}
return
}
var errLeadingInt = errors.New("time: bad [0-9]*") // never printed
return 0, "", errLeadingInt
}
= *10 + int64() - '0'
return 0, "", errLeadingInt
}
}
return , [:], nil
}
= true
continue
}
:= *10 + int64() - '0'
if < 0 {
= true
continue
}
=
*= 10
}
return , , [:]
}
var unitMap = map[string]int64{
"ns": int64(Nanosecond),
"us": int64(Microsecond),
"µs": int64(Microsecond), // U+00B5 = micro symbol
"μs": int64(Microsecond), // U+03BC = Greek letter mu
"ms": int64(Millisecond),
"s": int64(Second),
"m": int64(Minute),
"h": int64(Hour),
}
if != "" {
:= [0]
if == '-' || == '+' {
= == '-'
= [1:]
}
:= false
if != "" && [0] == '.' {
= [1:]
:= len()
, , = leadingFraction()
= != len()
}
![]() |
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. |