首页 > 编程语言 >C++中UNIX时间戳与日期互转

C++中UNIX时间戳与日期互转

时间:2024-03-03 15:00:12浏览次数:14  
标签:-% 02d int lt C++ UNIX tm year 互转

C++中UNIX时间戳与日期互转

  1. 使用time.h头文件
  2. localtime 可以把时间戳转为 tm 结构体, tm结构体中可以格式化输出时间
  3. mktime可以把tm结构体转为时间戳
  4. tm 结构体中: year需要+1900, tm_mon的范围是0-11, tm_mday的范围是1-31
    struct tm
    {
        int tm_sec;   // seconds after the minute - [0, 60] including leap second
        int tm_min;   // minutes after the hour - [0, 59]
        int tm_hour;  // hours since midnight - [0, 23]
        int tm_mday;  // day of the month - [1, 31]
        int tm_mon;   // months since January - [0, 11]
        int tm_year;  // years since 1900
        int tm_wday;  // days since Sunday - [0, 6]
        int tm_yday;  // days since January 1 - [0, 365]
        int tm_isdst; // daylight savings time flag
    };

     

  5. 示例代码
    #define _CRT_SECURE_NO_WARNINGS
    
    #include <string>
    #include <time.h>
    #include <iostream>
    
    using namespace std;
    
    void int_to_time(long long int u)
    {
    	tm* lt = localtime(&u);
    	printf("%d-%02d-%02d %02d:%02d:%02d\n", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
    }
    
    tm* build_string_line(string s)
    {
    	int year, month, day, hh, mm, ss;
    	int _e = sscanf(s.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hh, &mm, &ss);
    	tm* lt = new tm();
    	lt->tm_year = year - 1900;
    	lt->tm_mon = month - 1;
    	lt->tm_mday = day;
    	lt->tm_hour = hh;
    	lt->tm_min = mm;
    	lt->tm_sec = ss;
    	return lt;
    }
    
    void time_to_int(tm* lt)
    {
    	//printf("%d-%02d-%02d %02d:%02d:%02d\n", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
    	long long int u2 = mktime(lt);
    	printf("%lld\n", u2);
    }
    

tm* build_string_line(string s)
{
	int year, month, day, hh, mm, ss;
	int _e = sscanf(s.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hh, &mm, &ss);
	tm* lt = new tm();
	lt->tm_year = year - 1900;
	lt->tm_mon = month - 1;
	lt->tm_mday = day;
	lt->tm_hour = hh;
	lt->tm_min = mm;
	lt->tm_sec = ss;
	return lt;
}

void time_to_int(tm* lt)
{
	//printf("%d-%02d-%02d %02d:%02d:%02d\n", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
	long long int u2 = mktime(lt);
	printf("%lld\n", u2);
}

 

标签:-%,02d,int,lt,C++,UNIX,tm,year,互转
From: https://www.cnblogs.com/JiangOil/p/18050058

相关文章

  • pytorch报错:Variable._execution_engine.run_backward( # Calls into the C++ engine
    GPU模式下运行pytorch代码报错,pytorch为2.2.1,NVIDIA驱动版本535.161.07File"/home/devil/anaconda3/envs/sample-factory/lib/python3.11/site-packages/torch/_tensor.py",line522,inbackwardtorch.autograd.backward(File"/home/devil/anaconda3/envs/sample-......
  • Halcon HObject和C#的Bitmap图像互转
    转载:https://blog.51cto.com/u_15088375/3247784#:~:text=HalconHObject和C%23的Bitmap图像互转1HalconHObject和C%23的Bitmap图像互转2一,Bitmap转HObject3方法一:HOperatorSet.GenImageInterleaved直接转换4方法二:C%23获取图像各个通道内存首地址和HOperatorSet.GenIma......
  • C++新U4-贪心算法1
    学习目标贪心算法的概念[【贪心算法(一)】书架]    【题意分析】选出最少的奶牛数量,让他们的身高相加超过书架的高度。【思路分析】优先选择身高高的奶牛,这样子奶牛使用的数量最少。定义排序规则,将数从大到小排序定义奶牛数量n和书架高度b,并且输入输......
  • docker Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is th
    应该是docker异常关闭导致无法启动。步骤1参考《https://www.cnblogs.com/langgeligelang/p/14607658.html》执行systemctldaemon-reloadsystemctlrestartdocker如果失败,提示Authorizationnotavailable.Checkifpolkitserviceisrunningorseedebugmessagef......
  • C++第二课 while循环
    循环while(条件){   循环体}#include<bits/stdc++.h>usingnamespacestd;intmain(){   inti,s;   i=1;   s=0;   while(i<=100)   {      s=s+i;      i=i+1;   }   cout<<s<<endl;   return0;}计算1......
  • 是学习c++还是java?
    上高中时,自己第一次接触到学校的中华学习机和苹果机,当时中华学习机上可以进行basic编程,那时候自己其实就爱上编程,但是没有人指点,也学习不得法,所以就没有进行下去!大学时,自己的主攻专业并不是计算机,但是学习了《计算机基础》和《c程序设计》,前者主要学习dos命令和wps文字处理,后者主......
  • C++第一课 输出Hello World
    #include<bits/stdc++.h>usingnamespacestd;intmain(){ }这是一个固定的格式,记住就行了。#include<bits/stdc++.h>usingnamespacestd;intmain(){   cout<<"Hello,World!"<<endl;   return0;}这是一个简单的输出Hello,World! #include<bits/stdc......
  • Qt/C++音视频开发67-保存裸流加入sps/pps信息/支持264/265裸流/转码保存/拉流推流
    一、前言音视频组件除了支持保存MP4文件外,同时还支持保存裸流即264/265文件,以及解码后最原始的yuv文件。在实际使用过程中,会发现部分视频文件保存的裸流文件,并不能直接用播放器播放,查阅资料得知原来是缺少sps/pps信息,监控行业的rtsp/rtmp/录像mp4文件都是会带的,所以很少遇到这个......
  • C++ 多态
    原文多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。C++多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。1#include<iostream>2usingnamespacestd;3classShape{4protected:5......
  • C++第七节课 new开辟空间 delete释放空间
    #include<iostream>usingnamespacestd;//C中开辟空间的方式所有的返回值都是void*///int*p=(int*)malloc(sizeof(int))///malloc在堆上开辟空间并没有进行初始化//////int*pa=(int*)calloc(1,sizeof(int));///calloc在堆上开辟空间是有初始化的......