// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// The time package provides functionality for measuring and
+// displaying time.
package time
import (
"time"
)
-// Seconds since January 1, 1970 00:00:00 UTC
+// Seconds reports the number of seconds since the Unix epoch,
+// January 1, 1970 00:00:00 UTC.
func Seconds() int64 {
sec, nsec, err := os.Time();
if err != nil {
return sec
}
-// Nanoseconds since January 1, 1970 00:00:00 UTC
+// Nanoseconds reports the number of nanoseconds since the Unix epoch,
+// January 1, 1970 00:00:00 UTC.
func Nanoseconds() int64 {
sec, nsec, err := os.Time();
if err != nil {
return sec*1e9 + nsec
}
+// Days of the week.
const (
Sunday = iota;
Monday;
Saturday;
)
+// 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 = 0, Monday = 1, ...
+ Weekday int; // Sunday, Monday, ...
ZoneOffset int; // seconds west of UTC
Zone string;
}
days1970To2001 = 31*365+8;
)
+// SecondsToUTC converts sec, in number of seconds since the Unix epoch,
+// into a parsed Time value in the UTC time zone.
func SecondsToUTC(sec int64) *Time {
t := new(Time);
return t;
}
+// UTC returns the current time as a parsed Time value in the UTC time zone.
func UTC() *Time {
return SecondsToUTC(Seconds())
}
+// SecondsToLocalTime converts sec, in number of seconds since the Unix epoch,
+// into a parsed Time value in the local time zone.
func SecondsToLocalTime(sec int64) *Time {
- z, offset, err := time.LookupTimezone(sec);
+ z, offset, err := time.lookupTimezone(sec);
if err != nil {
return SecondsToUTC(sec)
}
return t
}
+// LocalTime returns the current time as a parsed Time value in the local time zone.
func LocalTime() *Time {
return SecondsToLocalTime(Seconds())
}
-// Compute number of seconds since January 1, 1970.
+// Seconds returns the number of seconds since January 1, 1970 represented by the
+// parsed Time value.
func (t *Time) Seconds() int64 {
// First, accumulate days since January 1, 2001.
// Using 2001 instead of 1970 makes the leap-year
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")
}
+// 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")
}
+// 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")
}
+// 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")
}
-
zoneDir = "/usr/share/zoneinfo/";
)
+// Errors that can be generated recovering time zone information.
var (
- BadZoneinfo = os.NewError("time: malformed zoneinfo");
- NoZoneinfo = os.NewError("time: unknown time zone")
+ badZoneinfo = os.NewError("time: malformed zoneinfo");
+ noZoneinfo = os.NewError("time: unknown time zone")
)
// Simple I/O interface to binary blob of data.
// 4-byte magic "TZif"
if magic := d.read(4); string(magic) != "TZif" {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
// 1-byte version, then 15 bytes of padding
var p []byte;
if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
vers := p[0];
for i := 0; i < 6; i++ {
nn, ok := d.big4();
if !ok {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
n[i] = int(nn);
}
isutc := d.read(n[NUTCLocal]);
if d.error { // ran out of data
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
// If version == 2, the entire file repeats, this time using
var ok bool;
var n uint32;
if n, ok = zonedata.big4(); !ok {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
z[i].utcoff = int(n);
var b byte;
if b, ok = zonedata.byte(); !ok {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
z[i].isdst = b != 0;
if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
z[i].name = byteString(abbrev[b:len(abbrev)])
}
var ok bool;
var n uint32;
if n, ok = txtimes.big4(); !ok {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
zt[i].time = int32(n);
if int(txzones[i]) >= len(z) {
- return nil, BadZoneinfo
+ return nil, badZoneinfo
}
zt[i].zone = &z[txzones[i]];
if i < len(isstd) {
n, err1 := io.Readn(fd, p);
fd.Close();
if err1 == nil { // too long
- return nil, BadZoneinfo;
+ return nil, badZoneinfo;
}
if err1 != io.ErrEOF {
return nil, err1;
}
}
-func LookupTimezone(sec int64) (zone string, offset int, err *os.Error) {
+func lookupTimezone(sec int64) (zone string, offset int, err *os.Error) {
once.Do(setupZone);
if zoneerr != nil || len(zones) == 0 {
return "UTC", 0, zoneerr