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

Since I was the one who originally discovered the |0 trick, I'll give you a little more info on it which should help you clean up your code a little.
1. You don't need to use it on variables which are already integers (Yes, there is an integer type for variables, not just float and string.) Plain integers like 5 and 2 are included in this.
This means you can use (%a+%b)|0 instead of ((%a|0)+(%b|0))|0 if you know %a and %b are integers.
2. You don't need to use any other functions on it to convert it to an integer, |0 automatically converts to integer to the same effect as mFloor.
3. Pretty much all of the basic math operations convert to float. I haven't found one that doesn't, so, yeah. always use |0
4. You can use ~~(%a+%b) instead of (%a+%b)|0, ~~(~~%a+~~%b) and vice versa. It makes the code a little less ugly but uses an extra operation so it's a little slower. Use only if the function isn't going to be called often I suppose. ~ is the bitwise invert operator if you were curious.
« Last Edit: January 07, 2016, 08:07:00 PM by Ipquarx »

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:24:57 AM by Shift Kitty »

1. You don't need to use it on variables which are already integers (Yes, there is an integer type for variables, not just float and string.) Plain integers like 5 and 2 are included in this.
This means you can use (%a+%b)|0 instead of ((%a|0)+(%b|0))|0 if you know %a and %b are integers.

What's going on here?

==>$x = -1 | 0;
==>echo($x);
-1
==>echo($x == -1);
0
==>echo($x == (-1 | 0));
0
==>echo(($x | 0) == -1);
0
==>echo(($x | 0) == (-1 | 0));
1

What's going on here?

==>$x = -1 | 0;
==>echo($x);
-1
==>echo($x == -1);
0
==>echo($x == (-1 | 0));
0
==>echo(($x | 0) == -1);
0
==>echo(($x | 0) == (-1 | 0));
1

torque at its finest
this is why variables should have typing and not just dynamic switching between strings and numbers or whatever

What's going on here?

==>$x = -1 | 0;
==>echo($x);
-1
==>echo($x == -1);
0
==>echo($x == (-1 | 0));
0
==>echo(($x | 0) == -1);
0
==>echo(($x | 0) == (-1 | 0));
1

Not sure to be honest but:


it seems to work fine with positives.

probably should put this limitation somewhere in your description of this function up at the top of this page