Prepare required data folder for integration tests
[prosody.git] / util / datetime.lua
blob2d27ece4658062e761d0faeab5e5cb526338ece4
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
10 -- XEP-0082: XMPP Date and Time Profiles
12 local os_date = os.date;
13 local os_time = os.time;
14 local os_difftime = os.difftime;
15 local tonumber = tonumber;
17 local _ENV = nil;
18 -- luacheck: std none
20 local function date(t)
21 return os_date("!%Y-%m-%d", t);
22 end
24 local function datetime(t)
25 return os_date("!%Y-%m-%dT%H:%M:%SZ", t);
26 end
28 local function time(t)
29 return os_date("!%H:%M:%S", t);
30 end
32 local function legacy(t)
33 return os_date("!%Y%m%dT%H:%M:%S", t);
34 end
36 local function parse(s)
37 if s then
38 local year, month, day, hour, min, sec, tzd;
39 year, month, day, hour, min, sec, tzd = s:match("^(%d%d%d%d)%-?(%d%d)%-?(%d%d)T(%d%d):(%d%d):(%d%d)%.?%d*([Z+%-]?.*)$");
40 if year then
41 local now = os_time();
42 local time_offset = os_difftime(os_time(os_date("*t", now)), os_time(os_date("!*t", now))); -- to deal with local timezone
43 local tzd_offset = 0;
44 if tzd ~= "" and tzd ~= "Z" then
45 local sign, h, m = tzd:match("([+%-])(%d%d):?(%d*)");
46 if not sign then return; end
47 if #m ~= 2 then m = "0"; end
48 h, m = tonumber(h), tonumber(m);
49 tzd_offset = h * 60 * 60 + m * 60;
50 if sign == "-" then tzd_offset = -tzd_offset; end
51 end
52 sec = (sec + time_offset) - tzd_offset;
53 return os_time({year=year, month=month, day=day, hour=hour, min=min, sec=sec, isdst=false});
54 end
55 end
56 end
58 return {
59 date = date;
60 datetime = datetime;
61 time = time;
62 legacy = legacy;
63 parse = parse;