anchor.diff:      {N1, N2, N3} = os:timestamp(),
       (N1 * 1000000 + N2) * 1000000 + N3.
   
  +
   %% @doc Get current epoch in binary for sorting, in hex format
  -%% For secs: 8 bytes (10 bytes in about 100 years)
  +%% See epoch_to_hex/2
  +-spec now_hex(epoch_unit()) ->
  +    binary().
  +
  +now_hex(Unit) ->
  +    epoch_to_hex(epoch(Unit), Unit).
  +
  +
  +%% @doc Get current epoch in binary for sorting, in hex format
  +%% For secs:  10 bytes
   %% For msecs: 12 bytes
   %% For usecs: 14 byes
  --spec now_hex(epoch_unit()) ->
  -    binary().
  -
  -now_hex(Unit) ->
  -    Base = case Unit of
  -        secs -> 1550723145;
  -        msecs -> 467989515763;
  -        usecs -> 1286025660953095
  +%% It will never wrap, see epoch_to_hex_test/0
  +
  +epoch_to_hex(Epoch, Unit) ->
  +    Hex = nklib_util:hex(binary:encode_unsigned(Epoch)),
  +    Size = case Unit of
  +        secs -> 10;
  +        msecs -> 12;
  +        usecs -> 14
       end,
  -    nklib_util:hex(binary:encode_unsigned(epoch(Unit)-Base)).
  +    case byte_size(Hex) of
  +        Size ->
  +            Hex;
  +        HexSize ->
  +            BinPad = binary:copy(<<"0">>, Size - HexSize),
  +            <<BinPad/binary, Hex/binary>>
  +    end.
   
   
   %% @doc Get current epoch time, using the cached pattern
  
positive.code: to_bin(K) -> nklib_util:to_binary(K).
  
  
  %% ===================================================================
  %% EUnit tests
  %% ===================================================================
  
negative.code: %% @doc NetComposer Standard Library
  -module(nklib_date).
  -author('Carlos Gonzalez <carlosj.gf@gmail.com>').
  -export([epoch/1, now_hex/1, epoch_to_hex/2, now_3339/1]).
  -export([to_3339/2, to_epoch/2, is_3339/1]).
  -export([age/1]).
  -export_type([epoch_unit/0, epoch/1]).
  