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:
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:
new ScriptGroup(GameTime)
{
class = DateTime;
second = 0;
minute = 0;
hour = 0;
day = 1;
month = 1;
year = 1;
};
That's all for now.