Hye i am running difftime tu different 2 time values, but unfortunately when i run on my laptop it does return values, but when i pass it to my friend the value returned is 0... and also when i run in windows the value returned also zero.. here is a part of my code:

FILE *cfPtr;
        struct budget newBudget={0, "", 0};
        if ((cfPtr = fopen("parking_budget.dat", "rb"))==NULL) {

        printf("File Could Not Open.\n");}
        else{
        printf("Enter parking no : ");
        int p;
        scanf("%d", &p);
            fseek(cfPtr, (p-1) * sizeof(struct budget), SEEK_SET);
            fread(&newBudget, sizeof (struct budget), 1, cfPtr);
            time_t rawtime, timein, timeout;
            struct tm * timeinfo;
            int year, month, day, hour, min;
            double dif;
            printf("Date and Time (year/month/day hour:minute) (EG: 2012/6/30 23:11) : ");
            fscanf(stdin, "%d/%d/%d %d:%d", &year, &month, &day, &hour, &min);

            timein = mktime(newBudget.timeinfo);
            printf("Car Number : %s\n", newBudget.plat);
            printf("Date & time of entry : %d/%d/%d at %d:%d\n", newBudget.timeinfo->tm_year, newBudget.timeinfo->tm_mon, newBudget.timeinfo->tm_mday, newBudget.timeinfo->tm_hour, newBudget.timeinfo->tm_min);

            time ( &rawtime );
            timeinfo = localtime ( &rawtime );
            timeinfo->tm_year = year;
            timeinfo->tm_mon = month;
            timeinfo->tm_mday = day;
            timeinfo->tm_hour = hour;
            timeinfo->tm_min = min;

            timeout = mktime(timeinfo);
            dif = difftime(timeout, timein);

            printf("Date & time out : %d/%d/%d at %d:%d\n", timeinfo->tm_year, timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min);
            char * weekday[] = { "Sunday", "Monday",
                       "Tuesday", "Wednesday",
                       "Thursday", "Friday", "Saturday"};

            if(weekday[timeinfo->tm_wday]=="Sunday"){
                total=p_time*1.00;  
            }
            else if(weekday[timeinfo->tm_wday]=="Monday"|| "Tuesday"){
                total=p_time*1.00;  
            }
            else{
            total=p_time*2.00;  
            }
            printf("%s",weekday[timeinfo->tm_wday]);
            printf("Parking time : %.2f hours \n", p_time);
            printf("Total amount: Rm %.2f \n", total);      


            fseek(cfPtr, (p-1) * sizeof(struct budget), SEEK_SET);
            fwrite(&newBudget, sizeof (struct budget), 1, cfPtr);
}           
        fclose(cfPtr);

Edited 4 Years Ago by wan632

One thing to note is that difftime() returns a double. If you then cast the result to int, any precision is lost. Another common issue when difftime() appears to return 0 and the dates are clearly different is using the wrong specifier with printf().

Can you post a small, complete program that exhibits the problem. That would be easier than requiring people to extract the relevant parts of your snippet and write a framework around it. Unfortunately, both of those tasks can hide the error on our end, so something that can be cut, paste, and run without changes would be ideal for troubleshooting.

Here is a smaller part:

time_t rawtime, timein, timeout;
double dif;
timein = mktime(newBudget.timeinfo);
timeout = mktime(timeinfo);
dif = difftime(timeout, timein);

total=p_time*1.00;  
printf("Parking time : %.2f hours \n", p_time);

What part of "small, complete program that exhibits the problem" was unclear? I want to copy the code as-is, paste it into my IDE, and compile it without changing anything.

What part of "small, complete program that exhibits the problem" was unclear?

Considering "Hye i am running difftime tu different 2 time values" my guess would be all of it... ;o)

This article has been dead for over six months. Start a new discussion instead.