时间操作(struct tm、time_t)求指定日期 前n天的日期
1.在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:
#ifndef _TM_DEFINED
struct tm {
int tm_sec; /* 秒–取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值从1900开始 */
int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ typedef __int64 __time64_t; /* 64-bit time value */
typedef __time64_t time_t; /* time value */
/* time_t 是一种时间类型,一般用来存放格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数 */ #include <stdio.h>
#include <time.h> int GetNDaysGapDate(int iNowDate, int iNum)
{
struct tm ptm;
ptm.tm_year = iNowDate / 10000 % 10000 - 1900;
ptm.tm_mon = iNowDate / 100 % 100 - 1;
ptm.tm_mday = iNowDate % 100;
ptm.tm_hour = 0;
ptm.tm_min = 0;
ptm.tm_sec = 0; time_t timep;
timep = mktime(&ptm); //mktime把struct tm类型转换成time_t
timep += iNum * 24 * 60 * 60; ptm = *localtime(&timep); //localtime把time_t类型转换成struct tm return (ptm.tm_year + 1900) * 10000 + (ptm.tm_mon + 1) * 100 + ptm.tm_mday;
} int main()
{
int iDate = 20170120;
int n = 30;
int iPre30Date = GetNDaysGapDate(iDate, (-1)*n); //获取 iDate 30天前的日期 return 0;
} //距9:30的分钟数可以表示成:
min = ptm.tm_hour*60 + ptm.tm_min - (9*60 + 30); // long 型可直接赋值给 time_t 对象
long lTime = 1513318455;
time_t timestamp = 1513318455;
struct tm ptm;
ptm = *localtime(×tamp); //获取当前时间戳
time_t timep;
time(&timep);
cout << timep << endl; //数据库读取的 update_time(YYYY-mm-dd HH:MM::SS)格式转换成 struct tm
void strTimestampToStTm(const string strTimestamp, struct tm &stTm)
{
memset(&stTm, 0, sizeof(stTm)); sscanf(strTimestamp.c_str(), "%d-%d-%d %d:%d:%d", &stTm.tm_year, &stTm.tm_mon, &stTm.tm_mday, &stTm.tm_hour, &stTm.tm_min, &stTm.tm_sec); stTm.tm_year -= 1900;
stTm.tm_mon--;
}
时间操作(struct tm、time_t)求指定日期 前n天的日期的更多相关文章
- 将日期和时间作为 struct tm型的值直接向二进制文件进行读写
#include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...
- js获取当前指定的前几天的日期(如当前时间的前七天的日期)
这里就不多说了,直接贴上代码: <html> <head> <meta http-equiv="Content-Type" content=" ...
- c++ 时间类型详解 time_t
Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00 ...
- hive中时间操作(一)
转:https://blog.csdn.net/u012474716/article/details/78925319/ hive中常用的时间为时间戳和日期格式之间的转换 常用的函数为: to_dat ...
- 【hive 日期函数】Hive常用日期函数整理
1.to_date:日期时间转日期函数 select to_date('2015-04-02 13:34:12');输出:2015-04-02122.from_unixtime:转化unix时间戳到当 ...
- 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16
http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...
- struct tm 和 time_t 时间和日期的使用方法(转
关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元) .概念 在C/C++中,对字符串的操作有很多值得注意的问题,同样,C ...
- C语言中两种方式表示时间日期值time_t和struct tm类型的相互转换
使用gmtime函数或localtime函数将time_t类型的时间日期转换为structtm类型: 使用time函数返回的是一个long值,该值对用户的意义不大,一般不能根据其值确定具体的年.月.日 ...
- [Boost]boost的时间和日期处理-(1)日期的操作
<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...
随机推荐
- 深度学习图像标注工具VGG Image Annotator (VIA)使用教程
VGG Image Annotator (VIA)是一款开源的图像标注工具,由Visual Geometry Group开发. 可以在线和离线使用,可标注矩形.圆.椭圆.多边形.点和线.标注完成后,可 ...
- introduction to python for statistics,analysis笔记2
一.行列式连接concatenate函数,axis=0是垂直拼接,axis=1是水平拼接 x=np.array([[],[,]]); y=np.array([[],[,]]); z=np.concat ...
- log4net 存储到oracle 调试 Could not load type [log4net.Appender.OracleAppender]
近期在弄webfrom oracle 调用 log4net 開始调试时不出数据,打开了log4net 自己的debug功能后发现: log4net: Logger [root] level set t ...
- [Linux]运维三十六计--腾讯两位大神的总结
这里是腾讯两位大神梁定安.周小军总记得运维DBA三十六计,So有道理
- nfs missing codepage or helper program, or other error
[root@xxxxx ~]# /bin/mount -t nfs -o nosuid,noexec,nodev,noatime,intr,rsize=,wsize= xxx.xxx.xxx.xxx: ...
- CCTableView(一)
#ifndef __TABLEVIEWTESTSCENE_H__ #define __TABLEVIEWTESTSCENE_H__ #include "cocos2d.h" #in ...
- [转帖]cocos2d-x 3.0rc开发指南:Windows下Android环境搭建
原文请看:http://blog.csdn.net/linzhengqun/article/details/21663341 鲜红字体请注意:文中红色字体乃是本文博主阳光下的蒲公英添加.红色字体部分造 ...
- .net 中异步SOCKET发送数据时碰到的内存问题
做CS的开发一直都是这样的方式: server端用 C++编写,采用IOCP机制处理大量客户端连接.数据接收发送的问题 client端用 C++ 或C# 写,没什么特殊要求. 最近工作时间上比较宽裕, ...
- vue路由配置,vue子路由配置
上一篇关于vue环境配置已经写好了!按照操作就行了! 现在一个项目已经部署完成,接下来我们从路由开始! 还记得在初始化项目的时候,有提示是否需要安装vue-router,对没错,vue中路由全靠它! ...
- dp:FZU2030括号问题
http://acm.fzu.edu.cn/problem.php?pid=2030 给出一个字符串,其中包括3种字符: ‘(‘, ‘)’, ‘?’.其中?表示这个字符可以是’(‘也可以是’)’. 现 ...