2008-09-17 17:20:00 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os";
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Seconds since January 1, 1970 00:00:00 GMT
|
2009-01-20 15:40:40 -07:00
|
|
|
func Seconds() int64 {
|
2008-12-08 18:45:50 -07:00
|
|
|
sec, nsec, err := os.Time();
|
|
|
|
if err != nil {
|
|
|
|
panic("time: os.Time: ", err.String());
|
|
|
|
}
|
|
|
|
return sec
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nanoseconds since January 1, 1970 00:00:00 GMT
|
2009-01-20 15:40:40 -07:00
|
|
|
func Nanoseconds() int64 {
|
2008-12-08 18:45:50 -07:00
|
|
|
sec, nsec, err := os.Time();
|
|
|
|
if err != nil {
|
|
|
|
panic("time: os.Time: ", err.String());
|
|
|
|
}
|
|
|
|
return sec*1e9 + nsec
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
const (
|
2008-09-17 17:20:00 -06:00
|
|
|
Sunday = iota;
|
|
|
|
Monday;
|
|
|
|
Tuesday;
|
|
|
|
Wednesday;
|
|
|
|
Thursday;
|
|
|
|
Friday;
|
|
|
|
Saturday;
|
|
|
|
)
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Time struct {
|
2009-01-26 12:22:21 -07:00
|
|
|
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 = 0, Monday = 1, ...
|
|
|
|
ZoneOffset int; // seconds west of UTC
|
|
|
|
Zone string;
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
var nonleapyear = []int{
|
2008-09-17 17:20:00 -06:00
|
|
|
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
|
|
|
}
|
2009-01-16 11:14:12 -07:00
|
|
|
var leapyear = []int{
|
2008-09-17 17:20:00 -06:00
|
|
|
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
func months(year int64) []int {
|
2008-09-17 17:20:00 -06:00
|
|
|
if year%4 == 0 && (year%100 != 0 || year%400 == 0) {
|
2009-01-16 11:14:12 -07:00
|
|
|
return leapyear
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
2009-01-16 11:14:12 -07:00
|
|
|
return nonleapyear
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2009-01-16 11:14:12 -07:00
|
|
|
_SecondsPerDay = 24*60*60;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
_DaysPer400Years = 365*400+97;
|
|
|
|
_DaysPer100Years = 365*100+24;
|
|
|
|
_DaysPer4Years = 365*4+1;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
_Days1970To2001 = 31*365+8;
|
2008-09-17 17:20:00 -06:00
|
|
|
)
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func SecondsToUTC(sec int64) *Time {
|
2009-01-06 16:19:02 -07:00
|
|
|
t := new(Time);
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Split into time and day.
|
2009-01-16 11:14:12 -07:00
|
|
|
day := sec/_SecondsPerDay;
|
|
|
|
sec -= day*_SecondsPerDay;
|
2008-09-17 17:20:00 -06:00
|
|
|
if sec < 0 {
|
2008-10-07 13:31:31 -06:00
|
|
|
day--;
|
2009-01-16 11:14:12 -07:00
|
|
|
sec += _SecondsPerDay
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Time
|
2009-01-26 12:22:21 -07:00
|
|
|
t.Hour = int(sec/3600);
|
|
|
|
t.Minute = int((sec/60)%60);
|
|
|
|
t.Second = int(sec%60);
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Day 0 = January 1, 1970 was a Thursday
|
2009-01-26 12:22:21 -07:00
|
|
|
t.Weekday = int((day + Thursday) % 7);
|
|
|
|
if t.Weekday < 0 {
|
|
|
|
t.Weekday += 7
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change day from 0 = 1970 to 0 = 2001,
|
|
|
|
// to make leap year calculations easier
|
|
|
|
// (2001 begins 4-, 100-, and 400-year cycles ending in a leap year.)
|
2009-01-16 11:14:12 -07:00
|
|
|
day -= _Days1970To2001;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
2008-10-07 13:31:31 -06:00
|
|
|
year := int64(2001);
|
2008-09-17 17:20:00 -06:00
|
|
|
if day < 0 {
|
|
|
|
// Go back enough 400 year cycles to make day positive.
|
2009-01-16 11:14:12 -07:00
|
|
|
n := -day/_DaysPer400Years + 1;
|
2008-09-17 17:20:00 -06:00
|
|
|
year -= 400*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day += _DaysPer400Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
} else {
|
|
|
|
// Cut off 400 year cycles.
|
2009-01-16 11:14:12 -07:00
|
|
|
n := day/_DaysPer400Years;
|
2008-09-17 17:20:00 -06:00
|
|
|
year += 400*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day -= _DaysPer400Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cut off 100-year cycles
|
2009-01-16 11:14:12 -07:00
|
|
|
n := day/_DaysPer100Years;
|
2008-09-17 17:20:00 -06:00
|
|
|
year += 100*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day -= _DaysPer100Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Cut off 4-year cycles
|
2009-01-16 11:14:12 -07:00
|
|
|
n = day/_DaysPer4Years;
|
2008-09-17 17:20:00 -06:00
|
|
|
year += 4*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day -= _DaysPer4Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Cut off non-leap years.
|
|
|
|
n = day/365;
|
|
|
|
year += n;
|
|
|
|
day -= 365*n;
|
|
|
|
|
2009-01-26 12:22:21 -07:00
|
|
|
t.Year = year;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// If someone ever needs yearday,
|
|
|
|
// tyearday = day (+1?)
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
months := months(year);
|
2008-09-17 17:20:00 -06:00
|
|
|
var m int;
|
|
|
|
yday := int(day);
|
|
|
|
for m = 0; m < 12 && yday >= months[m]; m++ {
|
|
|
|
yday -= months[m]
|
|
|
|
}
|
2009-01-26 12:22:21 -07:00
|
|
|
t.Month = m+1;
|
|
|
|
t.Day = yday+1;
|
|
|
|
t.Zone = "GMT";
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func UTC() *Time {
|
2008-12-09 11:27:30 -07:00
|
|
|
return SecondsToUTC(Seconds())
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Should this return an error?
|
2009-01-20 15:40:40 -07:00
|
|
|
func SecondsToLocalTime(sec int64) *Time {
|
2008-10-07 13:31:31 -06:00
|
|
|
zone, offset, err := time.LookupTimezone(sec);
|
2008-09-22 17:41:14 -06:00
|
|
|
if err != nil {
|
2008-09-17 17:20:00 -06:00
|
|
|
return SecondsToUTC(sec)
|
|
|
|
}
|
|
|
|
t := SecondsToUTC(sec+int64(offset));
|
2009-01-26 12:22:21 -07:00
|
|
|
t.Zone = zone;
|
|
|
|
t.ZoneOffset = offset;
|
2008-09-17 17:20:00 -06:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func LocalTime() *Time {
|
2008-12-09 11:27:30 -07:00
|
|
|
return SecondsToLocalTime(Seconds())
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute number of seconds since January 1, 1970.
|
|
|
|
func (t *Time) Seconds() int64 {
|
|
|
|
// First, accumulate days since January 1, 2001.
|
|
|
|
// Using 2001 instead of 1970 makes the leap-year
|
2008-10-07 13:31:31 -06:00
|
|
|
// handling easier (see SecondsToUTC), because
|
2008-09-17 17:20:00 -06:00
|
|
|
// it is at the beginning of the 4-, 100-, and 400-year cycles.
|
|
|
|
day := int64(0);
|
|
|
|
|
|
|
|
// Rewrite year to be >= 2001.
|
2009-01-26 12:22:21 -07:00
|
|
|
year := t.Year;
|
2008-09-17 17:20:00 -06:00
|
|
|
if year < 2001 {
|
|
|
|
n := (2001 - year)/400 + 1;
|
|
|
|
year += 400*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day -= _DaysPer400Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add in days from 400-year cycles.
|
|
|
|
n := (year - 2001) / 400;
|
|
|
|
year -= 400*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day += _DaysPer400Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Add in 100-year cycles.
|
|
|
|
n = (year - 2001) / 100;
|
|
|
|
year -= 100*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day += _DaysPer100Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Add in 4-year cycles.
|
|
|
|
n = (year - 2001) / 4;
|
|
|
|
year -= 4*n;
|
2009-01-16 11:14:12 -07:00
|
|
|
day += _DaysPer4Years*n;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Add in non-leap years.
|
|
|
|
n = year - 2001;
|
|
|
|
day += 365*n;
|
|
|
|
|
|
|
|
// Add in days this year.
|
2009-01-26 12:22:21 -07:00
|
|
|
months := months(t.Year);
|
|
|
|
for m := 0; m < t.Month-1; m++ {
|
2008-09-17 17:20:00 -06:00
|
|
|
day += int64(months[m])
|
|
|
|
}
|
2009-01-26 12:22:21 -07:00
|
|
|
day += int64(t.Day - 1);
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Convert days to seconds since January 1, 2001.
|
2009-01-16 11:14:12 -07:00
|
|
|
sec := day * _SecondsPerDay;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Add in time elapsed today.
|
2009-01-26 12:22:21 -07:00
|
|
|
sec += int64(t.Hour) * 3600;
|
|
|
|
sec += int64(t.Minute) * 60;
|
|
|
|
sec += int64(t.Second);
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Convert from seconds since 2001 to seconds since 1970.
|
2009-01-16 11:14:12 -07:00
|
|
|
sec += _Days1970To2001 * _SecondsPerDay;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
// Account for local time zone.
|
2009-01-26 12:22:21 -07:00
|
|
|
sec -= int64(t.ZoneOffset);
|
2008-09-17 17:20:00 -06:00
|
|
|
return sec
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
var _LongDayNames = []string{
|
2008-09-17 17:20:00 -06:00
|
|
|
"Sunday",
|
|
|
|
"Monday",
|
|
|
|
"Tuesday",
|
|
|
|
"Wednesday",
|
|
|
|
"Thursday",
|
|
|
|
"Friday",
|
|
|
|
"Saturday"
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
var _ShortDayNames = []string{
|
2008-09-17 17:20:00 -06:00
|
|
|
"Sun",
|
|
|
|
"Mon",
|
|
|
|
"Tue",
|
|
|
|
"Wed",
|
|
|
|
"Thu",
|
|
|
|
"Fri",
|
|
|
|
"Sat"
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
var _ShortMonthNames = []string{
|
2008-09-17 17:20:00 -06:00
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec"
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
func _Copy(dst []byte, s string) {
|
2008-09-17 17:20:00 -06:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
dst[i] = s[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
func _Decimal(dst []byte, n int) {
|
2008-09-17 17:20:00 -06:00
|
|
|
if n < 0 {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
for i := len(dst)-1; i >= 0; i-- {
|
|
|
|
dst[i] = byte(n%10 + '0');
|
|
|
|
n /= 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:12 -07:00
|
|
|
func _AddString(buf []byte, bp int, s string) int {
|
2008-09-17 17:20:00 -06:00
|
|
|
n := len(s);
|
2009-01-16 11:14:12 -07:00
|
|
|
_Copy(buf[bp:bp+n], s);
|
2008-09-17 17:20:00 -06:00
|
|
|
return bp+n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just enough of strftime to implement the date formats below.
|
|
|
|
// Not exported.
|
2009-01-16 11:14:12 -07:00
|
|
|
func _Format(t *Time, fmt string) string {
|
2009-01-06 16:19:02 -07:00
|
|
|
buf := make([]byte, 128);
|
2008-10-07 13:31:31 -06:00
|
|
|
bp := 0;
|
2008-09-17 17:20:00 -06:00
|
|
|
|
|
|
|
for i := 0; i < len(fmt); i++ {
|
|
|
|
if fmt[i] == '%' {
|
2008-10-07 13:31:31 -06:00
|
|
|
i++;
|
2008-09-17 17:20:00 -06:00
|
|
|
switch fmt[i] {
|
|
|
|
case 'A': // %A full weekday name
|
2009-01-26 12:22:21 -07:00
|
|
|
bp = _AddString(buf, bp, _LongDayNames[t.Weekday]);
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'a': // %a abbreviated weekday name
|
2009-01-26 12:22:21 -07:00
|
|
|
bp = _AddString(buf, bp, _ShortDayNames[t.Weekday]);
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'b': // %b abbreviated month name
|
2009-01-26 12:22:21 -07:00
|
|
|
bp = _AddString(buf, bp, _ShortMonthNames[t.Month-1]);
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'd': // %d day of month (01-31)
|
2009-01-26 12:22:21 -07:00
|
|
|
_Decimal(buf[bp:bp+2], t.Day);
|
2008-10-07 13:31:31 -06:00
|
|
|
bp += 2;
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'e': // %e day of month ( 1-31)
|
2009-01-26 12:22:21 -07:00
|
|
|
if t.Day >= 10 {
|
|
|
|
_Decimal(buf[bp:bp+2], t.Day)
|
2008-09-17 17:20:00 -06:00
|
|
|
} else {
|
|
|
|
buf[bp] = ' ';
|
2009-01-26 12:22:21 -07:00
|
|
|
buf[bp+1] = byte(t.Day + '0')
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
bp += 2;
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'H': // %H hour 00-23
|
2009-01-26 12:22:21 -07:00
|
|
|
_Decimal(buf[bp:bp+2], t.Hour);
|
2008-10-07 13:31:31 -06:00
|
|
|
bp += 2;
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'M': // %M minute 00-59
|
2009-01-26 12:22:21 -07:00
|
|
|
_Decimal(buf[bp:bp+2], t.Minute);
|
2008-10-07 13:31:31 -06:00
|
|
|
bp += 2;
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'S': // %S second 00-59
|
2009-01-26 12:22:21 -07:00
|
|
|
_Decimal(buf[bp:bp+2], t.Second);
|
2008-10-07 13:31:31 -06:00
|
|
|
bp += 2;
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'Y': // %Y year 2008
|
2009-01-26 12:22:21 -07:00
|
|
|
_Decimal(buf[bp:bp+4], int(t.Year));
|
2008-10-07 13:31:31 -06:00
|
|
|
bp += 4;
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'y': // %y year 08
|
2009-01-26 12:22:21 -07:00
|
|
|
_Decimal(buf[bp:bp+2], int(t.Year%100));
|
2008-10-07 13:31:31 -06:00
|
|
|
bp += 2;
|
2008-09-17 17:20:00 -06:00
|
|
|
case 'Z':
|
2009-01-26 12:22:21 -07:00
|
|
|
bp = _AddString(buf, bp, t.Zone);
|
2008-09-17 17:20:00 -06:00
|
|
|
default:
|
|
|
|
buf[bp] = '%';
|
|
|
|
buf[bp+1] = fmt[i];
|
|
|
|
bp += 2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf[bp] = fmt[i];
|
2008-10-07 13:31:31 -06:00
|
|
|
bp++;
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(buf[0:bp])
|
|
|
|
}
|
|
|
|
|
|
|
|
// ANSI C asctime: Sun Nov 6 08:49:37 1994
|
|
|
|
func (t *Time) Asctime() string {
|
2009-01-16 11:14:12 -07:00
|
|
|
return _Format(t, "%a %b %e %H:%M:%S %Y")
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 850: Sunday, 06-Nov-94 08:49:37 GMT
|
|
|
|
func (t *Time) RFC850() string {
|
2009-01-16 11:14:12 -07:00
|
|
|
return _Format(t, "%A, %d-%b-%y %H:%M:%S %Z")
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 1123: Sun, 06 Nov 1994 08:49:37 GMT
|
|
|
|
func (t *Time) RFC1123() string {
|
2009-01-16 11:14:12 -07:00
|
|
|
return _Format(t, "%a, %d %b %Y %H:%M:%S %Z")
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// date(1) - Sun Nov 6 08:49:37 GMT 1994
|
|
|
|
func (t *Time) String() string {
|
2009-01-16 11:14:12 -07:00
|
|
|
return _Format(t, "%a %b %e %H:%M:%S %Z %Y")
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|
|
|
|
|