summaryrefslogtreecommitdiff
path: root/src/lib/time/time.go
blob: 27db87ff3dd3e3f15d365d81cffea771bcc800e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// 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
func Seconds() int64 {
	sec, nsec, err := os.Time();
	if err != nil {
		panic("time: os.Time: ", err.String());
	}
	return sec
}

// Nanoseconds since January 1, 1970 00:00:00 GMT
func Nanoseconds() int64 {
	sec, nsec, err := os.Time();
	if err != nil {
		panic("time: os.Time: ", err.String());
	}
	return sec*1e9 + nsec
}

const (
	Sunday = iota;
	Monday;
	Tuesday;
	Wednesday;
	Thursday;
	Friday;
	Saturday;
)

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 nonleapyear = []int(
	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
)
var leapyear = []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 leapyear
	}
	return nonleapyear
}

const (
	_SecondsPerDay = 24*60*60;

	_DaysPer400Years = 365*400+97;
	_DaysPer100Years = 365*100+24;
	_DaysPer4Years = 365*4+1;

	_Days1970To2001 = 31*365+8;
)

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;
}

func UTC() *Time {
	return SecondsToUTC(Seconds())
}

// TODO: Should this return an error?
func SecondsToLocalTime(sec int64) *Time {
	zone, offset, err := time.LookupTimezone(sec);
	if err != nil {
		return SecondsToUTC(sec)
	}
	t := SecondsToUTC(sec+int64(offset));
	t.Zone = zone;
	t.ZoneOffset = offset;
	return t
}

func LocalTime() *Time {
	return SecondsToLocalTime(Seconds())
}

// 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 := make([]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")
}