-- vim: ft=terra
local m={}
local pstr = lib.str.t

terra m.datetime(pool: &lib.mem.pool, when: lib.osclock.time_t)
 -- formats a unix epoch time as a dumbfuck XSD datetime spec
	var td: lib.osclock.tm
	if lib.osclock.gmtime_r(&when, &td) == nil then
		return pstr.null()
	end

	var tpl = [lib.tpl.mk ('@#year:-@MM@#month:-@dd@#day:T'..
	                       '@hh@#hour::@mm@#min::@ss@#sec:Z')] {
		year = td.tm_year + 1900, month = td.tm_mon + 1, day = td.tm_mday;
		hour = td.tm_hour, min = td.tm_min, sec = td.tm_sec;
		MM = lib.trn(td.tm_mon+1 < 10, '0', '');
		dd = lib.trn(td.tm_mday < 10, '0', '');
		ss = lib.trn(td.tm_sec < 10, '0', '');
		mm = lib.trn(td.tm_min < 10, '0', '');
		hh = lib.trn(td.tm_min < 10, '0', '');
	}

	return tpl:poolstr(pool)
end

return m
