Coverage Report

Created: 2025-12-14 06:19

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
15.4k
    {
50
15.4k
      static WideLife<TimeZonePtr> tz = std::make_shared<GMTTimeZone>();
51
15.4k
      return tz;
52
15.4k
    }
53
54
    /** Explode time to human readable form. */
55
    log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const
56
26.1k
    {
57
26.1k
      apr_status_t stat;
58
59
      //  APR 1.1 and early mishandles microseconds on dates
60
      //   before 1970, APR bug 32520
61
26.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
26.1k
      else
68
26.1k
      {
69
26.1k
        stat = apr_time_exp_gmt( result, input );
70
26.1k
      }
71
72
26.1k
      return stat;
73
26.1k
    }
74
75
6
    GMTTimeZone() : TimeZone( LOG4CXX_STR("GMT") )
76
6
    {
77
6
    }
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
166k
    {
89
166k
      static WideLife<TimeZonePtr> tz = std::make_shared<LocalTimeZone>();
90
166k
      return tz;
91
166k
    }
92
93
    /** Explode time to human readable form. */
94
    log4cxx_status_t explode( apr_time_exp_t* result, log4cxx_time_t input ) const
95
18.1k
    {
96
18.1k
      apr_status_t stat;
97
98
      //  APR 1.1 and early mishandles microseconds on dates
99
      //   before 1970, APR bug 32520
100
18.1k
      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
18.1k
      else
107
18.1k
      {
108
18.1k
        stat = apr_time_exp_lt( result, input );
109
18.1k
      }
110
111
18.1k
      return stat;
112
18.1k
    }
113
114
115
9
    LocalTimeZone() : TimeZone( getTimeZoneName() )
116
9
    {
117
9
    }
118
119
  private:
120
    static const LogString getTimeZoneName()
121
18
    {
122
18
      const int MAX_TZ_LENGTH = 255;
123
18
      char tzName[MAX_TZ_LENGTH];
124
18
      apr_size_t tzLength;
125
18
      apr_time_exp_t tm;
126
18
      apr_time_exp_lt(&tm, 0);
127
18
      apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%Z", &tm);
128
129
18
      if (tzLength == 0)
130
0
      {
131
0
        apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%z", &tm);
132
0
      }
133
134
18
      tzName[tzLength] = 0;
135
18
      LogString retval;
136
18
      LOG4CXX_NS::helpers::Transcoder::decode(tzName, retval);
137
18
      return retval;
138
18
    }
log4cxx::helpers::TimeZoneImpl::LocalTimeZone::getTimeZoneName()
Line
Count
Source
121
9
    {
122
9
      const int MAX_TZ_LENGTH = 255;
123
9
      char tzName[MAX_TZ_LENGTH];
124
9
      apr_size_t tzLength;
125
9
      apr_time_exp_t tm;
126
9
      apr_time_exp_lt(&tm, 0);
127
9
      apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%Z", &tm);
128
129
9
      if (tzLength == 0)
130
0
      {
131
0
        apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%z", &tm);
132
0
      }
133
134
9
      tzName[tzLength] = 0;
135
9
      LogString retval;
136
9
      LOG4CXX_NS::helpers::Transcoder::decode(tzName, retval);
137
9
      return retval;
138
9
    }
log4cxx::helpers::TimeZoneImpl::LocalTimeZone::getTimeZoneName()
Line
Count
Source
121
9
    {
122
9
      const int MAX_TZ_LENGTH = 255;
123
9
      char tzName[MAX_TZ_LENGTH];
124
9
      apr_size_t tzLength;
125
9
      apr_time_exp_t tm;
126
9
      apr_time_exp_lt(&tm, 0);
127
9
      apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%Z", &tm);
128
129
9
      if (tzLength == 0)
130
0
      {
131
0
        apr_strftime(tzName, &tzLength, MAX_TZ_LENGTH, "%z", &tm);
132
0
      }
133
134
9
      tzName[tzLength] = 0;
135
9
      LogString retval;
136
9
      LOG4CXX_NS::helpers::Transcoder::decode(tzName, retval);
137
9
      return retval;
138
9
    }
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.43k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
7.43k
    {
150
7.43k
    }
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
4.30k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
4.30k
    {
150
4.30k
    }
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.12k
    FixedTimeZone( const LogString& name, apr_int32_t offset1 ) : TimeZone( name ), offset( offset1 )
149
3.12k
    {
150
3.12k
    }
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.25k
    {
155
1.25k
      apr_status_t stat;
156
157
      //  APR 1.1 and early mishandles microseconds on dates
158
      //   before 1970, APR bug 32520
159
1.25k
      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.25k
      else
166
1.25k
      {
167
1.25k
        stat = apr_time_exp_tz( result, input, offset );
168
1.25k
      }
169
170
1.25k
      return stat;
171
1.25k
    }
172
173
174
  private:
175
    const apr_int32_t offset;
176
};
177
178
}
179
}
180
}
181
182
183
184
7.44k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
7.44k
{
186
7.44k
}
log4cxx::helpers::TimeZone::TimeZone(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
184
4.31k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
4.31k
{
186
4.31k
}
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.13k
TimeZone::TimeZone( const LogString& id1 ) : id( id1 )
185
3.13k
{
186
3.13k
}
187
188
TimeZone::~TimeZone()
189
7.44k
{
190
7.44k
}
191
192
const TimeZonePtr& TimeZone::getDefault()
193
332k
{
194
332k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
332k
}
log4cxx::helpers::TimeZone::getDefault()
Line
Count
Source
193
166k
{
194
166k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
166k
}
log4cxx::helpers::TimeZone::getDefault()
Line
Count
Source
193
166k
{
194
166k
  return LOG4CXX_NS::helpers::TimeZoneImpl::LocalTimeZone::getInstance();
195
166k
}
196
197
const TimeZonePtr& TimeZone::getGMT()
198
29.5k
{
199
29.5k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
29.5k
}
log4cxx::helpers::TimeZone::getGMT()
Line
Count
Source
198
14.7k
{
199
14.7k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
14.7k
}
log4cxx::helpers::TimeZone::getGMT()
Line
Count
Source
198
14.7k
{
199
14.7k
  return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
200
14.7k
}
201
202
const TimeZonePtr TimeZone::getTimeZone( const LogString& id )
203
28.2k
{
204
28.2k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
28.2k
  if ( id == gmt )
207
632
  {
208
632
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
632
  }
210
211
27.5k
  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
4.92k
    {
219
4.92k
      sign = -1;
220
4.92k
    }
221
222
19.7k
    LogString off( id.substr( 4 ) );
223
224
19.7k
    if ( id.length() >= 7 )
225
13.1k
    {
226
13.1k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
13.1k
      if ( colonPos == LogString::npos )
229
6.20k
      {
230
6.20k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
6.20k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
6.20k
      }
233
6.91k
      else
234
6.91k
      {
235
6.91k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
6.91k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
6.91k
      }
238
13.1k
    }
239
6.63k
    else
240
6.63k
    {
241
6.63k
      hours = StringHelper::toInt(off);
242
6.63k
    }
243
244
    // Make sure that our offset can't be crazy
245
19.7k
    if( hours < -12 || 14 < hours)
246
1.68k
    {
247
1.68k
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
1.68k
    }
249
18.0k
    if (minutes < 0 || 60 < minutes)
250
1.17k
    {
251
1.17k
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
1.17k
    }
253
254
16.9k
    LogString s(gmt);
255
16.9k
    Pool p;
256
16.9k
    LogString hh;
257
16.9k
    StringHelper::toString(hours, p, hh);
258
259
16.9k
    if (sign > 0)
260
5.46k
    {
261
5.46k
      s.append(1, (logchar) 0x2B /* '+' */);
262
5.46k
    }
263
11.4k
    else
264
11.4k
    {
265
11.4k
      s.append(1, (logchar) 0x2D /* '-' */);
266
11.4k
    }
267
268
16.9k
    if (hh.length() == 1)
269
5.83k
    {
270
5.83k
      s.append(1, (logchar) 0x30 /* '0' */);
271
5.83k
    }
272
273
16.9k
    s.append(hh);
274
16.9k
    s.append(1, (logchar) 0x3A /*' :' */);
275
16.9k
    LogString mm;
276
16.9k
    StringHelper::toString(minutes, p, mm);
277
278
16.9k
    if (mm.length() == 1)
279
6.33k
    {
280
6.33k
      s.append(1, (logchar) 0x30 /* '0' */);
281
6.33k
    }
282
283
16.9k
    s.append(mm);
284
16.9k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
285
16.9k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
286
18.0k
  }
287
288
7.83k
  const TimeZonePtr& ltz = getDefault();
289
290
7.83k
  if ( ltz->getID() == id )
291
437
  {
292
437
    return ltz;
293
437
  }
294
295
7.39k
  return getGMT();
296
7.83k
}
log4cxx::helpers::TimeZone::getTimeZone(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
203
13.0k
{
204
13.0k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
13.0k
  if ( id == gmt )
207
280
  {
208
280
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
280
  }
210
211
12.8k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
9.83k
  {
213
9.83k
    int hours = 0;
214
9.83k
    int minutes = 0;
215
9.83k
    int sign = 1;
216
217
9.83k
    if (id[3] == 0x2D /* '-' */)
218
1.88k
    {
219
1.88k
      sign = -1;
220
1.88k
    }
221
222
9.83k
    LogString off( id.substr( 4 ) );
223
224
9.83k
    if ( id.length() >= 7 )
225
6.29k
    {
226
6.29k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
6.29k
      if ( colonPos == LogString::npos )
229
2.96k
      {
230
2.96k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
2.96k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
2.96k
      }
233
3.33k
      else
234
3.33k
      {
235
3.33k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
3.33k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
3.33k
      }
238
6.29k
    }
239
3.53k
    else
240
3.53k
    {
241
3.53k
      hours = StringHelper::toInt(off);
242
3.53k
    }
243
244
    // Make sure that our offset can't be crazy
245
9.83k
    if( hours < -12 || 14 < hours)
246
701
    {
247
701
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
701
    }
249
9.13k
    if (minutes < 0 || 60 < minutes)
250
614
    {
251
614
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
614
    }
253
254
8.52k
    LogString s(gmt);
255
8.52k
    Pool p;
256
8.52k
    LogString hh;
257
8.52k
    StringHelper::toString(hours, p, hh);
258
259
8.52k
    if (sign > 0)
260
3.54k
    {
261
3.54k
      s.append(1, (logchar) 0x2B /* '+' */);
262
3.54k
    }
263
4.97k
    else
264
4.97k
    {
265
4.97k
      s.append(1, (logchar) 0x2D /* '-' */);
266
4.97k
    }
267
268
8.52k
    if (hh.length() == 1)
269
3.23k
    {
270
3.23k
      s.append(1, (logchar) 0x30 /* '0' */);
271
3.23k
    }
272
273
8.52k
    s.append(hh);
274
8.52k
    s.append(1, (logchar) 0x3A /*' :' */);
275
8.52k
    LogString mm;
276
8.52k
    StringHelper::toString(minutes, p, mm);
277
278
8.52k
    if (mm.length() == 1)
279
3.65k
    {
280
3.65k
      s.append(1, (logchar) 0x30 /* '0' */);
281
3.65k
    }
282
283
8.52k
    s.append(mm);
284
8.52k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
285
8.52k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
286
9.13k
  }
287
288
2.96k
  const TimeZonePtr& ltz = getDefault();
289
290
2.96k
  if ( ltz->getID() == id )
291
420
  {
292
420
    return ltz;
293
420
  }
294
295
2.54k
  return getGMT();
296
2.96k
}
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
15.1k
{
204
15.1k
  const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 };
205
206
15.1k
  if ( id == gmt )
207
352
  {
208
352
    return LOG4CXX_NS::helpers::TimeZoneImpl::GMTTimeZone::getInstance();
209
352
  }
210
211
14.7k
  if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt )
