Author Topic: [Solved] Converting an integer to a date format.  (Read 2934 times)

getUTC() outputs an integer. This is fine for storing information and making long-term comparisons.
However, when it comes to turning this into a MM/DD/YYYY HH:MM:SS string, or anything similar, I can't figure it out.
Google isn't any help, the only things I can find on there is just all the functions that other languages have that do all the work for you.
TS doesn't seem to have this. It has getTimeString(int) but, this just converts it into HHHHHH::MM:SS.

The code if anyone's interested, or maybe wants to clean it up or make any improvements.

Code: [Select]
function UnixToDateTime(%unix,%twelve,%adj)
{
%adj*=3600;
%unix = (%unix+%adj)|0;
%z = mfloor((%unix/86400)|0);
%time = %unix-(%z*86400);
%hours = mfloor(%time/(60*60));
%time-=%hours*(60*60);
%minutes = mfloor(%time/60);
%seconds = %time - %minutes*60;
if(strLen(%minutes) < 2)
%minutes = "0" @ %minutes;
if(strLen(%seconds) < 2)
%seconds = "0" @ %seconds;
if(%twelve)
{
if(%hours < 12)
%AP = "AM";
else
{
%AP = "PM";
%hours-=12;
}
if(%hours == 0)
%hours = 12;
if(strLen(%hours) < 2)
%hours = "0" @ %hours;
%timestring = %hours @ ":" @ %minutes @ ":" @ %seconds SPC %AP;
}
else
{
if(strLen(%hours) < 2)
%hours = "0" @ %hours;
%timestring = %hours @ ":" @ %minutes @ ":" @ %seconds;
}
%z = (%z+719468)|0;
%era = ((%z >= 0 ? %z : %z-146096)/146097)|0;
%doe = ((%z - %era * 146097))|0;
%yoe = ((%doe - (%doe/1460|0) + (%doe/36524|0) - (%doe/146096|0)) / 365)|0;
%y = (%yoe + %era * 400)|0;
%doy = (%doe - ((365*%yoe|0) + (%yoe/4|0) - (%yoe/100|0)))|0;
%mp = ((5*%doy + 2)/153)|0;
%d = %doy - ((153*%mp+2)/5|0) + 1;
%m = %mp + (%mp < 10 ? 3 : -9);
%y = (%y + (%m <= 2))|0;
%d = mceil(%d);
if(strLen(%d) == 1)
%d = "0" @ %d;
if(strLen(%m) == 1)
%m = "0" @ %m;
return %m @ "/" @ %d @ "/" @ %y SPC %timestring;
}

Edit: Threw in timezone because why not.
It's manual and doesn't calculate DST though.

Didn't feel like cleaning it up, it really makes the code a huge mess. I know I should but, the function isn't going to get called too much anyway. Or at least shouldn't be.

Decided to make the inverse as well. The timezone arg here works inverse, so that you get UTC time by inputting your timezone again.

Code: [Select]
function DateTimeToUnix(%dt,%adj)
{
%date = getWord(%dt,0);
%time = getWord(%dt,1);
%time = strReplace(%time,":"," ");
%hours = getword(%time,0);
%minutes = getWord(%time,1);
%seconds = getWord(%time,2);
if(strStr(%dt,"M") > -1)
{
if(%hours == 12)
%hours-=12;
if(strStr(%dt,"P") > -1)
%hours+=12;
}
%date = strReplace(%date,"/"," ");
%y = getWord(%date,2);
%m = getWord(%date,0);
%d = getWord(%date,1);
%y -= %m <= 2;
%era = ((%y >= 0 ? %y : %y-399) / 400)|0;
%yoe = (%y - %era * 400)|0;
%doy = (((153*(%m + (%m > 2 ? -3 : 9)) + 2)/5|0) + %d-1)|0;
%doe = (%yoe * 365 + (%yoe/4|0) - (%yoe/100|0) + %doy)|0;
%z = (%era * 146097 + %doe - 719468)|0;
%time = %seconds + (%minutes*60) + (%hours*60*60);
%z = ((%z*86400)+(%adj*-1*3600))|0;
return (%time+%z)|0;
}

Edit: And may as well throw weekday in there.

Code: [Select]
function Weekday(%udt,%n,%adj) //accepts datetime or unix as input, converts dt to unix anyway
{ //%n, 0 = int (0=sun, 6=sat), 1 = short, 2 = long //adj not used for dt
if(strStr(%udt,"/") > -1)
%unix = DateTimeToUnix(%udt);
else
%unix = (%udt+(%adj*3600))|0;
%z = mfloor((%unix/86400)|0);
%int = (%z >= -4 ? (%z+4) % 7 : (%z+5) % 7 + 6)|0;
%ret = %int;
if(%n)
{
switch(%int)
{
case 0: %ret="Sunday";
case 1: %ret="Monday";
case 2: %ret="Tuesday";
case 3: %ret="Wednesday";
case 4: %ret="Thursday";
case 5: %ret="Friday";
case 6: %ret="Saturday";
}
if(%n == 1)
%ret = getSubStr(%ret,0,3);
}
return %ret;
}
« Last Edit: January 08, 2016, 01:25:18 AM by Shift Kitty »

oh that's... a unix timestamp.

...we've had this?

EDIT: to clarify, it's the number of seconds since Jan 1st, 1970 00:00 UTC

