Coverage Report

Created: 2026-06-15 06:23

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
9.32k
    {
50
9.32k
      static WideLife<TimeZonePtr> tz = std::make_shared<GMTTimeZone>();
51
9.32k
      return tz;
52
9.32k
    }
53
54
    /** Explode time to human readable form. */
55
    log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const
56
12.1k
    {
57
12.1k
      apr_status_t stat;
58
59
      //  APR 1.1 and early mishandles microseconds on dates
60
      //   before 1970, APR bug 32520
61
12.1k
      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
12.1k
      else
68
12.1k
      {
69
12.1k
        stat = apr_time_exp_gmt( result, input );
70
12.1k
      }
71
72
12.1k
      return stat;
73
12.1k
    }
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
146k
    {
89
146k
      static WideLife<TimeZonePtr> tz = std::make_shared<LocalTimeZone>();
90
146k
      return tz;
91
146k
    }
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.2k
    {
96
29.2k
      apr_status_t stat;
97
98
      //  APR 1.1 and early mishandles microseconds on dates
99
      //   before 1970, APR bug 32520
100
29.2k
      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.2k
      else
107
29.2k
      {
108
29.2k
        stat = apr_time_exp_lt( result, input );
109
29.2k
      }
110
111
29.2k
      return stat;
112
29.2k
    }
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
7.38k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
7.38k
    {
150
7.38k
    }
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.90k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
3.90k
    {
150
3.90k
    }
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.48k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
3.48k
    {
150
3.48k
    }
151
152
    /** Explode time to human readable form. */
153
    log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const
154
575
    {
155
575
      apr_status_t stat;
156
157
      //  APR 1.1 and early mishandles microseconds on dates
158
      //   before 1970, APR bug 32520
159
575
      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
575
      else
166
575
      {
167
575
        stat = apr_time_exp_tz( result, input, offset );
168
575
      }
169
170
575
      return stat;
171
575
    }
172
173
174
  private:
175
    const apr_int32_t offset;
