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.
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.
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;
}