Coverage Report

Created: 2026-08-13 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/muduo/muduo/base/TimeZone.h
Line
Count
Source
1
// Use of this source code is governed by a BSD-style license
2
// that can be found in the License file.
3
//
4
// Author: Shuo Chen (chenshuo at chenshuo dot com)
5
6
#ifndef MUDUO_BASE_TIMEZONE_H
7
#define MUDUO_BASE_TIMEZONE_H
8
9
#include "muduo/base/copyable.h"
10
#include "muduo/base/Types.h"
11
#include <memory>
12
#include <time.h>
13
14
namespace muduo
15
{
16
17
// Local time in unspecified timezone.
18
// A minute is always 60 seconds, no leap seconds.
19
struct DateTime
20
{
21
0
  DateTime() {}
22
  explicit DateTime(const struct tm&);
23
  DateTime(int _year, int _month, int _day, int _hour, int _minute, int _second)
24
      : year(_year), month(_month), day(_day), hour(_hour), minute(_minute), second(_second)
25
0
  {
26
0
  }
27
28
  // "2011-12-31 12:34:56"
29
  string toIsoString() const;
30
31
  int year = 0;     // [1900, 2500]
32
  int month = 0;    // [1, 12]
33
  int day = 0;      // [1, 31]
34
  int hour = 0;     // [0, 23]
35
  int minute = 0;   // [0, 59]
36
  int second = 0;   // [0, 59]
37
};
38
39
// TimeZone for 1970~2100
40
class TimeZone : public muduo::copyable
41
{
42
 public:
43
  TimeZone() = default;  // an invalid timezone
44
  TimeZone(int eastOfUtc, const char* tzname);  // a fixed timezone
45
46
  static TimeZone UTC();
47
  static TimeZone China();  // Fixed at GMT+8, no DST
48
  static TimeZone loadZoneFile(const char* zonefile);
49
50
  // default copy ctor/assignment/dtor are Okay.
51
52
  bool valid() const
53
0
  {
54
    // 'explicit operator bool() const' in C++11
55
0
    return static_cast<bool>(data_);
56
0
  }
57
58
  struct DateTime toLocalTime(int64_t secondsSinceEpoch, int* utcOffset = nullptr) const;
59
  int64_t fromLocalTime(const struct DateTime&, bool postTransition = false) const;
60
61
  // gmtime(3)
62
  static struct DateTime toUtcTime(int64_t secondsSinceEpoch);
63
  // timegm(3)
64
  static int64_t fromUtcTime(const struct DateTime&);
65
66
  struct Data;
67
68
 private:
69
  explicit TimeZone(std::unique_ptr<Data> data);
70
71
  std::shared_ptr<Data> data_;
72
73
  friend class TimeZoneTestPeer;
74
};
75
76
}  // namespace muduo
77
78
#endif  // MUDUO_BASE_TIMEZONE_H