Mercurial > prosody-hg
comparison util/datetime.lua @ 3430:970690b3cb28
util.datetime: Added implementation for function parse().
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Mon, 02 Aug 2010 20:11:08 +0500 |
| parents | b7049746bd29 |
| children | ed80c4c56b9c |
comparison
equal
deleted
inserted
replaced
| 3429:8cdb0179371a | 3430:970690b3cb28 |
|---|---|
| 8 | 8 |
| 9 | 9 |
| 10 -- XEP-0082: XMPP Date and Time Profiles | 10 -- XEP-0082: XMPP Date and Time Profiles |
| 11 | 11 |
| 12 local os_date = os.date; | 12 local os_date = os.date; |
| 13 local os_time = os.time; | |
| 14 local os_difftime = os.difftime; | |
| 13 local error = error; | 15 local error = error; |
| 16 local tonumber = tonumber; | |
| 14 | 17 |
| 15 module "datetime" | 18 module "datetime" |
| 16 | 19 |
| 17 function date(t) | 20 function date(t) |
| 18 return os_date("!%Y-%m-%d", t); | 21 return os_date("!%Y-%m-%d", t); |
| 29 function legacy(t) | 32 function legacy(t) |
| 30 return os_date("!%Y%m%dT%H:%M:%S", t); | 33 return os_date("!%Y%m%dT%H:%M:%S", t); |
| 31 end | 34 end |
| 32 | 35 |
| 33 function parse(s) | 36 function parse(s) |
| 34 error("datetime.parse: Not implemented"); -- TODO | 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 time_offset = os_difftime(os_time(os_date("*t")), os_time(os_date("!*t"))); -- to deal with local timezone | |
| 42 local tzd_offset = 0; | |
| 43 if tzd ~= "" and tzd ~= "Z" then | |
| 44 local sign, h, m = tzd:match("([+%-])(%d%d):(%d%d)"); | |
| 45 if not sign then return; end | |
| 46 h, m = tonumber(h), tonumber(m); | |
| 47 tzd_offset = h * 60 * 60 + m * 60; | |
| 48 if sign == "-" then tzd_offset = -tzd_offset; end | |
| 49 end | |
| 50 sec = sec + time_offset + tzd_offset; | |
| 51 return os_time({year=year, month=month, day=day, hour=hour, min=min, sec=sec}); | |
| 52 end | |
| 53 end | |
| 35 end | 54 end |
| 36 | 55 |
| 37 return _M; | 56 return _M; |
