Coverage Report

Created: 2026-08-13 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
6.42k
    {
50
6.42k
      static WideLife<TimeZonePtr> tz = std::make_shared<GMTTimeZone>();
51
6.42k
      return tz;
52
6.42k
    }
53
54
    /** Explode time to human readable form. */
55
    log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const
56
5.15k
    {
57
5.15k
      apr_status_t stat;
58
59
      //  APR 1.1 and early mishandles microseconds on dates
60
      //   before 1970, APR bug 32520
61
5.15k
      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
5.15k
      else
68
5.15k
      {
69
5.15k
        stat = apr_time_exp_gmt( result, input );
70
5.15k
      }
71
72
5.15k
      return stat;
73
5.15k
    }
74
75
4
    GMTTimeZone() : TimeZone( LOG4CXX_STR("GMT") )
76
4
    {
77
4
    }
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
163k
    {
89
163k
      static WideLife<TimeZonePtr> tz = std::make_shared<LocalTimeZone>();
90
163k
      return tz;
91
163k
    }
92
93
    /** Explode time to human readable form. */
94
    log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const
95
29.3k
    {
96
29.3k
      apr_status_t stat;
97
98
      //  APR 1.1 and early mishandles microseconds on dates
99
      //   before 1970, APR bug 32520
100
29.3k
      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
29.3k
      else
107
29.3k
      {
108
29.3k
        stat = apr_time_exp_lt( result, input );
109
29.3k
      }
110
111
29.3k
      return stat;
112
29.3k
    }
113
114
115
8
    LocalTimeZone() : TimeZone( getTimeZoneName() )
116
8
    {
117
8
    }
118
119
  private:
120
    static const LogString getTimeZoneName()
121
16
    {
122
16
      const int MAX_TZ_LENGTH = 255;
123
16
      char tzName[MAX_TZ_LENGTH];
124
16
      apr_size_t tzLength;
125
16
      apr_time_exp_t tm;
126
16
      apr_time_exp_lt(&tm, 0);
127
16
      apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%Z", &tm);
128
129
16
      if (tzLength == 0)
130
0
      {
131
0
        apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%z", &tm);
132
0
      }
133
134
16
      tzName[tzLength] = 0;
135
16
      LogString retval;
136
16
      LOG4CXX_NS::helpers::Transcoder::decode(tzName, retval);
137
16
      return retval;
138
16
    }
log4cxx::helpers::TimeZoneImpl::LocalTimeZone::getTimeZoneName()
Line
Count
Source
121
8
    {
122
8
      const int MAX_TZ_LENGTH = 255;
123
8
      char tzName[MAX_TZ_LENGTH];
124
8
      apr_size_t tzLength;
125
8
      apr_time_exp_t tm;
126
8
      apr_time_exp_lt(&tm, 0);
127
8
      apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%Z", &tm);
128
129
8
      if (tzLength == 0)
130
0
      {
131
0
        apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%z", &tm);
132
0
      }
133
134
8
      tzName[tzLength] = 0;
135
8
      LogString retval;
136
8
      LOG4CXX_NS::helpers::Transcoder::decode(tzName, retval);
137
8
      return retval;
138
8
    }
log4cxx::helpers::TimeZoneImpl::LocalTimeZone::getTimeZoneName()
Line
Count
Source
121
8
    {
122
8
      const int MAX_TZ_LENGTH = 255;
123
8
      char tzName[MAX_TZ_LENGTH];
124
8
      apr_size_t tzLength;
125
8
      apr_time_exp_t tm;
126
8
      apr_time_exp_lt(&tm, 0);
127
8
      apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%Z", &tm);
128
129
8
      if (tzLength == 0)
130
0
      {
131
0
        apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%z", &tm);
132
0
      }
133
134
8
      tzName[tzLength] = 0;
135
8
      LogString retval;
136
8
      LOG4CXX_NS::helpers::Transcoder::decode(tzName, retval);
137
8
      return retval;
138
8
    }
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
6.93k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
6.93k
    {
150
6.93k
    }
log4cxx::helpers::TimeZoneImpl::FixedTimeZone::FixedTimeZone(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)
Line
Count
Source
148
3.12k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
3.12k
    {
150
3.12k
    }
log4cxx::helpers::TimeZoneImpl::FixedTimeZone::FixedTimeZone(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, int)
Line
Count
Source
148
3.80k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
3.80k
    {
150
3.80k
    }
151
152
    /** Explode time to human readable form. */
153
    log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const
154
1.41k
    {
155
1.41k
      apr_status_t stat;
156
157
      //  APR 1.1 and early mishandles microseconds on dates
158
      //   before 1970, APR bug 32520
159
1.41k
      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
1.41k
      else
166
1.41k
      {
167
1.41k
        stat = apr_time_exp_tz( result, input, offset );
168
1.41k
      }
169
170
1.41k
      return stat;
171
1.41k
    }
172
173
174
  private:
