怎么将一个 time_t 的时间加上 n 秒,然后得到一个新的时间?

laiwen68 2011-02-24 11:44:32
就像求两个 time_t 时间之间的差,用difftime, 
那么,已知一个 time_t 时间和时间跨度,求一下新的 time_t 时间,用什么办法?

谢谢!!!
...全文
2112 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

double __cdecl _difftime32 (
__time32_t b,
__time32_t a
)
{
_VALIDATE_RETURN_NOEXC(
( ( a >= 0 ) && ( b >= 0 ) ),
EINVAL,
0
)

return( (double)( b - a ) );
}

就求了个差值,还真不明白多这个函数干嘛用的
mstlq 2011-02-24
  • 打赏
  • 举报
回复
time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。在time.h中,我们也可以看到time_t是一个长整型数:

#ifndef _TIME_T_DEFINED
typedef long time_t; /* 时间值 */
#define _TIME_T_DEFINED /* 避免重复定义 time_t */
#endif

所以,过了多少秒直接加就是
laiwen68 2011-02-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yunchao630 的回复:]

直接做加减法就行吧
[/Quote]

那么,为什么求两个 time_t 的差,还要用一个 difftime 函数 ?
laiwen68 2011-02-24
  • 打赏
  • 举报
回复
回#1楼:

感谢你的回复,不过,根据那个结构自己去做函数太麻烦了,还要考虑润月等的问题,还要花时间去调试.....

我想这种时间想加的需求,应该是最常见的需求之一,不知道有没有现成的成熟代码?
翅膀又硬了 2011-02-24
  • 打赏
  • 举报
回复
直接做加减法就行吧
「已注销」 2011-02-24
  • 打赏
  • 举报
回复
结合time结构应该可行。
参考:
C语言的标准库函数包括一系列日期和时间处理函数,它们都在头文件中说明。下面列出了这些函数。在头文件中定义了三种类型:time_t,struct tm和clock_t。
在中说明的C语言时间函数
time_t time(time_t *timer);
double difftime(time_t time1,time_t time2);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr);
time_t mktime(struct tm *timeptr);
clock_t clock(void);
下面是我从网上收集到的时间函数集
asctime(将时间和日期以字符串格式表示)
相关函数
time,ctime,gmtime,localtime
表头文件
#i nclude
定义函数
char * asctime(const struct tm * timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:"Wed Jun 30 21:49:08 1993\n"
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
yui 2011-02-24
  • 打赏
  • 举报
回复
看这个就明白了

http://www.epochconverter.com/programming/functions-c.php

now += n_seconds;
loveanybody 2011-02-24
  • 打赏
  • 举报
回复
time_t date = timeGetTime();
date+=28800; //增加28800毫秒
struct tm *m_tm;
int year=0,mon=0,day=0,hour=0,min=0,sec=0;
m_tm = gmtime(&date);

year = m_tm->tm_year+1900;
mon = m_tm->tm_mon+1;
day = m_tm->tm_mday;
hour = m_tm->tm_hour;
min = m_tm->tm_min;
sec = m_tm->tm_sec;
loveanybody 2011-02-24
  • 打赏
  • 举报
回复
直接用 time_t date = timeGetTime()就可以了 这个获取出来的时间是格林尼治时间 如果想变成北京时间必须在data的基础上再加个28800毫秒 才是正确的北京时间 另外在 得到年的同时要+ 1900 才能得到现在的时间。
justkk 2011-02-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 laiwen68 的回复:]
引用 2 楼 yunchao630 的回复:
直接做加减法就行吧
那么,为什么求两个 time_t 的差,还要用一个 difftime 函数 ?
[/Quote]

NOTES
On a POSIX system, time_t is an arithmetic type, and one could just define

#define difftime(t1,t0) (double)(t1 - t0)

when the possible overflow in the subtraction is not a concern. On other systems, the data type time_t might use some other encoding where subtraction
doesn't work directly.
赵4老师 2011-02-24
  • 打赏
  • 举报
回复
COleDateTime
COleDateTimeSpan
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
COleDateTime t;
COleDateTimeSpan ts;
CString s,fmt;
int nYear;
int nMonth;
int nDay;
int nHour;
int nMin;
int nSec;
int lDays;
int nHours;
int nMins;
int nSecs;
int i,N;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
printf("Fatal Error: MFC initialization failed\n");
return 1;
}
if (argc<13) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 2;
}
if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";
else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";
else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";
else {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 3;
}
nYear =atoi(argv[ 1]);
nMonth=atoi(argv[ 2]);
nDay =atoi(argv[ 3]);
nHour =atoi(argv[ 4]);
nMin =atoi(argv[ 5]);
nSec =atoi(argv[ 6]);
lDays =atoi(argv[ 7]);
nHours=atoi(argv[ 8]);
nMins =atoi(argv[ 9]);
nSecs =atoi(argv[10]);
N =atoi(argv[11]);
if (N<=0) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 4;
}
t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);
ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );
for (i=1;i<=N;i++) {
s=t.Format(fmt);
printf("%08d %s\n",i,s);
t=t+ts;
}
return 0;
}

65,176

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