Calculating Dates
#1

Hello all,

I would like some help on the following matters.
For use in a hotel system, I will need to calculate dates. This will include things like calculating tomorrow's date, calculating which data is earlier etc.
I have managed to create a function which returns whether the chosen date is earlier than the present one. What I cannot manage though is solving the first problem. What I basically want to do is allow players to enter a number of nights to stay. After this the date of the 'checkout' day will be stored. So, if I for example on the 30th of January enter 4 nights, that would set the checkout date to the third of February. So I would need a function like 'CalculateDateXDaysFromNow(days)'. But how would I calculate such a thing? I have found an example in C++, that would be too complicated to write in PAWN though.. And it wasn't bugfree neither.
An other option would be to do it the other way around: The player would enter the checkout-date after which the server would calculate the nights to be billed. But this would probably involve similar calculations..?
Would anyone be willing to lend a hand?

Thank you in advance!
Reply
#2

I suppose this is realtime?
pawn Код:
stock CalcDate(&year, &month, &day, extradays)
{
    getdate(year, month, day);
    day += extradays;
    // suppose day = 27 and extradays = 8
    // day is now 35

    switch(month)
    {
        case 1, 3, 5, 7, 8, 10, 12: // 31 day months
        {
            if(day > 31 )
            {
                if(month == 12)
                {
                    month = 1;
                    year++;
                }
                else
                {
                    month++;
                }
                day -= 31; // day is now 4
            }
        }
        case 4, 6, 9, 11: // 30 day months
        {
            if(day > 30 )
            {
                month++;
                day -= 30;
            }
        }
        case 2: // february
        {
            if((day + extradays) > 28 )
            {
                month++;
                day -= 28;
            }
        }
    }
}
Something along those lines ..
Reply
#3

Well, that actually looks good! Yes, I use realtime. The only problem with that piece of code would be years in which februari has 29 days instead of 28 days. I don't know whether I will still run my server next year (which is such a 'special' year) to be honest though. So this will probably do the trick.. Thanks a lot!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)