|
| 31 May 2011 02:59 PM |
Having horrible trouble here :P
I want to change an amount of seconds to a more understandable format, EG:
"86401 seconds" -> "1 Day 0 Hours 0 Minutes 1 Second"
Here's what I managed to do. Neadless to say, it fails horrendously.
Help please? xP
Seconds = SecondsArg % 60 Minutes = SecondsArg / 60 Hours = SecondsArg / (60 * 60) Days = SecondsArg / (60 * 60 * 24)
RetSeconds = math.floor(SecondsArg % 60) RetMinutes = math.floor(SecondsArg / 60 - (Hours * 60)) RetHours = math.floor(SecondsArg / (60 * 60) - (Days * 24)) RetDays = math.floor(SecondsArg / (60 * 60 * 24)) |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 03:36 PM |
Well its a pretty hard thing to do. I wouldn't blame you.
TotalSeconds = 86401 Days = math.floor(TotalSeconds / 86400) NewSeconds = TotalSeconds - (Days * 86400) Hours = math.floor(NewSeconds / 3600) NewSeconds2 = NewSeconds - (Hours * 3600) Minutes = math.floor(NewSeconds2 / 60) Seconds = NewSeconds2 - (Minutes * 60) |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 03:55 PM |
:L Confusing
How far off was I? :P |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:01 PM |
| Well from what you did I can't really tell. I trying to work out the equation right now. Got most except Days is ending up 5 days off. 2011/5/26 18:0:55 |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:02 PM |
I managed to figure out this bit:
Days = math.floor(SecondsArg / 86400) NewSeconds = SecondsArg - (Days * 86400) Hours = math.floor(SecondsArg / 3600) NewSeconds2 = SecondsArg - (Hours * 3600) Minutes = math.floor(NewSeconds2 / 60) Seconds = NewSeconds2 - (Minutes * 60) Hours = Hours - (Days * 24)
The hours bit at the end perfects it I think. |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:03 PM |
It begs the question why they didn't base time on 10s, like everything else is:
10 ms = 1 s 10 s = 1 m 10 m = 1 hr 10 hr = 1 d 10 d = 1 yr 10 yr = 1 decade 10 decade = 1 century
:L |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:04 PM |
You do realize you need to compensate for extra days in leap years, correct? Of course, leap years are more confusing than need be, so have some copypasta pseudo-code to help you out.
if year modulo 400 is 0 then is_leap_year else if year modulo 100 is 0 then not_leap_year else if year modulo 4 is 0 then is_leap_year else not_leap_year |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:04 PM |
| Well really that would just mess up the whole time system |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:06 PM |
| OMG I forgot about leap years. Thats why mine would be about 5 days off |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:06 PM |
| @trapping, Google decimal time |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:07 PM |
@B3tter
Har har, that's why I'm not specifying months. |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:08 PM |
LOL So I specified leap years with it, Now it says its April 19th. Here is what I got.
Seconds = math.floor(tick()) Years = math.floor(Seconds / 31557600) NewSeconds = Seconds - (Years * 31557600) Days = math.floor(NewSeconds / 86400) Months = math.floor(Days / (365 / 12)) NewDays = math.floor(Days * (Months / (365 / 12))) NewSeconds2 = NewSeconds - (Days * 86400) Hours = math.floor(NewSeconds2 / 3600) NewSeconds3 = NewSeconds2 - (Hours * 3600) Minutes = math.floor(NewSeconds3 / 60) Seconds = NewSeconds3 - (Minutes * 60)
print(("%i/%.2i/%.2i %i:%.2i:%.2i"):format(Years + 1970, Months, NewDays, Hours, Minutes, Seconds)) |
|
|
| Report Abuse |
|
|
|
| 31 May 2011 04:53 PM |
If either of you still care...
local seconds = math.floor(tick()) local minutes = math.floor(seconds / 60) seconds = seconds % 60
local hours = math.floor(minutes / 60) minutes = minutes % 60
local days = math.floor(hours / 24) hours = hours % 24
local years = 0 while (days >= 365) do local t_year = years + 1970 days = days - ((((t_year % 4) == 0) and not ((t_year % 100) == 0)) and 366 or 365) years = years + 1 end
local months = { leap = {[1] = 31, [2] = 29, [3] = 31, [4] = 30, [5] = 31, [6] = 30, [7] = 31, [8] = 31, [9] = 30, [10] = 31, [11] = 30, [12] = 31}, norm = {[1] = 31, [2] = 28, [3] = 31, [4] = 30, [5] = 31, [6] = 30, [7] = 31, [8] = 31, [9] = 30, [10] = 31, [11] = 30, [12] = 31}, names = {[1] = "January", [2] = "February", [3] = "March", [4] = "April", [5] = "May", [6] = "June", [7] = "July", [8] = "August", [9] = "September", [10] = "October", [11] = "November", [12] = "December"} }
local month; local mtable = ((((years + 1970) % 4) == 0) and not (((years + 1970) % 100) == 0)) and months.leap or months.norm for i = 1, 12 do if (days <= mtable[i]) then month = i break end days = days - mtable[i] end
local form = "%s %i, %i %.2i:%.2i:%.2i %s" form = form:format(months.names[month], days, (years + 1970), ((hours % 12) == 0) and 12 or (hours % 12), minutes, seconds, ((hours / 12) > 1) and "PM" or "AM") print(form)
I personally think that's really ugly, though. So, you might consider putting that into a function or something. |
|
|
| Report Abuse |
|
|