Doubt about date manipulation using time. h

Asked

Viewed 68 times

0

I am studying C and thinking about a problem I came across the need to study the time library. h.

Basically, I would like to calculate the difference between two dates without having to reinvent the wheel. For this I considered 2 hypothetical dates:

04/12/2018 12:00

04/12/2018 13:00

I hope the difference between these two dates is 1 hour (3600 seconds).

For this, I am using the following routine:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void){
    time_t seconds, sec1, sec2;

    struct tm *dataTimeBegin = (struct tm *)malloc(sizeof(struct tm));
    struct tm *dataTimeEnd = (struct tm *)malloc(sizeof(struct tm));

    dataTimeBegin->tm_mday = 4;
    dataTimeBegin->tm_mon = 11;
    dataTimeBegin->tm_year = 2018 - 1900;
    dataTimeBegin->tm_hour = 12;
    dataTimeBegin->tm_min = 00;
    dataTimeBegin->tm_sec = 00;

    dataTimeEnd->tm_mday = 4;
    dataTimeEnd->tm_mon = 11;
    dataTimeEnd->tm_year = 2018 - 1900;
    dataTimeEnd->tm_hour = 13;
    dataTimeEnd->tm_min = 00;
    dataTimeEnd->tm_sec = 00;

    sec1 = mktime(dataTimeBegin);
    sec2 = mktime(dataTimeEnd);

    printf("sec1: %ld\n", sec1);
    printf("sec2: %ld\n", sec2);

    printf("difftime(sec2, sec1): %f", difftime(sec2, sec1));

    return 0;
}

Bruise, I checked for strange behavior. My exit indicates:

sec1: 1543935600

sec2: 1543935600

difftime(sec2, sec1): 0.000000

Why sec2 is equal sec1? When I do:

    dataTimeBegin->tm_mday = 4;
    dataTimeBegin->tm_mon = 11;
    dataTimeBegin->tm_year = 2018 - 1900;
    dataTimeBegin->tm_hour = 13;
    dataTimeBegin->tm_min = 00;
    dataTimeBegin->tm_sec = 00;

    dataTimeEnd->tm_mday = 4;
    dataTimeEnd->tm_mon = 11;
    dataTimeEnd->tm_year = 2018 - 1900;
    dataTimeEnd->tm_hour = 13;
    dataTimeEnd->tm_min = 00;
    dataTimeEnd->tm_sec = 00;

My way out is:

sec1: 1543939200

sec2: 1543935600

difftime(sec2, sec1): -3600.000000

This behavior doesn’t make sense to me. I’m stumbling on some concept?

EDIT: I got home and came to test, I will add two images (I got to modify the code not to use pointer to struct tm) and now when I use the pointer is working correctly and when I use the direct struct goes wrong.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

EDIT 2: I followed the zentrunix recommendation and initialized tm_isdst and it worked in both versions (using the pointer or without)

    1. Why are you using declaring the structures tm as pointers ? do not need. 2. I did the test here with your example and it ran drain as expected.. ,the impression you get is that you reversed the tests...
  • I used pointers for tm because in the application I’m thinking it will be necessary. Here’s just an initial part I started testing. I just tested the same code in linux environment and it worked. When I am in vs code + mingw it results exactly in the output I posted. I came to a linux VM, ran and it worked. is there any peculiarity that I’m not paying attention to? Was it some cache?

  • But you are compiling as ? With gcc ? What exact command do you use ? Since it indicates different behaviors in different systems, it is important to know exactly how you are compiling.

  • Hey @Isac, I’ll check. But my visual studio code is in a windows environment with Mingw installed. In the linux environment I ran "gcc test. c -o test". Regarding the compilation in windows environment, I will check as soon as I get home, but I believe it is the same thing, but with the . exe "gcc test. c -o test.exe".

  • Initialize the daylight saving time flag (Daylight saving time), this is possible. (tm_isdst field of the tm structure).

  • Dear @zentrunix, I edited my question showing the print. It happens that using pointer worked (yesterday was not working) and using direct struct did not work (sec1 and sec2 returned equal). I did what you said, started tm_isdst and now it worked in both cases. Thank you.

  • Don’t put the solution in the question, that’s not how the site works. Create an answer to the question by explaining what you did to solve the problem.

Show 2 more comments

1 answer

-1

Try to allocate the tm structures using calloc, or wipe them completely using bzero, the structure has other fields than what you filled in and some dirt in them may be influencing the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.