OpenTime做最称心的C++开发时间库

OpenTime做最称心的C++开发时间库

跨平台多线程设计!
程序开发频繁涉及时间处理,有一个好用的时间库可以大幅提高工作效率。
OpenTime是最简单易用的C++处理时间工具。

https://github.com/OpenMiniServer

跨平台支持

Windows、linux、Mac、iOS、Android等跨平台设计

编译和执行

请安装cmake工具,用cmake可以构建出VS或者XCode工程,就可以在vs或者xcode上编译运行。
源代码:https://github.com/OpenMiniServer/opentime

git clone https://github.com/OpenMiniServer/opentime
cd ./opentime
mkdir build
cd build
cmake ..
#如果是win32,在该目录出现opentime.sln,点击它就可以启动vs写代码调试
make
./test

全部源文件

  • src/opentime.h
  • src/opentime.cpp

1.时间戳转换成字符串

#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
    OpenTime openTime(OpenTime::Unixtime());
    std::cout << "Current Unixtime:" << openTime.unixtime() << std::endl;
    std::cout << "Current Date:" << openTime.toString() << std::endl;

    openTime = 1676704738;
    assert(openTime.unixtime() == 1676704738);
    assert(openTime.wday() == 6);
    assert(openTime.toString() == "2023-02-18 15:18:58");
    assert(openTime.toGMT() == "Mon, 18 Feb 2023 15:18:58 GMT");

    //2023-02-18 13:37:55
    openTime = 1676698675;
    assert(openTime.toString("") == "2023-02-18 13:37:55");
    assert(openTime.toString("_") == "2023_02_18_13_37_55");

    //1991-03-18 13:37:55
    openTime = 669274675;
    assert(openTime.toString("%Y_%M_%D") == "1991_03_18");

    //2010-12-18 03:37:55
    openTime = 1292614675;
    assert(openTime.toString("%Y_%M_%D_%h_%m_%s") == "2010_12_18_03_37_55");

    //1972-06-18 13:37:55
    openTime = 77693875;
    assert(openTime.toString("date:%h:%m:%s %Y/%M/%D%d") == "date:13:37:55 1972/06/18%d");
    return 0;
}

2.字符串转换成时间戳

#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
    OpenTime openTime;

    openTime = "2023-02-18 15:18:58";
    int64_t thatTime = 1676704738;
    assert(openTime.unixtime() == thatTime);

    openTime += 3600 * 24 * 2;
    assert(openTime.toString() == "2023-02-20 15:18:58");

    openTime.fromGMT("Sat, 18 Feb 2023 15:18:58 GMT");
    assert(openTime.unixtime() == thatTime);

    //2023-02-18 13:37:55
    std::string strTime = "2023-02-18 13:37:55";
    openTime = strTime;
    assert(openTime.unixtime() == 1676698675);

    //1991-03-18 00:00:00
    strTime = "1991_03_18";
    openTime.fromString(strTime, "%Y_%M_%D");
    assert(openTime.unixtime() == 669225600);

    //2010-12-18 03:37:55
    strTime = "2010_12_18_03_37_55";
    openTime.fromString(strTime, "%Y_%M_%D_%h_%m_%s");
    assert(openTime.unixtime() == 1292614675);

    //1972-06-18 13:37:55
    strTime = "date:13:37:55 1972/06/18%d";
    openTime.fromString(strTime, "date:%h:%m:%s %Y/%M/%D%d");
    assert(openTime.unixtime() == 77693875);

    int64_t milliTimeStamp = OpenTime::MilliUnixtime();
    std::cout << "Current milliTimeStamp:" << OpenTime(milliTimeStamp / 1000).toString(milliTimeStamp % 1000) << std::endl;
    return 0;
}

3.GMT时间与时间戳转换

#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
    OpenTime openTime;

    openTime = "2023-02-18 15:18:58";
    openTime += 3600 * 24 * 2;
    assert(openTime.toString() == "2023-02-20 15:18:58");

    int64_t thatTime = openTime.unixtime();
    assert(openTime.toGMT() == "Mon, 20 Feb 2023 15:18:58 GMT");
    openTime.fromGMT("Mon, 20 Feb 2023 15:18:58 GMT");
    assert(openTime.unixtime() == thatTime);

    // sleep 1 second
    OpenTime::Sleep(1000);
    return 0;
}

4.整数时间与时间戳转换

#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
    OpenTime openTime;
    
    int64_t inttime = 20230218133755;
    openTime.fromIntTime(inttime);
    assert(openTime.toString() == "2023-02-18 13:37:55");
    assert(openTime.toIntTime() == 20230218133755);
    return 0;
}

5.时间戳按日对齐

#include <assert.h>
#include "opentime.h"

int main()
{
    OpenTime openTime;
    //2023-02-18 00:00:00
    openTime = "2023-02-18 15:18:58";
    int64_t alignTime = openTime.alignDay();
    assert(OpenTime::ToString(alignTime) == "2023-02-18 00:00:00");
    
    alignTime = OpenTime::AlignDay(1642490337);
    assert(OpenTime::ToString(alignTime) == "2022-01-18 00:00:00");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值