Time in Erlang
This Erlang code demonstrates various time-related operations similar to the Go example. Here’s a breakdown of the differences and similarities:
Erlang uses the
erlang:timestamp()
function to get the current time, which is then converted to a datetime tuple usingcalendar:now_to_universal_time/1
.Datetime values in Erlang are represented as nested tuples:
{{Year, Month, Day}, {Hour, Minute, Second}}
.Erlang’s
calendar
module provides functions for working with dates and times, similar to Go’stime
package.The
calendar:day_of_the_week/3
function returns the day of the week as an integer (1 for Monday, 7 for Sunday).Time comparisons in Erlang are done by converting datetime tuples to Gregorian seconds using
calendar:datetime_to_gregorian_seconds/1
.Erlang doesn’t have a built-in duration type like Go. Instead, we calculate the difference in seconds and perform arithmetic on that.
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.