Thread: comparing times

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    comparing times

    how we compare times
    i tried difftime() funtion.it returns the diffrence in time.
    but i wan twhich one is early

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What format is the timestamp in? I assume its a UNIX timestamp, which is seconds since the UNIX epoch.

    So,
    Code:
    time1 = 600079
    time2 = 700960
    difference = time2 - time1
    print difference " seconds apart"
    Similar rules apply to struct time_t, just research it!

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    my time format like this
    04:10:46,03:15:45
    i ant to compare these to time stamps

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Your best bet would be to convert it to your own 'timestamp' (convert it all to seconds for example),

    Code:
    unsigned short int  time1 = 0,
                                   time2 = 0,
                                   h,
                                   m,
                                   s;
    
    sscanf("04:10:46", "%od:%od:%od", &h, &m &s);
    time1 = (h * pow(60, 2)) + (m * 60) + s;
    /* and so on */
    
    /* You can now compare the two by, time1 - time2, to get the seconds in-between */

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i tried difftime() funtion.it returns the diffrence in time.
    I imagine by looking at whether the result is positive or negative.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    it's not giving any negitive value.
    it's only displaying the absolute value.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could post some actual code, with some actual data, rather than keep posting "it doesn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    ok
    printf("%d \n",difftime("04:10:52","05:14:36"));
    it's printing absolute valueee.
    one more problem is how we can compare datee.
    thank u

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Writing random code are we?

    Code:
    :6: warning: passing arg 1 of `difftime' makes integer from pointer without a cast
    :6: warning: passing arg 2 of `difftime' makes integer from pointer without a cast

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not to mention trying to print a float result with %d

    > "04:10:52"
    The first thing you need to do is parse your time into a "struct tm" variable, then call mktime() on it to return a "time_t" variable.
    Then you can call difftime()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    i tried this
    char buffer[20]="04:30:12";
    char buffer1[20]="03:34:36";
    int h,m,s;
    struct tm ctime,ptime;
    sscanf(buffer, "%od:%od:%od", &h, &m, &s);
    ctime.tm_sec=s;
    ctime.tm_min=m;
    ctime.tm_hour=h;
    sscanf(buffer1, "%od:%od:%od", &h, &m, &s);
    ptime.tm_sec=s;
    ptime.tm_min=m;
    ptime.tm_hour=h;
    time_t now1,now2;
    now1=mktime(&ctime);
    now2=mktime(&ptime);
    g_print("***************** %d \n",difftime(now2,now1));

    but
    again same problem geting "0 " or garbage values

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    #include <time.h>
    int main ( ) {
        char buffer[20]="04:30:12";
        char buffer1[20]="03:34:36";
        int h,m,s;
        struct tm ctime,ptime;
        sscanf(buffer, "&#37;od:%od:%od", &h, &m, &s);
        ctime.tm_sec=s;
        ctime.tm_min=m;
        ctime.tm_hour=h;
        sscanf(buffer1, "%od:%od:%od", &h, &m, &s);
        ptime.tm_sec=s;
        ptime.tm_min=m;
        ptime.tm_hour=h;
        time_t now1,now2;
        now1=mktime(&ctime);
        now2=mktime(&ptime);
        printf("***************** %d \n",difftime(now2,now1));
        return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:8: warning: unsigned int format, int arg (arg 3)
    foo.c:8: warning: unsigned int format, int arg (arg 4)
    foo.c:8: warning: unsigned int format, int arg (arg 5)
    foo.c:12: warning: unsigned int format, int arg (arg 3)
    foo.c:12: warning: unsigned int format, int arg (arg 4)
    foo.c:12: warning: unsigned int format, int arg (arg 5)
    foo.c:16: warning: ISO C90 forbids mixed declarations and code
    foo.c:19: warning: int format, double arg (arg 2)
    Both your sscanf and printf argument conversions are broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    thank u for ur rely
    is it necessary to give the value of tm_year,month,day.
    i only intialised sec,min,hour,
    so that it's taking default year,month,day.that's why iam getting garbage values.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    struct tm ctime = { 0 };
    Something basic as making sure your variables are initialised.

    Man.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  2. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  3. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. Fastest STL container?
    By Shakti in forum C++ Programming
    Replies: 18
    Last Post: 02-17-2006, 02:07 AM