Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
c5560d3a
Commit
c5560d3a
authored
Mar 07, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
document time
R=rsc DELTA=42 (23 added, 1 deleted, 18 changed) OCL=25881 CL=25886
parent
1e37e8a4
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
19 deletions
+41
-19
src/lib/time/sleep.go
src/lib/time/sleep.go
+2
-0
src/lib/time/tick.go
src/lib/time/tick.go
+3
-0
src/lib/time/time.go
src/lib/time/time.go
+22
-6
src/lib/time/zoneinfo.go
src/lib/time/zoneinfo.go
+14
-13
No files found.
src/lib/time/sleep.go
View file @
c5560d3a
...
...
@@ -10,6 +10,8 @@ import (
"unsafe"
;
)
// Sleep pauses the current goroutine for ns nanoseconds.
// It returns os.EINTR if interrupted.
func
Sleep
(
ns
int64
)
*
os
.
Error
{
var
tv
syscall
.
Timeval
;
syscall
.
Nstotimeval
(
ns
,
&
tv
);
...
...
src/lib/time/tick.go
View file @
c5560d3a
...
...
@@ -48,6 +48,9 @@ func ticker(ns int64, c chan int64) {
}
}
// Tick creates a synchronous channel that will send the time, in nanoseconds,
// every ns nanoseconds. It adjusts the intervals to make up for pauses in
// delivery of the ticks.
func
Tick
(
ns
int64
)
chan
int64
{
if
ns
<=
0
{
return
nil
...
...
src/lib/time/time.go
View file @
c5560d3a
...
...
@@ -2,6 +2,8 @@
// 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
(
...
...
@@ -9,7 +11,8 @@ 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
{
...
...
@@ -18,7 +21,8 @@ func Seconds() int64 {
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
{
...
...
@@ -27,6 +31,7 @@ func Nanoseconds() int64 {
return
sec
*
1e9
+
nsec
}
// Days of the week.
const
(
Sunday
=
iota
;
Monday
;
...
...
@@ -37,11 +42,12 @@ const (
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
;
}
...
...
@@ -70,6 +76,8 @@ const (
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
);
...
...
@@ -143,12 +151,15 @@ func SecondsToUTC(sec int64) *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
.
L
ookupTimezone
(
sec
);
z
,
offset
,
err
:=
time
.
l
ookupTimezone
(
sec
);
if
err
!=
nil
{
return
SecondsToUTC
(
sec
)
}
...
...
@@ -158,11 +169,13 @@ func SecondsToLocalTime(sec int64) *Time {
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
...
...
@@ -334,23 +347,26 @@ func format(t *Time, fmt string) string {
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"
)
}
src/lib/time/zoneinfo.go
View file @
c5560d3a
...
...
@@ -22,9 +22,10 @@ const (
zoneDir
=
"/usr/share/zoneinfo/"
;
)
// Errors that can be generated recovering time zone information.
var
(
B
adZoneinfo
=
os
.
NewError
(
"time: malformed zoneinfo"
);
N
oZoneinfo
=
os
.
NewError
(
"time: unknown time zone"
)
b
adZoneinfo
=
os
.
NewError
(
"time: malformed zoneinfo"
);
n
oZoneinfo
=
os
.
NewError
(
"time: unknown time zone"
)
)
// Simple I/O interface to binary blob of data.
...
...
@@ -92,13 +93,13 @@ func parseinfo(bytes []byte) (zt []zonetime, err *os.Error) {
// 4-byte magic "TZif"
if
magic
:=
d
.
read
(
4
);
string
(
magic
)
!=
"TZif"
{
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
// 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
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
vers
:=
p
[
0
];
...
...
@@ -121,7 +122,7 @@ func parseinfo(bytes []byte) (zt []zonetime, err *os.Error) {
for
i
:=
0
;
i
<
6
;
i
++
{
nn
,
ok
:=
d
.
big4
();
if
!
ok
{
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
n
[
i
]
=
int
(
nn
);
}
...
...
@@ -150,7 +151,7 @@ func parseinfo(bytes []byte) (zt []zonetime, err *os.Error) {
isutc
:=
d
.
read
(
n
[
NUTCLocal
]);
if
d
.
error
{
// ran out of data
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
// If version == 2, the entire file repeats, this time using
...
...
@@ -165,16 +166,16 @@ func parseinfo(bytes []byte) (zt []zonetime, err *os.Error) {
var
ok
bool
;
var
n
uint32
;
if
n
,
ok
=
zonedata
.
big4
();
!
ok
{
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
z
[
i
]
.
utcoff
=
int
(
n
);
var
b
byte
;
if
b
,
ok
=
zonedata
.
byte
();
!
ok
{
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
z
[
i
]
.
isdst
=
b
!=
0
;
if
b
,
ok
=
zonedata
.
byte
();
!
ok
||
int
(
b
)
>=
len
(
abbrev
)
{
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
z
[
i
]
.
name
=
byteString
(
abbrev
[
b
:
len
(
abbrev
)])
}
...
...
@@ -185,11 +186,11 @@ func parseinfo(bytes []byte) (zt []zonetime, err *os.Error) {
var
ok
bool
;
var
n
uint32
;
if
n
,
ok
=
txtimes
.
big4
();
!
ok
{
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
zt
[
i
]
.
time
=
int32
(
n
);
if
int
(
txzones
[
i
])
>=
len
(
z
)
{
return
nil
,
B
adZoneinfo
return
nil
,
b
adZoneinfo
}
zt
[
i
]
.
zone
=
&
z
[
txzones
[
i
]];
if
i
<
len
(
isstd
)
{
...
...
@@ -211,7 +212,7 @@ func readfile(name string, max int) (p []byte, err *os.Error) {
n
,
err1
:=
io
.
Readn
(
fd
,
p
);
fd
.
Close
();
if
err1
==
nil
{
// too long
return
nil
,
B
adZoneinfo
;
return
nil
,
b
adZoneinfo
;
}
if
err1
!=
io
.
ErrEOF
{
return
nil
,
err1
;
...
...
@@ -251,7 +252,7 @@ func setupZone() {
}
}
func
L
ookupTimezone
(
sec
int64
)
(
zone
string
,
offset
int
,
err
*
os
.
Error
)
{
func
l
ookupTimezone
(
sec
int64
)
(
zone
string
,
offset
int
,
err
*
os
.
Error
)
{
once
.
Do
(
setupZone
);
if
zoneerr
!=
nil
||
len
(
zones
)
==
0
{
return
"UTC"
,
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