R
R
rstepanov2014-10-16 11:38:24
linux
rstepanov, 2014-10-16 11:38:24

Changing clocks in Solaris and localtime/mktime/asctime, what am I doing wrong?

Colleagues, there is such a code in C:

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

int main() {

    time_t t;
    struct tm tm1;

    tzset();

    time(&t);
    localtime_r(&t, &tm1);
    printf("\n now:\t\t\t(%d) %s", t, asctime(&tm1));

    tm1.tm_mday = 26;
    tm1.tm_sec = 59;
    tm1.tm_min = 59;
    tm1.tm_hour = 1;
    tm1.tm_mon = 9;

    t = mktime(&tm1);
    printf(" tm_hour = 1:\t\t(%d) %s", t, asctime(&tm1));

    tm1.tm_mday = 26;
    tm1.tm_sec = 59;
    tm1.tm_min = 59;
    tm1.tm_hour = 0;
    tm1.tm_mon = 9;

    t = mktime(&tm1);
    printf(" tm_hour = 0:\t\t(%d) %s", t, asctime(&tm1));

    t += 3602;

    localtime_r(&t, &tm1);
    printf(" 3602 seconds later:\t(%d) %s", t, asctime(&tm1));

    t -= 3604;

    localtime_r(&t, &tm1);
    printf(" 3604 seconds before:\t(%d) %s\n", t, asctime(&tm1));
}

On RHEL with tzdata 2014h we get the expected results:
now: (1413448301) Thu Oct 16 12:31:41 2014
tm_hour = 1: (1414277999) Sun Oct 26 01:59:59 2014
tm_hour = 0: (1414270799) Sun Oct 26 00 :59:59 2014
3602 seconds later: (1414274401) Sun Oct 26 01:00:01 2014
3604 seconds before: (1414270797) Sun Oct 26 00:59:57 2014
On Solaris 11.2 with timezone 2014f we get some kind of hell:
now: (1413447693) Thu Oct 16 12:21:33 2014
tm_hour = 1: (1414277999) Sun Oct 26 01:59:59 2014
tm_hour = 0: (1414274399) Sun Oct 26 01:59:59 2014
3602 seconds later: (801422 ) Sun Oct 26 02:00:01 2014
3604 seconds before: (1414274397) Sun Oct 26 01:59:57 2014 The
traditional question, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rstepanov, 2014-10-16
@rstepanov

More hell:

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

int main() {

    time_t t;
    struct tm tm1;

    tzset();
    time(&t);
    localtime_r(&t, &tm1);

    tm1.tm_mday = 27;
    tm1.tm_sec = 59;
    tm1.tm_min = 59;
    tm1.tm_hour = 0;
    tm1.tm_mon = 9;

    t = mktime(&tm1);
    printf(" tm_hour = %d:\t\t(%d) %s", tm1.tm_hour, t, asctime(&tm1));

    tm1.tm_mday = 26;
    tm1.tm_sec = 59;
    tm1.tm_min = 59;
    tm1.tm_hour = 0;
    tm1.tm_mon = 9;

    t = mktime(&tm1);
    printf(" tm_hour = %d:\t\t(%d) %s", tm1.tm_hour, t, asctime(&tm1));
}

Result:
./tzcheck3
tm_hour = 0: (1414360799) Mon Oct 27 00:59:59 2014
tm_hour = 1: (1414274399) Sun Oct 26 01:59:59 2014

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question