You are not logged in.

#1 2013-05-25 13:37:48

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

[SOLVED] C/C++ beginner, unix time question

Hi

To learn C/C++ I'm trying to make a little space calculator.

First I would like to calculate the julian date of a given date. Using the Unix time this should be fairly simple.
(Ignoring calendar changes etc. for now.)

This is what I did so far:

// Setting the time manually
    struct tm str_time;
    time_t unix_time; // (time in seconds from 1. Jan 1970 00:00:00 GMT)

    str_time.tm_year = 1970 - 1900; // (years from 1900)
    str_time.tm_mon = 1 - 1; // (month starting at 0)
    str_time.tm_mday = 1;
    str_time.tm_hour = 00;
    str_time.tm_min = 00;
    str_time.tm_sec = 00;
    str_time.tm_isdst = 0;
    
    unix_time = mktime(&str_time);

    printf("Given time is: %s\n", ctime(&unix_time));   
    printf("Unix time: %i s\n", unix_time);     

ctime returns the correct given date Jan 1 00:00:00 1970.
But the unix_time is -3600.

Why is there an error of 1h/Shouldn't the unix_time be 0 ?

Is it wrong to initialize tm this very simple way ?

Last edited by rebootl (2013-05-25 16:12:33)


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#2 2013-05-25 14:02:28

portix
Member
Registered: 2009-01-13
Posts: 757

Re: [SOLVED] C/C++ beginner, unix time question

I assume that you are living in CET-timezone. mktime takes a time-structure represented in local time and converts it to calender representation in UTC (=CET+1h). ctime converts it back to local time, that's why you get the correct result where printing unix_time directly will print your localtime (which is 0) - 1 hour = -3600.

Last edited by portix (2013-05-25 14:02:57)

Offline

#3 2013-05-25 14:47:01

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

Re: [SOLVED] C/C++ beginner, unix time question

Thanks. Yes that's correct.

Then I can correct it by simply doing:

int unix_time_corr = unix_time+3600;

However, mktime seems to be limited to 1900-?? or so... so eventually I'll have to find another solution for this...

This is all a bit intransparent to me.

For example when I use str_time.tm_year = 1300 - 1900; The year 1300.

unix_time is 331760832, which is wrong. That's OK cause it's outside the range of mktime (1900-..).
But then ctime returns: Fri Jan  1 12:00:00 1300, the correct year. How is this possible ?


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#4 2013-05-25 15:42:59

Old user, new ID
Member
Registered: 2012-09-18
Posts: 13

Re: [SOLVED] C/C++ beginner, unix time question

rebootl wrote:

Thanks. Yes that's correct.

Then I can correct it by simply doing:

int unix_time_corr = unix_time+3600;

However, mktime seems to be limited to 1900-?? or so... so eventually I'll have to find another solution for this...

This is all a bit intransparent to me.

For example when I use str_time.tm_year = 1300 - 1900; The year 1300.

unix_time is 331760832, which is wrong. That's OK cause it's outside the range of mktime (1900-..).
But then ctime returns: Fri Jan  1 12:00:00 1300, the correct year. How is this possible ?

1st of all, time_t is 'long unsigned', so your second printf()'s output is faulty. You should always compile with at least '-Wall' to detect those errors.

As to why ctime() is returning correct date. My 1st suggestion is to read the man pages carefully.

tm members are "int"s. So, they can accept negative values. ctime(t) is is equivalent to asctime(localtime(t)).

My guess is that localtime(t) internally casts t to a signed value. And then assigns correct values to tm members. If that's the case. Then what's passed to asctime() is correct.

Offline

#5 2013-05-25 16:10:35

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

Re: [SOLVED] C/C++ beginner, unix time question

1st of all, time_t is 'long unsigned', so your second printf()'s output is faulty. You should always compile with at least '-Wall' to detect those errors.

Thanks for this tip. The compiler here reported time_t as long int. Using %li in the printf() the values for unix_time look better now !


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#6 2013-06-07 10:50:04

mthinkcpp
Member
Registered: 2013-05-11
Posts: 23

Re: [SOLVED] C/C++ beginner, unix time question

Language you are programming in; The title should be "C beginner, unix time question".
C and C++ are different. You are programming in C (based on looking at your coding style). As you are new to the area I understand that you may think that they are the same, but if you were programming in C++ your code would look very different.
As an example, a C programmer uses printf, a C++ programmer uses cout<< (which is unsupported by C).
See http://www.stroustrup.com/bs_faq.html#C-slash for an explanation.

Offline

Board footer

Powered by FluxBB