intl/icu/source/i18n/wintzimpl.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/i18n/wintzimpl.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 +********************************************************************************
     1.6 +*   Copyright (C) 2009-2011, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +********************************************************************************
     1.9 +*
    1.10 +* File WINTZIMPL.CPP
    1.11 +*
    1.12 +********************************************************************************
    1.13 +*/
    1.14 +
    1.15 +#include "unicode/utypes.h"
    1.16 +
    1.17 +#if U_PLATFORM_HAS_WIN32_API
    1.18 +
    1.19 +#include "wintzimpl.h"
    1.20 +
    1.21 +#include "unicode/unistr.h"
    1.22 +#include "unicode/timezone.h"
    1.23 +#include "unicode/basictz.h"
    1.24 +#include "putilimp.h"
    1.25 +#include "uassert.h"
    1.26 +#include "cmemory.h"
    1.27 +
    1.28 +#   define WIN32_LEAN_AND_MEAN
    1.29 +#   define VC_EXTRALEAN
    1.30 +#   define NOUSER
    1.31 +#   define NOSERVICE
    1.32 +#   define NOIME
    1.33 +#   define NOMCX
    1.34 +
    1.35 +#include <windows.h>
    1.36 +
    1.37 +U_NAMESPACE_USE
    1.38 +
    1.39 +static UBool getSystemTimeInformation(TimeZone *tz, SYSTEMTIME &daylightDate, SYSTEMTIME &standardDate, int32_t &bias, int32_t &daylightBias, int32_t &standardBias) {
    1.40 +    UErrorCode status = U_ZERO_ERROR;
    1.41 +    UBool result = TRUE;
    1.42 +    BasicTimeZone *btz = (BasicTimeZone*)tz; // we should check type
    1.43 +    InitialTimeZoneRule *initial = NULL;
    1.44 +    AnnualTimeZoneRule *std = NULL, *dst = NULL;
    1.45 +
    1.46 +    btz->getSimpleRulesNear(uprv_getUTCtime(), initial, std, dst, status);
    1.47 +    if (U_SUCCESS(status)) {
    1.48 +        if (std == NULL || dst == NULL) {
    1.49 +            bias = -1 * (initial->getRawOffset()/60000);
    1.50 +            standardBias = 0;
    1.51 +            daylightBias = 0;
    1.52 +            // Do not use DST.  Set 0 to all stadardDate/daylightDate fields
    1.53 +            standardDate.wYear = standardDate.wMonth  = standardDate.wDayOfWeek = standardDate.wDay = 
    1.54 +            standardDate.wHour = standardDate.wMinute = standardDate.wSecond    = standardDate.wMilliseconds = 0;
    1.55 +            daylightDate.wYear = daylightDate.wMonth  = daylightDate.wDayOfWeek = daylightDate.wDay =
    1.56 +            daylightDate.wHour = daylightDate.wMinute = daylightDate.wSecond    = daylightDate.wMilliseconds = 0;
    1.57 +        } else {
    1.58 +            U_ASSERT(std->getRule()->getDateRuleType() == DateTimeRule::DOW);
    1.59 +            U_ASSERT(dst->getRule()->getDateRuleType() == DateTimeRule::DOW);
    1.60 +
    1.61 +            bias = -1 * (std->getRawOffset()/60000);
    1.62 +            standardBias = 0;
    1.63 +            daylightBias = -1 * (dst->getDSTSavings()/60000);
    1.64 +            // Always use DOW type rule
    1.65 +            int32_t hour, min, sec, mil;
    1.66 +            standardDate.wYear = 0;
    1.67 +            standardDate.wMonth = std->getRule()->getRuleMonth() + 1;
    1.68 +            standardDate.wDay = std->getRule()->getRuleWeekInMonth();
    1.69 +            if (standardDate.wDay < 0) {
    1.70 +                standardDate.wDay = 5;
    1.71 +            }
    1.72 +            standardDate.wDayOfWeek = std->getRule()->getRuleDayOfWeek() - 1;
    1.73 +
    1.74 +            mil = std->getRule()->getRuleMillisInDay();
    1.75 +            hour = mil/3600000;
    1.76 +            mil %= 3600000;
    1.77 +            min = mil/60000;
    1.78 +            mil %= 60000;
    1.79 +            sec = mil/1000;
    1.80 +            mil %= 1000;
    1.81 +
    1.82 +            standardDate.wHour = hour;
    1.83 +            standardDate.wMinute = min;
    1.84 +            standardDate.wSecond = sec;
    1.85 +            standardDate.wMilliseconds = mil;
    1.86 +
    1.87 +            daylightDate.wYear = 0;
    1.88 +            daylightDate.wMonth = dst->getRule()->getRuleMonth() + 1;
    1.89 +            daylightDate.wDay = dst->getRule()->getRuleWeekInMonth();
    1.90 +            if (daylightDate.wDay < 0) {
    1.91 +                daylightDate.wDay = 5;
    1.92 +            }
    1.93 +            daylightDate.wDayOfWeek = dst->getRule()->getRuleDayOfWeek() - 1;
    1.94 +
    1.95 +            mil = dst->getRule()->getRuleMillisInDay();
    1.96 +            hour = mil/3600000;
    1.97 +            mil %= 3600000;
    1.98 +            min = mil/60000;
    1.99 +            mil %= 60000;
   1.100 +            sec = mil/1000;
   1.101 +            mil %= 1000;
   1.102 +
   1.103 +            daylightDate.wHour = hour;
   1.104 +            daylightDate.wMinute = min;
   1.105 +            daylightDate.wSecond = sec;
   1.106 +            daylightDate.wMilliseconds = mil;
   1.107 +        }
   1.108 +    } else {
   1.109 +        result = FALSE;
   1.110 +    }
   1.111 +
   1.112 +    delete initial;
   1.113 +    delete std;
   1.114 +    delete dst;
   1.115 +
   1.116 +    return result;
   1.117 +}
   1.118 +
   1.119 +static UBool getWindowsTimeZoneInfo(TIME_ZONE_INFORMATION *zoneInfo, const UChar *icuid, int32_t length) {
   1.120 +    UBool result = FALSE;
   1.121 +    UnicodeString id = UnicodeString(icuid, length);
   1.122 +    TimeZone *tz = TimeZone::createTimeZone(id);
   1.123 +    
   1.124 +    if (tz != NULL) {
   1.125 +        int32_t bias;
   1.126 +        int32_t daylightBias;
   1.127 +        int32_t standardBias;
   1.128 +        SYSTEMTIME daylightDate;
   1.129 +        SYSTEMTIME standardDate;
   1.130 +
   1.131 +        if (getSystemTimeInformation(tz, daylightDate, standardDate, bias, daylightBias, standardBias)) {
   1.132 +            uprv_memset(zoneInfo, 0, sizeof(TIME_ZONE_INFORMATION)); // We do not set standard/daylight names, so nullify first.
   1.133 +            zoneInfo->Bias          = bias;
   1.134 +            zoneInfo->DaylightBias  = daylightBias;
   1.135 +            zoneInfo->StandardBias  = standardBias;
   1.136 +            zoneInfo->DaylightDate  = daylightDate;
   1.137 +            zoneInfo->StandardDate  = standardDate;
   1.138 +
   1.139 +            result = TRUE;
   1.140 +        }
   1.141 +    }
   1.142 +
   1.143 +    return result;
   1.144 +}
   1.145 +
   1.146 +/*
   1.147 + * Given the timezone icuid, fill in zoneInfo by calling auxillary functions that creates a timezone and extract the 
   1.148 + * information to put into zoneInfo. This includes bias and standard time date and daylight saving date.
   1.149 + */
   1.150 +U_CAPI UBool U_EXPORT2
   1.151 +uprv_getWindowsTimeZoneInfo(TIME_ZONE_INFORMATION *zoneInfo, const UChar *icuid, int32_t length)
   1.152 +{
   1.153 +    if (getWindowsTimeZoneInfo(zoneInfo, icuid, length)) {
   1.154 +        return TRUE;
   1.155 +    } else {
   1.156 +        return FALSE;
   1.157 +    }
   1.158 +}
   1.159 +
   1.160 +#endif

mercurial