176
};
177
178
}
179
}
180
}
181
182
183
184
7.40k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
7.40k
{
186
7.40k
}
log4cxx::helpers::TimeZone::TimeZone(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
184
3.91k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
3.91k
{
186
3.91k
}
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.49k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
3.49k
{
186
3.49k
}
187
188
TimeZone::~TimeZone()
189
7.40k
{
190
7.40k
}
191
192
const TimeZonePtr& TimeZone::getDefault()
193
293k
{
194
293k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
293k
}
log4cxx::helpers::TimeZone::getDefault()
Line
Count
Source
193
146k
{
194
146k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
146k
}
log4cxx::helpers::TimeZone::getDefault()
Line
Count
Source
193
146k
{
194
146k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
146k
}
196
197
const TimeZonePtr& TimeZone::getGMT()
198
17.7k
{
199
17.7k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
17.7k
}
log4cxx::helpers::TimeZone::getGMT()
Line
Count
Source
198
8.87k
{
199
8.87k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
8.87k
}
log4cxx::helpers::TimeZone::getGMT()
Line
Count
Source
198
8.87k
{
199
8.87k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
8.87k
}
201
202
const TimeZonePtr TimeZone::getTimeZone( const LogString& id )
203
26.2k
{
204
26.2k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
26.2k
  if ( id == gmt )
207
446
  {
208
446
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
446
  }
210
211
25.8k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
19.7k
  {
213
19.7k
    int hours = 0;
214
19.7k
    int minutes = 0;
215
19.7k
    int sign = 1;
216
217
19.7k
    if (id[3] == 0x2D /* '-' */)
218
5.29k
    {
219
5.29k
      sign = -1;
220
5.29k
    }
221
222
19.7k
    LogString off( id.substr( 4 ) );
223
224
19.7k
    if ( id.length() >= 7 )
225
13.8k
    {
226
13.8k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
13.8k
      if ( colonPos == LogString::npos )
229
6.70k
      {
230
6.70k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
6.70k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
6.70k
      }
233
7.17k
      else
234
7.17k
      {
235
7.17k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
7.17k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
7.17k
      }
238
13.8k
    }
239
5.86k
    else
240
5.86k
    {
241
5.86k
      hours = StringHelper::toInt(off);
242
5.86k
    }
243
244
    // Make sure that our offset can't be crazy
245
19.7k
    if( hours < -12 || 14 < hours)
246
1.69k
    {
247
1.69k
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
1.69k
    }
249
18.0k
    if (minutes < 0 || 60 < minutes)
250
1.53k
    {
251
1.53k
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
1.53k
    }
253
254
16.5k
    LogString s(gmt);
255
16.5k
    LogString hh;
256
16.5k
    StringHelper::toString(hours, hh);
257
258
16.5k
    if (sign > 0)
259
4.79k
    {
260
4.79k
      s.append(1, (logchar) 0x2B /* '+' */);
261
4.79k
    }
262
11.7k
    else
263
11.7k
    {
264
11.7k
      s.append(1, (logchar) 0x2D /* '-' */);
265
11.7k
    }
266
267
16.5k
    if (hh.length() == 1)
268
5.77k
    {
269
5.77k
      s.append(1, (logchar) 0x30 /* '0' */);
270
5.77k
    }
271
272
16.5k
    s.append(hh);
273
16.5k
    s.append(1, (logchar) 0x3A /*' :' */);
274
16.5k
    LogString mm;
275
16.5k
    StringHelper::toString(minutes, mm);
276
277
16.5k
    if (mm.length() == 1)
278
6.01k
    {
279
6.01k
      s.append(1, (logchar) 0x30 /* '0' */);
280
6.01k
    }
281
282
16.5k
    s.append(mm);
283
16.5k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
284
16.5k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
285
18.0k
  }
286
287
6.08k
  const TimeZonePtr& ltz = getDefault();
288
289
6.08k
  if ( ltz->getID() == id )
290
727
  {
291
727
    return ltz;
292
727
  }
293
294
5.36k
  return getGMT();
295
6.08k
}
log4cxx::helpers::TimeZone::getTimeZone(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
203
12.9k
{
204
12.9k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
12.9k
  if ( id == gmt )
207
203
  {
208
203
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
203
  }
210
211
12.7k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
9.88k
  {
213
9.88k
    int hours = 0;
214
9.88k
    int minutes = 0;
215
9.88k
    int sign = 1;
216
217
9.88k
    if (id[3] == 0x2D /* '-' */)
218
1.77k
    {
219
1.77k
      sign = -1;
220
1.77k
    }
221
222
9.88k
    LogString off( id.substr( 4 ) );
223
224
9.88k
    if ( id.length() >= 7 )
225
6.51k
    {
226
6.51k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
6.51k
      if ( colonPos == LogString::npos )
229
3.25k
      {
230
3.25k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
3.25k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
3.25k
      }
233
3.26k
      else
234
3.26k
      {
235
3.26k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
3.26k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
3.26k
      }
238
6.51k
    }
239
3.37k
    else
240
3.37k
    {
241
3.37k
      hours = StringHelper::toInt(off);
242
3.37k
    }
243
244
    // Make sure that our offset can't be crazy
245
9.88k
    if( hours < -12 || 14 < hours)
246
1.02k
    {
247
1.02k
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
1.02k
    }
249
8.86k
    if (minutes < 0 || 60 < minutes)
250
697
    {
251
697
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
697
    }
253
254
8.16k
    LogString s(gmt);
255
8.16k
    LogString hh;
256
8.16k
    StringHelper::toString(hours, hh);
257
258
8.16k
    if (sign > 0)
259
3.11k
    {
260
3.11k
      s.append(1, (logchar) 0x2B /* '+' */);
261
3.11k
    }
262
5.05k
    else
263
5.05k
    {
264
5.05k
      s.append(1, (logchar) 0x2D /* '-' */);
265
5.05k
    }
266
267
8.16k
    if (hh.length() == 1)
268
2.96k
    {
269
2.96k
      s.append(1, (logchar) 0x30 /* '0' */);
270
2.96k
    }
271
272
8.16k
    s.append(hh);
273
8.16k
    s.append(1, (logchar) 0x3A /*' :' */);
274
8.16k
    LogString mm;
275
8.16k
    StringHelper::toString(minutes, mm);
276
277
8.16k
    if (mm.length() == 1)
278
3.14k
    {
279
3.14k
      s.append(1, (logchar) 0x30 /* '0' */);
280
3.14k
    }
281
282
8.16k
    s.append(mm);
283
8.16k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
284
8.16k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
285
8.86k
  }
286
287
2.84k
  const TimeZonePtr& ltz = getDefault();
288
289
2.84k
  if ( ltz->getID() == id )
290
289
  {
291
289
    return ltz;
292
289
  }
293
294
2.55k
  return getGMT();
295
2.84k
}
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
13.3k
{
204
13.3k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
13.3k
  if ( id == gmt )
207
243
  {
208
243
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
243
  }
210
211
13.1k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
9.86k
  {
213
9.86k
    int hours = 0;
214
9.86k
    int minutes = 0;
215
9.86k
    int sign = 1;
216
217
9.86k
    if (id[3] == 0x2D /* '-' */)
218
3.51k
    {
219
3.51k
      sign = -1;
220
3.51k
    }
221
222
9.86k
    LogString off( id.substr( 4 ) );
223
224
9.86k
    if ( id.length() >= 7 )
225
7.36k
    {
226
7.36k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
7.36k
      if ( colonPos == LogString::npos )
229
3.45k
      {
230
3.45k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
3.45k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
3.45k
      }
233
3.91k
      else
234
3.91k
      {
235
3.91k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
3.91k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
3.91k
      }
238
7.36k
    }
239
2.49k
    else
240
2.49k
    {
241
2.49k
      hours = StringHelper::toInt(off);
242
2.49k
    }
243
244
    // Make sure that our offset can't be crazy
245
9.86k
    if( hours < -12 || 14 < hours)
246
672
    {
247
672
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
672
    }
249
9.19k
    if (minutes < 0 || 60 < minutes)
250
839
    {
251
839
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
839
    }
253
254
8.35k
    LogString s(gmt);
255
8.35k
    LogString hh;
256
8.35k
    StringHelper::toString(hours, hh);
257
258
8.35k
    if (sign > 0)
259
1.67k
    {
260
1.67k
      s.append(1, (logchar) 0x2B /* '+' */);
261
1.67k
    }
262
6.67k
    else
263
6.67k
    {
264
6.67k
      s.append(1, (logchar) 0x2D /* '-' */);
265
6.67k
    }
266
267
8.35k
    if (hh.length() == 1)
268
2.80k
    {
269
2.80k
      s.append(1, (logchar) 0x30 /* '0' */);
270
2.80k
    }
271
272
8.35k
    s.append(hh);
273
8.35k
    s.append(1, (logchar) 0x3A /*' :' */);
274
8.35k
    LogString mm;
275
8.35k
    StringHelper::toString(minutes, mm);
276
277
8.35k
    if (mm.length() == 1)
278
2.86k
    {
279
2.86k
      s.append(1, (logchar) 0x30 /* '0' */);
280
2.86k
    }
281
282
8.35k
    s.append(mm);
283
8.35k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
284
8.35k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
285
9.19k
  }
286
287
3.24k
  const TimeZonePtr& ltz = getDefault();
288
289
3.24k
  if ( ltz->getID() == id )
290
438
  {
291
438
    return ltz;
292
438
  }
293
294
2.80k
  return getGMT();
295
3.24k
}
296