Skip to content

Commit

Permalink
avoid a webmachine dependency for a single function
Browse files Browse the repository at this point in the history
  • Loading branch information
djnym committed Apr 20, 2017
1 parent cbbcecb commit 7459e3a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 6.8.1 (molinaro)
* avoid webmachine dependency

Version 6.8.0 (molinaro)
* add user state to the mondemand_statdb:map function so that you can do
the equivalent of mapfold there.
Expand Down
2 changes: 1 addition & 1 deletion src/mondemand.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ application, mondemand,
[
{ description, "Erlang Mondemand Bindings." },
{ vsn, "6.8.0" },
{ vsn, "6.8.1" },
{ modules, [] },
{ registered, [mondemand,mondemand_sup]},
{ applications, [kernel,stdlib,syntax_tools,lwes,inets]},
Expand Down
2 changes: 1 addition & 1 deletion src/mondemand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ flush ({FlushModule, FlushStatePrepFunction, FlushFunction}) ->
),
Finish = os:timestamp (),
error_logger:info_msg ("flushing took ~p millis and returned ~p",
[webmachine_util:now_diff_milliseconds (Finish,Start),
[mondemand_util:now_diff_milliseconds (Finish,Start),
Ret]),
Ret.

Expand Down
44 changes: 44 additions & 0 deletions src/mondemand_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
now_to_epoch_millis/1, % ({_,_,_}) -> MillisSinceEpoch
now_to_epoch_secs/1, % ({_,_,_}) -> SecondsSinceEpoch
now_to_epoch_minutes/1, % ({_,_,_}) -> MinutesSinceEpoch
now_diff_milliseconds/2,
current/0,
current_minute/0,
millis_to_next_round_second/0,
Expand Down Expand Up @@ -155,6 +156,42 @@ now_to_epoch_secs ({Mega, Secs, _}) ->
now_to_epoch_minutes (Now) ->
trunc (now_to_epoch_secs (Now) / 60).

%% NOTE: Copied from webmachine with the mailing list link corrected,
%% webmachine license is
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% File is/was
%%
%% https://github.com/webmachine/webmachine/blob/master/src/webmachine_util.erl
%%
%% Test is also copied below
%%
%% This is faster than timer:now_diff() because it does not use bignums.
%% But it returns *milliseconds* (timer:now_diff returns microseconds.)
%% From http://erlang.org/pipermail/erlang-questions/2002-June/005037.html
%%
%% @doc Compute the difference between two now() tuples, in milliseconds.
%% @spec now_diff_milliseconds(now(), now()) -> integer()
now_diff_milliseconds(undefined, undefined) ->
0;
now_diff_milliseconds(undefined, T2) ->
now_diff_milliseconds(os:timestamp(), T2);
now_diff_milliseconds({M,S,U}, {M,S1,U1}) ->
((S-S1) * 1000) + ((U-U1) div 1000);
now_diff_milliseconds({M,S,U}, {M1,S1,U1}) ->
((M-M1)*1000000+(S-S1))*1000 + ((U-U1) div 1000).

current () ->
{{Year, Month, Day},{Hour,Minute,_}} = now_to_mdyhms (os:timestamp()),
EpochStartSeconds =
Expand Down Expand Up @@ -293,5 +330,12 @@ dummy () ->
-ifdef (TEST).
-include_lib ("eunit/include/eunit.hrl").

%% Copied from webmachine, see NOTE above
now_diff_milliseconds_test() ->
Late = {10, 10, 10},
Early1 = {10, 9, 9},
Early2 = {9, 9, 9},
?assertEqual(1000, now_diff_milliseconds(Late, Early1)),
?assertEqual(1000001000, now_diff_milliseconds(Late, Early2)).

-endif.

0 comments on commit 7459e3a

Please sign in to comment.