Commit 9f35e8b2 authored by Russ Cox's avatar Russ Cox

time & date.

rename AddrToInt, StatToInt, etc -> BytePtr, StatPtr, ...

R=r
OCL=15450
CL=15456
parent 119324d7
......@@ -14,15 +14,25 @@ do
cd ..
done
# Don't sort the files in the for loop - some of the orderings matter.
rm -f *.6
for i in fmt.go flag.go container/vector.go rand.go sort.go io.go bufio.go strings.go
for i in \
fmt.go\
flag.go\
container/vector.go\
rand.go\
sort.go\
io.go\
bufio.go\
strings.go\
do
base=$(basename $i .go)
echo 6g -o $GOROOT/pkg/$base.6 $i
6g -o $GOROOT/pkg/$base.6 $i
done
for i in net
for i in net time
do
echo; echo; echo %%%% making lib/$i %%%%; echo
cd $i
......
......@@ -10,7 +10,8 @@ PKG=$(GOROOT)/pkg/os.a
O1=\
os_error.$O
O2=\
os_file.$O
os_file.$O\
os_time.$O\
install: nuke $(PKG)
......
// 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 os
import (
"os";
"syscall"
)
export func Time() (sec int64, nsec int64, err *Error) {
var errno int64;
sec, nsec, errno = syscall.gettimeofday()
if errno != 0 {
return 0, 0, ErrnoToError(errno)
}
return sec, nsec, nil
}
# 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.
O=6
GC=$(O)g
PKG=$(GOROOT)/pkg/time.a
O1=\
zoneinfo.$O
O2=\
time.$O\
install: nuke $(PKG)
$(PKG): a1 a2
a1: $(O1)
$(O)ar grc $(PKG) $(O1)
a2: $(O2)
$(O)ar grc $(PKG) $(O2)
nuke:
rm -f *.$(O) *.a $(PKG)
clean:
rm -f *.$(O) *.a
%.$O: %.go
$(GC) $<
// 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
export func Seconds() (sec int64, err *os.Error) {
var nsec int64;
sec, nsec, err = os.Time()
return sec, err
}
// Nanoseconds since January 1, 1970 00:00:00 GMT
export func Nanoseconds() (nsec int64, err *os.Error) {
var sec int64;
sec, nsec, err = os.Time()
return sec*1e9 + nsec, err
}
export const (
Sunday = iota;
Monday;
Tuesday;
Wednesday;
Thursday;
Friday;
Saturday;
)
export 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, ...
zoneoffset int; // seconds west of UTC
zone string;
}
var RegularMonths = []int{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}
var LeapMonths = []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 &LeapMonths
} else {
return &RegularMonths
}
return nil // not reached
}
const (
SecondsPerDay = 24*60*60;
DaysPer400Years = 365*400+97;
DaysPer100Years = 365*100+24;
DaysPer4Years = 365*4+1;
Days1970To2001 = 31*365+8;
)
export func SecondsToUTC(sec int64) *Time {
t := new(Time);
// Split into time and day.
day := sec/SecondsPerDay;
sec -= day*SecondsPerDay;
if sec < 0 {
day--
sec += SecondsPerDay
}
// Time
t.hour = int(sec/3600);
t.minute = int((sec/60)%60);
t.second = int(sec%60);
// Day 0 = January 1, 1970 was a Thursday
t.weekday = int((day + Thursday) % 7)
if t.weekday < 0 {
t.weekday += 7
}
// 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.)
day -= Days1970To2001;
year := int64(2001)
if day < 0 {
// Go back enough 400 year cycles to make day positive.
n := -day/DaysPer400Years + 1;
year -= 400*n;
day += DaysPer400Years*n;
} else {
// Cut off 400 year cycles.
n := day/DaysPer400Years;
year += 400*n;
day -= DaysPer400Years*n;
}
// Cut off 100-year cycles
n := day/DaysPer100Years;
year += 100*n;
day -= DaysPer100Years*n;
// Cut off 4-year cycles
n = day/DaysPer4Years;
year += 4*n;
day -= DaysPer4Years*n;
// Cut off non-leap years.
n = day/365;
year += n;
day -= 365*n;
t.year = year;
// If someone ever needs yearday,
// tyearday = day (+1?)
months := Months(year);
var m int;
yday := int(day);
for m = 0; m < 12 && yday >= months[m]; m++ {
yday -= months[m]
}
t.month = m+1;
t.day = yday+1;
t.zone = "GMT";
return t;
}
export func UTC() (t *Time, err *os.Error) {
var sec int64;
sec, err = Seconds()
if err != nil {
return nil, err
}
return SecondsToUTC(sec), nil
}
// TODO: Should this return an error?
export func SecondsToLocalTime(sec int64) *Time {
zone, offset, ok := time.LookupTimezone(sec)
if !ok {
return SecondsToUTC(sec)
}
t := SecondsToUTC(sec+int64(offset));
t.zone = zone;
t.zoneoffset = offset;
return t
}
export func LocalTime() (t *Time, err *os.Error) {
var sec int64;
sec, err = Seconds()
if err != nil {
return nil, err
}
return SecondsToLocalTime(sec), nil
}
// 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
// handling easier (see SecondsToUTC), because
// it is at the beginning of the 4-, 100-, and 400-year cycles.
day := int64(0);
// Rewrite year to be >= 2001.
year := t.year;
if year < 2001 {
n := (2001 - year)/400 + 1;
year += 400*n;
day -= DaysPer400Years*n;
}
// Add in days from 400-year cycles.
n := (year - 2001) / 400;
year -= 400*n;
day += DaysPer400Years*n;
// Add in 100-year cycles.
n = (year - 2001) / 100;
year -= 100*n;
day += DaysPer100Years*n;
// Add in 4-year cycles.
n = (year - 2001) / 4;
year -= 4*n;
day += DaysPer4Years*n;
// Add in non-leap years.
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])
}
day += int64(t.day - 1);
// Convert days to seconds since January 1, 2001.
sec := day * SecondsPerDay;
// Add in time elapsed today.
sec += int64(t.hour) * 3600;
sec += int64(t.minute) * 60;
sec += int64(t.second);
// Convert from seconds since 2001 to seconds since 1970.
sec += Days1970To2001 * SecondsPerDay;
// Account for local time zone.
sec -= int64(t.zoneoffset)
return sec
}
var LongDayNames = []string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
}
var ShortDayNames = []string{
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
}
var ShortMonthNames = []string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
}
func Copy(dst *[]byte, s string) {
for i := 0; i < len(s); i++ {
dst[i] = s[i]
}
}
func Decimal(dst *[]byte, n int) {
if n < 0 {
n = 0
}
for i := len(dst)-1; i >= 0; i-- {
dst[i] = byte(n%10 + '0');
n /= 10
}
}
func AddString(buf *[]byte, bp int, s string) int {
n := len(s);
Copy(buf[bp:bp+n], s)
return bp+n
}
// Just enough of strftime to implement the date formats below.
// Not exported.
func Format(t *Time, fmt string) string {
buf := new([]byte, 128);
bp := 0
for i := 0; i < len(fmt); i++ {
if fmt[i] == '%' {
i++
switch fmt[i] {
case 'A': // %A full weekday name
bp = AddString(buf, bp, LongDayNames[t.weekday])
case 'a': // %a abbreviated weekday name
bp = AddString(buf, bp, ShortDayNames[t.weekday])
case 'b': // %b abbreviated month name
bp = AddString(buf, bp, ShortMonthNames[t.month-1])
case 'd': // %d day of month (01-31)
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)
} else {
buf[bp] = ' ';
buf[bp+1] = byte(t.day + '0')
}
bp += 2
case 'H': // %H hour 00-23
Decimal(buf[bp:bp+2], t.hour);
bp += 2
case 'M': // %M minute 00-59
Decimal(buf[bp:bp+2], t.minute);
bp += 2
case 'S': // %S second 00-59
Decimal(buf[bp:bp+2], t.second);
bp += 2
case 'Y': // %Y year 2008
Decimal(buf[bp:bp+4], int(t.year));
bp += 4
case 'y': // %y year 08
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
}
} else {
buf[bp] = fmt[i];
bp++
}
}
return string(buf[0:bp])
}
// 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")
}
// RFC 850: Sunday, 06-Nov-94 08:49:37 GMT
func (t *Time) RFC850() string {
return Format(t, "%A, %d-%b-%y %H:%M:%S %Z")
}
// RFC 1123: Sun, 06 Nov 1994 08:49:37 GMT
func (t *Time) RFC1123() string {
return Format(t, "%a, %d %b %Y %H:%M:%S %Z")
}
// date(1) - Sun Nov 6 08:49:37 GMT 1994
func (t *Time) String() string {
return Format(t, "%a %b %e %H:%M:%S %Z %Y")
}
// 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.
// TODO: Figure out a better place to put this.
package time
// TODO(rsc): Parse zone info file.
// Amazingly, they are portable across OS X, Linux, BSD, Sun, etc.
// I know how, I just don't want to do it right now.
export func LookupTimezone(sec int64) (zone string, offset int, ok bool) {
return "PDT", -7*60*60, true
}
......@@ -16,6 +16,7 @@ O1=\
O2=\
file_$(GOARCH)_$(GOOS).$O \
time_$(GOARCH)_$(GOOS).$O \
syscall_$(GOARCH)_$(GOOS).$O \
......
......@@ -13,7 +13,7 @@ import syscall "syscall"
//export open, creat, close, read, write, pipe
//export unlink
func StatToInt(s *Stat) int64;
func StatPtr(s *Stat) int64;
type dev_t uint32;
type ino_t uint64;
......@@ -73,7 +73,7 @@ export func open(name string, mode int64, perm int64) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSOPEN = 5;
r1, r2, err := syscall.Syscall(SYSOPEN, AddrToInt(&namebuf[0]), mode, perm);
r1, r2, err := syscall.Syscall(SYSOPEN, BytePtr(&namebuf[0]), mode, perm);
return r1, err;
}
......@@ -83,7 +83,7 @@ export func creat(name string, perm int64) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSOPEN = 5;
r1, r2, err := syscall.Syscall(SYSOPEN, AddrToInt(&namebuf[0]), O_CREAT|O_WRONLY|O_TRUNC, perm);
r1, r2, err := syscall.Syscall(SYSOPEN, BytePtr(&namebuf[0]), O_CREAT|O_WRONLY|O_TRUNC, perm);
return r1, err;
}
......@@ -95,13 +95,13 @@ export func close(fd int64) (ret int64, errno int64) {
export func read(fd int64, buf *byte, nbytes int64) (ret int64, errno int64) {
const SYSREAD = 3;
r1, r2, err := syscall.Syscall(SYSREAD, fd, AddrToInt(buf), nbytes);
r1, r2, err := syscall.Syscall(SYSREAD, fd, BytePtr(buf), nbytes);
return r1, err;
}
export func write(fd int64, buf *byte, nbytes int64) (ret int64, errno int64) {
const SYSWRITE = 4;
r1, r2, err := syscall.Syscall(SYSWRITE, fd, AddrToInt(buf), nbytes);
r1, r2, err := syscall.Syscall(SYSWRITE, fd, BytePtr(buf), nbytes);
return r1, err;
}
......@@ -122,19 +122,19 @@ export func stat(name string, buf *Stat) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSSTAT = 338;
r1, r2, err := syscall.Syscall(SYSSTAT, AddrToInt(&namebuf[0]), StatToInt(buf), 0);
r1, r2, err := syscall.Syscall(SYSSTAT, BytePtr(&namebuf[0]), StatPtr(buf), 0);
return r1, err;
}
export func lstat(name *byte, buf *Stat) (ret int64, errno int64) {
const SYSLSTAT = 340;
r1, r2, err := syscall.Syscall(SYSLSTAT, AddrToInt(name), StatToInt(buf), 0);
r1, r2, err := syscall.Syscall(SYSLSTAT, BytePtr(name), StatPtr(buf), 0);
return r1, err;
}
export func fstat(fd int64, buf *Stat) (ret int64, errno int64) {
const SYSFSTAT = 339;
r1, r2, err := syscall.Syscall(SYSFSTAT, fd, StatToInt(buf), 0);
r1, r2, err := syscall.Syscall(SYSFSTAT, fd, StatPtr(buf), 0);
return r1, err;
}
......@@ -144,6 +144,6 @@ export func unlink(name string) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSUNLINK = 10;
r1, r2, err := syscall.Syscall(SYSUNLINK, AddrToInt(&namebuf[0]), 0, 0);
r1, r2, err := syscall.Syscall(SYSUNLINK, BytePtr(&namebuf[0]), 0, 0);
return r1, err;
}
......@@ -13,8 +13,8 @@ import syscall "syscall"
//export open, creat, close, read, write, pipe
//export unlink
func StatToInt(s *Stat) int64;
func Addr32ToInt(s *int32) int64;
func StatPtr(s *Stat) int64;
func Int32Ptr(s *int32) int64;
type dev_t uint64;
type ino_t uint64;
......@@ -74,7 +74,7 @@ export func open(name string, mode int64, perm int64) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSOPEN = 2;
r1, r2, err := syscall.Syscall(SYSOPEN, AddrToInt(&namebuf[0]), mode, perm);
r1, r2, err := syscall.Syscall(SYSOPEN, BytePtr(&namebuf[0]), mode, perm);
return r1, err;
}
......@@ -84,7 +84,7 @@ export func creat(name string, perm int64) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSOPEN = 2;
r1, r2, err := syscall.Syscall(SYSOPEN, AddrToInt(&namebuf[0]), O_CREAT|O_WRONLY|O_TRUNC, perm);
r1, r2, err := syscall.Syscall(SYSOPEN, BytePtr(&namebuf[0]), O_CREAT|O_WRONLY|O_TRUNC, perm);
return r1, err;
}
......@@ -96,20 +96,20 @@ export func close(fd int64) (ret int64, errno int64) {
export func read(fd int64, buf *byte, nbytes int64) (ret int64, errno int64) {
const SYSREAD = 0;
r1, r2, err := syscall.Syscall(SYSREAD, fd, AddrToInt(buf), nbytes);
r1, r2, err := syscall.Syscall(SYSREAD, fd, BytePtr(buf), nbytes);
return r1, err;
}
export func write(fd int64, buf *byte, nbytes int64) (ret int64, errno int64) {
const SYSWRITE = 1;
r1, r2, err := syscall.Syscall(SYSWRITE, fd, AddrToInt(buf), nbytes);
r1, r2, err := syscall.Syscall(SYSWRITE, fd, BytePtr(buf), nbytes);
return r1, err;
}
export func pipe(fds *[2]int64) (ret int64, errno int64) {
const SYSPIPE = 22;
var t [2] int32;
r1, r2, err := syscall.Syscall(SYSPIPE, Addr32ToInt(&t[0]), 0, 0);
r1, r2, err := syscall.Syscall(SYSPIPE, Int32Ptr(&t[0]), 0, 0);
if r1 < 0 {
return r1, err;
}
......@@ -124,19 +124,19 @@ export func stat(name string, buf *Stat) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSSTAT = 4;
r1, r2, err := syscall.Syscall(SYSSTAT, AddrToInt(&namebuf[0]), StatToInt(buf), 0);
r1, r2, err := syscall.Syscall(SYSSTAT, BytePtr(&namebuf[0]), StatPtr(buf), 0);
return r1, err;
}
export func lstat(name *byte, buf *Stat) (ret int64, errno int64) {
const SYSLSTAT = 6;
r1, r2, err := syscall.Syscall(SYSLSTAT, AddrToInt(name), StatToInt(buf), 0);
r1, r2, err := syscall.Syscall(SYSLSTAT, BytePtr(name), StatPtr(buf), 0);
return r1, err;
}
export func fstat(fd int64, buf *Stat) (ret int64, errno int64) {
const SYSFSTAT = 5;
r1, r2, err := syscall.Syscall(SYSFSTAT, fd, StatToInt(buf), 0);
r1, r2, err := syscall.Syscall(SYSFSTAT, fd, StatPtr(buf), 0);
return r1, err;
}
......@@ -146,6 +146,6 @@ export func unlink(name string) (ret int64, errno int64) {
return -1, syscall.ENAMETOOLONG
}
const SYSUNLINK = 87;
r1, r2, err := syscall.Syscall(SYSUNLINK, AddrToInt(&namebuf[0]), 0, 0);
r1, r2, err := syscall.Syscall(SYSUNLINK, BytePtr(&namebuf[0]), 0, 0);
return r1, err;
}
......@@ -10,7 +10,7 @@ package syscall
export func Syscall(trap int64, a1, a2, a3 int64) (r1, r2, err int64);
export func Syscall6(trap int64, a1, a2, a3, a4, a5, a6 int64) (r1, r2, err int64);
export func AddrToInt(b *byte) int64;
export func BytePtr(b *byte) int64;
/*
* Used to convert file names to byte arrays for passing to kernel,
......
......@@ -48,12 +48,12 @@ TEXT syscall·Syscall6(SB),7,$-8
RET
// conversion operators - really just casts
TEXT syscall·AddrToInt(SB),7,$-8
TEXT syscall·BytePtr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT syscall·StatToInt(SB),7,$-8
TEXT syscall·StatPtr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
......@@ -50,17 +50,22 @@ TEXT syscall·Syscall6(SB),7,$-8
RET
// conversion operators - really just casts
TEXT syscall·AddrToInt(SB),7,$-8
TEXT syscall·BytePtr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT syscall·Addr32ToInt(SB),7,$-8
TEXT syscall·Int32Ptr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT syscall·StatToInt(SB),7,$-8
TEXT syscall·Int64Ptr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT syscall·StatPtr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
// 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 syscall
import syscall "syscall"
export func gettimeofday() (sec, nsec, errno int64) {
const GETTIMEOFDAY = 116;
// The "1" in the call is the timeval pointer, which must be
// non-zero but is otherwise unused. The results
// are returned in r1, r2.
r1, r2, err := syscall.Syscall(GETTIMEOFDAY, 1, 0, 0);
if err != 0 {
return 0, 0, err
}
return r1, r2, 0
}
// 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 syscall
import syscall "syscall"
func Int64Ptr(s *int64) int64;
export func gettimeofday() (sec, nsec, errno int64) {
const GETTIMEOFDAY = 96
var tv [2]int64; // struct timeval
r1, r2, err := syscall.Syscall(GETTIMEOFDAY, Int64Ptr(&tv[0]), 0, 0);
if err != 0 {
return 0, 0, err
}
return tv[0], tv[1], 0
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment