ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

timezone.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   LGPL, See LGPL.txt in the top level directory
00003  * PROJECT:     ReactOS CRT library
00004  * FILE:        lib/sdk/crt/time/timezone.c
00005  * PURPOSE:     Implementation of time zone functions
00006  * PROGRAMERS:  Timo Kreuzer
00007  */
00008 #include "precomp.h"
00009 
00010 char _tz_is_set = 0;
00011 
00012 /* buffers must hold 64 characters! */
00013 static char tz_name[64] = "PST";
00014 static char tz_dst_name[64] = "PDT";
00015 
00016 long dst_begin = 0;
00017 long dst_end = 0;
00018 
00019 /******************************************************************************
00020  * \var _tzname
00021  */
00022 char * _tzname[2] = {
00023   tz_name,
00024   tz_dst_name,
00025 };
00026 
00027 /******************************************************************************
00028  * \var _daylight
00029  */
00030 int _daylight = 0;
00031 
00032 /******************************************************************************
00033  * \name __p__daylight
00034  * \brief Returns a pointer to the _daylight variable;
00035  */
00036 void *
00037 __p__daylight(void)
00038 {
00039    return &_daylight;
00040 }
00041 
00042 /******************************************************************************
00043  * \var _timezone
00044  * \brief
00045  */
00046 long _timezone = 28800;
00047 
00048 /******************************************************************************
00049  * \name __p__timezone
00050  * \brief Returns a pointer to the _timezone variable;
00051  */
00052 long *
00053 __p__timezone(void)
00054 {
00055    return &_timezone;
00056 }
00057 
00058 /******************************************************************************
00059  * \var _dstbias
00060  * \brief
00061  */
00062 long _dstbias = 0;
00063 
00064 /******************************************************************************
00065  * \name __p__dstbias
00066  * \brief Returns a pointer to the _dstbias variable;
00067  */
00068 long *
00069 __p__dstbias(void)
00070 {
00071     return &_dstbias;
00072 }
00073 
00074 /******************************************************************************
00075  * \name __p__tzname
00076  * \brief Returns a pointer to the _tzname buffer;
00077  */
00078 char **
00079 __p__tzname(void)
00080 {
00081     return _tzname;
00082 }
00083 
00084 /******************************************************************************
00085  * \name _tzset
00086  * \brief Initializes the variables _daylight, _timezone, and _tzname from the
00087  *        "TZ" environment variable if available or else by calling
00088  *        GetTimeZoneInformation.
00089  * \sa http://msdn.microsoft.com/en-us/library/90s5c885.aspx
00090  */
00091 void
00092 _tzset(void)
00093 {
00094     const char * str;
00095 
00096     if (_tz_is_set)
00097     {
00098         return;
00099     }
00100 
00101     /* Try to read the timezone from environment */
00102     str = getenv("TZ");
00103     if (str && str[0] != 0)
00104     {
00105         long hour = 0, min = 0, sec = 0;
00106         size_t len = strnlen(str, 16);
00107         int sign = 1;
00108 
00109         dst_begin = 0;
00110 
00111         for (;;)
00112         {
00113             /* Copy timezone name */
00114             strncpy(tz_name, str, 3);
00115             str += 3;
00116             len -= 3;
00117 
00118             if (len < 1) break;
00119 
00120             if (*str == '+' || *str == '-')
00121             {
00122                 sign = *str == '-' ? -1 : 1;
00123                 str++;
00124                 len--;
00125             }
00126 
00127             if (len < 1) break;
00128 
00129             hour = atol(str);
00130 
00131             while (*str != 0 && *str != ':') str++;
00132             if (*str == 0) break;
00133 
00134             min = atol(++str);
00135 
00136             while (*str != 0 && *str != ':') str++;
00137             if (*str == 0) break;
00138 
00139             sec = atol(++str);
00140 
00141             while (*str != 0 && *str <= '9') str++;
00142             if (*str == 0) break;
00143 
00144             /* Copy DST name */
00145             strncpy(tz_dst_name, str, 3);
00146 
00147             // FIXME: set dst_begin etc
00148 
00149             /* We are finished */
00150             break;
00151         }
00152 
00153         _timezone = sign * (((hour * 60) + min) * 60 + sec);
00154 
00155     }
00156     else
00157     {
00158         TIME_ZONE_INFORMATION tzi;
00159         DWORD ret;
00160 
00161         ret = GetTimeZoneInformation(&tzi);
00162         if (ret == TIME_ZONE_ID_INVALID)
00163         {
00164             return;
00165         }
00166 
00167         ret = WideCharToMultiByte(CP_ACP,
00168                                   0,
00169                                   tzi.StandardName,
00170                                   -1,
00171                                   tz_name,
00172                                   sizeof(tz_name),
00173                                   NULL,
00174                                   NULL);
00175 
00176         ret = WideCharToMultiByte(CP_ACP,
00177                                   0,
00178                                   tzi.DaylightName,
00179                                   -1,
00180                                   tz_dst_name,
00181                                   sizeof(tz_dst_name),
00182                                   NULL,
00183                                   NULL);
00184 
00185         _timezone = tzi.Bias * 60;
00186 
00187         if (tzi.DaylightDate.wMonth)
00188         {
00189             struct tm _tm;
00190 
00191             _daylight = 1;
00192             _dstbias = (tzi.DaylightBias - tzi.StandardBias) * 60;
00193             _tm.tm_year = 70;
00194             _tm.tm_mon = tzi.DaylightDate.wMonth - 1;
00195             _tm.tm_mday = tzi.DaylightDate.wDay;
00196             _tm.tm_hour = tzi.DaylightDate.wHour;
00197             _tm.tm_min = tzi.DaylightDate.wMinute;
00198             _tm.tm_sec = tzi.DaylightDate.wSecond;
00199             dst_begin = (long)_mkgmtime(&_tm);
00200             _tm.tm_mon = tzi.StandardDate.wMonth - 1;
00201             _tm.tm_mday = tzi.StandardDate.wDay;
00202             _tm.tm_hour = tzi.StandardDate.wHour;
00203             _tm.tm_min = tzi.StandardDate.wMinute;
00204             _tm.tm_sec = tzi.StandardDate.wSecond;
00205             dst_end = (long)_mkgmtime(&_tm);
00206         }
00207         else
00208         {
00209             _daylight = 0;
00210             _dstbias = 0;
00211         }
00212 
00213     }
00214     _tz_is_set = 1;
00215 }
00216 
00217 /*********************************************************************
00218  *      _get_tzname (MSVCRT.@)
00219  */
00220 int CDECL _get_tzname(size_t *ret, char *buf, size_t bufsize, int index)
00221 {
00222     char *timezone;
00223 
00224     switch(index)
00225     {
00226     case 0:
00227         timezone = tz_name;
00228         break;
00229     case 1:
00230         timezone = tz_dst_name;
00231         break;
00232     default:
00233         *_errno() = EINVAL;
00234         return EINVAL;
00235     }
00236 
00237     if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
00238     {
00239         *_errno() = EINVAL;
00240         return EINVAL;
00241     }
00242 
00243     *ret = strlen(timezone)+1;
00244     if(!buf && !bufsize)
00245         return 0;
00246 
00247     strcpy(buf, timezone);
00248     return 0;
00249 }

Generated on Sat May 26 2012 04:19:48 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.