localtime返回在cygwin shell上运行的Windows程序的GMT

Raf*_*sta 7 c c++ time cygwin

请考虑以下代码:

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

如果我使用MSVC构建此代码,并在Windows DOS shell下运行它,我得到正确的本地时间:

timezone: , current local time: Wed Jul 25 13:05:08 2012
Run Code Online (Sandbox Code Playgroud)

但是如果我在像bash这样的cygwin shell下运行相同的程序,这段代码会返回GMT!

timezone: America/New_York, current local time: Wed Jul 25 18:05:08 2012
Run Code Online (Sandbox Code Playgroud)

如果我在Linux或OsX中运行此程序,它也会返回正确的本地时间.

为什么?

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

似乎对某些程序来说,未设置TZ并不总是有效.我不知道为什么.但是有一个繁琐的解决方法.基本上,在您取消设置TZ之后,您必须检查本地时间确实不再返回GMT,但仅当您实际上不在GMT时区时,并且当您调用localtime()时计算对time_t的手动调整或找时间()

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;
}
Run Code Online (Sandbox Code Playgroud)

拨打当地时间:

  time_t t = getAtTimeFromSomewhere();
  t -= localTimeOffset;
  timeInfo = localtime( &t );
Run Code Online (Sandbox Code Playgroud)

致电maketime:

 struct tm timestr;
 makeATMFromAStringForExample( time, timestr );
 time_t timet = mktime( &timestr );
 timet += localTimeOffset;
Run Code Online (Sandbox Code Playgroud)

美好时光.

Raf*_*sta 11

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

POSIX函数localtime会使用环境变量TZ来确定要使用的时区.如果TZ未设置,则将使用系统的默认时区.

如果我在Linux或OS X下运行,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 ));
}
Run Code Online (Sandbox Code Playgroud)