aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHolger Weiss <holger@zedat.fu-berlin.de>2014-12-11 23:11:35 +0100
committerHolger Weiss <holger@zedat.fu-berlin.de>2014-12-11 23:11:35 +0100
commit0a19dac4fdc3acb6c862d6e6b8f87b43c6710d20 (patch)
treec7278bd450e32becd5f531a61186054b0bd007f1 /src
parentLet CSI code add timestamp at later point in time (diff)
Add fractions of seconds to <delay/> timestamps
Include fractions of a second with XEP-0203 <delay/> timestamps, as specified in XEP-0082. Old timestamp: 2014-05-19T11:55:00Z New timestamp: 2014-05-19T11:55:00.123Z
Diffstat (limited to 'src')
-rw-r--r--src/jlib.erl23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/jlib.erl b/src/jlib.erl
index 74d8503e4..f15d71a3c 100644
--- a/src/jlib.erl
+++ b/src/jlib.erl
@@ -622,8 +622,7 @@ add_delay_info(El, From, Time, Desc, Name, XMLNS) ->
case xml:get_subtag_with_xmlns(El, Name, XMLNS) of
false ->
%% Add new tag
- DelayTag = create_delay_tag(calendar:now_to_universal_time(Time),
- From, Desc, XMLNS),
+ DelayTag = create_delay_tag(Time, From, Desc, XMLNS),
xml:append_subtags(El, [DelayTag]);
DelayTag ->
%% Update existing tag
@@ -648,16 +647,16 @@ add_delay_info(El, From, Time, Desc, Name, XMLNS) ->
xml:append_subtags(NewEl, [NewDelayTag])
end.
--spec create_delay_tag(calendar:datetime(), jid() | binary(),
- binary(), binary()) -> xmlel() | error.
+-spec create_delay_tag(erlang:timestamp(), jid() | binary(), binary(),
+ binary()) -> xmlel() | error.
-create_delay_tag(DateTime, FromJID, Desc, XMLNS) when is_tuple(FromJID) ->
+create_delay_tag(TimeStamp, FromJID, Desc, XMLNS) when is_tuple(FromJID) ->
From = jlib:jid_to_string(FromJID),
{Name, Stamp} = case XMLNS of
?NS_DELAY ->
- {T, Tz} = timestamp_to_iso(DateTime, utc),
- {<<"delay">>, <<T/binary, Tz/binary>>};
+ {<<"delay">>, now_to_utc_string(TimeStamp, 3)};
?NS_DELAY91 ->
+ DateTime = calendar:now_to_universal_time(TimeStamp),
{<<"x">>, timestamp_to_iso(DateTime)}
end,
Children = case Desc of
@@ -710,13 +709,19 @@ timestamp_to_iso({{Year, Month, Day},
-spec now_to_utc_string(erlang:timestamp()) -> binary().
now_to_utc_string({MegaSecs, Secs, MicroSecs}) ->
+ now_to_utc_string({MegaSecs, Secs, MicroSecs}, 6).
+
+-spec now_to_utc_string(erlang:timestamp(), 1..6) -> binary().
+
+now_to_utc_string({MegaSecs, Secs, MicroSecs}, Precision) ->
{{Year, Month, Day}, {Hour, Minute, Second}} =
calendar:now_to_universal_time({MegaSecs, Secs,
MicroSecs}),
- list_to_binary(io_lib:format("~4..0w-~2..0w-~2..0wT~2..0w:~2..0w:~2..0w.~6."
+ FracOfSec = round(MicroSecs / math:pow(10, 6 - Precision)),
+ list_to_binary(io_lib:format("~4..0w-~2..0w-~2..0wT~2..0w:~2..0w:~2..0w.~*."
".0wZ",
[Year, Month, Day, Hour, Minute, Second,
- MicroSecs])).
+ Precision, FracOfSec])).
-spec now_to_local_string(erlang:timestamp()) -> binary().