diff options
Diffstat (limited to 'src/pkg/time/time.go')
| -rw-r--r-- | src/pkg/time/time.go | 144 |
1 files changed, 69 insertions, 75 deletions
diff --git a/src/pkg/time/time.go b/src/pkg/time/time.go index bd4988aa0..83ee535bd 100644 --- a/src/pkg/time/time.go +++ b/src/pkg/time/time.go @@ -17,7 +17,7 @@ func Seconds() int64 { if err != nil { panic("time: os.Time: ", err.String()); } - return sec + return sec; } // Nanoseconds reports the number of nanoseconds since the Unix epoch, @@ -27,12 +27,12 @@ func Nanoseconds() int64 { if err != nil { panic("time: os.Time: ", err.String()); } - return sec*1e9 + nsec + return sec*1e9 + nsec; } // Days of the week. const ( - Sunday = iota; + Sunday = iota; Monday; Tuesday; Wednesday; @@ -43,36 +43,30 @@ const ( // Time is the struct representing a parsed time value. type Time struct { - Year int64; // 2008 is 2008 - Month, Day int; // Sep-17 is 9, 17 - Hour, Minute, Second int; // 10:43:12 is 10, 43, 12 - Weekday int; // Sunday, Monday, ... - ZoneOffset int; // seconds east of UTC - Zone string; + Year int64; // 2008 is 2008 + Month, Day int; // Sep-17 is 9, 17 + Hour, Minute, Second int; // 10:43:12 is 10, 43, 12 + Weekday int; // Sunday, Monday, ... + ZoneOffset int; // seconds east of UTC + Zone string; } -var nonleapyear = []int{ - 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 -} -var leapyear = []int{ - 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 -} +var nonleapyear = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} +var leapyear = []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} func months(year int64) []int { if year%4 == 0 && (year%100 != 0 || year%400 == 0) { - return leapyear + return leapyear; } - return nonleapyear + return nonleapyear; } const ( - secondsPerDay = 24*60*60; - - daysPer400Years = 365*400+97; - daysPer100Years = 365*100+24; - daysPer4Years = 365*4+1; - - days1970To2001 = 31*365+8; + secondsPerDay = 24*60*60; + daysPer400Years = 365*400 + 97; + daysPer100Years = 365*100 + 24; + daysPer4Years = 365*4 + 1; + days1970To2001 = 31*365 + 8; ) // SecondsToUTC converts sec, in number of seconds since the Unix epoch, @@ -81,11 +75,11 @@ func SecondsToUTC(sec int64) *Time { t := new(Time); // Split into time and day. - day := sec/secondsPerDay; - sec -= day*secondsPerDay; + day := sec / secondsPerDay; + sec -= day * secondsPerDay; if sec < 0 { day--; - sec += secondsPerDay + sec += secondsPerDay; } // Time @@ -94,9 +88,9 @@ func SecondsToUTC(sec int64) *Time { t.Second = int(sec%60); // Day 0 = January 1, 1970 was a Thursday - t.Weekday = int((day + Thursday) % 7); + t.Weekday = int((day+Thursday)%7); if t.Weekday < 0 { - t.Weekday += 7 + t.Weekday += 7; } // Change day from 0 = 1970 to 0 = 2001, @@ -107,25 +101,25 @@ func SecondsToUTC(sec int64) *Time { year := int64(2001); if day < 0 { // Go back enough 400 year cycles to make day positive. - n := -day/daysPer400Years + 1; + n := -day / daysPer400Years + 1; year -= 400*n; - day += daysPer400Years*n; + day += daysPer400Years * n; } else { // Cut off 400 year cycles. - n := day/daysPer400Years; + n := day / daysPer400Years; year += 400*n; - day -= daysPer400Years*n; + day -= daysPer400Years * n; } // Cut off 100-year cycles - n := day/daysPer100Years; + n := day / daysPer100Years; year += 100*n; - day -= daysPer100Years*n; + day -= daysPer100Years * n; // Cut off 4-year cycles - n = day/daysPer4Years; + n = day / daysPer4Years; year += 4*n; - day -= daysPer4Years*n; + day -= daysPer4Years * n; // Cut off non-leap years. n = day/365; @@ -141,7 +135,7 @@ func SecondsToUTC(sec int64) *Time { var m int; yday := int(day); for m = 0; m < 12 && yday >= months[m]; m++ { - yday -= months[m] + yday -= months[m]; } t.Month = m+1; t.Day = yday+1; @@ -152,7 +146,7 @@ func SecondsToUTC(sec int64) *Time { // UTC returns the current time as a parsed Time value in the UTC time zone. func UTC() *Time { - return SecondsToUTC(Seconds()) + return SecondsToUTC(Seconds()); } // SecondsToLocalTime converts sec, in number of seconds since the Unix epoch, @@ -162,12 +156,12 @@ func SecondsToLocalTime(sec int64) *Time { t := SecondsToUTC(sec+int64(offset)); t.Zone = z; t.ZoneOffset = offset; - return t + return t; } // LocalTime returns the current time as a parsed Time value in the local time zone. func LocalTime() *Time { - return SecondsToLocalTime(Seconds()) + return SecondsToLocalTime(Seconds()); } // Seconds returns the number of seconds since January 1, 1970 represented by the @@ -182,34 +176,34 @@ func (t *Time) Seconds() int64 { // Rewrite year to be >= 2001. year := t.Year; if year < 2001 { - n := (2001 - year)/400 + 1; + n := (2001-year)/400 + 1; year += 400*n; - day -= daysPer400Years*n; + day -= daysPer400Years * n; } // Add in days from 400-year cycles. - n := (year - 2001) / 400; + n := (year-2001)/400; year -= 400*n; - day += daysPer400Years*n; + day += daysPer400Years * n; // Add in 100-year cycles. - n = (year - 2001) / 100; + n = (year-2001)/100; year -= 100*n; - day += daysPer100Years*n; + day += daysPer100Years * n; // Add in 4-year cycles. - n = (year - 2001) / 4; + n = (year-2001)/4; year -= 4*n; - day += daysPer4Years*n; + day += daysPer4Years * n; // Add in non-leap years. - n = year - 2001; + n = year-2001; day += 365*n; // Add in days this year. months := months(t.Year); - for m := 0; m < t.Month-1; m++ { - day += int64(months[m]) + for m := 0; m < t.Month - 1; m++ { + day += int64(months[m]); } day += int64(t.Day - 1); @@ -226,7 +220,7 @@ func (t *Time) Seconds() int64 { // Account for local time zone. sec -= int64(t.ZoneOffset); - return sec + return sec; } var longDayNames = []string{ @@ -236,7 +230,7 @@ var longDayNames = []string{ "Wednesday", "Thursday", "Friday", - "Saturday" + "Saturday", } var shortDayNames = []string{ @@ -246,7 +240,7 @@ var shortDayNames = []string{ "Wed", "Thu", "Fri", - "Sat" + "Sat", } var shortMonthNames = []string{ @@ -262,29 +256,29 @@ var shortMonthNames = []string{ "Sep", "Oct", "Nov", - "Dec" + "Dec", } func copy(dst []byte, s string) { for i := 0; i < len(s); i++ { - dst[i] = s[i] + dst[i] = s[i]; } } func decimal(dst []byte, n int) { if n < 0 { - n = 0 + n = 0; } for i := len(dst)-1; i >= 0; i-- { dst[i] = byte(n%10 + '0'); - n /= 10 + n /= 10; } } func addString(buf []byte, bp int, s string) int { n := len(s); - copy(buf[bp:bp+n], s); - return bp+n + copy(buf[bp : bp+n], s); + return bp+n; } // Just enough of strftime to implement the date formats below. @@ -304,66 +298,66 @@ func format(t *Time, fmt string) string { case 'b': // %b abbreviated month name bp = addString(buf, bp, shortMonthNames[t.Month]); case 'd': // %d day of month (01-31) - decimal(buf[bp:bp+2], t.Day); + decimal(buf[bp : bp+2], t.Day); bp += 2; case 'e': // %e day of month ( 1-31) if t.Day >= 10 { - decimal(buf[bp:bp+2], t.Day) + decimal(buf[bp : bp+2], t.Day); } else { buf[bp] = ' '; - buf[bp+1] = byte(t.Day + '0') + buf[bp+1] = byte(t.Day + '0'); } bp += 2; case 'H': // %H hour 00-23 - decimal(buf[bp:bp+2], t.Hour); + decimal(buf[bp : bp+2], t.Hour); bp += 2; case 'M': // %M minute 00-59 - decimal(buf[bp:bp+2], t.Minute); + decimal(buf[bp : bp+2], t.Minute); bp += 2; case 'S': // %S second 00-59 - decimal(buf[bp:bp+2], t.Second); + decimal(buf[bp : bp+2], t.Second); bp += 2; case 'Y': // %Y year 2008 - decimal(buf[bp:bp+4], int(t.Year)); + decimal(buf[bp : bp+4], int(t.Year)); bp += 4; case 'y': // %y year 08 - decimal(buf[bp:bp+2], int(t.Year%100)); + decimal(buf[bp : bp+2], int(t.Year % 100)); bp += 2; case 'Z': bp = addString(buf, bp, t.Zone); default: buf[bp] = '%'; buf[bp+1] = fmt[i]; - bp += 2 + bp += 2; } } else { buf[bp] = fmt[i]; bp++; } } - return string(buf[0:bp]) + return string(buf[0:bp]); } // Asctime formats the parsed time value in the style of // ANSI C asctime: Sun Nov 6 08:49:37 1994 func (t *Time) Asctime() string { - return format(t, "%a %b %e %H:%M:%S %Y") + return format(t, "%a %b %e %H:%M:%S %Y"); } // RFC850 formats the parsed time value in the style of // RFC 850: Sunday, 06-Nov-94 08:49:37 UTC func (t *Time) RFC850() string { - return format(t, "%A, %d-%b-%y %H:%M:%S %Z") + return format(t, "%A, %d-%b-%y %H:%M:%S %Z"); } // RFC1123 formats the parsed time value in the style of // RFC 1123: Sun, 06 Nov 1994 08:49:37 UTC func (t *Time) RFC1123() string { - return format(t, "%a, %d %b %Y %H:%M:%S %Z") + return format(t, "%a, %d %b %Y %H:%M:%S %Z"); } // String formats the parsed time value in the style of // date(1) - Sun Nov 6 08:49:37 UTC 1994 func (t *Time) String() string { - return format(t, "%a %b %e %H:%M:%S %Z %Y") + return format(t, "%a %b %e %H:%M:%S %Z %Y"); } |
