util/datetime.lua
changeset 6780 5de6b93d0190
parent 5776 bd0ff8ae98a8
child 7262 d8300985f2bb
equal deleted inserted replaced
6777:3965662ae091 6780:5de6b93d0190
    13 local os_time = os.time;
    13 local os_time = os.time;
    14 local os_difftime = os.difftime;
    14 local os_difftime = os.difftime;
    15 local error = error;
    15 local error = error;
    16 local tonumber = tonumber;
    16 local tonumber = tonumber;
    17 
    17 
    18 module "datetime"
    18 local _ENV = nil;
    19 
    19 
    20 function date(t)
    20 local function date(t)
    21 	return os_date("!%Y-%m-%d", t);
    21 	return os_date("!%Y-%m-%d", t);
    22 end
    22 end
    23 
    23 
    24 function datetime(t)
    24 local function datetime(t)
    25 	return os_date("!%Y-%m-%dT%H:%M:%SZ", t);
    25 	return os_date("!%Y-%m-%dT%H:%M:%SZ", t);
    26 end
    26 end
    27 
    27 
    28 function time(t)
    28 local function time(t)
    29 	return os_date("!%H:%M:%S", t);
    29 	return os_date("!%H:%M:%S", t);
    30 end
    30 end
    31 
    31 
    32 function legacy(t)
    32 local function legacy(t)
    33 	return os_date("!%Y%m%dT%H:%M:%S", t);
    33 	return os_date("!%Y%m%dT%H:%M:%S", t);
    34 end
    34 end
    35 
    35 
    36 function parse(s)
    36 local function parse(s)
    37 	if s then
    37 	if s then
    38 		local year, month, day, hour, min, sec, tzd;
    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+%-]?.*)$");
    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
    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
    41 			local time_offset = os_difftime(os_time(os_date("*t")), os_time(os_date("!*t"))); -- to deal with local timezone
    52 			return os_time({year=year, month=month, day=day, hour=hour, min=min, sec=sec, isdst=false});
    52 			return os_time({year=year, month=month, day=day, hour=hour, min=min, sec=sec, isdst=false});
    53 		end
    53 		end
    54 	end
    54 	end
    55 end
    55 end
    56 
    56 
    57 return _M;
    57 return {
       
    58 	date     = date;
       
    59 	datetime = datetime;
       
    60 	time     = time;
       
    61 	legacy   = legacy;
       
    62 	parse    = parse;
       
    63 };