*******************求一时间加减函数*******************

lynx1111 2010-02-24 11:35:21
第一个输入参数:时间字符串,格式 2010-02-14 14:02:38 (如果此格式不方便,其他类似的格式也行)
第二个输入参数:数字,例子:8,-8
第三个输入参数:类型,共四种:D,H,M,S。表示天,小时,分,秒。
返回值为时间字符串

例子:
DatetimeAdd('2010-02-14 14:02:38',8,'D') 返回:2010-02-22 14:02:38
DatetimeAdd('2010-02-14 14:02:38',-8,'H') 返回:2010-02-14 06:02:38
DatetimeAdd('2010-02-14 14:02:38',-8,'M') 返回:2010-02-14 13:54:38
DatetimeAdd('2010-02-14 14:02:38',8,'S') 返回:2010-02-14 14:02:46

请写具体点,本人极端菜鸟。多谢!
...全文
257 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
最帅马老师 2010-02-24
  • 打赏
  • 举报
回复
引用 2 楼 hqin6 的回复:
C/C++ code#include<iostream>
#include<string>
#include<time.h>usingnamespace std;string DatetimeAdd(constchar* time,int num,char type)
{
time_t secs=0;switch (type)
{case'D':
secs= num*24*60*60;break;case'H':
secs= num*60*60;break;case'M':
secs= num*60;break;case'S':
secs= num;break;default:return"";
}struct tm _tmp;
strptime(time,"%Y-%m-%d %T",&_tmp);
time_t oldTime= mktime(&_tmp);
oldTime+= secs;struct tm* _res= localtime(&oldTime);char buf[20];
sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d", _res->tm_year+1900, _res->tm_mon+1, _res->tm_mday,
_res->tm_hour, _res->tm_min, _res->tm_sec);returnstring(buf);


}int main()
{char buf[20]="2010-02-14 14:02:38";
cout<< DatetimeAdd(buf,8,'D');return0;
}


正解
forster 2010-02-24
  • 打赏
  • 举报
回复
推荐COleDateTime类
是ctime的超集
ctime好像超过3000多年就异常了
COleDateTime支持到9999年
太乙 2010-02-24
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

string DatetimeAdd(const char* time, int num, char type)
{
time_t secs = 0;
switch (type)
{
case 'D':
secs = num * 24 * 60 * 60;
break;
case 'H':
secs = num * 60 * 60;
break;
case 'M':
secs = num * 60;
break;
case 'S':
secs = num;
break;
default:
return "";
}
struct tm _tmp;
strptime(time, "%Y-%m-%d %T", &_tmp);
time_t oldTime = mktime(&_tmp);
oldTime += secs;
struct tm* _res = localtime(&oldTime);
char buf[20];
sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d", _res->tm_year + 1900, _res->tm_mon + 1, _res->tm_mday,
_res->tm_hour, _res->tm_min, _res->tm_sec);

return string(buf);


}
int main()
{
char buf[20] = "2010-02-14 14:02:38";
cout << DatetimeAdd(buf, 8, 'D');
return 0;
}
最帅马老师 2010-02-24
  • 打赏
  • 举报
回复
大概思路先用ctime类进行转换,然后加或减去相应值,再转为字符串即可

早上工作忙,下午帮你整一个
dskit 2010-02-24
  • 打赏
  • 举报
回复
/*test.cpp*/

#include<iostream>
#include<ctime>
#include<stdlib.h>

using namespace std;

int main(int argc, char* argv[])
{

char time[] = "2009-10-25 12:34:23";
struct tm t;
sscanf(time,"%d-%d-%d %d:%d:%d",&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
t.tm_year -= 1900;
t.tm_mon -= 1;
int diff = 0;
char type = 'D';
scanf("%d %c", &diff, &type);
time_t cur_t = 0;
cur_t = mktime(&t);
switch(type)
{
case 'D':
cur_t = cur_t + diff * 24 * 60 * 60;
break;
case 'H':
cur_t += diff * 60 * 60;
break;
case 'M':
cur_t += diff * 60;
break;
case 'S':
cur_t += diff;
break;
}

printf(asctime(&t));
printf(ctime(&cur_t));
return 0;
}


c++ test.cpp
./a.exe
2 D
.....

./a.exe
-2 D
.....

65,187

社区成员

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

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