File: test-date.lua

package info (click to toggle)
lua-penlight 1.0.2%2Bhtmldoc-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,860 kB
  • sloc: makefile: 7
file content (94 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download
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
local test = require 'pl.test'
local asserteq, assertmatch = test.asserteq, test.assertmatch
local dump = require 'pl.pretty'.dump
local T = require 'pl.test'.tuple

local Date = require 'pl.Date'

--[[
d = Date()
print(d)
print(d:year())
d:day(20)
print(d)
d:add {day = 2}
print(d:day())
d = Date() -- 'now'
print(d:last_day():day())
print(d:month(7):last_day())
--]]

function check_df(fmt,str,no_check)
    local df = Date.Format(fmt)
    local d = df:parse(str)
    --print(str,d)
    if not no_check then
        asserteq(df:tostring(d),str)
    end
end

check_df('dd/mm/yy','02/04/10')
check_df('mm/dd/yyyy','04/02/2010')
check_df('yyyy-mm-dd','2011-02-20')
check_df('yyyymmdd','20070320')

-- use single fields for 'slack' parsing
check_df('m/d/yyyy','1/5/2001',true)

check_df('HH:MM','23:10')

iso = Date.Format 'yyyy-mm-dd' -- ISO date
d = iso:parse '2010-04-10'
asserteq(T(d:day(),d:month(),d:year()),T(10,4,2010))
amer = Date.Format 'mm/dd/yyyy' -- American style
s = amer:tostring(d)
dc = amer:parse(s)
asserteq(d,dc)

d = Date() -- today
d:add { day = 1 }  -- tomorrow
assert(d > Date())

-------- testing 'flexible' date parsing ---------


local df = Date.Format()

function parse_date (s)
    return df:parse(s)
end

-- ISO 8601
-- specified as UTC plus/minus offset

function parse_utc (s)
    local d = parse_date(s)
    d:toUTC()
    return d
end

asserteq(parse_utc '2010-05-10 12:35:23Z', Date(2010,05,10,12,35,23))
asserteq(parse_utc '2008-10-03T14:30+02', Date(2008,10,03,12,30))
asserteq(parse_utc '2008-10-03T14:00-02:00',Date(2008,10,03,16,0))

---- can't do anything before 1970, which is somewhat unfortunate....
--parse_date '20/03/59'

asserteq(parse_date '15:30', Date {hour=15,min=30})
asserteq(parse_date '8.05pm', Date {hour=20,min=5})
asserteq(parse_date '28/10/02', Date {year=2002,month=10,day=28})
asserteq(parse_date ' 5 Feb 2012 ', Date {year=2012,month=2,day=5})
asserteq(parse_date '20 Jul ', Date {month=7,day=20})
asserteq(parse_date '05/04/02 15:30:43', Date{year=2002,month=4,day=5,hour=15,min=30,sec=43})
asserteq(parse_date 'march', Date {month=3})
asserteq(parse_date '2010-05-23T0130', Date{year=2010,month=5,day=23,hour=1,min=30})
asserteq(parse_date '2008-10-03T14:30:45', Date{year=2008,month=10,day=3,hour=14,min=30,sec=45})

function err (status,e)
    return e
end

assertmatch(err(parse_date('2005-10-40 01:30')),'40 is not between 1 and 31')
assertmatch(err(parse_date('14.20pm')),'14 is not between 0 and 12')