YAW(N)S

Last post on this subject for now, hopefully, since even I’m getting bored of it. Just for the sake of completeness, I did the ISO date formatting ‘properly’, as follows:

w3cdtf(GregSecs) ->
	Date=calendar:gregorian_seconds_to_datetime(GregSecs),
	[UDate|_]=calendar:local_time_to_universal_time_dst(Date),
	USecs=calendar:datetime_to_gregorian_seconds(UDate),
	{{Y, M, D},{HH, MM, SS}} = Date,
	io_lib:format("~4..0b-~2..0b-~2..0bT~2..0b:~2..0b:~2..0b~s",
		[Y,M,D,HH,MM,SS,tzid(GregSecs-USecs)]).
 
tzid(Secs) when Secs==0 ->
	"Z";
tzid(Secs) ->
	{HH,MM,_}=calendar:seconds_to_time(abs(Secs)),
	io_lib:format("~c~2..0b:~2..0b",
		[case Secs >= 0 of false -> $-; true -> $+ end,HH,MM]).

As well as being shorter and clearer still, it doesn’t use the calendar:time_difference like the original did. That’s good because not only is that function very silly, it turns out it is marked as obsolete in the documentation. I suspect those two points are not unrelated.

Anyway, I’ll probably give the above some more rigourous testing and submit a patch. It may seem rather trivial, but actually it was a good exercise in getting to know Erlang a bit better. Half an hour of practical problem is usually worth ten of reading TFM.

Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">