Search code examples
cwindows-7timecygwin

Time calculations fail in Cygwin


I wrote a function which creates a time_t from a time, date and time zone input. The function works fine on Linux and even Solaris. But I'm expiriencing a strange behaviour in Cygwin on Windows 7. In Cygwin it prints the same time for each test.

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

time_t create_time( const char* time_s, const char* date_s, const char* zone_s ) {
    time_t now;
    char *old_tz;
    struct tm *comptime;
    int x, y, z;

    time( &now );
    old_tz = getenv( "TZ" );

    if ( setenv("TZ", zone_s, 1) ) {
        printf( "Can't set environment variable TZ to %s\n", zone_s );
        return (time_t)(-1);
    }
    comptime = localtime( &now );

    if ( time_s ) {
        if ( 2 == sscanf(time_s, "%d:%d", &x, &y) ) {
            comptime->tm_hour = x;
            comptime->tm_min = y;
        }
        else {
            return (time_t)(-1);
        }
    }
    if ( date_s ) {
        if ( 3 == sscanf( date_s, "%d-%d-%d", &x, &y, &z ) ) {
            comptime->tm_year = x-1900;
            comptime->tm_mon = y-1;
            comptime->tm_mday = z;
        }
        else {
            return (time_t)(-1);
        }
    }
    comptime->tm_sec = 0;

    now = mktime( comptime );

    if ( old_tz ) setenv( "TZ", old_tz, 1 );
    else unsetenv( "TZ" );

    return now;
}

int main( int argc, char** argv ) {
    char buffer1[32];
    char buffer2[32];
    char buffer3[32];
    char *time_s;
    char *date_s;
    char *zone_s;
    int offset;
    time_t rawtime;

    time( &rawtime );
    printf( "Local time: %s", asctime( localtime(&rawtime) ) );
    printf( "GMT time: %s", asctime( gmtime(&rawtime) ) );

    /* Test first version */
    puts("\nTest 1");
    strcpy( buffer1, "11:30" );
    strcpy( buffer2, "2011-01-07" );
    strcpy( buffer3, "CET" );
    offset = 60;

    time_s = buffer1;
    date_s = buffer2;
    zone_s = buffer3;

    printf( "Input: %s %s %s\n", date_s, time_s, zone_s );
    rawtime = create_time( time_s, date_s, zone_s );

    if ( (time_t)(-1) == rawtime ) {
        strcpy( buffer1, "Error in time expression\n" );
    }
    else {
        strcpy( buffer1, ctime(&rawtime) );
    }
    printf( "Local time (%s): %s", zone_s, buffer1 );

    /* Test second version */
    puts("\nTest 2");
    strcpy( buffer1, "11:30" );
    strcpy( buffer2, "2011-01-07" );
    strcpy( buffer3, "GMT" );
    printf( "Input: %s %s %s\n", date_s, time_s, zone_s );
    rawtime = create_time( time_s, date_s, zone_s );

    if ( (time_t)(-1) == rawtime ) {
        strcpy( buffer1, "Error in time expression\n" );
    }
    else {
        strcpy( buffer1, ctime(&rawtime) );
    }
    printf( "Local time (%s): %s", zone_s, buffer1 );

    return 0;
}

Do you have any idea what could cause this (mis)behaviour?

Regards,

Martin.


Solution

  • Windows natively put local time everywhere (and Cygwin follows it), except you'll ask UTC time explicitly. This is not your misbehavior, your code is correct. You can google to more details about.

    As direction you can try to dig into .. source code of python(java, ruby, php,....) lang to check how it works there to make right decision.