如何一天添加到从时间而获得的时间() [英] How to add one day to a time obtained from time()

查看:125
本文介绍了如何一天添加到从时间而获得的时间()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我psented为午夜以来经过的秒数,1970年1月1日,时间再$ P $,UTC(先前调用的结果,时间())。我如何添加一天这个时间呢?

I have a time represented as the number of seconds elapsed since midnight, January 1, 1970, UTC (the results of an earlier call to time()). How do I add one day to this time?

添加在大多数情况下,24 * 60 * 60作品,但是,如果夏令时来打开或关闭在之间失败。换句话说,我主要是想增加24小时,但有时23或25个小时。

Adding 24 * 60 * 60 works in most cases, but fails if the daylight saving time comes on or off in between. In other words, I mostly want to add 24 hours, but sometimes 23 or 25 hours.

要说明 - 程序:

#include <time.h>
#include <iostream>

int main()
{
  time_t base = 1142085600;
  for(int i = 0; i < 4; ++i) {
    time_t time = base + i * 24 * 60 * 60;
    std::cout << ctime(&time);
  }
  return 0;

}

产地:

Sat Mar 11 08:00:00 2006
Sun Mar 12 09:00:00 2006
Mon Mar 13 09:00:00 2006
Tue Mar 14 09:00:00 2006

我要为12年3月13日,与时俱进,......到上午八时。

I want the times for March 12, 13, ... to also be 8 AM.


由FigBug提供的答案我指出了正确的方向。但我不得不本地时间,而不是使用gmtime的。

The answer provided by FigBug pointed me in the right direction. But I had to use localtime instead of gmtime.

int main()
{
  time_t base = 1142085600;
  for(int i = 0; i < 4; ++i) {
    struct tm* tm = localtime(&base);
    tm->tm_mday += i;
    std::cout << asctime(tm);
 }
 return 0;
}

给我:

Sat Mar 11 08:00:00 2006
Sat Mar 12 08:00:00 2006
Sat Mar 13 08:00:00 2006
Sat Mar 14 08:00:00 2006

这就是我想要的。使用gmtime的给我次14:00:00

Which is what I want. Using gmtime gives me the times at 14:00:00

但是请注意,所有的日子都是星期六此外,它进入三月32,33,等等。如果我扔在mktime函数我回到那里,我开始:

However, note that all days are Sat. Also, it goes to March 32, 33, etc. If I throw in the mktime function I am back where I started:

#include <time.h>
#include <iostream>

int main()
{
  time_t base = 1142085600;
  for(int i = 0; i < 4; ++i) {
    struct tm* tm = localtime(&base);
    tm->tm_mday += i;
    time_t time = mktime(tm);
    std::cout << asctime(tm);
 }
 return 0;
}

给我:

Sat Mar 11 08:00:00 2006
Sun Mar 12 09:00:00 2006
Mon Mar 13 09:00:00 2006
Tue Mar 14 09:00:00 2006

我缺少什么???

What am I missing???


OK,我已经尝试了FigBug的是使用最新的建议是:

OK, I have tried out FigBug's latest suggestion that is to use:

 std::cout << ctime(&time);

而不是asctime,但我得到了相同的结果。所以我想,我的库和/或编译器搞砸。我使用的G ++ 3.4.4 cygwin的。我将文件复制到的Solaris 5.8,使用G ++ 3.3没有进行编译。我到那里正确的结果!事实上,我得到正确的结果我是使用的ctime或asctime输出:

instead of asctime, but I get the same results. So I guess that my library and/or compiler is messed up. I am using g++ 3.4.4 on cygwin. I copied the files over to Solaris 5.8 and used g++ 3.3 there to compile. I get the correct results there! In fact I get the correct results whether I use ctime or asctime for output:

Sat Mar 11 08:00:00 2006
Sun Mar 12 08:00:00 2006
Mon Mar 13 08:00:00 2006
Tue Mar 14 08:00:00 2006

我也得到红色小屋Linux的正确的结果(与两个输出功能)使用g ++ 3.4.6。

I also get the correct results (with both output functions) on Red Hut Linux with g++ 3.4.6.

所以我想,我所遇到的一个Cygwin的错误。

So I guess that I have come across a Cygwin bug.

感谢各位的帮助和建议....

Thank you for all your help and advice....

推荐答案

使用的 gmtime的()的转换的 time_t的结构TM

添加一个到天( tm_mday

使用的 mktime()根据的转换的的结构TM 的回 time_t的

use mktime() to convert the struct tm back to a time_t

请参见 time.h中了解更多信息。

编辑:

我刚刚试了一下,这个作品:

I just tried it, this works:

int main()
{
  time_t base = 1142085600;
  for(int i = 0; i < 4; ++i) {
    struct tm* tm = localtime(&base);
    tm->tm_mday += i;
    time_t next = mktime(tm);
    std::cout << ctime(&next);
 }
 return 0;
}

这篇关于如何一天添加到从时间而获得的时间()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