本地时间在运行在Cygwin shell上的Windows程序中返回GMT

2024-10-01 17:29:14 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑以下代码:

time_t t;
t = time( NULL );
elog << "timezone: " << getenv( "TZ" ) 
     << ", current local time: " << asctime( localtime( &t ));

如果我使用MSVC生成此代码,并在windows DOS shell下运行,则会获得正确的本地时间:

^{pr2}$

但是如果我在类似于bash的cygwin shell下运行相同的程序,这个代码将返回GMT!在

timezone: America/New_York, current local time: Wed Jul 25 18:05:08 2012

如果我在Linux或OsX上运行这个程序,它也会返回正确的本地时间。在

为什么?在

@Update:一年后的今天,我发现我下面给出的答案并不总是有效的。在

似乎对于某些程序来说,使TZ不稳定并不总是有效的。我不知道为什么。但是有一个麻烦的解决办法。如果你不在本地时间段调整时间,那就调用本地时间调整器()

u64 localTimeOffset = 0;

static void unsetTz()
{
   static bool unsetTZ = false;
   if ( !unsetTZ )
   {
      putenv( "TZ=" );
      unsetTZ = true;

      // unsetting TZ does not always work. So we have to check if it worked
      // and make a manual adjustment if it does not. For long running programs
      // that may span DST changes, this may cause the DST change to not take 
      // effect.
      s32 tzOffset = getTzOffset();

      if ( tzOffset )
      {
         static char timeBuf[48];
         char* s = &(timeBuf[0]);
         struct tm* timeInfoGMT;
         struct tm* timeInfoLocal;

         time_t zero = 86400;
         timeInfoGMT = gmtime( &zero );
         u32 GMTHour = timeInfoGMT->tm_hour;

         timeInfoLocal = localtime( &zero );
         u32 localHour = timeInfoLocal->tm_hour;

         if ( localHour == GMTHour )
         {
            // unsetting tz failed. So we have to make a manual adjustment
            localTimeOffset = tzOffset * 60 * 60;
         }
      }
   }
}

s32 getTzOffset()
{
   TIME_ZONE_INFORMATION tzInfo;
   GetTimeZoneInformation( &tzInfo );
   s32 tz = ( tzInfo.Bias / 60 );
   return tz;
}

拨打当地时间:

  time_t t = getAtTimeFromSomewhere();
  t -= localTimeOffset;
  timeInfo = localtime( &t );

打电话给maketime:

 struct tm timestr;
 makeATMFromAStringForExample( time, timestr );
 time_t timet = mktime( &timestr );
 timet += localTimeOffset;

好时光。在


Tags: to代码程序iftime时间notstatic
3条回答

在Cygwin shell中使用export TZ=""可能是最好的方法。在

@更新
嗨,图纳基。感谢编辑!在

这花了我一些时间才弄明白,我希望它对其他人有用。在

localtime这样的POSIX函数将使用环境变量TZ来确定要使用哪个时区。如果未设置TZ,则将使用系统的默认时区。在

如果我在Linux或osx下运行,TZ设置正确,一切正常。如果我在Windows上的shell中运行这个程序,TZ没有设置,因此函数返回操作系统的默认时区,这将再次产生正确的结果。在

如果我在一个Cygwin shell中运行,TZ被设置了-但是由于我使用MSVC,使用MSVC自己的stdc库来构建程序,它不能解释Cygwin的TZ变量。所以它默认为GMT

如果这个程序是在Cygwin下用GCC构建的,我敢打赌它在Cygwin shell中会正常工作。在

因此,答案是确保在调用POSIX时间函数(如localtime())的程序中,如果希望时间函数在Cygwin shell下工作,则必须取消设置TZ。在

我是这样做的:

void getLocalTime()
{
   #ifdef WIN32
   static bool unsetTZ = false;
   if ( !unsetTZ )
   {
      putenv( "TZ=" );
      unsetTZ = true;
   }
   #endif // !WIN32

   time_t t;
   t = time( NULL );
   elog << "timezone: " << getenv( "TZ" ) 
        << ", current local time: " << asctime( localtime( &t ));
}

尝试在struct tm中设置tm_isdst标志。在

夏令时标志(tm_isdst)如果夏令时有效,则大于零;如果夏令时不生效,则小于零;如果信息不可用,则小于零。在

如果我没记错的话,在Windows中它必须是-1才能正常工作。在

相关问题 更多 >

    热门问题