<commit_before>-module(riak_kv_quanta).

-export([quanta/3,
         timestamp_to_ms/1,
         ms_to_timestamp/1]).

%% @doc This module serves to generate time quanta on multi - (year, month, day, hour, minute,
%% second) boundaries. The quanta are based on an origin time of Jan 1, 1970 00:00:00 (Unix Epoch).
%% The function `quanta/3` takes a time in milliseconds to quantify, a size of the quanta, and the
%% units of said quanta. For instance, the following call would create buckets for timestamps on 15
%% minute boundaries: `quanta(Time, 15, m)`. The quanta time is returned in milliseconds since the
%% Unix epoch.

%% @doc A timestamp in millisconds representing number of millisconds from Unix epoch
-type time_ms() :: non_neg_integer().

%% @doc The units of quantization available to quanta/3
-type time_unit() :: y | mo | d | h | m | s.

-type err() :: {error, term()}.

%% The Number of Days from Jan 1, 0 to Jan 1, 1970
%% We need this to compute years and months properly including leap years and variable length
%% months.
-define(DAYS_FROM_0_TO_1970, 719528).

-ifdef(TEST).
-ifdef(EQC).
-include_lib("eqc/include/eqc.hrl").
-define(QC_OUT(P),
        eqc:on_output(fun(Str, Args) ->
                              io:format(user, Str, Args) end, P)).
-compile(export_all).
-endif.
-endif.

%% @doc Return the time in milliseconds since 00:00 GMT Jan 1, 1970 (Unix Epoch)
-spec timestamp_to_ms(erlang:timestamp()) -> time_ms().
timestamp_to_ms({Mega, Secs, Micro}) ->
    Mega*1000000000 + Secs*1000 + Micro div 1000.

-spec ms_to_timestamp(time_ms()) -> erlang:timestamp().
ms_to_timestamp(Time) ->
    Seconds = Time div 1000,
    MicroSeconds = (Time rem 1000) * 1000,
    {0, Seconds, MicroSeconds}.

-spec quanta(time_ms(), non_neg_integer(), time_unit()) -> time_ms() | err().<commit_msg>fix edoc location for module doc<commit_after>%% @doc This module serves to generate time quanta on multi - (year, month, day, hour, minute,
%% second) boundaries. The quanta are based on an origin time of Jan 1, 1970 00:00:00 (Unix Epoch).
%% The function <em>quanta/3</em> takes a time in milliseconds to bucketize, a size of the quanta, and the
%% units of said quanta. For instance, the following call would create buckets for timestamps on 15
%% minute boundaries: <em>quanta(Time, 15, m)</em>. The quanta time is returned in milliseconds since the
%% Unix epoch.

-module(riak_kv_quanta).

-export([quanta/3,
         timestamp_to_ms/1,
         ms_to_timestamp/1]).

%% @doc A timestamp in millisconds representing number of millisconds from Unix epoch
-type time_ms() :: non_neg_integer().

%% @doc The units of quantization available to quanta/3
-type time_unit() :: y | mo | d | h | m | s.

-type err() :: {error, term()}.

%% @doc The Number of Days from Jan 1, 0 to Jan 1, 1970
%% We need this to compute years and months properly including leap years and variable length
%% months.
-define(DAYS_FROM_0_TO_1970, 719528).

-ifdef(TEST).
-ifdef(EQC).
-include_lib("eqc/include/eqc.hrl").
-define(QC_OUT(P),
        eqc:on_output(fun(Str, Args) ->
                              io:format(user, Str, Args) end, P)).
-compile(export_all).
-endif.
-endif. %% @clear

%% @doc Return the time in milliseconds since 00:00 GMT Jan 1, 1970 (Unix Epoch)
-spec timestamp_to_ms(erlang:timestamp()) -> time_ms().
timestamp_to_ms({Mega, Secs, Micro}) ->
    Mega*1000000000 + Secs*1000 + Micro div 1000.

-spec ms_to_timestamp(time_ms()) -> erlang:timestamp().
ms_to_timestamp(Time) ->
    Seconds = Time div 1000,
    MicroSeconds = (Time rem 1000) * 1000,
    {0, Seconds, MicroSeconds}.

-spec quanta(time_ms(), non_neg_integer(), time_unit()) -> time_ms() | err().
{'fraction_non_alphanumeric': 0.13101386246262572, 'fraction_numerical': 0.04240282685512368, 'mean_word_length': 3.7730220492866406, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3063374', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1198, 'n_words': 429}