c++11 时间戳 windows

windows 获取时间戳,时间戳转时间

me:

获取并打印当前时间

void getNowTime()//获取并打印当前时间
{
	system_clock::duration d = system_clock::now().time_since_epoch();
	minutes min = duration_cast<minutes>(d);
	seconds sec = duration_cast<seconds>(d);
	milliseconds mil = duration_cast<milliseconds>(d);
	microseconds mic = duration_cast<microseconds>(d);
	nanoseconds nan = duration_cast<nanoseconds>(d);
	cout << min.count() << "分钟" << endl;
	cout << sec.count() << "秒" << endl;
	cout << mil.count() << "毫秒" << endl;
	cout << mic.count() << "微妙" << endl;
	cout << nan.count() << "纳秒" << endl;

	char time_str[4][16];
	std::time_t timestamp = mil.count();
	auto time_ptr = gettm(timestamp);
	sprintf(time_str[0], "%02d", time_ptr->tm_mon + 1); //月份要加1
	sprintf(time_str[1], "%02d", time_ptr->tm_mday);//天
	sprintf(time_str[2], "%02d", time_ptr->tm_hour);//时
	sprintf(time_str[3], "%02d", time_ptr->tm_min);// 分
	for (int i = 0; i < 4; i++)
	{
		std::cout << "time_str[" << i << "] is: " << time_str[i] << std::endl;
	}
}
/****************/
#include <iostream>
#include<thread>
#include<chrono>
#include<ctime>
#include <time.h>
#include<stddef.h>
#include <Windows.h>
using namespace std;
using namespace std::chrono;


std::tm *gettm(long long timestamp)
{
	auto milli = timestamp + (long long)8 * 60 * 60 * 1000; //此处转化为东八区北京时间,如果是其它时区需要按需求修改
	auto mTime = std::chrono::milliseconds(milli);
	auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
	auto tt = std::chrono::system_clock::to_time_t(tp);
	std::tm *now = std::gmtime(&tt);
	printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
	return now;
}

int main()
{
	system_clock::duration d = system_clock::now().time_since_epoch();
	minutes min = duration_cast<minutes>(d);
	seconds sec = duration_cast<seconds>(d);
	milliseconds mil = duration_cast<milliseconds>(d);
	microseconds mic = duration_cast<microseconds>(d);
	nanoseconds nan = duration_cast<nanoseconds>(d);
	cout << min.count() << "分钟" << endl;
	cout << sec.count() << "秒" << endl;
	cout << mil.count() << "毫秒" << endl;
	cout << mic.count() << "微妙" << endl;
	cout << nan.count() << "纳秒" << endl;

	char time_str[4][16];
	std::time_t timestamp = mil.count();
	auto time_ptr = gettm(timestamp);
	sprintf(time_str[0], "%02d", time_ptr->tm_mon + 1); //月份要加1
	sprintf(time_str[1], "%02d", time_ptr->tm_mday);//天
	sprintf(time_str[2], "%02d", time_ptr->tm_hour);//时
	sprintf(time_str[3], "%02d", time_ptr->tm_min);// 分
	for (int i = 0; i < 4; i++)
	{
		std::cout << "time_str[" << i << "] is: " << time_str[i] << std::endl;
	}
}

 

https://blog.csdn.net/weixin_41855721/article/details/81810692

https://blog.csdn.net/xiangxianghehe/article/details/10557465

windows 获取时间戳


    system_clock::duration d = system_clock::now().time_since_epoch();
	minutes min = duration_cast<minutes>(d);
	seconds sec = duration_cast<seconds>(d);
	milliseconds mil = duration_cast<milliseconds>(d);
	microseconds mic = duration_cast<microseconds>(d);
	nanoseconds nan = duration_cast<nanoseconds>(d);
	cout << min.count() << "分钟" << endl;
	cout << sec.count() << "秒" << endl;
	cout << mil.count() << "毫秒" << endl;
	cout << mic.count() << "微妙" << endl;
	cout << nan.count() << "纳秒" << endl;

头文件:

#include <chrono>

命名空间:

using namespace std::chrono;

运行结果为:

25576390分钟
1534583456秒
1534583456057毫秒
1534583456057614微妙
1534583456057614000纳秒

之前我用的steady_clock::duration d = steady_clock::now().time_since_epoch();。经网友指出问题后,发现这么用确实有问题。在Linux系统中得到的是开机后的时长,而不是距epoch的时长。

获取时间戳 时间戳转日期

// 需要开启c++11支持,g++ test.cpp -std=c++11 -o test
// 这里默认是东八区北京时间格式
#include <iostream>
#include <chrono>
#include <cstdio>
using namespace std;

std::time_t getTimeStamp()
{
    std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
    auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
    std::time_t timestamp = tmp.count();
    //std::time_t timestamp = std::chrono::system_clock::to_time_t(tp);
    return timestamp;
}

std::tm *gettm(long long timestamp)
{
    auto milli = timestamp + (long long)8 * 60 * 60 * 1000; //此处转化为东八区北京时间,如果是其它时区需要按需求修改
    auto mTime = std::chrono::milliseconds(milli);
    auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
    auto tt = std::chrono::system_clock::to_time_t(tp);
    std::tm *now = std::gmtime(&tt);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
    return now;
}


int main()
{
    char time_str[4][16];
    auto t = getTimeStamp();
    std::cout << "Millisecond timestamp is: " << t << std::endl;
    auto time_ptr = gettm(t);
    sprintf(time_str[0], "%02d", time_ptr->tm_mon + 1); //月份要加1
    sprintf(time_str[1], "%02d", time_ptr->tm_mday);//天
    sprintf(time_str[2], "%02d", time_ptr->tm_hour);//时
    sprintf(time_str[3], "%02d", time_ptr->tm_min);// 分
    for(int i = 0; i < 4; i++)
    {
        std::cout << "time_str[" << i << "] is: " << time_str[i] << std::endl;
    }
}

时间戳转日期

std::tm *gettm(long long timestamp)
{
    auto milli = timestamp + (long long)8 * 60 * 60 * 1000; //此处转化为东八区北京时间,如果是其它时区需要按需求修改
    auto mTime = std::chrono::milliseconds(milli);
    auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
    auto tt = std::chrono::system_clock::to_time_t(tp);
    std::tm *now = std::gmtime(&tt);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
    return now;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值