Cygwin 中的时间计算失败
我编写了一个函数,它根据时间、日期和时区输入创建 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;
}
您知道什么可能导致这种(错误)行为吗?
问候,
马丁。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Windows 本身将本地时间放在任何地方(Cygwin 也遵循它),除非您明确询问 UTC 时间。这不是你的不当行为,你的代码是正确的。你可以google一下,了解更多详情。
作为指导,您可以尝试深入研究 .. python(java, ruby, php,....) lang 的源代码,以检查它是如何工作的以做出正确的决定。
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.