Cygwin中的时间计算失败
我写了一个函数,它根据时间、日期和时区输入创建一个时间。该函数在Linux甚至Solaris上运行良好。但我在Windows7上看到了Cygwin的一个奇怪行为。在Cygwin中,它为每个测试打印相同的时间Cygwin中的时间计算失败,c,windows-7,time,cygwin,C,Windows 7,Time,Cygwin,我写了一个函数,它根据时间、日期和时区输入创建一个时间。该函数在Linux甚至Solaris上运行良好。但我在Windows7上看到了Cygwin的一个奇怪行为。在Cygwin中,它为每个测试打印相同的时间 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> time_t create_time( const char* time_s, cons
#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;
}
#包括
#包括
#包括
#包括
创建时间(常量字符*时间,常量字符*日期,常量字符*区域){
现在是时候了;
char*old_tz;
struct tm*comptime;
int x,y,z;
时间(现在);
old_tz=getenv(“tz”);
if(setenv(“TZ”,1区)){
printf(“无法将环境变量TZ设置为%s\n”,区域_s);
返回时间(t)(-1);
}
comptime=localtime(&now);
如果(时间){
如果(2==sscanf(时间,%d:%d,&x,&y)){
comptime->tm_hour=x;
comptime->tm_min=y;
}
否则{
返回时间(t)(-1);
}
}
如果(日期){
如果(3==sscanf(日期,%d-%d-%d“,&x,&y,&z)){
comptime->tm_year=x-1900;
comptime->tm_mon=y-1;
comptime->tm_mday=z;
}
否则{
返回时间(t)(-1);
}
}
comptime->tm_sec=0;
现在=mktime(comptime);
if(old_tz)setenv(“tz”,old_tz,1);
其他不确定因素(“TZ”);
现在就回来;
}
int main(int argc,字符**argv){
字符缓冲区1[32];
字符缓冲区2[32];
char-buffer3[32];
字符*时间;
字符*日期;
字符*分区;
整数偏移量;
时间与时间;
时间(&rawtime);
printf(“本地时间:%s”,asctime(本地时间(&rawtime));
printf(“格林尼治标准时间:%s”,asctime(格林尼治标准时间(&rawtime));
/*测试第一版*/
卖出(“\n测试1”);
strcpy(buffer1,“11:30”);
strcpy(buffer2,“2011-01-07”);
strcpy(缓冲区3,“CET”);
偏移量=60;
时间=1;
日期=2;
分区_s=缓冲区3;
printf(“输入:%s%s%s\n”,日期、时间、区域);
rawtime=创建时间(时间、日期、区域);
如果((时间)(-1)=原始时间){
strcpy(buffer1,“时间表达式中的错误”);
}
否则{
strcpy(buffer1、ctime和rawtime);
}
printf(“本地时间(%s):%s”,分区,缓冲区1);
/*测试第二版本*/
卖出(“\n测试2”);
strcpy(buffer1,“11:30”);
strcpy(buffer2,“2011-01-07”);
strcpy(3,格林尼治标准时间);
printf(“输入:%s%s%s\n”,日期、时间、区域);
rawtime=创建时间(时间、日期、区域);
如果((时间)(-1)=原始时间){
strcpy(buffer1,“时间表达式中的错误”);
}
否则{
strcpy(buffer1、ctime和rawtime);
}
printf(“本地时间(%s):%s”,分区,缓冲区1);
返回0;
}
你知道是什么导致了这种(错误的)行为吗
问候,
Martin.Windows本机将本地时间放在所有地方(Cygwin紧随其后),除非您明确询问UTC时间。这不是你的错误行为,你的代码是正确的。你可以在谷歌上搜索到更多的细节
作为你可以尝试挖掘的方向。。python(java、ruby、php等)lang的源代码,以检查它如何在那里工作以做出正确的决定。因此,基本上,mktime()忽略Cygwin中的TZ值,因为它映射到Windows调用。对吗?没有操作系统调用Windows操作系统下的脚本语言存在许多时间问题