请问如何将UTC秒数转换为日期,100分,在线等

ahduo 2008-05-02 09:17:16
我现在有一个整型值,就是从1970.1.1 0:00以来的秒数,
请问在C语言里面,如何能得到日期,就是用DDMMYY表示?

谢谢。
...全文
1141 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahduo 2008-05-02
  • 打赏
  • 举报
回复
好的,明白了,谢谢。结帐。
baihacker 2008-05-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ahduo 的回复:]
baihacker你好。

你用time_t result = time(NULL);这样是让系统给出当前时间。
但是我这里秒数(或者分钟数)是已知的,可能不是当前时间。
我主要是不清楚,我只有一个秒数或者分钟数的“整型值”,如何得到日期。
[/Quote]

明显是不对的,只有一个秒数或者分钟数,是得不出任何信息的

如果知道的是今年的秒数那有办法,我在4楼给的是笨办法
聪明的办法如下
1.转换为秒数,赋给一个time_t
2.调用我在2楼的函数转换为一个struct tm
3.struct tm内的年份设置为今年的
4.如果今年不是闰年,struct tm就是你所求的
5.如果今年是闰年,且在2.28以后,那么减去一天.
ahduo 2008-05-02
  • 打赏
  • 举报
回复
当然,实在不行,就只有像你说的自己算了。呵呵。
ahduo 2008-05-02
  • 打赏
  • 举报
回复
恩,谢谢,你的思路我大概明白。
我主要的问题是,我不知道怎么用一个整型值,秒数
来构造一个struct tm
所以就用不了localtime这样的函数。
ahduo 2008-05-02
  • 打赏
  • 举报
回复
baihacker你好。

你用time_t result = time(NULL);这样是让系统给出当前时间。
但是我这里秒数(或者分钟数)是已知的,可能不是当前时间。
我主要是不清楚,我只有一个秒数或者分钟数的“整型值”,如何得到日期。
baihacker 2008-05-02
  • 打赏
  • 举报
回复
1.乘以60,加上前N多年的秒数,然后用上面的函数呗...

2.除以60换算为小时,再除以24换算为天,然后就可以算出来是哪个日期了..(注意闰年)

ahduo 2008-05-02
  • 打赏
  • 举报
回复
不好意思,问题写错了一点。

我现在有的这个整型数是当年过去的分钟数,现在想求道目前的日期和时间。
baihacker 2008-05-02
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <time.h>
#include <string.h>
int main()
{
time_t result = time(NULL);
struct tm t;
memcpy(&t, localtime(&result), sizeof(struct tm));
printf("%d-%d-%d\t%d:%d:%d", t.tm_year + 1900, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
return 0;
}
baihacker 2008-05-02
  • 打赏
  • 举报
回复
// crt_mktime.c
/* The example takes a number of days
* as input and returns the time, the current
* date, and the specified number of days.
*/

#include <time.h>
#include <stdio.h>

int main( void )
{
struct tm when;
__time64_t now, result;
int days;
char buff[80];

time( &now );
_localtime64_s( &when, &now );
asctime_s( buff, sizeof(buff), &when );
printf( "Current time is %s\n", buff );
days = 20;
when.tm_mday = when.tm_mday + days;
if( (result = mktime( &when )) != (time_t)-1 ) {
asctime_s( buff, sizeof(buff), &when );
printf( "In %d days the time will be %s\n", days, buff );
} else
perror( "mktime failed" );
}

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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