Thread: difftime issues

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Question difftime issues

    Code:
    #include<iostream.h>
    #include<time.h>
    
    int main()
    {
        // Set first date (22/2/1998):
        struct tm date1_struct={0};
        time_t    date1_t;
        date1_struct.tm_year = (1998 - 1900);
        date1_struct.tm_mon  = 2;
        date1_struct.tm_mday  = 22;
        date1_t = mktime(&date1_struct);
    
        // Set second date (1/3/1998):
        struct tm date2_struct={0};
        time_t    date2_t;
        date2_struct.tm_year = (1998 - 1900);
        date2_struct.tm_mon  = 3;
        date2_struct.tm_mday  = 1;
        date2_t = mktime(&date2_struct);
    
        // Display difference:
        cout << "Difference in days: " 
             << (((difftime(date2_t, date1_t)/60)/60)/24) << endl;
    
        return 0;
    }
    returns a value of 10 instead of 7.... I have a funny feeling I am missing something very small here.....

    Thanks for the help!!

    Yes, this is homework!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That months count from 0 (not 1 as you expect), so you're really looking at march to april, not february to march
    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. iterator issues
    By Elkvis in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2009, 09:52 PM
  2. Solution to culling issues?
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 03-14-2006, 06:59 PM
  3. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM
  4. Help: using time difftime function
    By fe344 in forum C Programming
    Replies: 5
    Last Post: 11-13-2002, 08:08 PM
  5. difftime? i have a ?
    By correlcj in forum C Programming
    Replies: 1
    Last Post: 08-12-2002, 05:55 PM