aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-24 15:01:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-24 15:01:59 -0300
commit0eb6aa4013051c8c0148c09d8c85ee7cbdc96f42 (patch)
treedcf79de5e0efc790142ed7e2fd418813c70d0c60 /testes
parent7f5c31cdcac5388b3c48a26112dfb6d2cadb7321 (diff)
downloadlua-0eb6aa4013051c8c0148c09d8c85ee7cbdc96f42.tar.gz
lua-0eb6aa4013051c8c0148c09d8c85ee7cbdc96f42.tar.bz2
lua-0eb6aa4013051c8c0148c09d8c85ee7cbdc96f42.zip
Some improvements in date/time functions
- Range in date table extended to full 32 bits. - Easier support for times represented as floats. - Added more tests.
Diffstat (limited to 'testes')
-rw-r--r--testes/files.lua65
1 files changed, 52 insertions, 13 deletions
diff --git a/testes/files.lua b/testes/files.lua
index c8f23d18..6e7bd9e2 100644
--- a/testes/files.lua
+++ b/testes/files.lua
@@ -775,11 +775,24 @@ assert(os.date(string.rep("%d", 1000), t) ==
775 string.rep(os.date("%d", t), 1000)) 775 string.rep(os.date("%d", t), 1000))
776assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) 776assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
777 777
778local t = os.time() 778local function checkDateTable (t)
779D = os.date("*t", t) 779 _G.D = os.date("*t", t)
780load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and 780 assert(os.time(D) == t)
781 D.hour==%H and D.min==%M and D.sec==%S and 781 load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
782 D.wday==%w+1 and D.yday==%j)]], t))() 782 D.hour==%H and D.min==%M and D.sec==%S and
783 D.wday==%w+1 and D.yday==%j)]], t))()
784 _G.D = nil
785end
786
787checkDateTable(os.time())
788if not _port then
789 -- assume that time_t can represent these values
790 checkDateTable(0)
791 checkDateTable(1)
792 checkDateTable(1000)
793 checkDateTable(0x7fffffff)
794 checkDateTable(0x80000000)
795end
783 796
784checkerr("invalid conversion specifier", os.date, "%") 797checkerr("invalid conversion specifier", os.date, "%")
785checkerr("invalid conversion specifier", os.date, "%9") 798checkerr("invalid conversion specifier", os.date, "%9")
@@ -793,11 +806,24 @@ checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5})
793 806
794checkerr("missing", os.time, {hour = 12}) -- missing date 807checkerr("missing", os.time, {hour = 12}) -- missing date
795 808
809
810if string.packsize("i") == 4 then -- 4-byte ints
811 checkerr("field 'year' is out-of-bound", os.time,
812 {year = -(1 << 31) + 1899, month = 1, day = 1})
813end
814
796if not _port then 815if not _port then
797 -- test Posix-specific modifiers 816 -- test Posix-specific modifiers
798 assert(type(os.date("%Ex")) == 'string') 817 assert(type(os.date("%Ex")) == 'string')
799 assert(type(os.date("%Oy")) == 'string') 818 assert(type(os.date("%Oy")) == 'string')
800 819
820 -- test large dates (assume at least 4-byte ints and time_t)
821 local t0 = os.time{year = 1970, month = 1, day = 0}
822 local t1 = os.time{year = 1970, month = 1, day = 0, sec = (1 << 31) - 1}
823 assert(t1 - t0 == (1 << 31) - 1)
824 t0 = os.time{year = 1970, month = 1, day = 1}
825 t1 = os.time{year = 1970, month = 1, day = 1, sec = -(1 << 31)}
826 assert(t1 - t0 == -(1 << 31))
801 827
802 -- test out-of-range dates (at least for Unix) 828 -- test out-of-range dates (at least for Unix)
803 if maxint >= 2^62 then -- cannot do these tests in Small Lua 829 if maxint >= 2^62 then -- cannot do these tests in Small Lua
@@ -812,25 +838,37 @@ if not _port then
812 -- time_t has 8 bytes; an int year cannot represent a huge time 838 -- time_t has 8 bytes; an int year cannot represent a huge time
813 print(" 8-byte time_t") 839 print(" 8-byte time_t")
814 checkerr("cannot be represented", os.date, "%Y", 2^60) 840 checkerr("cannot be represented", os.date, "%Y", 2^60)
815 -- it should have no problems with year 4000 841
816 assert(tonumber(os.time{year=4000, month=1, day=1})) 842 -- this is the maximum year
843 assert(tonumber(os.time
844 {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=59}))
845
846 -- this is too much
847 checkerr("represented", os.time,
848 {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=60})
817 end 849 end
850
851 -- internal 'int' fields cannot hold these values
852 checkerr("field 'day' is out-of-bound", os.time,
853 {year = 0, month = 1, day = 2^32})
854
855 checkerr("field 'month' is out-of-bound", os.time,
856 {year = 0, month = -((1 << 31) + 1), day = 1})
857
858 checkerr("field 'year' is out-of-bound", os.time,
859 {year = (1 << 31) + 1900, month = 1, day = 1})
860
818 else -- 8-byte ints 861 else -- 8-byte ints
819 -- assume time_t has 8 bytes too 862 -- assume time_t has 8 bytes too
820 print(" 8-byte time_t") 863 print(" 8-byte time_t")
821 assert(tonumber(os.date("%Y", 2^60))) 864 assert(tonumber(os.date("%Y", 2^60)))
865
822 -- but still cannot represent a huge year 866 -- but still cannot represent a huge year
823 checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1}) 867 checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
824 end 868 end
825 end 869 end
826end 870end
827 871
828
829D = os.date("!*t", t)
830load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and
831 D.hour==%H and D.min==%M and D.sec==%S and
832 D.wday==%w+1 and D.yday==%j)]], t))()
833
834do 872do
835 local D = os.date("*t") 873 local D = os.date("*t")
836 local t = os.time(D) 874 local t = os.time(D)
@@ -844,6 +882,7 @@ do
844 assert(t == t1) -- if isdst is absent uses correct default 882 assert(t == t1) -- if isdst is absent uses correct default
845end 883end
846 884
885local D = os.date("*t")
847t = os.time(D) 886t = os.time(D)
848D.year = D.year-1; 887D.year = D.year-1;
849local t1 = os.time(D) 888local t1 = os.time(D)