This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Waitdev_'s profile picture

converting timestamp to readable date

Started by Waitdev_, 26 November 2015 - 09:37 AM
Waitdev_ #1
Posted 26 November 2015 - 10:37 AM
simple as it sounds. how can i convert a timestamp to a readable date? in a string?
DaKillerBear1 #2
Posted 26 November 2015 - 11:30 AM
What kind of timestamp are you talking about?
Dragon53535 #3
Posted 26 November 2015 - 03:46 PM
textutils.formatTime()

textutils.formatTime(number time, boolean twentyFourHour)
Blue #4
Posted 26 November 2015 - 04:12 PM
textutils.formatTime()

textutils.formatTime(number time, boolean twentyFourHour)
I didn't even know that that existed,awesome!
Dragon53535 #5
Posted 26 November 2015 - 04:14 PM
Always look up your api's :P/>
Of course, the problem might be greater if the information he's trying to convert is not a number.
Edited on 26 November 2015 - 03:14 PM
Waitdev_ #6
Posted 27 November 2015 - 06:09 AM
lol, i didn't know os.time() returned a timestamp :P/>

also, thanks for the responses!

Edit: just tried it, and i guess i should be more clear about everything :P/>
i mean unix timestamps, e.g. 1448642448 which should convert to Fri, 27 Nov 2015 16:40:48 or something like that.
Edited on 27 November 2015 - 05:14 AM
Bomb Bloke #7
Posted 27 November 2015 - 07:31 AM
Division and modulus operations. Dividing by the seconds in a year gives you the number of years (stamp/secsPerYear). Taking the modulus instead (stamp%secsPerYear) gets you the remainder, which you can use to figure out months, then days, hours, minutes and finally seconds.

The main trick is that not all years (or months) have the same number of seconds. I guess I'd first divide by the average amount of seconds per four years, and then you should be able to deduce from the number of seconds in the remainder as to whether the last few years contain (or even end on) a leap year.

For months, a series of if/then statements checking the twelve possible ranges may be easiest. Getting the final days/hours/minutes/seconds is relatively simple division from there.
Waitdev_ #8
Posted 27 November 2015 - 08:08 AM
can you please give me an example of the if/then statements please? thanks :)/>
Bomb Bloke #9
Posted 27 November 2015 - 08:37 AM
Well, let's say you've already used division/modulus operations to figure out what year the stamp points to, and how many seconds are remaining since the point where that year began.

There are 86,400 seconds in a day, and 31 days in the first month of a year. So we first check if the remainder is less than 31 * 86,400:

if remainder < 2678400 then
  -- Stamp points to January.
  month = "Jan"
  remainder = remainder - 2678400

The next month, February, either contains 28 or 29 days depending on whether it's a leap year. It may be easiest to set a "bump" variable to either 0 or 86,400 before the "if" block, for use in the subsequent checks. Eg:

local bump = 0

if <yearIndicatedByStampIsALeapYear> then
  bump = 86400
end

if remainder < 2678400 then
  month = "Jan"
  remainder = remainder - 2678400

elseif remainder < 5097600 + bump then  --  5,097,600 seconds == 59 days, Jan (31) + Feb (28). +1 day if a leap year.
  month = "Feb"
  remainder = remainder - 5097600 - bump

elseif remainder < 7776000 + bump then
  month = "Mar"
  remainder = remainder - 7776000 - bump

-- etc
Dragon53535 #10
Posted 27 November 2015 - 04:21 PM
To be honest, dealing with time is probably a hard thing, if you're fine with using the http api

This site, returns the data in a table, so you could textutils.unserialize it, and find your date if you wanted to.
http://www.convert-unix-time.com/api?timestamp=0