【问题标题】:Time calculations fail in CygwinCygwin 中的时间计算失败
【发布时间】:2011-01-10 15:05:37
【问题描述】:

我编写了一个函数,它根据时间、日期和时区输入创建 time_t。该功能在 Linux 甚至 Solaris 上运行良好。但是我在 Windows 7 上的 Cygwin 中遇到了一个奇怪的行为。在 Cygwin 中,它为每个测试打印相同的时间。

#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;
}

您知道什么可能导致这种(错误)行为吗?

问候,

马丁。

【问题讨论】:

    标签: c windows-7 time cygwin


    【解决方案1】:

    Windows 本机将本地时间放在任何地方(Cygwin 也遵循它),除非您明确询问 UTC 时间。这不是您的不当行为,您的代码是正确的。你可以google到更多的细节。

    作为方向,您可以尝试深入研究 python(java, ruby​​, php,....) lang 的源代码,以检查它是如何工作的,以便做出正确的决定。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 2015-07-07
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多