Time in Erlang

-module(time_example).
-export([main/0]).

main() ->
    P = fun(X) -> io:format("~p~n", [X]) end,

    % We'll start by getting the current time.
    Now = erlang:timestamp(),
    P(calendar:now_to_universal_time(Now)),

    % You can build a datetime tuple by providing the
    % year, month, day, etc. Times are always associated
    % with a time zone.
    Then = {{2009, 11, 17}, {20, 34, 58}},
    P(Then),

    % You can extract the various components of the time
    % value as expected.
    {{Year, Month, Day}, {Hour, Minute, Second}} = Then,
    P(Year),
    P(Month),
    P(Day),
    P(Hour),
    P(Minute),
    P(Second),

    % The day of the week is also available.
    P(calendar:day_of_the_week(Year, Month, Day)),

    % These functions compare two times, testing if the
    % first occurs before, after, or at the same time
    % as the second, respectively.
    P(calendar:datetime_to_gregorian_seconds(Then) < calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(Now))),
    P(calendar:datetime_to_gregorian_seconds(Then) > calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(Now))),
    P(calendar:datetime_to_gregorian_seconds(Then) =:= calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(Now))),

    % We can compute the difference between two times.
    Diff = calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(Now)) - calendar:datetime_to_gregorian_seconds(Then),
    P(Diff),

    % We can compute the length of the duration in
    % various units.
    P(Diff div 3600),  % Hours
    P(Diff div 60),    % Minutes
    P(Diff),           % Seconds

    % You can use `calendar:gregorian_seconds_to_datetime/1` 
    % to advance a time by a given number of seconds, 
    % or subtract to move backwards.
    P(calendar:gregorian_seconds_to_datetime(calendar:datetime_to_gregorian_seconds(Then) + Diff)),
    P(calendar:gregorian_seconds_to_datetime(calendar:datetime_to_gregorian_seconds(Then) - Diff)).

This Erlang code demonstrates various time-related operations similar to the Go example. Here’s a breakdown of the differences and similarities:

  1. Erlang uses the erlang:timestamp() function to get the current time, which is then converted to a datetime tuple using calendar:now_to_universal_time/1.

  2. Datetime values in Erlang are represented as nested tuples: {{Year, Month, Day}, {Hour, Minute, Second}}.

  3. Erlang’s calendar module provides functions for working with dates and times, similar to Go’s time package.

  4. The calendar:day_of_the_week/3 function returns the day of the week as an integer (1 for Monday, 7 for Sunday).

  5. Time comparisons in Erlang are done by converting datetime tuples to Gregorian seconds using calendar:datetime_to_gregorian_seconds/1.

  6. Erlang doesn’t have a built-in duration type like Go. Instead, we calculate the difference in seconds and perform arithmetic on that.

  7. To add or subtract time, we convert to Gregorian seconds, perform the arithmetic, and then convert back to a datetime tuple using calendar:gregorian_seconds_to_datetime/1.

When you run this program, you’ll see output similar to the Go example, showing various time operations and comparisons.