[ejabberd] mod_time:process_local_iq/3 doesn't work for negative TZ values

Alexander Zhukov zuav at yandex.ru
Thu Nov 18 13:35:23 MSK 2010


Hello!

If Seconds_diff has a negative value, than calendar:seconds_to_time
fails with an exception like this:

-------

(emacs at ithilien)3> calendar:seconds_to_time(-1800).
** exception error: no function clause matching 
                    calendar:seconds_to_time(-1800)

-------

I'd made a quick fix (see below) and it works for us.
But I'm not sure if TZ value ({Hd, Md}) can have an Hd == 0.

F.e. if Seconds_diff == -1800 the fix will fail. Even worse, it will
produce an incorrect result.

Does anybody have a better ideas?


Fix:

--------------------------------

process_local_iq(_From, _To, #iq{type = Type, sub_el = SubEl} = IQ) ->
    case Type of
	set ->
	    IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
	get ->
	    Now = now(),
	    Now_universal = calendar:now_to_universal_time(Now),
	    Now_local = calendar:now_to_local_time(Now),
	    {UTC, UTC_diff} = jlib:timestamp_to_iso(Now_universal, utc),
	    Seconds_diff = calendar:datetime_to_gregorian_seconds(Now_local)
	     - calendar:datetime_to_gregorian_seconds(Now_universal),
            Sign = sign(Seconds_diff),
	    {Hd, Md, _} = calendar:seconds_to_time(abs(Seconds_diff)),
	    {_, TZO_diff} = jlib:timestamp_to_iso({{0, 0, 0}, {0, 0, 0}}, {Sign*Hd, Md}),
	    IQ#iq{type = result,
		  sub_el = [{xmlelement, "time",
			     [{"xmlns", ?NS_TIME}],
			     [{xmlelement, "tzo", [],
			       [{xmlcdata, TZO_diff}]},
			      {xmlelement, "utc", [],
			       [{xmlcdata, UTC ++ UTC_diff}]}]}]}
    end.

sign(N) when N < 0 -> -1;
sign(_)            -> 1.

--------------------------------

-- 
Alexander Zhukov


More information about the ejabberd mailing list