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(&timestamp); //获取当前时间戳
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天的日期的更多相关文章

  1. 将日期和时间作为 struct tm型的值直接向二进制文件进行读写

    #include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...

  2. js获取当前指定的前几天的日期(如当前时间的前七天的日期)

    这里就不多说了,直接贴上代码: <html> <head> <meta http-equiv="Content-Type" content=" ...

  3. c++ 时间类型详解 time_t

    Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00 ...

  4. hive中时间操作(一)

    转:https://blog.csdn.net/u012474716/article/details/78925319/ hive中常用的时间为时间戳和日期格式之间的转换 常用的函数为: to_dat ...

  5. 【hive 日期函数】Hive常用日期函数整理

    1.to_date:日期时间转日期函数 select to_date('2015-04-02 13:34:12');输出:2015-04-02122.from_unixtime:转化unix时间戳到当 ...

  6. 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16

    http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...

  7. struct tm 和 time_t 时间和日期的使用方法(转

    关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元) .概念 在C/C++中,对字符串的操作有很多值得注意的问题,同样,C ...

  8. C语言中两种方式表示时间日期值time_t和struct tm类型的相互转换

    使用gmtime函数或localtime函数将time_t类型的时间日期转换为structtm类型: 使用time函数返回的是一个long值,该值对用户的意义不大,一般不能根据其值确定具体的年.月.日 ...

  9. [Boost]boost的时间和日期处理-(1)日期的操作

    <开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...

随机推荐

  1. shell教程一:字符串操作

    一:Linux shell字符串截取与拼接 假设有变量 var=http://www.linuxidc.com/123.htm 1  # 号截取,删除左边字符,保留右边字符. echo ${var#* ...

  2. 【Android】20.0 第20章 音频、视频、拍照、语音合成

    分类:C#.Android.VS2015: 创建日期:2016-03-11 一.简介 Android提供了常见的多媒体文件编码.解码机制,你可以直接调用Android提供的API,实现相册.播放器.录 ...

  3. nyoj905 卡片游戏

    卡片游戏 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 小明最近宅在家里无聊,于是他发明了一种有趣的游戏,游戏道具是N张叠在一起的卡片,每张卡片上都有一个数字,数字 ...

  4. iOS登录单例

    iOS登录单例 一,工程图. 二,代码. UserInfo.h #import <Foundation/Foundation.h> @interface UserInfo : NSObje ...

  5. Exception时信息的记录

    系统总有出现异常的时候,那么出现异常时应该如何处理? 一直以来,我都以为这么处理就足够的: 在日志中打印Exception的堆栈信息,以便排查原因 反馈给用户系统xxx出现问题 package com ...

  6. feginclient demo

    1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  7. appium安卓自动化的 常用driver方法封装

    appium安卓自动化的 常用driver方法封装 做安卓自动化的时候,很多方法写起来会造成代码冗余,把这部分封装起来 ,添加到androidUI工具类里,随时可调用 都放在这个类下面: @Compo ...

  8. Linux Jenkins配置Git

    1.卸载Centos自带的git1.7.1:通过git –version查看系统带的版本,Centos应该自带的是git版本是1.7.1 终端输入:yum remove git 2.安装所需软件包 终 ...

  9. 说说http协议中的编码和解码

    http://www.csdn1 2 3.com/html/itweb/20130730/29422_29378_29408.htm ****************************** 一. ...

  10. python 字典格式嵌套,相同项做叠加

    all_dict = {} for tg_id in ['com.qq_a','com.qq_b','com.qq_c','com.qq_c']: tmp_dict = all_dict.get(tg ...