Converting between epoch and dates
Agreed that majority of the people are into making games and no one really works hard to use the date time functions. This also means that if you need to get the number of seconds, there are simple functions that give you the same. However if you try to get a timestamp, all you get is an epoch number in Lua
The epoch number is 1st January 1970 00:00, so the current date and time is the number of seconds that have passed since that date and time. So can we sue this to get information, YES!!
Let's say the time stamp is 1314592406. What do you think that is in terms of date and time? simple, let us determine the year, by dividing this number by (365 * 24 * 60 * 60) the 365 for the number of days to a year, 24 for the number of hours in a day, 60 for the number f minutes in the hour and 60 for the number of seconds in the minute. So we get about 41.6859. let's say 41, so 1970 + 41 = 2011, the year from this is 2011.
Now to determine the month, the day and the hour, minutes, seconds... BAh!! too complicated...
Is there an easier way? Why should date and time be so difficult? Well, here is a class that I have for all of you that makes using this epoch date/time easy. So if you save the numeric timestamp, and would want to get the date/time from it, you can use this class as
easy as that. you can always get the class from GitHub
The epoch number is 1st January 1970 00:00, so the current date and time is the number of seconds that have passed since that date and time. So can we sue this to get information, YES!!
Let's say the time stamp is 1314592406. What do you think that is in terms of date and time? simple, let us determine the year, by dividing this number by (365 * 24 * 60 * 60) the 365 for the number of days to a year, 24 for the number of hours in a day, 60 for the number f minutes in the hour and 60 for the number of seconds in the minute. So we get about 41.6859. let's say 41, so 1970 + 41 = 2011, the year from this is 2011.
Now to determine the month, the day and the hour, minutes, seconds... BAh!! too complicated...
Is there an easier way? Why should date and time be so difficult? Well, here is a class that I have for all of you that makes using this epoch date/time easy. So if you save the numeric timestamp, and would want to get the date/time from it, you can use this class as
local dater = require("dater").init(1314592406) print(dater:date()) print(dater:dayOfWeek()) print(dater:year()) print(dater:month()) print(dater:time())
easy as that. you can always get the class from GitHub
RE: Dater - Great work from a Lua newbie ...
ReplyDeleteSuggest adding:
function dateTable:isodate()
return string.format("%04d-%02d-%04d", temp.year,
temp.month, temp.day)
end
Correction to last comment "%04d-%02d-%02d":
ReplyDeletefunction dateTable:isodate()
return string.format("%04d-%02d-%02d", temp.year,
temp.month, temp.day)
end