[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: os.date question
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Mon, 8 Jan 2007 11:53:59 -0200
> os.time() returns a time_t cast to a number. And time_t is very
> likely a 32 bit type on your machine. It counts in seconds, so it
> has a limited range, usually from 1970-2038 (signed 32 bit int).
>
> I.e. you've found a (well known) limitation in your C library.
Note that time_t is signed. And indeed, os.date does work with negative times,
up till around 1901, which is consistent with signed 32-bit values
(2038-1970==1970-1902).
On my Linux box the program
for i=1,200 do
local t=365.25*24*60*60
print(os.date(nil,-t*i))
end
gives
Tue Dec 31 15:00:00 1968
Mon Jan 1 10:00:00 1968
Sun Jan 1 04:00:00 1967
...
Sun Dec 31 20:53:32 1905
Sat Dec 31 14:53:32 1904
Fri Jan 1 08:53:32 1904
Thu Jan 1 02:53:32 1903
Tue Dec 31 20:53:32 1901
Fri Dec 13 17:39:24 1901
Fri Dec 13 17:39:24 1901
...
On the other hand, this program
for y=1960,1980 do
print(y,os.time{year=y,month=1,day=1})
end
cannot handle years before 1970 (see below). So it seems a limitation
of localtime (which is what os.date uses) not of mktime (which is what
os.time uses). [I wonder why mktime does not give an error past 1902.]
1960 nil
1961 nil
1962 nil
1963 nil
1964 nil
1965 nil
1966 nil
1967 nil
1968 nil
1969 nil
1970 54000
1971 31590000
1972 63126000
1973 94748400
1974 126284400
1975 157820400
1976 189356400
1977 220978800
1978 252514800
1979 284050800
1980 315586800
Oh, the misteries of libc...
--lhf