Author Topic: [Resource] DateTime  (Read 1177 times)

Calendar stuff sux, so I have something to help you a bit on your way. Sorry about not having days of the week (monday, tuesday), that stuff boggles my mind.
Also, to perfect the isLeapYear, I suppose you could make the rules dependent on when the rules were introduced in real life (for instance: Gregorian calendar)

Here's some DateTime functions:
Code: [Select]
function DateTime::getDate(%this)
{
return %this.day@"/"@%this.month@"-"@%this.year;
}

function DateTime::addTime(%this, %time, %timeFormat)
{
if(%timeFormat $= "")
%timeFormat = "s";
switch$(%timeFormat)
{
case "s" or "second":
%this.second = %this.second+%time;
%this.cleanTime(s);
case "m" or "minute":
%this.minute = %this.minute+%time;
%this.cleanTime(m);
case "h" or "hour":
%this.hour = %this.hour+%time;
%this.cleanTime(h);
case "d" or "day":
%this.day = %this.day+%time;
%this.cleanTime(d);
case "mo" or "month":
%this.month = %this.month+%time;
%this.cleanTime(mo);
case "y" or "year":
%this.year = %this.year+%time;
}
}

function DateTime::daysInMonth(%this, %month, %year)
{
if(%month $= "")
%month = %this.month;
if(%year $= "")
%year = %this.year;

if(%month < 1 || %month > 12)
return 0;
if(%month == 2)
if(%this.isLeapYear())
return 29;
else
return 28;
else if(%month == 4 || %month == 6 || %month == 9 || %month == 11)
return 30;
else
return 31;
}

function DateTime::cleanTime(%this, %unit) //Method that cleans the time, making sure there's not gonna be a 26:61 37/-5/2015
{
switch$(%unit)
{
case "s":
if(%this.second > 59)
{
%this.minute = %this.minute + mFloor(%this.second / 60);
%this.second = %this.second % 60;
return %this.cleanTime("m");
}
case "m":
if(%this.minute > 59)
{
%this.hour = %this.hour + mFloor(%this.minute / 60);
%this.minute = %this.minute % 60;
return %this.cleanTime("h");
}
if(%this.minute < 0)
{
%this.hour--;
%this.minute = 60 + %this.minute;
return %this.cleanTime("m");
}
case "h":
if(%this.hour > 23)
{
%this.day = %this.day + mFloor(%this.hour / 24);
%this.hour = %this.hour % 24;
return %this.cleanTime("d");
}
if(%this.hour < 0)
{
%this.day--;
%this.hour = 24 + %this.hour;
return %this.cleanTime("h");
}
case "d":
if(%this.day > %this.daysInMonth())
{
%daysInMonth = %this.daysInMonth();
%this.month = %this.month + mFloor(%this.day / %daysInMonth+1);
%this.day = %this.day % %daysInMonth;
return %this.cleanTime("mo");
}
if(%this.day < 1)
{
%this.month--;
%this.day = %this.getDaysInMonth() + %this.day;
return %this.cleanTime("d");
}
case "mo":
if(%this.month > 12)
{
%this.year = %this.year + mFloor(%this.month / 12);
%this.month = %this.month % 12;
}
if(%this.month < 1)
{
%this.year--;
%this.month = 12 + %this.month;
return %this.cleanTime("mo");
}
}
}

function DateTime::ToString(%this)
{

return %this.TimeToString() SPC %this.DateToString();
}

function DateTime::isLeapYear(%this, %year)
{
if(%year $= "")
%year = %this.year;


if(%year % 4 == 0)
if(%year % 100 == 0)
if(%year % 400 == 0)
return true;
else
return false;
else
return true;
else
return false;
}

function DateTime::DateToString(%this)
{
return %this.day@"-"@%this.month@"-"@%this.year;
}

function DateTime::TimeToString(%this)
{
%hour = %this.hour; %minute = %this.minute; %second = %this.second;
if(%hour<10)
%hour = "0"@%this.hour;
if(%minute<10)
%minute = "0"@%this.minute;
if(%second<10)
%second = "0"@%this.second;
return %hour@":"@%minute@":"@%second;
}


function DateTime::setTime(%this, %hour, %minute, %second)
{
if(%hour > 23 || %hour < 0 || %minute > 59 || %minute < 0 || %second > 59 || %second < 0)
return;
%this.hour = %hour;
%this.minute = %minute;
%this.second = %second;
}

function DateTime::setDate(%this, %day, %month, %year)
{
if(%day > %this.getDaysInMonth(%month, %year) || %day < 1 || %month > 12 || %month < 1)
return;
%this.day = %day;
%this.month = %month;
%this.year = %year;
}

Create a DateTime Object:
Code: [Select]
new ScriptGroup(GameTime)
{
class = DateTime;

second = 0;
minute = 0;
hour = 0;
day = 1;
month = 1;
year = 1;
};

That's all for now.

Is this an interpretation of simtime or a virtual calendar

Is this an interpretation of simtime or a virtual calendar
It's a virtual calendar, without the days of the week.

The determination of what day a specified date seems "numerically-solved" to me, which is a bit ugly to me.
« Last Edit: September 07, 2018, 03:38:47 AM by Quartz »

This looks useful.



Also I was curious if the day of the week thing had been done before.  It has.

Implementation-dependent methods
In the C language expressions below, y, m and d are, respectively, integer variables representing the year (e.g., 1988), month (1-12) and day of the month (1-31).

        (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7 

This looks useful.



Also I was curious if the day of the week thing had been done before.  It has.
Ah, that's interesting thanks!
EDIT: Happy birthday!
« Last Edit: September 08, 2018, 03:20:16 AM by Quartz »