Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
e83c85ac
Commit
e83c85ac
authored
Jan 16, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
casify time
R=r DELTA=103 (1 added, 0 deleted, 102 changed) OCL=22914 CL=22937
parent
2c8d9a56
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
97 additions
and
96 deletions
+97
-96
tick.go
src/lib/time/tick.go
+2
-2
time.go
src/lib/time/time.go
+53
-53
time_test.go
src/lib/time/time_test.go
+15
-15
zoneinfo.go
src/lib/time/zoneinfo.go
+27
-26
No files found.
src/lib/time/tick.go
View file @
e83c85ac
...
@@ -25,7 +25,7 @@ import (
...
@@ -25,7 +25,7 @@ import (
// c <- nsec;
// c <- nsec;
// }
// }
func
T
icker
(
ns
int64
,
c
chan
int64
)
{
func
t
icker
(
ns
int64
,
c
chan
int64
)
{
var
tv
syscall
.
Timeval
;
var
tv
syscall
.
Timeval
;
now
:=
time
.
Nanoseconds
();
now
:=
time
.
Nanoseconds
();
when
:=
now
;
when
:=
now
;
...
@@ -54,7 +54,7 @@ export func Tick(ns int64) chan int64 {
...
@@ -54,7 +54,7 @@ export func Tick(ns int64) chan int64 {
return
nil
return
nil
}
}
c
:=
make
(
chan
int64
);
c
:=
make
(
chan
int64
);
go
T
icker
(
ns
,
c
);
go
t
icker
(
ns
,
c
);
return
c
;
return
c
;
}
}
src/lib/time/time.go
View file @
e83c85ac
...
@@ -46,39 +46,39 @@ export type Time struct {
...
@@ -46,39 +46,39 @@ export type Time struct {
zone
string
;
zone
string
;
}
}
var
RegularMonths
=
[]
int
{
var
nonleapyear
=
[]
int
{
31
,
28
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
31
,
28
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
}
}
var
LeapMonths
=
[]
int
{
var
leapyear
=
[]
int
{
31
,
29
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
31
,
29
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
}
}
func
M
onths
(
year
int64
)
[]
int
{
func
m
onths
(
year
int64
)
[]
int
{
if
year
%
4
==
0
&&
(
year
%
100
!=
0
||
year
%
400
==
0
)
{
if
year
%
4
==
0
&&
(
year
%
100
!=
0
||
year
%
400
==
0
)
{
return
LeapMonths
return
leapyear
}
}
return
RegularMonths
return
nonleapyear
}
}
const
(
const
(
SecondsPerDay
=
24
*
60
*
60
;
_
SecondsPerDay
=
24
*
60
*
60
;
DaysPer400Years
=
365
*
400
+
97
;
_
DaysPer400Years
=
365
*
400
+
97
;
DaysPer100Years
=
365
*
100
+
24
;
_
DaysPer100Years
=
365
*
100
+
24
;
DaysPer4Years
=
365
*
4
+
1
;
_
DaysPer4Years
=
365
*
4
+
1
;
Days1970To2001
=
31
*
365
+
8
;
_
Days1970To2001
=
31
*
365
+
8
;
)
)
export
func
SecondsToUTC
(
sec
int64
)
*
Time
{
export
func
SecondsToUTC
(
sec
int64
)
*
Time
{
t
:=
new
(
Time
);
t
:=
new
(
Time
);
// Split into time and day.
// Split into time and day.
day
:=
sec
/
SecondsPerDay
;
day
:=
sec
/
_
SecondsPerDay
;
sec
-=
day
*
SecondsPerDay
;
sec
-=
day
*
_
SecondsPerDay
;
if
sec
<
0
{
if
sec
<
0
{
day
--
;
day
--
;
sec
+=
SecondsPerDay
sec
+=
_
SecondsPerDay
}
}
// Time
// Time
...
@@ -95,30 +95,30 @@ export func SecondsToUTC(sec int64) *Time {
...
@@ -95,30 +95,30 @@ export func SecondsToUTC(sec int64) *Time {
// Change day from 0 = 1970 to 0 = 2001,
// Change day from 0 = 1970 to 0 = 2001,
// to make leap year calculations easier
// to make leap year calculations easier
// (2001 begins 4-, 100-, and 400-year cycles ending in a leap year.)
// (2001 begins 4-, 100-, and 400-year cycles ending in a leap year.)
day
-=
Days1970To2001
;
day
-=
_
Days1970To2001
;
year
:=
int64
(
2001
);
year
:=
int64
(
2001
);
if
day
<
0
{
if
day
<
0
{
// Go back enough 400 year cycles to make day positive.
// Go back enough 400 year cycles to make day positive.
n
:=
-
day
/
DaysPer400Years
+
1
;
n
:=
-
day
/
_
DaysPer400Years
+
1
;
year
-=
400
*
n
;
year
-=
400
*
n
;
day
+=
DaysPer400Years
*
n
;
day
+=
_
DaysPer400Years
*
n
;
}
else
{
}
else
{
// Cut off 400 year cycles.
// Cut off 400 year cycles.
n
:=
day
/
DaysPer400Years
;
n
:=
day
/
_
DaysPer400Years
;
year
+=
400
*
n
;
year
+=
400
*
n
;
day
-=
DaysPer400Years
*
n
;
day
-=
_
DaysPer400Years
*
n
;
}
}
// Cut off 100-year cycles
// Cut off 100-year cycles
n
:=
day
/
DaysPer100Years
;
n
:=
day
/
_
DaysPer100Years
;
year
+=
100
*
n
;
year
+=
100
*
n
;
day
-=
DaysPer100Years
*
n
;
day
-=
_
DaysPer100Years
*
n
;
// Cut off 4-year cycles
// Cut off 4-year cycles
n
=
day
/
DaysPer4Years
;
n
=
day
/
_
DaysPer4Years
;
year
+=
4
*
n
;
year
+=
4
*
n
;
day
-=
DaysPer4Years
*
n
;
day
-=
_
DaysPer4Years
*
n
;
// Cut off non-leap years.
// Cut off non-leap years.
n
=
day
/
365
;
n
=
day
/
365
;
...
@@ -130,7 +130,7 @@ export func SecondsToUTC(sec int64) *Time {
...
@@ -130,7 +130,7 @@ export func SecondsToUTC(sec int64) *Time {
// If someone ever needs yearday,
// If someone ever needs yearday,
// tyearday = day (+1?)
// tyearday = day (+1?)
months
:=
M
onths
(
year
);
months
:=
m
onths
(
year
);
var
m
int
;
var
m
int
;
yday
:=
int
(
day
);
yday
:=
int
(
day
);
for
m
=
0
;
m
<
12
&&
yday
>=
months
[
m
];
m
++
{
for
m
=
0
;
m
<
12
&&
yday
>=
months
[
m
];
m
++
{
...
@@ -176,37 +176,37 @@ func (t *Time) Seconds() int64 {
...
@@ -176,37 +176,37 @@ func (t *Time) Seconds() int64 {
if
year
<
2001
{
if
year
<
2001
{
n
:=
(
2001
-
year
)
/
400
+
1
;
n
:=
(
2001
-
year
)
/
400
+
1
;
year
+=
400
*
n
;
year
+=
400
*
n
;
day
-=
DaysPer400Years
*
n
;
day
-=
_
DaysPer400Years
*
n
;
}
}
// Add in days from 400-year cycles.
// Add in days from 400-year cycles.
n
:=
(
year
-
2001
)
/
400
;
n
:=
(
year
-
2001
)
/
400
;
year
-=
400
*
n
;
year
-=
400
*
n
;
day
+=
DaysPer400Years
*
n
;
day
+=
_
DaysPer400Years
*
n
;
// Add in 100-year cycles.
// Add in 100-year cycles.
n
=
(
year
-
2001
)
/
100
;
n
=
(
year
-
2001
)
/
100
;
year
-=
100
*
n
;
year
-=
100
*
n
;
day
+=
DaysPer100Years
*
n
;
day
+=
_
DaysPer100Years
*
n
;
// Add in 4-year cycles.
// Add in 4-year cycles.
n
=
(
year
-
2001
)
/
4
;
n
=
(
year
-
2001
)
/
4
;
year
-=
4
*
n
;
year
-=
4
*
n
;
day
+=
DaysPer4Years
*
n
;
day
+=
_
DaysPer4Years
*
n
;
// Add in non-leap years.
// Add in non-leap years.
n
=
year
-
2001
;
n
=
year
-
2001
;
day
+=
365
*
n
;
day
+=
365
*
n
;
// Add in days this year.
// Add in days this year.
months
:=
M
onths
(
t
.
year
);
months
:=
m
onths
(
t
.
year
);
for
m
:=
0
;
m
<
t
.
month
-
1
;
m
++
{
for
m
:=
0
;
m
<
t
.
month
-
1
;
m
++
{
day
+=
int64
(
months
[
m
])
day
+=
int64
(
months
[
m
])
}
}
day
+=
int64
(
t
.
day
-
1
);
day
+=
int64
(
t
.
day
-
1
);
// Convert days to seconds since January 1, 2001.
// Convert days to seconds since January 1, 2001.
sec
:=
day
*
SecondsPerDay
;
sec
:=
day
*
_
SecondsPerDay
;
// Add in time elapsed today.
// Add in time elapsed today.
sec
+=
int64
(
t
.
hour
)
*
3600
;
sec
+=
int64
(
t
.
hour
)
*
3600
;
...
@@ -214,14 +214,14 @@ func (t *Time) Seconds() int64 {
...
@@ -214,14 +214,14 @@ func (t *Time) Seconds() int64 {
sec
+=
int64
(
t
.
second
);
sec
+=
int64
(
t
.
second
);
// Convert from seconds since 2001 to seconds since 1970.
// Convert from seconds since 2001 to seconds since 1970.
sec
+=
Days1970To2001
*
SecondsPerDay
;
sec
+=
_Days1970To2001
*
_
SecondsPerDay
;
// Account for local time zone.
// Account for local time zone.
sec
-=
int64
(
t
.
zoneoffset
);
sec
-=
int64
(
t
.
zoneoffset
);
return
sec
return
sec
}
}
var
LongDayNames
=
[]
string
{
var
_
LongDayNames
=
[]
string
{
"Sunday"
,
"Sunday"
,
"Monday"
,
"Monday"
,
"Tuesday"
,
"Tuesday"
,
...
@@ -231,7 +231,7 @@ var LongDayNames = []string{
...
@@ -231,7 +231,7 @@ var LongDayNames = []string{
"Saturday"
"Saturday"
}
}
var
ShortDayNames
=
[]
string
{
var
_
ShortDayNames
=
[]
string
{
"Sun"
,
"Sun"
,
"Mon"
,
"Mon"
,
"Tue"
,
"Tue"
,
...
@@ -241,7 +241,7 @@ var ShortDayNames = []string{
...
@@ -241,7 +241,7 @@ var ShortDayNames = []string{
"Sat"
"Sat"
}
}
var
ShortMonthNames
=
[]
string
{
var
_
ShortMonthNames
=
[]
string
{
"Jan"
,
"Jan"
,
"Feb"
,
"Feb"
,
"Mar"
,
"Mar"
,
...
@@ -256,13 +256,13 @@ var ShortMonthNames = []string{
...
@@ -256,13 +256,13 @@ var ShortMonthNames = []string{
"Dec"
"Dec"
}
}
func
Copy
(
dst
[]
byte
,
s
string
)
{
func
_
Copy
(
dst
[]
byte
,
s
string
)
{
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
dst
[
i
]
=
s
[
i
]
dst
[
i
]
=
s
[
i
]
}
}
}
}
func
Decimal
(
dst
[]
byte
,
n
int
)
{
func
_
Decimal
(
dst
[]
byte
,
n
int
)
{
if
n
<
0
{
if
n
<
0
{
n
=
0
n
=
0
}
}
...
@@ -272,15 +272,15 @@ func Decimal(dst []byte, n int) {
...
@@ -272,15 +272,15 @@ func Decimal(dst []byte, n int) {
}
}
}
}
func
AddString
(
buf
[]
byte
,
bp
int
,
s
string
)
int
{
func
_
AddString
(
buf
[]
byte
,
bp
int
,
s
string
)
int
{
n
:=
len
(
s
);
n
:=
len
(
s
);
Copy
(
buf
[
bp
:
bp
+
n
],
s
);
_
Copy
(
buf
[
bp
:
bp
+
n
],
s
);
return
bp
+
n
return
bp
+
n
}
}
// Just enough of strftime to implement the date formats below.
// Just enough of strftime to implement the date formats below.
// Not exported.
// Not exported.
func
Format
(
t
*
Time
,
fmt
string
)
string
{
func
_
Format
(
t
*
Time
,
fmt
string
)
string
{
buf
:=
make
([]
byte
,
128
);
buf
:=
make
([]
byte
,
128
);
bp
:=
0
;
bp
:=
0
;
...
@@ -289,39 +289,39 @@ func Format(t *Time, fmt string) string {
...
@@ -289,39 +289,39 @@ func Format(t *Time, fmt string) string {
i
++
;
i
++
;
switch
fmt
[
i
]
{
switch
fmt
[
i
]
{
case
'A'
:
// %A full weekday name
case
'A'
:
// %A full weekday name
bp
=
AddString
(
buf
,
bp
,
LongDayNames
[
t
.
weekday
]);
bp
=
_AddString
(
buf
,
bp
,
_
LongDayNames
[
t
.
weekday
]);
case
'a'
:
// %a abbreviated weekday name
case
'a'
:
// %a abbreviated weekday name
bp
=
AddString
(
buf
,
bp
,
ShortDayNames
[
t
.
weekday
]);
bp
=
_AddString
(
buf
,
bp
,
_
ShortDayNames
[
t
.
weekday
]);
case
'b'
:
// %b abbreviated month name
case
'b'
:
// %b abbreviated month name
bp
=
AddString
(
buf
,
bp
,
ShortMonthNames
[
t
.
month
-
1
]);
bp
=
_AddString
(
buf
,
bp
,
_
ShortMonthNames
[
t
.
month
-
1
]);
case
'd'
:
// %d day of month (01-31)
case
'd'
:
// %d day of month (01-31)
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
day
);
_
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
day
);
bp
+=
2
;
bp
+=
2
;
case
'e'
:
// %e day of month ( 1-31)
case
'e'
:
// %e day of month ( 1-31)
if
t
.
day
>=
10
{
if
t
.
day
>=
10
{
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
day
)
_
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
day
)
}
else
{
}
else
{
buf
[
bp
]
=
' '
;
buf
[
bp
]
=
' '
;
buf
[
bp
+
1
]
=
byte
(
t
.
day
+
'0'
)
buf
[
bp
+
1
]
=
byte
(
t
.
day
+
'0'
)
}
}
bp
+=
2
;
bp
+=
2
;
case
'H'
:
// %H hour 00-23
case
'H'
:
// %H hour 00-23
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
hour
);
_
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
hour
);
bp
+=
2
;
bp
+=
2
;
case
'M'
:
// %M minute 00-59
case
'M'
:
// %M minute 00-59
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
minute
);
_
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
minute
);
bp
+=
2
;
bp
+=
2
;
case
'S'
:
// %S second 00-59
case
'S'
:
// %S second 00-59
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
second
);
_
Decimal
(
buf
[
bp
:
bp
+
2
],
t
.
second
);
bp
+=
2
;
bp
+=
2
;
case
'Y'
:
// %Y year 2008
case
'Y'
:
// %Y year 2008
Decimal
(
buf
[
bp
:
bp
+
4
],
int
(
t
.
year
));
_
Decimal
(
buf
[
bp
:
bp
+
4
],
int
(
t
.
year
));
bp
+=
4
;
bp
+=
4
;
case
'y'
:
// %y year 08
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
;
bp
+=
2
;
case
'Z'
:
case
'Z'
:
bp
=
AddString
(
buf
,
bp
,
t
.
zone
);
bp
=
_
AddString
(
buf
,
bp
,
t
.
zone
);
default
:
default
:
buf
[
bp
]
=
'%'
;
buf
[
bp
]
=
'%'
;
buf
[
bp
+
1
]
=
fmt
[
i
];
buf
[
bp
+
1
]
=
fmt
[
i
];
...
@@ -337,21 +337,21 @@ func Format(t *Time, fmt string) string {
...
@@ -337,21 +337,21 @@ func Format(t *Time, fmt string) string {
// ANSI C asctime: Sun Nov 6 08:49:37 1994
// ANSI C asctime: Sun Nov 6 08:49:37 1994
func
(
t
*
Time
)
Asctime
()
string
{
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"
)
}
}
// RFC 850: Sunday, 06-Nov-94 08:49:37 GMT
// RFC 850: Sunday, 06-Nov-94 08:49:37 GMT
func
(
t
*
Time
)
RFC850
()
string
{
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"
)
}
}
// RFC 1123: Sun, 06 Nov 1994 08:49:37 GMT
// RFC 1123: Sun, 06 Nov 1994 08:49:37 GMT
func
(
t
*
Time
)
RFC1123
()
string
{
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"
)
}
}
// date(1) - Sun Nov 6 08:49:37 GMT 1994
// date(1) - Sun Nov 6 08:49:37 GMT 1994
func
(
t
*
Time
)
String
()
string
{
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"
)
}
}
src/lib/time/time_test.go
View file @
e83c85ac
...
@@ -9,27 +9,27 @@ import (
...
@@ -9,27 +9,27 @@ import (
"time"
;
"time"
;
)
)
type
TimeTest
struct
{
type
_
TimeTest
struct
{
seconds
int64
;
seconds
int64
;
golden
Time
;
golden
Time
;
}
}
var
utctests
=
[]
TimeTest
{
var
utctests
=
[]
_
TimeTest
{
TimeTest
{
0
,
Time
{
1970
,
1
,
1
,
0
,
0
,
0
,
Thursday
,
0
,
"GMT"
}},
_
TimeTest
{
0
,
Time
{
1970
,
1
,
1
,
0
,
0
,
0
,
Thursday
,
0
,
"GMT"
}},
TimeTest
{
1221681866
,
Time
{
2008
,
9
,
17
,
20
,
4
,
26
,
Wednesday
,
0
,
"GMT"
}},
_
TimeTest
{
1221681866
,
Time
{
2008
,
9
,
17
,
20
,
4
,
26
,
Wednesday
,
0
,
"GMT"
}},
TimeTest
{
-
1221681866
,
Time
{
1931
,
4
,
16
,
3
,
55
,
34
,
Thursday
,
0
,
"GMT"
}},
_
TimeTest
{
-
1221681866
,
Time
{
1931
,
4
,
16
,
3
,
55
,
34
,
Thursday
,
0
,
"GMT"
}},
TimeTest
{
1e18
,
Time
{
31688740476
,
10
,
23
,
1
,
46
,
40
,
Friday
,
0
,
"GMT"
}},
_
TimeTest
{
1e18
,
Time
{
31688740476
,
10
,
23
,
1
,
46
,
40
,
Friday
,
0
,
"GMT"
}},
TimeTest
{
-
1e18
,
Time
{
-
31688736537
,
3
,
10
,
22
,
13
,
20
,
Tuesday
,
0
,
"GMT"
}},
_
TimeTest
{
-
1e18
,
Time
{
-
31688736537
,
3
,
10
,
22
,
13
,
20
,
Tuesday
,
0
,
"GMT"
}},
TimeTest
{
0x7fffffffffffffff
,
Time
{
292277026596
,
12
,
4
,
15
,
30
,
7
,
Sunday
,
0
,
"GMT"
}},
_
TimeTest
{
0x7fffffffffffffff
,
Time
{
292277026596
,
12
,
4
,
15
,
30
,
7
,
Sunday
,
0
,
"GMT"
}},
TimeTest
{
-
0x8000000000000000
,
Time
{
-
292277022657
,
1
,
27
,
8
,
29
,
52
,
Sunday
,
0
,
"GMT"
}}
_
TimeTest
{
-
0x8000000000000000
,
Time
{
-
292277022657
,
1
,
27
,
8
,
29
,
52
,
Sunday
,
0
,
"GMT"
}}
}
}
var
localtests
=
[]
TimeTest
{
var
localtests
=
[]
_
TimeTest
{
TimeTest
{
0
,
Time
{
1969
,
12
,
31
,
16
,
0
,
0
,
Wednesday
,
-
8
*
60
*
60
,
"PST"
}},
_
TimeTest
{
0
,
Time
{
1969
,
12
,
31
,
16
,
0
,
0
,
Wednesday
,
-
8
*
60
*
60
,
"PST"
}},
TimeTest
{
1221681866
,
Time
{
2008
,
9
,
17
,
13
,
4
,
26
,
Wednesday
,
-
7
*
60
*
60
,
"PDT"
}}
_
TimeTest
{
1221681866
,
Time
{
2008
,
9
,
17
,
13
,
4
,
26
,
Wednesday
,
-
7
*
60
*
60
,
"PDT"
}}
}
}
func
Same
(
t
,
u
*
Time
)
bool
{
func
_
Same
(
t
,
u
*
Time
)
bool
{
return
t
.
year
==
u
.
year
return
t
.
year
==
u
.
year
&&
t
.
month
==
u
.
month
&&
t
.
month
==
u
.
month
&&
t
.
day
==
u
.
day
&&
t
.
day
==
u
.
day
...
@@ -50,7 +50,7 @@ export func TestSecondsToUTC(t *testing.T) {
...
@@ -50,7 +50,7 @@ export func TestSecondsToUTC(t *testing.T) {
if
newsec
!=
sec
{
if
newsec
!=
sec
{
t
.
Errorf
(
"SecondsToUTC(%d).Seconds() = %d"
,
sec
,
newsec
);
t
.
Errorf
(
"SecondsToUTC(%d).Seconds() = %d"
,
sec
,
newsec
);
}
}
if
!
Same
(
tm
,
golden
)
{
if
!
_
Same
(
tm
,
golden
)
{
t
.
Errorf
(
"SecondsToUTC(%d):"
,
sec
);
t
.
Errorf
(
"SecondsToUTC(%d):"
,
sec
);
t
.
Errorf
(
" want=%v"
,
*
golden
);
t
.
Errorf
(
" want=%v"
,
*
golden
);
t
.
Errorf
(
" have=%v"
,
*
tm
);
t
.
Errorf
(
" have=%v"
,
*
tm
);
...
@@ -67,7 +67,7 @@ export func TestSecondsToLocalTime(t *testing.T) {
...
@@ -67,7 +67,7 @@ export func TestSecondsToLocalTime(t *testing.T) {
if
newsec
!=
sec
{
if
newsec
!=
sec
{
t
.
Errorf
(
"SecondsToLocalTime(%d).Seconds() = %d"
,
sec
,
newsec
);
t
.
Errorf
(
"SecondsToLocalTime(%d).Seconds() = %d"
,
sec
,
newsec
);
}
}
if
!
Same
(
tm
,
golden
)
{
if
!
_
Same
(
tm
,
golden
)
{
t
.
Errorf
(
"SecondsToLocalTime(%d):"
,
sec
);
t
.
Errorf
(
"SecondsToLocalTime(%d):"
,
sec
);
t
.
Errorf
(
" want=%v"
,
*
golden
);
t
.
Errorf
(
" want=%v"
,
*
golden
);
t
.
Errorf
(
" have=%v"
,
*
tm
);
t
.
Errorf
(
" have=%v"
,
*
tm
);
...
...
src/lib/time/zoneinfo.go
View file @
e83c85ac
...
@@ -10,13 +10,14 @@
...
@@ -10,13 +10,14 @@
package
time
package
time
import
(
import
(
"io"
;
"once"
;
"once"
;
"os"
"os"
)
)
const
(
const
(
MaxFileSize
=
8192
;
// actual files are closer to 1K
_
MaxFileSize
=
8192
;
// actual files are closer to 1K
HeaderSize
=
4
+
16
+
4
*
7
_
HeaderSize
=
4
+
16
+
4
*
7
)
)
export
var
(
export
var
(
...
@@ -25,13 +26,13 @@ export var (
...
@@ -25,13 +26,13 @@ export var (
)
)
// Simple I/O interface to binary blob of data.
// Simple I/O interface to binary blob of data.
type
Data
struct
{
type
_
Data
struct
{
p
[]
byte
;
p
[]
byte
;
error
bool
;
error
bool
;
}
}
func
(
d
*
Data
)
Read
(
n
int
)
[]
byte
{
func
(
d
*
_
Data
)
Read
(
n
int
)
[]
byte
{
if
len
(
d
.
p
)
<
n
{
if
len
(
d
.
p
)
<
n
{
d
.
p
=
nil
;
d
.
p
=
nil
;
d
.
error
=
true
;
d
.
error
=
true
;
...
@@ -42,7 +43,7 @@ func (d *Data) Read(n int) []byte {
...
@@ -42,7 +43,7 @@ func (d *Data) Read(n int) []byte {
return
p
return
p
}
}
func
(
d
*
Data
)
Big4
()
(
n
uint32
,
ok
bool
)
{
func
(
d
*
_
Data
)
Big4
()
(
n
uint32
,
ok
bool
)
{
p
:=
d
.
Read
(
4
);
p
:=
d
.
Read
(
4
);
if
len
(
p
)
<
4
{
if
len
(
p
)
<
4
{
d
.
error
=
true
;
d
.
error
=
true
;
...
@@ -51,7 +52,7 @@ func (d *Data) Big4() (n uint32, ok bool) {
...
@@ -51,7 +52,7 @@ func (d *Data) Big4() (n uint32, ok bool) {
return
uint32
(
p
[
0
])
<<
24
|
uint32
(
p
[
1
])
<<
16
|
uint32
(
p
[
2
])
<<
8
|
uint32
(
p
[
3
]),
true
return
uint32
(
p
[
0
])
<<
24
|
uint32
(
p
[
1
])
<<
16
|
uint32
(
p
[
2
])
<<
8
|
uint32
(
p
[
3
]),
true
}
}
func
(
d
*
Data
)
Byte
()
(
n
byte
,
ok
bool
)
{
func
(
d
*
_
Data
)
Byte
()
(
n
byte
,
ok
bool
)
{
p
:=
d
.
Read
(
1
);
p
:=
d
.
Read
(
1
);
if
len
(
p
)
<
1
{
if
len
(
p
)
<
1
{
d
.
error
=
true
;
d
.
error
=
true
;
...
@@ -62,7 +63,7 @@ func (d *Data) Byte() (n byte, ok bool) {
...
@@ -62,7 +63,7 @@ func (d *Data) Byte() (n byte, ok bool) {
// Make a string by stopping at the first NUL
// Make a string by stopping at the first NUL
func
ByteString
(
p
[]
byte
)
string
{
func
_
ByteString
(
p
[]
byte
)
string
{
for
i
:=
0
;
i
<
len
(
p
);
i
++
{
for
i
:=
0
;
i
<
len
(
p
);
i
++
{
if
p
[
i
]
==
0
{
if
p
[
i
]
==
0
{
return
string
(
p
[
0
:
i
])
return
string
(
p
[
0
:
i
])
...
@@ -72,21 +73,21 @@ func ByteString(p []byte) string {
...
@@ -72,21 +73,21 @@ func ByteString(p []byte) string {
}
}
// Parsed representation
// Parsed representation
type
Zone
struct
{
type
_
Zone
struct
{
utcoff
int
;
utcoff
int
;
isdst
bool
;
isdst
bool
;
name
string
;
name
string
;
}
}
type
Zonetime
struct
{
type
_
Zonetime
struct
{
time
int32
;
// transition time, in seconds since 1970 GMT
time
int32
;
// transition time, in seconds since 1970 GMT
zone
*
Zone
;
// the zone that goes into effect at that time
zone
*
_
Zone
;
// the zone that goes into effect at that time
isstd
,
isutc
bool
;
// ignored - no idea what these mean
isstd
,
isutc
bool
;
// ignored - no idea what these mean
}
}
func
ParseZoneinfo
(
bytes
[]
byte
)
(
zt
[]
Zonetime
,
err
*
os
.
Error
)
{
func
parseinfo
(
bytes
[]
byte
)
(
zt
[]
_
Zonetime
,
err
*
os
.
Error
)
{
data1
:=
Data
{
bytes
,
false
};
data1
:=
_
Data
{
bytes
,
false
};
data
:=
&
data1
;
data
:=
&
data1
;
// 4-byte magic "TZif"
// 4-byte magic "TZif"
...
@@ -126,21 +127,21 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
...
@@ -126,21 +127,21 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
}
}
// Transition times.
// Transition times.
txtimes1
:=
Data
{
data
.
Read
(
n
[
NTime
]
*
4
),
false
};
txtimes1
:=
_
Data
{
data
.
Read
(
n
[
NTime
]
*
4
),
false
};
txtimes
:=
&
txtimes1
;
txtimes
:=
&
txtimes1
;
// Time zone indices for transition times.
// Time zone indices for transition times.
txzones
:=
data
.
Read
(
n
[
NTime
]);
txzones
:=
data
.
Read
(
n
[
NTime
]);
// Zone info structures
// Zone info structures
zonedata1
:=
Data
{
data
.
Read
(
n
[
NZone
]
*
6
),
false
};
zonedata1
:=
_
Data
{
data
.
Read
(
n
[
NZone
]
*
6
),
false
};
zonedata
:=
&
zonedata1
;
zonedata
:=
&
zonedata1
;
// Time zone abbreviations.
// Time zone abbreviations.
abbrev
:=
data
.
Read
(
n
[
NChar
]);
abbrev
:=
data
.
Read
(
n
[
NChar
]);
// Leap-second time pairs
// Leap-second time pairs
leapdata1
:=
Data
{
data
.
Read
(
n
[
NLeap
]
*
8
),
false
};
leapdata1
:=
_
Data
{
data
.
Read
(
n
[
NLeap
]
*
8
),
false
};
leapdata
:=
&
leapdata1
;
leapdata
:=
&
leapdata1
;
// Whether tx times associated with local time types
// Whether tx times associated with local time types
...
@@ -162,7 +163,7 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
...
@@ -162,7 +163,7 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
// Now we can build up a useful data structure.
// Now we can build up a useful data structure.
// First the zone information.
// First the zone information.
// utcoff[4] isdst[1] nameindex[1]
// utcoff[4] isdst[1] nameindex[1]
zone
:=
make
([]
Zone
,
n
[
NZone
]);
zone
:=
make
([]
_
Zone
,
n
[
NZone
]);
for
i
:=
0
;
i
<
len
(
zone
);
i
++
{
for
i
:=
0
;
i
<
len
(
zone
);
i
++
{
var
ok
bool
;
var
ok
bool
;
var
n
uint32
;
var
n
uint32
;
...
@@ -178,11 +179,11 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
...
@@ -178,11 +179,11 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
if
b
,
ok
=
zonedata
.
Byte
();
!
ok
||
int
(
b
)
>=
len
(
abbrev
)
{
if
b
,
ok
=
zonedata
.
Byte
();
!
ok
||
int
(
b
)
>=
len
(
abbrev
)
{
return
nil
,
BadZoneinfo
return
nil
,
BadZoneinfo
}
}
zone
[
i
]
.
name
=
ByteString
(
abbrev
[
b
:
len
(
abbrev
)])
zone
[
i
]
.
name
=
_
ByteString
(
abbrev
[
b
:
len
(
abbrev
)])
}
}
// Now the transition time info.
// Now the transition time info.
zt
=
make
([]
Zonetime
,
n
[
NTime
]);
zt
=
make
([]
_
Zonetime
,
n
[
NTime
]);
for
i
:=
0
;
i
<
len
(
zt
);
i
++
{
for
i
:=
0
;
i
<
len
(
zt
);
i
++
{
var
ok
bool
;
var
ok
bool
;
var
n
uint32
;
var
n
uint32
;
...
@@ -204,7 +205,7 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
...
@@ -204,7 +205,7 @@ func ParseZoneinfo(bytes []byte) (zt []Zonetime, err *os.Error) {
return
zt
,
nil
return
zt
,
nil
}
}
func
ReadF
ile
(
name
string
,
max
int
)
(
p
[]
byte
,
err
*
os
.
Error
)
{
func
readf
ile
(
name
string
,
max
int
)
(
p
[]
byte
,
err
*
os
.
Error
)
{
fd
,
e
:=
os
.
Open
(
name
,
os
.
O_RDONLY
,
0
);
fd
,
e
:=
os
.
Open
(
name
,
os
.
O_RDONLY
,
0
);
if
e
!=
nil
{
if
e
!=
nil
{
return
nil
,
e
return
nil
,
e
...
@@ -228,29 +229,29 @@ func ReadFile(name string, max int) (p []byte, err *os.Error) {
...
@@ -228,29 +229,29 @@ func ReadFile(name string, max int) (p []byte, err *os.Error) {
}
}
func
ReadZoneinfoFile
(
name
string
)
(
tx
[]
Zonetime
,
err
*
os
.
Error
)
{
func
readinfofile
(
name
string
)
(
tx
[]
_
Zonetime
,
err
*
os
.
Error
)
{
data
,
e
:=
ReadFile
(
name
,
MaxFileSize
);
data
,
e
:=
readfile
(
name
,
_
MaxFileSize
);
if
e
!=
nil
{
if
e
!=
nil
{
return
nil
,
e
return
nil
,
e
}
}
tx
,
err
=
ParseZon
einfo
(
data
);
tx
,
err
=
pars
einfo
(
data
);
return
tx
,
err
return
tx
,
err
}
}
var
zones
[]
Zonetime
var
zones
[]
_
Zonetime
var
zoneerr
*
os
.
Error
var
zoneerr
*
os
.
Error
func
SetupZone
()
{
func
_
SetupZone
()
{
// TODO: /etc/localtime is the default time zone info
// TODO: /etc/localtime is the default time zone info
// for the system, but libc allows setting an environment
// for the system, but libc allows setting an environment
// variable in order to direct reading a different file
// variable in order to direct reading a different file
// (in /usr/share/zoneinfo). We should check that
// (in /usr/share/zoneinfo). We should check that
// environment variable.
// environment variable.
zones
,
zoneerr
=
ReadZoneinfoF
ile
(
"/etc/localtime"
);
zones
,
zoneerr
=
readinfof
ile
(
"/etc/localtime"
);
}
}
export
func
LookupTimezone
(
sec
int64
)
(
zone
string
,
offset
int
,
err
*
os
.
Error
)
{
export
func
LookupTimezone
(
sec
int64
)
(
zone
string
,
offset
int
,
err
*
os
.
Error
)
{
once
.
Do
(
&
SetupZone
);
once
.
Do
(
&
_
SetupZone
);
if
zoneerr
!=
nil
||
len
(
zones
)
==
0
{
if
zoneerr
!=
nil
||
len
(
zones
)
==
0
{
return
"GMT"
,
0
,
zoneerr
return
"GMT"
,
0
,
zoneerr
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment