[SOLVED] Check difference between two dates
#1

Hey,
I'm trying yo check time difference between 2 dates..
Here's my unsuccessfull go:
pawn Code:
new y,m,d,h,i;
    new y2,m2,d2,h2,i2;
    new FullDate[12],FullDate2[12],DATE2[12],TIME2[8]; // FullDate(2) - "YYYYMMDDHHII" ; DATE2 - "YYYY-MM-DD"; TIME2 - "HH:II";
    new DateINT,DateINT2; // DateINT(2) - strval(FullDate(2))
    getdate(y,m,d);
    gettime(h,i);
    format(FullDate,12,"%d%02d%02d%02i%02i",y,m,d,h,i); // This will be date without any other symbols (not YYYY-MM-DD, HH:II , but YYYYMMDDHHII)
    DateINT = strval(FullDate); // Should return FullDate in integer*..
   
    sscanf("2010-01-01, 12:00","p,ss",DATE2,TIME2); // Splits 2010-01-01, 12:00 >> to 2010-01-01 and 12:00
    sscanf(DATE2,"p-ddd",y2,m2,d2); // Splits 2010-01-01 to 2010 01 01
    sscanf(TIME2,"p:dd",h2,i2); // Splits 12:00 to 12 00
    format(FullDate2,12,"%d%02d%02d%02i%02i",y2,m2,d2,h2,i2); // YYYYMMDDHHII - this is date from file
    DateINT2 = strval(FullDate2); // This SHOULD return FullDate2 in integer*..
    printf("FullDate: %s , FullDate2: %s , DateINT: %d , DateINT2: %d",FullDate,FullDate2,DateINT,DateINT2); //DEBUG *
* - I tried printing stuff that is on the last line of this code.. Here's what it printed:
Quote:

[19:12:32] FullDate: 201001061912 , FullDate2: 201001061822 , DateINT: -862401000 , DateINT2: -862401090

Why DateINT and DateINT2 isn't same as FullDate and FullDate2 ??

Maybe you know any better ways to check difference between two dates?

What I want to do is to check if one day has passed before player was at specified location...
Thanks for HUGE help..
Reply
#2

Do you need to do the second sscanfs? You could remove them and just set the int to 201001011200, unless it actually has a function that gets the date from somewhere.
Secondly try adding prints to all of the lines, so you could better work out the problem
Reply
#3

Quote:
Originally Posted by bogeyman_EST
Do you need to do the second sscanfs? You could remove them and just set the int to 201001011200, unless it actually has a function that gets the date from somewhere.
Secondly try adding prints to all of the lines, so you could better work out the problem
1. I tried using just one sscanf (using parameters "p-dddp,dp:d" but it didn't worked.. So that's why I'm using 3 sscanfs..
2. I don't think it's needed cause I print almost all results on the last line.. :S
Reply
#4

I believe strval wraps to undefined values when the number gets bigger than the maximum value of a 32bit integer (signed 2147483647, unsigned 4294967295)
Reply
#5

Quote:
Originally Posted by MaVe;
I believe strval wraps to undefined values when the number gets bigger than the maximum value of a 32bit integer (signed 2147483647, unsigned 4294967295)
Do u have any ideas how can I fix it?
Would this: 201001061912 number work in strval?
Reply
#6

Quote:
Originally Posted by SiJ
Would this: 201001061912 number work in strval?
strval would return 10619 on that one, so should work
Reply
#7

Quote:
Originally Posted by MaVe;
Quote:
Originally Posted by SiJ
Would this: 201001061912 number work in strval?
strval would return 10619 on that one, so should work
but I need it to return with zeros... :\
maybe I'll try this 10010619..
Thanks for help..
Reply
#8

But maybe there's an easier way than this to check difference between two dates?
Reply
#9

Quote:
Originally Posted by SiJ
But maybe there's an easier way than this to check difference between two dates?
bump
Reply
#10

You could use the sqlite date functions.
pawn Code:
new DB:db = db_open("tmp.db");
if(db) {
    new string[80];
    format(string, sizeof(string), "SELECT ifnull(julianday('%s', 'utc') - julianday('now'),'')", date);
    new DBResult:result = db_query(db, string);
    if(db_num_rows(result)) {
        db_get_field(result, 0, string, sizeof(string));
        if(string[0]) {
            printf("Time difference: %s days", string);
        } else {
            print("Invalid date");
        }
    }
    db_free_result(result);
    db_close(db);
} else {
    print("Unable to open database");
}
SQLite is a bit picky about dates but that shouldn't be a problem if they're all coming from the script.
pawn Code:
stock ReturnTimeStamp()
{
    new datetime[30],
        year, month, day,
        hour, minute, second;

    getdate(year, month, day);
    gettime(hour, minute, second);
    format(datetime, sizeof(datetime), "%04i-%02i-%02i %02i:%02i:%02i", year, month, day, hour, minute, second);

    return datetime;
}

A better idea would be to use gettime() and store the time as an integer
pawn Code:
public OnRconCommand(cmd[])
{
    if(strcmp(cmd, "test", true) == 0) {
        static time;
        if(!time) {
            time = gettime();
            print("Time stored");
        } else {
            printf("Time passed: %i seconds", gettime() - time);
            time = gettime();
        }
        return 1;
    }
    return 0;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)