175
    const apr_int32_t offset;
176
};
177
178
}
179
}
180
}
181
182
183
184
6.94k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
6.94k
{
186
6.94k
}
log4cxx::helpers::TimeZone::TimeZone(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
184
3.12k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
3.12k
{
186
3.12k
}
log4cxx::helpers::TimeZone::TimeZone(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
Line
Count
Source
184
3.81k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
3.81k
{
186
3.81k
}
187
188
TimeZone::~TimeZone()
189
6.94k
{
190
6.94k
}
191
192
const TimeZonePtr& TimeZone::getDefault()
193
326k
{
194
326k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
326k
}
log4cxx::helpers::TimeZone::getDefault()
Line
Count
Source
193
163k
{
194
163k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
163k
}
log4cxx::helpers::TimeZone::getDefault()
Line
Count
Source
193
163k
{
194
163k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
163k
}
196
197
const TimeZonePtr& TimeZone::getGMT()
198
11.8k
{
199
11.8k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
11.8k
}
log4cxx::helpers::TimeZone::getGMT()
Line
Count
Source
198
5.94k
{
199
5.94k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
5.94k
}
log4cxx::helpers::TimeZone::getGMT()
Line
Count
Source
198
5.94k
{
199
5.94k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
5.94k
}
201
202
const TimeZonePtr TimeZone::getTimeZone( const LogString& id )
203
27.5k
{
204
27.5k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
27.5k
  if ( id == gmt )
207
477
  {
208
477
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
477
  }
210
211
27.0k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
20.1k
  {
213
20.1k
    int hours = 0;
214
20.1k
    int minutes = 0;
215
20.1k
    int sign = 1;
216
217
20.1k
    if (id[3] == 0x2D /* '-' */)
218
5.42k
    {
219
5.42k
      sign = -1;
220
5.42k
    }
221
222
20.1k
    LogString off( id.substr( 4 ) );
223
224
20.1k
    if ( id.length() >= 7 )
225
14.8k
    {
226
14.8k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
14.8k
      if ( colonPos == LogString::npos )
229
7.25k
      {
230
7.25k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
7.25k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
7.25k
      }
233
7.55k
      else
234
7.55k
      {
235
7.55k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
7.55k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
7.55k
      }
238
14.8k
    }
239
5.34k
    else
240
5.34k
    {
241
5.34k
      hours = StringHelper::toInt(off);
242
5.34k
    }
243
244
    // Make sure that our offset can't be crazy
245
20.1k
    if( hours < -12 || 14 < hours)
246
1.76k
    {
247
1.76k
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
1.76k
    }
249
18.3k
    if (minutes < 0 || 60 < minutes)
250
1.92k
    {
251
1.92k
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
1.92k
    }
253
254
16.4k
    LogString s(gmt);
255
16.4k
    LogString hh;
256
16.4k
    StringHelper::toString(hours, hh);
257
258
16.4k
    if (sign > 0)
259
4.34k
    {
260
4.34k
      s.append(1, (logchar) 0x2B /* '+' */);
261
4.34k
    }
262
12.1k
    else
263
12.1k
    {
264
12.1k
      s.append(1, (logchar) 0x2D /* '-' */);
265
12.1k
    }
266
267
16.4k
    if (hh.length() == 1)
268
5.83k
    {
269
5.83k
      s.append(1, (logchar) 0x30 /* '0' */);
270
5.83k
    }
271
272
16.4k
    s.append(hh);
273
16.4k
    s.append(1, (logchar) 0x3A /*' :' */);
274
16.4k
    LogString mm;
275
16.4k
    StringHelper::toString(minutes, mm);
276
277
16.4k
    if (mm.length() == 1)
278
5.68k
    {
279
5.68k
      s.append(1, (logchar) 0x30 /* '0' */);
280
5.68k
    }
281
282
16.4k
    s.append(mm);
283
16.4k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
284
16.4k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
285
18.3k
  }
286
287
6.87k
  const TimeZonePtr& ltz = getDefault();
288
289
6.87k
  if ( ltz->getID() == id )
290
928
  {
291
928
    return ltz;
292
928
  }
293
294
5.94k
  return getGMT();
295
6.87k
}
log4cxx::helpers::TimeZone::getTimeZone(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
203
12.7k
{
204
12.7k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
12.7k
  if ( id == gmt )
207
269
  {
208
269
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
269
  }
210
211
12.4k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
9.16k
  {
213
9.16k
    int hours = 0;
214
9.16k
    int minutes = 0;
215
9.16k
    int sign = 1;
216
217
9.16k
    if (id[3] == 0x2D /* '-' */)
218
1.69k
    {
219
1.69k
      sign = -1;
220
1.69k
    }
221
222
9.16k
    LogString off( id.substr( 4 ) );
223
224
9.16k
    if ( id.length() >= 7 )
225
6.65k
    {
226
6.65k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
6.65k
      if ( colonPos == LogString::npos )
229
3.21k
      {
230
3.21k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
3.21k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
3.21k
      }
233
3.44k
      else
234
3.44k
      {
235
3.44k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
3.44k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
3.44k
      }
238
6.65k
    }
239
2.51k
    else
240
2.51k
    {
241
2.51k
      hours = StringHelper::toInt(off);
242
2.51k
    }
243
244
    // Make sure that our offset can't be crazy
245
9.16k
    if( hours < -12 || 14 < hours)
246
787
    {
247
787
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
787
    }
249
8.38k
    if (minutes < 0 || 60 < minutes)
250
854
    {
251
854
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
854
    }
253
254
7.52k
    LogString s(gmt);
255
7.52k
    LogString hh;
256
7.52k
    StringHelper::toString(hours, hh);
257
258
7.52k
    if (sign > 0)
259
2.53k
    {
260
2.53k
      s.append(1, (logchar) 0x2B /* '+' */);
261
2.53k
    }
262
4.99k
    else
263
4.99k
    {
264
4.99k
      s.append(1, (logchar) 0x2D /* '-' */);
265
4.99k
    }
266
267
7.52k
    if (hh.length() == 1)
268
2.75k
    {
269
2.75k
      s.append(1, (logchar) 0x30 /* '0' */);
270
2.75k
    }
271
272
7.52k
    s.append(hh);
273
7.52k
    s.append(1, (logchar) 0x3A /*' :' */);
274
7.52k
    LogString mm;
275
7.52k
    StringHelper::toString(minutes, mm);
276
277
7.52k
    if (mm.length() == 1)
278
2.36k
    {
279
2.36k
      s.append(1, (logchar) 0x30 /* '0' */);
280
2.36k
    }
281
282
7.52k
    s.append(mm);
283
7.52k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
284
7.52k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
285
8.38k
  }
286
287
3.28k
  const TimeZonePtr& ltz = getDefault();
288
289
3.28k
  if ( ltz->getID() == id )
290
574
  {
291
574
    return ltz;
292
574
  }
293
294
2.71k
  return getGMT();
295
3.28k
}
log4cxx::helpers::TimeZone::getTimeZone(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
Line
Count
Source
203
14.7k
{
204
14.7k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
14.7k
  if ( id == gmt )
207
208
  {
208
208
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
208
  }
210
211
14.5k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
10.9k
  {
213
10.9k
    int hours = 0;
214
10.9k
    int minutes = 0;
215
10.9k
    int sign = 1;
216
217
10.9k
    if (id[3] == 0x2D /* '-' */)
218
3.72k
    {
219
3.72k
      sign = -1;
220
3.72k
    }
221
222
10.9k
    LogString off( id.substr( 4 ) );
223
224
10.9k
    if ( id.length() >= 7 )
225
8.14k
    {
226
8.14k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
8.14k
      if ( colonPos == LogString::npos )
229
4.03k
      {
230
4.03k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
4.03k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
4.03k
      }
233
4.11k
      else
234
4.11k
      {
235
4.11k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
4.11k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
4.11k
      }
238
8.14k
    }
239
2.83k
    else
240
2.83k
    {
241
2.83k
      hours = StringHelper::toInt(off);
242
2.83k
    }
243
244
    // Make sure that our offset can't be crazy
245
10.9k
    if( hours < -12 || 14 < hours)
246
981
    {
247
981
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
981
    }
249
10.0k
    if (minutes < 0 || 60 < minutes)
250
1.07k
    {
251
1.07k
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
1.07k
    }
253
254
8.92k
    LogString s(gmt);
255
8.92k
    LogString hh;
256
8.92k
    StringHelper::toString(hours, hh);
257
258
8.92k
    if (sign > 0)
259
1.80k
    {
260
1.80k
      s.append(1, (logchar) 0x2B /* '+' */);
261
1.80k
    }
262
7.12k
    else
263
7.12k
    {
264
7.12k
      s.append(1, (logchar) 0x2D /* '-' */);
265
7.12k
    }
266
267
8.92k
    if (hh.length() == 1)
268
3.07k
    {
269
3.07k
      s.append(1, (logchar) 0x30 /* '0' */);
270
3.07k
    }
271
272
8.92k
    s.append(hh);
273
8.92k
    s.append(1, (logchar) 0x3A /*' :' */);
274
8.92k
    LogString mm;
275
8.92k
    StringHelper::toString(minutes, mm);
276
277
8.92k
    if (mm.length() == 1)
278
3.32k
    {
279
3.32k
      s.append(1, (logchar) 0x30 /* '0' */);
280
3.32k
    }
281
282
8.92k
    s.append(mm);
283
8.92k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
284
8.92k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
285
10.0k
  }
286
287
3.59k
  const TimeZonePtr& ltz = getDefault();
288
289
3.59k
  if ( ltz->getID() == id )
290
354
  {
291
354
    return ltz;
292
354
  }
293
294
3.23k
  return getGMT();
295
3.59k
}
296