212
9.91k
  {
213
9.91k
    int hours = 0;
214
9.91k
    int minutes = 0;
215
9.91k
    int sign = 1;
216
217
9.91k
    if (id[3] == 0x2D /* '-' */)
218
3.03k
    {
219
3.03k
      sign = -1;
220
3.03k
    }
221
222
9.91k
    LogString off( id.substr( 4 ) );
223
224
9.91k
    if ( id.length() >= 7 )
225
6.82k
    {
226
6.82k
      size_t colonPos = off.find( 0x3A /* ':' */);
227
228
6.82k
      if ( colonPos == LogString::npos )
229
3.24k
      {
230
3.24k
        minutes = StringHelper::toInt(off.substr(off.length() - 2));
231
3.24k
        hours = StringHelper::toInt(off.substr(0, off.length() - 2));
232
3.24k
      }
233
3.58k
      else
234
3.58k
      {
235
3.58k
        minutes = StringHelper::toInt(off.substr(colonPos + 1));
236
3.58k
        hours = StringHelper::toInt(off.substr(0, colonPos));
237
3.58k
      }
238
6.82k
    }
239
3.09k
    else
240
3.09k
    {
241
3.09k
      hours = StringHelper::toInt(off);
242
3.09k
    }
243
244
    // Make sure that our offset can't be crazy
245
9.91k
    if( hours < -12 || 14 < hours)
246
981
    {
247
981
      throw RuntimeException(LOG4CXX_STR("Hour offset must be in (-12..14)"));
248
981
    }
249
8.93k
    if (minutes < 0 || 60 < minutes)
250
560
    {
251
560
      throw RuntimeException(LOG4CXX_STR("Minute offset must be in (0..60)"));
252
560
    }
253
254
8.37k
    LogString s(gmt);
255
8.37k
    Pool p;
256
8.37k
    LogString hh;
257
8.37k
    StringHelper::toString(hours, p, hh);
258
259
8.37k
    if (sign > 0)
260
1.92k
    {
261
1.92k
      s.append(1, (logchar) 0x2B /* '+' */);
262
1.92k
    }
263
6.45k
    else
264
6.45k
    {
265
6.45k
      s.append(1, (logchar) 0x2D /* '-' */);
266
6.45k
    }
267
268
8.37k
    if (hh.length() == 1)
269
2.59k
    {
270
2.59k
      s.append(1, (logchar) 0x30 /* '0' */);
271
2.59k
    }
272
273
8.37k
    s.append(hh);
274
8.37k
    s.append(1, (logchar) 0x3A /*' :' */);
275
8.37k
    LogString mm;
276
8.37k
    StringHelper::toString(minutes, p, mm);
277
278
8.37k
    if (mm.length() == 1)
279
2.67k
    {
280
2.67k
      s.append(1, (logchar) 0x30 /* '0' */);
281
2.67k
    }
282
283
8.37k
    s.append(mm);
284
8.37k
    apr_int32_t offset = sign * (hours * 3600 + minutes * 60);
285
8.37k
    return std::make_shared<helpers::TimeZoneImpl::FixedTimeZone>( s, offset );
286
8.93k
  }
287
288
4.86k
  const TimeZonePtr& ltz = getDefault();
289
290
4.86k
  if ( ltz->getID() == id )
291
17
  {
292
17
    return ltz;
293
17
  }
294
295
4.84k
  return getGMT();
296
4.86k
}
297