/src/logging-log4cxx/src/main/cpp/timezone.cpp
| Line | Count | Source | 
| 1 |  | /* | 
| 2 |  |  * Licensed to the Apache Software Foundation (ASF) under one or more | 
| 3 |  |  * contributor license agreements.  See the NOTICE file distributed with | 
| 4 |  |  * this work for additional information regarding copyright ownership. | 
| 5 |  |  * The ASF licenses this file to You under the Apache License, Version 2.0 | 
| 6 |  |  * (the "License"); you may not use this file except in compliance with | 
| 7 |  |  * the License.  You may obtain a copy of the License at | 
| 8 |  |  * | 
| 9 |  |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 |  |  * | 
| 11 |  |  * Unless required by applicable law or agreed to in writing, software | 
| 12 |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
| 13 |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 14 |  |  * See the License for the specific language governing permissions and | 
| 15 |  |  * limitations under the License. | 
| 16 |  |  */ | 
| 17 |  |  | 
| 18 |  | #define __STDC_CONSTANT_MACROS | 
| 19 |  | #include <log4cxx/logstring.h> | 
| 20 |  | #include <log4cxx/helpers/timezone.h> | 
| 21 |  | #include <stdlib.h> | 
| 22 |  |  | 
| 23 |  | #include <apr_time.h> | 
| 24 |  | #include <apr_pools.h> | 
| 25 |  | #include <apr_strings.h> | 
| 26 |  | #include <log4cxx/helpers/exception.h> | 
| 27 |  | #include <log4cxx/helpers/transcoder.h> | 
| 28 |  | #include <log4cxx/helpers/stringhelper.h> | 
| 29 |  | #include <log4cxx/helpers/pool.h> | 
| 30 |  | #include <log4cxx/logger.h> | 
| 31 |  |  | 
| 32 |  | using namespace LOG4CXX_NS; | 
| 33 |  | using namespace LOG4CXX_NS::helpers; | 
| 34 |  |  | 
| 35 |  | IMPLEMENT_LOG4CXX_OBJECT( TimeZone ) | 
| 36 |  |  | 
| 37 |  | namespace LOG4CXX_NS | 
| 38 |  | { | 
| 39 |  | namespace helpers | 
| 40 |  | { | 
| 41 |  | namespace TimeZoneImpl | 
| 42 |  | { | 
| 43 |  | /** Time zone object that represents GMT. */ | 
| 44 |  | class GMTTimeZone : public TimeZone | 
| 45 |  | { | 
| 46 |  |   public: | 
| 47 |  |     /** Class factory. */ | 
| 48 |  |     static const TimeZonePtr& getInstance() | 
| 49 | 0 |     { | 
| 50 | 0 |       static WideLife<TimeZonePtr> tz = std::make_shared<GMTTimeZone>(); | 
| 51 | 0 |       return tz; | 
| 52 | 0 |     } | 
| 53 |  |  | 
| 54 |  |     /** Explode time to human readable form. */ | 
| 55 |  |     log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const | 
| 56 | 0 |     { | 
| 57 | 0 |       apr_status_t stat; | 
| 58 |  |  | 
| 59 |  |       //  APR 1.1 and early mishandles microseconds on dates | 
| 60 |  |       //   before 1970, APR bug 32520 | 
| 61 | 0 |       if (LOG4CXX_UNLIKELY(input < 0 && apr_time_usec(input) < 0)) | 
| 62 | 0 |       { | 
| 63 | 0 |         apr_time_t floorTime = (apr_time_sec(input) - 1) * APR_USEC_PER_SEC; | 
| 64 | 0 |         stat = apr_time_exp_gmt(result, floorTime); | 
| 65 | 0 |         result->tm_usec = (int) (input - floorTime); | 
| 66 | 0 |       } | 
| 67 | 0 |       else | 
| 68 | 0 |       { | 
| 69 | 0 |         stat = apr_time_exp_gmt( result, input ); | 
| 70 | 0 |       } | 
| 71 |  | 
 | 
| 72 | 0 |       return stat; | 
| 73 | 0 |     } | 
| 74 |  |  | 
| 75 | 0 |     GMTTimeZone() : TimeZone( LOG4CXX_STR("GMT") ) | 
| 76 | 0 |     { | 
| 77 | 0 |     } | 
| 78 |  | }; | 
| 79 |  |  | 
| 80 |  |  | 
| 81 |  |  | 
| 82 |  | /** Time zone object that represents GMT. */ | 
| 83 |  | class LocalTimeZone : public TimeZone | 
| 84 |  | { | 
| 85 |  |   public: | 
| 86 |  |     /** Class factory. */ | 
| 87 |  |     static const TimeZonePtr& getInstance() | 
| 88 | 0 |     { | 
| 89 | 0 |       static WideLife<TimeZonePtr> tz = std::make_shared<LocalTimeZone>(); | 
| 90 | 0 |       return tz; | 
| 91 | 0 |     } | 
| 92 |  |  | 
| 93 |  |     /** Explode time to human readable form. */ | 
| 94 |  |     log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const | 
| 95 | 0 |     { | 
| 96 | 0 |       apr_status_t stat; | 
| 97 |  |  | 
| 98 |  |       //  APR 1.1 and early mishandles microseconds on dates | 
| 99 |  |       //   before 1970, APR bug 32520 | 
| 100 | 0 |       if (LOG4CXX_UNLIKELY(input < 0 && apr_time_usec(input) < 0)) | 
| 101 | 0 |       { | 
| 102 | 0 |         apr_time_t floorTime = (apr_time_sec(input) - 1) * APR_USEC_PER_SEC; | 
| 103 | 0 |         stat = apr_time_exp_lt(result, floorTime); | 
| 104 | 0 |         result->tm_usec = (int) (input - floorTime); | 
| 105 | 0 |       } | 
| 106 | 0 |       else | 
| 107 | 0 |       { | 
| 108 | 0 |         stat = apr_time_exp_lt( result, input ); | 
| 109 | 0 |       } | 
| 110 |  | 
 | 
| 111 | 0 |       return stat; | 
| 112 | 0 |     } | 
| 113 |  |  | 
| 114 |  |  | 
| 115 | 0 |     LocalTimeZone() : TimeZone( getTimeZoneName() ) | 
| 116 | 0 |     { | 
| 117 | 0 |     } | 
| 118 |  |  | 
| 119 |  |   private: | 
| 120 |  |     static const LogString getTimeZoneName() | 
| 121 | 0 |     { | 
| 122 | 0 |       const int MAX_TZ_LENGTH = 255; | 
| 123 | 0 |       char tzName[MAX_TZ_LENGTH]; | 
| 124 | 0 |       apr_size_t tzLength; | 
| 125 | 0 |       apr_time_exp_t tm; | 
| 126 | 0 |       apr_time_exp_lt(&tm, 0); | 
| 127 | 0 |       apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%Z", &tm); | 
| 128 |  | 
 | 
| 129 | 0 |       if (tzLength == 0) | 
| 130 | 0 |       { | 
| 131 | 0 |         apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%z", &tm); | 
| 132 | 0 |       } | 
| 133 |  | 
 | 
| 134 | 0 |       tzName[tzLength] = 0; | 
| 135 | 0 |       LogString retval; | 
| 136 | 0 |       LOG4CXX_NS::helpers::Transcoder::decode(tzName, retval); | 
| 137 | 0 |       return retval; | 
| 138 | 0 |     } | 
| 139 |  |  | 
| 140 |  | }; | 
| 141 |  |  | 
| 142 |  |  | 
| 143 |  |  | 
| 144 |  | /** Time zone object that represents a fixed offset from GMT. */ | 
| 145 |  | class FixedTimeZone : public TimeZone | 
| 146 |  | { | 
| 147 |  |   public: | 
| 148 | 0 |     FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 ) | 
| 149 | 0 |     { | 
| 150 | 0 |     } | 
| 151 |  |  | 
| 152 |  |     /** Explode time to human readable form. */ | 
| 153 |  |     log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const | 
| 154 | 0 |     { | 
| 155 | 0 |       apr_status_t stat; | 
| 156 |  |  | 
| 157 |  |       //  APR 1.1 and early mishandles microseconds on dates | 
| 158 |  |       //   before 1970, APR bug 32520 | 
| 159 | 0 |       if (LOG4CXX_UNLIKELY(input < 0 && apr_time_usec(input) < 0)) | 
| 160 | 0 |       { | 
| 161 | 0 |         apr_time_t floorTime = (apr_time_sec(input) - 1) * APR_USEC_PER_SEC; | 
| 162 | 0 |         stat = apr_time_exp_tz(result, floorTime, offset); | 
| 163 | 0 |         result->tm_usec = (int) (input - floorTime); | 
| 164 | 0 |       } | 
| 165 | 0 |       else | 
| 166 | 0 |       { | 
| 167 | 0 |         stat = apr_time_exp_tz( result, input, offset ); | 
| 168 | 0 |       } | 
| 169 |  | 
 | 
| 170 | 0 |       return stat; | 
| 171 | 0 |     } | 
| 172 |  |  | 
| 173 |  |  | 
| 174 |  |   private: | 
| 175 |  |     const apr_int32_t offset; | 
| 176 |  | }; | 
| 177 |  |  | 
| 178 |  | } | 
| 179 |  | } | 
| 180 |  | } | 
| 181 |  |  | 
| 182 |  |  | 
| 183 |  |  | 
| 184 | 0 | TimeZone::TimeZone( const LogString& id1 ) : id( id1 ) | 
| 185 | 0 | { | 
| 186 | 0 | } | 
| 187 |  |  | 
| 188 |  | TimeZone::~TimeZone() | 
| 189 | 0 | { | 
| 190 | 0 | } | 
| 191 |  |  | 
| 192 |  | const TimeZonePtr& TimeZone::getDefault() | 
| 193 | 0 | { | 
| 194 | 0 |   return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance(); | 
| 195 | 0 | } | 
| 196 |  |  | 
| 197 |  | const TimeZonePtr& TimeZone::getGMT() | 
| 198 | 0 | { | 
| 199 | 0 |   return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance(); | 
| 200 | 0 | } | 
| 201 |  |  | 
| 202 |  | const TimeZonePtr TimeZone::getTimeZone( const LogString& id ) | 
| 203 | 0 | { | 
| 204 | 0 |   const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 }; | 
| 205 |  | 
 | 
| 206 | 0 |   if ( id == gmt ) | 
| 207 | 0 |   { | 
| 208 | 0 |     return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance(); | 
| 209 | 0 |   } | 
| 210 |  |  | 
| 211 | 0 |   if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt ) | 
| 212 | 0 |   { | 
| 213 | 0 |     int hours = 0; | 
| 214 | 0 |     int minutes = 0; | 
| 215 | 0 |     int sign = 1; | 
| 216 |  | 
 | 
| 217 | 0 |     if (id[3] == 0x2D /* '-' */) | 
| 218 | 0 |     { | 
| 219 | 0 |       sign = -1; | 
| 220 | 0 |     } | 
| 221 |  | 
 | 
| 222 | 0 |     LogString off( id.substr( 4 ) ); | 
| 223 |  | 
 | 
| 224 | 0 |     if ( id.length() >= 7 ) | 
| 225 | 0 |     { | 
| 226 | 0 |       size_t colonPos = off.find( 0x3A /* ':' */); | 
| 227 |  | 
 | 
| 228 | 0 |       if ( colonPos == LogString::npos ) | 
| 229 | 0 |       { | 
| 230 | 0 |         minutes = StringHelper::toInt(off.substr(off.length() - 2)); | 
| 231 | 0 |         hours = StringHelper::toInt(off.substr(0, off.length() - 2)); | 
| 232 | 0 |       } | 
| 233 | 0 |       else | 
| 234 | 0 |       { | 
| 235 | 0 |         minutes = StringHelper::toInt(off.substr(colonPos + 1)); | 
| 236 | 0 |         hours = StringHelper::toInt(off.substr(0, colonPos)); | 
| 237 | 0 |       } | 
| 238 | 0 |     } | 
| 239 | 0 |     else | 
| 240 | 0 |     { | 
| 241 | 0 |       hours = StringHelper::toInt(off); | 
| 242 | 0 |     } | 
| 243 |  |  | 
| 244 |  |     // Make sure that our offset can't be crazy | 
| 245 | 0 |     if( hours < -12 || 14 < hours) | 
| 246 | 0 |     { | 
| 247 | 0 |       throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)")); | 
| 248 | 0 |     } | 
| 249 | 0 |     if (minutes < 0 || 60 < minutes) | 
| 250 | 0 |     { | 
| 251 | 0 |       throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)")); | 
| 252 | 0 |     } | 
| 253 |  |  | 
| 254 | 0 |     LogString s(gmt); | 
| 255 | 0 |     Pool p; | 
| 256 | 0 |     LogString hh; | 
| 257 | 0 |     StringHelper::toString(hours, p, hh); | 
| 258 |  | 
 | 
| 259 | 0 |     if (sign > 0) | 
| 260 | 0 |     { | 
| 261 | 0 |       s.append(1, (logchar) 0x2B /* '+' */); | 
| 262 | 0 |     } | 
| 263 | 0 |     else | 
| 264 | 0 |     { | 
| 265 | 0 |       s.append(1, (logchar) 0x2D /* '-' */); | 
| 266 | 0 |     } | 
| 267 |  | 
 | 
| 268 | 0 |     if (hh.length() == 1) | 
| 269 | 0 |     { | 
| 270 | 0 |       s.append(1, (logchar) 0x30 /* '0' */); | 
| 271 | 0 |     } | 
| 272 |  | 
 | 
| 273 | 0 |     s.append(hh); | 
| 274 | 0 |     s.append(1, (logchar) 0x3A /*' :' */); | 
| 275 | 0 |     LogString mm; | 
| 276 | 0 |     StringHelper::toString(minutes, p, mm); | 
| 277 |  | 
 | 
| 278 | 0 |     if (mm.length() == 1) | 
| 279 | 0 |     { | 
| 280 | 0 |       s.append(1, (logchar) 0x30 /* '0' */); | 
| 281 | 0 |     } | 
| 282 |  | 
 | 
| 283 | 0 |     s.append(mm); | 
| 284 | 0 |     apr_int32_t offset = sign * (hours * 3600 + minutes * 60); | 
| 285 | 0 |     return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset ); | 
| 286 | 0 |   } | 
| 287 |  |  | 
| 288 | 0 |   const TimeZonePtr& ltz = getDefault(); | 
| 289 |  | 
 | 
| 290 | 0 |   if ( ltz->getID() == id ) | 
| 291 | 0 |   { | 
| 292 | 0 |     return ltz; | 
| 293 | 0 |   } | 
| 294 |  |  | 
| 295 | 0 |   return getGMT(); | 
| 296 | 0 | } | 
| 297 |  |  |