time function in C always displays “Wed Dec 31 23:59:59 1969”

老子叫甜甜 提交于 2021-02-07 18:29:17

问题


I require the current date and time to be logged for my application. I have written the code in C. I have attached the code

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

int main()
{   time_t t;


     while(1)
     { time(&t);
       printf("Today's date and time : %s",ctime(&t));   
     } 

}

The output is

Today's date and time : Wed Dec 31 23:59:59 1969
Today's date and time : Wed Dec 31 23:59:59 1969
Today's date and time : Wed Dec 31 23:59:59 1969
Today's date and time : Wed Dec 31 23:59:59 1969

The time is not getting updated since the start of the UNIX time. I ran the same program in another computer and it ran just fine. Why do I get this error in my computer and How do i resolve it?

Thank you

Any help appreciated.

EDIT: There was a mistake in the code, I rectified it so that the time is updated within the while loop


回答1:


You have an error being returned by time, see the docs:

On error, ((time_t) -1) is returned, and errno is set appropriately.

Of course, a -1 relative to the EPOCH time is the date that is being printed. However, you're not storing or using the return value of time, so this means that t itself is -1 somehow. Are you posting the exact code you're using?

So since time returns -1 to signify you have an error, you have to check errno to see what the actual error is. However, apparently the only error that time should return is EFAULT, which in this context means:

t points outside your accessible address space.

UPDATE: Try doing this instead to see what happens:

time_t t = time(NULL);

There's not much of a reason to do it the way you were doing it.

If that's really the code you're using verbatim, then I can't account for how you're getting a -1 in t, since the -1 would be returned by time() but you're not accessing the return value in any way. This would mean that t would have had to already been -1. Given that it's uninitialized, I suppose that is possible, but I'm not sure if it's possible that t's uninitialized memory would always be -1 on every program run. Does anyone know? Still, it would also require that &t is somehow an invalid address for it to trigger an EFAULT, which would leave t's value of -1 unchanged.




回答2:


You haven't mentioned what operating system you are using. So it is unclear whether you have an RTC (Real Time Clock).

It is worth noting that :

Non-PC systems, such as embedded systems built around system-on-chip processors, use other implementations. They usually won't offer the same functionality as the RTC from a PC/AT.

As per the docs EFAULT happens when :

t points outside your accessible address space

However, I am not sure when this will happen.

In fact EFAULT is defined here as :

#define EFAULT          14      /* Bad address */


Your code gave me results similar to yours in the beginning. But this one worked for me :

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

int main()
{

    time_t *t;
    t=(time_t*)malloc(sizeof(*t));
    time(t);
    printf("Today's date and time : %s",ctime(t));
    free(t); //Clean up the mess we've created
    return 0;
}

But I don't know why.

Reference: RTC



来源:https://stackoverflow.com/questions/34469262/time-function-in-c-always-displays-wed-dec-31-235959-1969

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!