时间操作(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 ...
随机推荐
- C# 动态获取代码所在行号
通过System.Diagnostics.StackTrace获取代码所在行号和文件信息 获取行号信息 /// <summary> /// Get line number of code ...
- jenkins 发送邮件模板
jenkins 发送邮件模板 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 【转】js frame 框架编程
源地址:http://www.blogjava.net/lusm/archive/2008/02/11/179620.html 1 框架编程概述 一个Html 页面可以有一个或多个子框架,这些子框架以 ...
- libgdx 1.4.1公布
(转载自http://www.libgdx.cn/topic/4/libgdx-1-4-1%E5%8F%91%E5%B8%83) libgdx从未停止进步的脚步.10月10日.libgdx1.4.1公 ...
- Nginx用户认证配置方法详解(域名/目录)
Nginx超级强大它可以单独为一个域名设置用户认证,方法也很简单我们只要生成用户认证的用户名和密码,然后再Nginx添加auth认证配置即可 Nginx可以为某一个域名单独加用户认证,具体做法如下: ...
- ORACLE 11G在存储过程里面遍历游标, 调用job任务定时运行
ORACLE存储过程里游标遍历.调用job定时运行 1,第一种使用For 循环 for循环是比較简单有用的方法. 首先.它会自己主动open和close游标.攻克了你忘记打开或关闭游标的烦恼. 其次, ...
- 深入讲解Android Property机制
深入讲解Android Property机制 侯亮 1 概述 Android系统(本文以Android 4.4为准)的属性(Property)机制有点儿类似Windows系统的注册表,其中的 ...
- SourceInsight-Symbol not found
使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...
- Artificial-Intelligence BOOKs与算法
http://mindhacks.cn/2008/09/11/machine-learning-and-ai-resources/ https://www.amazon.com/Information ...
- vue实现复制粘贴的两种形式
方式一: 1.安装clipboard:npm install clipboard 2.src/utils/clipboard.js import Vue from 'vue' import Clipb ...