Converting DMYHMS to Seconds

From Erlang Community

(Difference between revisions)
Revision as of 09:23, 22 November 2006 (edit)
Kaiserpanda (Talk | contribs)
m
← Previous diff
Current revision (00:22, 8 August 2009) (edit) (undo)
Jashmenn (Talk | contribs)
(added sample function unix_seconds_since_epoch())
 
(One intermediate revision not shown.)
Line 24: Line 24:
5> 63249771661 - 62167219200. 5> 63249771661 - 62167219200.
1082552461 1082552461
 +</code>
 +
 +Wrapping this up in a nice function looks like this:
 +<code>
 +unix_seconds_since_epoch() ->
 + LocalDateTime = calendar:datetime_to_gregorian_seconds({date(),time()}),
 + UnixEpoch = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
 + LocalDateTime - UnixEpoch.
</code> </code>
[[Category:CookBook]][[Category:DateTimeRecipes]] [[Category:CookBook]][[Category:DateTimeRecipes]]
- 
- 
- 
-[http://www.casino-games-wiki.com/index.php/play_casino_games_games_online play casino games games online] 
-[http://www.casino-games-wiki.com/index.php/play_casino_games_games_online play casino games games online] 
-[http://www.gambling-online-theory.com/fortune-roulette/on-line-roulette-game.html on line roulette game] 
-[http://www.casino-theory.com/online-casino-royale/online-casino-gamble.html online casino gamble] 
-[http://www.magical-casino.com/casino_risk.html Gambling Online - Risks.] 
-[http://www.online-casino-wiki.com/index.php/online_casino_tips online casino tips] 
-[http://www.magical-casino.com/online_bonus.html Bonuses in Online Casinos.] 
-[http://www.gambling-online-theory.com/casinos/on-line-casinos.html on line casinos] 
-[http://www.gambling-online-theory.com/casinos-portal/internet-casinos-sites.html internet casinos sites] 
-[http://www.casino-theory.com/online-casino-bonus/index.html online casino bonus] 

Current revision

[edit] Problem

You want to convert a date, a time, or both with distinct values for day, month, year, etc. to seconds.

[edit] Solution

First, it is important to note that time (in Erlang) is always an integer based on a platform-specific starting date, an Epoch, (with a platform-specific minimum and maximum value). Keeping that into consideration, and using various Erlang built-in functions, we can get the number of seconds of a given date, and time. For instance, to get the seconds corresponding to the date, and time: "Wed Apr 21 13:01:01-0500 2004" you could simply:

1> LocalDateTime={{2004,04,21},{13,01,01}}.
{{2004,4,21},{13,1,1}}
2> calendar:datetime_to_gregorian_seconds(LocalDateTime).
63249771661

All of Erlangs date functions are in compliance with the Gregorian calendar, with its Epoch at year 0. So, the seconds value may differ from what we would expect from most UNIX languages (which would record their Epoch as January 1, 1970). So these languages would report a value of 1082570461, rather than 63249771661.

To prove this, let's get the gregorian seconds for 12:00:00 AM on January 1, 1970:

3> UnixEpoch={{1970,1,1},{0,0,0}}.
{{1970,1,1},{0,0,1}}
4> calendar:datetime_to_gregorian_seconds(UnixEpoch).
62167219200
5> 63249771661 - 62167219200.
1082552461

Wrapping this up in a nice function looks like this:

unix_seconds_since_epoch() ->
    LocalDateTime = calendar:datetime_to_gregorian_seconds({date(),time()}),
    UnixEpoch = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
    LocalDateTime - UnixEpoch.
Erlang/OTP Projects
Personal tools