Linux多线程C++工具库:liblmp_tool --TimeZone 时区类

实现了localtime 和 UTCtime的转换:Linux多线程编程工具库liblmp_tool github:https://github.com/Dwyane05/liblmp_toolTimezone类分析:头文件;定义了localtime和utctime 的转换;使用了shard_ptr 智能指针处理date/** Use of this source...
摘要由CSDN通过智能技术生成

实现了localtime 和 UTCtime的转换:

Linux多线程编程工具库liblmp_tool   github: https://github.com/Dwyane05/liblmp_tool

Timezone类分析:

头文件;定义了localtime和utctime 的转换;使用了shard_ptr 智能指针处理date

/*
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the License file.
 *
 *  TimeZone.h
 *  Created on: May 30, 2019
 *  Author:     cuiyongfei
 */

#ifndef LMP_TOOL_TIMEZONE_H
#define LMP_TOOL_TIMEZONE_H

#include "copyable.h"
#include <memory>
#include <time.h>

namespace lmp_tool
{

// TimeZone for 1970~2030
class TimeZone : public lmp_tool::copyable
{
 public:
  explicit TimeZone(const char* zonefile);
  TimeZone(int eastOfUtc, const char* tzname);  // a fixed timezone
  TimeZone() = default;  // an invalid timezone

  // default copy ctor/assignment/dtor are Okay.

  bool valid() const
  {
    // 'explicit operator bool() const' in C++11
    return static_cast<bool>(data_);
  }

  struct tm toLocalTime(time_t secondsSinceEpoch) const;
  time_t fromLocalTime(const struct tm&) const;

  // gmtime(3)
  static struct tm toUtcTime(time_t secondsSinceEpoch, bool yday = false);
  // timegm(3)
  static time_t fromUtcTime(const struct tm&);
  // year in [1900..2500], month in [1..12], day in [1..31]
  static time_t fromUtcTime(int year, int month, int day,
                            int hour, int minute, int seconds);

  struct Data;

 private:

  std::shared_ptr<Data> data_;
};

}  // namespace lmp_tool

#endif  // LMP_TOOL_TIMEZONE_H

具体代码实现太多就不粘贴了,见GitHub:lmp_src目录下

测试代码:

测试几个主要城市 的时间

#include "TimeZone.h"
#include "Common.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

using lmp_tool::TimeZone;

struct tm getTm(int year, int month, int day,
                int hour, int minute, int seconds)
{
  struct tm gmt;
  lmp_tool::memZero(&gmt, sizeof gmt);
  gmt.tm_year = year - 1900;
  gmt.tm_mon = month - 1;
  gmt.tm_mday = day;
  gmt.tm_hour = hour;
  gmt.tm_min = minute;
  gmt.tm_sec = seconds;
  return gmt;
}

struct tm getTm(const char* str)
{
  struct tm gmt;
  lmp_tool::memZero(&gmt, sizeof gmt);
  strptime(str, "

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dwyane05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值