http://www.unixtimestamp.com/
Quote
The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.
« Last Edit: January 07, 2016, 03:53:11 AM by TheBlackParrot »

That's a big number then for blockland.


oh that's... a unix timestamp.

...we've had this?

EDIT: to clarify, it's the number of seconds since Jan 1st, 1970 00:00 UTC

http://www.unixtimestamp.com/
Yeah, I just need to figure out how to turn this number into a date/time with Tork.

getDateTime() should be what you're looking for.

getDateTime() should be what you're looking for.
It's not what I'm looking for though.
I'm looking for a general function that will turn something from getUTC into something similar to getDateTime.

You want the amount of time since Jan 1st, 1970 00:00 UTC formatted like getDateTime()..?

he's probably doing some math based on getUTC or something, or he just generally doesn't want the current real-time date for his own purposes

http://stackoverflow.com/questions/7960318/math-to-convert-seconds-since-1970-into-date-and-vice-versa

obv it's not in TS but the math stays the same

he's probably doing some math based on getUTC or something, or he just generally doesn't want the current real-time date for his own purposes

http://stackoverflow.com/questions/7960318/math-to-convert-seconds-since-1970-into-date-and-vice-versa

obv it's not in TS but the math stays the same
Nice, thanks! I'll see if I can get something to work from this.

Edit: Ah damn, this calculation is in days, not seconds.
Though I could probably make some modifications to it.
« Last Edit: January 07, 2016, 03:12:03 PM by Shift Kitty »

Got it all working, will post once it's cleaned up.

What do you need this for? Perhaps there's a better way.

Nope, it's not all working.

What do you need this for? Perhaps there's a better way.
Going to start off with saying it doesn't matter what I need it for. I'm asking for a method of doing it this specific way. I'm very well aware of getDateTime. The very major issue with getDateTime however, is that you cannot accurately manipulate the date.

getUTC gives me exactly what I want and need. An integer which represents the number of seconds since 00:00:00 UTC on 1 January 1970.
Need to add a day? Easy, add 86400. An hour? 3600. Need to compare the amount of time between two times? Just do a simple + or -.
The only problem I have is that I can't figure out how to turn this integer into a date. Which, would be done far less often than manipulating it itself.

The problem with
http://stackoverflow.com/questions/7960318/math-to-convert-seconds-since-1970-into-date-and-vice-versa
is that it's using two variable types. Which is reasonable, considering the code was written in c++. however, torque just turns everything into a float or a string, depending on if you're doing math or string manipulation.

This is causing the math to be wrong. I'm trying different combinations of rounding, flooring, ceiling, on different values but the closest I can get is that it's 1 day behind, skips the 29th, then starts being right, for... idk how long.

You can kind of do int math in torkscript by casting back to int every time it tries to turn stuff into a float

Like instead of %a = %b + %c; you would do %a = ((%b|0) + (%c|0))|0;

It makes the code look terrible but it works

Yeah I was almost doing that.
I was doing %a = (%b + %c)|0;, which isn't good enough.
I put that stuff on everything and now it works. Though it's probably only necessary for division.

The code if anyone's interested, or maybe wants to clean it up or make any improvements.

Code: [Select]
function UnixToDateTime(%unix,%twelve,%adj)
{
%adj*=3600;
%unix = (%unix+%adj)|0;
%z = mfloor((%unix/86400)|0);
%time = %unix-(%z*86400);
%hours = mfloor(%time/(60*60));
%time-=%hours*(60*60);
%minutes = mfloor(%time/60);
%seconds = %time - %minutes*60;
if(strLen(%minutes) < 2)
%minutes = "0" @ %minutes;
if(strLen(%seconds) < 2)
%seconds = "0" @ %seconds;
if(%twelve)
{
if(%hours < 12)
%AP = "AM";
else
{
%AP = "PM";
%hours-=12;
}
if(%hours == 0)
%hours = 12;
if(strLen(%hours) < 2)
%hours = "0" @ %hours;
%timestring = %hours @ ":" @ %minutes @ ":" @ %seconds SPC %AP;
}
else
{
if(strLen(%hours) < 2)
%hours = "0" @ %hours;
%timestring = %hours @ ":" @ %minutes @ ":" @ %seconds;
}
%z = (%z+719468)|0;
%era = ((%z >= 0 ? %z : %z-146096)/146097)|0;
%doe = ((%z - %era * 146097))|0;
%yoe = ((%doe - (%doe/1460|0) + (%doe/36524|0) - (%doe/146096|0)) / 365)|0;
%y = (%yoe + %era * 400)|0;
%doy = (%doe - ((365*%yoe|0) + (%yoe/4|0) - (%yoe/100|0)))|0;
%mp = ((5*%doy + 2)/153)|0;
%d = %doy - ((153*%mp+2)/5|0) + 1;
%m = %mp + (%mp < 10 ? 3 : -9);
%y = (%y + (%m <= 2))|0;
%d = mceil(%d);
if(strLen(%d) == 1)
%d = "0" @ %d;
if(strLen(%m) == 1)
%m = "0" @ %m;
return %m @ "/" @ %d @ "/" @ %y SPC %timestring;
}

Edit: Threw in timezone because why not.
It's manual and doesn't calculate DST though.
« Last Edit: January 07, 2016, 06:58:12 PM by Shift Kitty »