Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言将长数字转换为日期、时间_C_Time.h - Fatal编程技术网

用C语言将长数字转换为日期、时间

用C语言将长数字转换为日期、时间,c,time.h,C,Time.h,我正在解决spoj问题 其中,当前时间戳为sqaured,然后除以400000007,剩余部分转换为dat时间 下面是我使用的技巧 remainder = result%4000000007 ; printf("%ul\n",remainder); //convert the timestamp into date , time dt = localtime(&remainder); // use any strftime format spec here // str

我正在解决spoj问题 其中,当前时间戳为sqaured,然后除以400000007,剩余部分转换为dat时间 下面是我使用的技巧

remainder = result%4000000007 ; 

printf("%ul\n",remainder); 


//convert the timestamp into date , time 

dt = localtime(&remainder); 
// use any strftime format spec here 
// strftime(b, sizeof(b), "%m%d%H%M%y", dt); 
// fprintf(stdout, "%s", b); 
// printf("%s\n",asctime(localtime(&remainder))); 
 ltime = (time_t)remainder; 
 printf("%s\n",asctime(dt)); 
 printf("%s\n",ctime(&ltime));
显示的错误是分段错误,在
asctime
,同时
ltime
是时间变量,'b'是缓冲区,
dt
是struct tm*,并且我尝试将剩余部分转换为
time\t
变量,但仍然无效。另外,注释代码显示了我的尝试。 使用调试器,我发现asctime返回null


问题出在哪里?

很难从给出的代码中准确地说出什么是错的。但是,在您的评论中,您指出
余数
的类型为
unsigned long
。但是,您将其地址传递给
localtime()
,这是一个错误
localtime()
需要
时间*

可能发生的情况是,由于传入了错误的类型,
localtime()
检测到一些错误并返回了一个
NULL
值。然后,将该值传递到
asctime()
也会导致
NULL
返回值


另外,您的
printf
的说明符错误。使用
%llu
打印
无符号长字符

是否为余数并至少产生一个长字符(64位整数)?此外,是否有启用了/wAll的编译器警告?是的,它们是无符号长的long@MichaelDorgan:但我不确定它们是否为64位,并且似乎没有崩溃(请参阅:)。显示更多代码?@CodeJack:那么您将收到一条警告,并将其传递到
localtime
。清除所有警告,然后查看它是否仍然崩溃。在没有警告的情况下显示代码。是的,因此我建议使用/wall。警告真的很重要:)@MichaeldOrganger,@user315052:如果时间戳是由用户给出的,那么我们需要将字符串输入转换为类型
time\u t
,因此我可以为此使用
strtoull
??我的意思是,如何将输入直接扫描到
time\t
变量中???@CodeJack:您可以,也可以将
余数
分配给
time\t
变量。您已经使用
ltime
执行了该操作。语句
scanf(“%llu”,ltime)可以吗???@code杰克:不,看