NO

Author Topic: possible bug ... end of month rollover in Time library  (Read 2049 times)

CommonTater

  • Guest
possible bug ... end of month rollover in Time library
« on: February 28, 2011, 04:48:03 PM »
There appears to be a problem with the mktime() and difftime() functions when the time rolls over the end of a month...  

Here's the code that exhibits the problem...
Code: [Select]
// CITY PARKING
#include <stdio.h>
#include <time.h>
// monetary values
#define PRICE_CAR    1.25      // car rate per hour
#define PRICE_TRUCK  2.50      // truck rate per hour
#define GATE_CHARGE  5.00      // flat rate for entry
#define GATE_TIME    30        // flat rate period
#define SALES_TAX    17.5      // tax addon in percent

int _cdecl main( void )
  { int tOnLot = 0;
    float Rate = PRICE_CAR;
    printf("\n\t\t *****  CITY PARKING *****\n\n");
    // get time in and time out
    { struct tm tIn = {0}, tOut = {0};
      printf("Enter times as\tYYYY MM DD HH MM\n");
      printf("Time In   :\t");
      scanf("%d %d %d %d %d",&tIn.tm_year,&tIn.tm_mon,&tIn.tm_mday,
                              &tIn.tm_hour,&tIn.tm_min);
      printf("Time Out  :\t");
      scanf("%d %d %d %d %d",&tOut.tm_year,&tOut.tm_mon,&tOut.tm_mday,
                              &tOut.tm_hour,&tOut.tm_min);
       tOnLot = difftime(mktime(&tOut),mktime(&tIn)) / 60; }  // time in minutes  
    // get vehicle type and set hourly rate
    { getchar();
      printf("\nIs this a Truck? (Y or N) : ");
      int vt = getchar();
      if ( vt == 'y' || vt == 'Y')
        Rate = PRICE_TRUCK; }  
    // display time
    printf("\nTime on Lot : %d minutes\n\n",tOnLot);    
    // calculate billable time
    { float Total = 0;
      float Time = 0;
      float Tax = 0;
      tOnLot -= GATE_TIME;
      printf("Gate Fee  :\t%8.2f\n",GATE_CHARGE);
      if( tOnLot > 0)
        { Time = (tOnLot * Rate) / 60.0;
          printf("Time      :\t%8.2f   (%dmin @ $%.2fhr) \n",Time,tOnLot,Rate); }
      Total = GATE_CHARGE + Time;
      printf("SubTotal  :\t%8.2f\n", Total);
      Tax = Total * (SALES_TAX / 100.0);
      printf("Sales Tax :\t%8.2f\n\n",Tax);
      Total += Tax;
      printf("Total Due :\t%8.2f\n\n",Total); }  
    return 0; }

I'm attaching an image that shows the problem... note that in all cases the "time on lot" should be 10 minutes.
 
« Last Edit: March 01, 2011, 05:33:08 AM by CommonTater »

CommonTater

  • Guest
Re: possible bug ... end of month rollover in Time library
« Reply #1 on: April 02, 2011, 08:39:21 PM »
This can be withdrawn... 

I didn't realize until i tried it with something else that the months run 0 -11 where most people would enter 1 - 12. 

No bug... just one of C's many "gotcha" bits.