4

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

Injitea
  • 69
  • 6
  • Did you check for `errno`? – Sourav Ghosh Dec 26 '15 at 07:07
  • Also, what's the date set in your computer clock? – Sourav Ghosh Dec 26 '15 at 07:10
  • You need to call `time()` every time you want a time. `ctime(&t)` will keep on printing the time as it was when `time()` was called. So your code will produce the same output. forever. As to why printing that particular time, the value is consistent with `t` having a value `-1`, which is reserved to indicate an error. Check that your system clock is set correctly (e.g. with an appropriate system utility). And check that the CMOS battery (which is used to keep the system clock ticking over, particularly if the computer is powered down) is not flat. If it is flat, replace it. – Peter Dec 26 '15 at 07:16
  • Add a check before the loop, `if ( t == (time_t)-1 ) { printf("time unavailable\n"; return 0; }` – M.M Dec 26 '15 at 07:19
  • 3
    Could you describe the hardware and operating system you are running on where this happens? Can you inspect the system clock some other way (not using a C program) ? – M.M Dec 26 '15 at 07:19
  • @sjsam well it might, if OP is on a system with no clock it would explain the result – M.M Dec 26 '15 at 08:09
  • @M.M : Thanks for pointing that. – sjsam Dec 26 '15 at 08:15
  • @SouravGhosh Computer's Date and time are correct, today's date 26th dec 2015 13:26 hours. I haven't checked with errno. I will. Thank you – Injitea Dec 26 '15 at 09:31
  • 1
    A good rule of thumb is to *initialize* most local variables. So you should perhaps declare `time_t t=0;` – Basile Starynkevitch Dec 26 '15 at 09:32

2 Answers2

5

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.

Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
  • Regarding `but you're not accessing the return value in any way.` The [doc](http://man7.org/linux/man-pages/man2/time.2.html) says _If t is non-NULL, the return value is also stored in the memory pointed to by t._ – sjsam Dec 28 '15 at 06:31
  • Indeed, but the only reason that `time()` would set it to -1 is if errno is EFAULT, which means that the `t` is "outside of the accessible address space," which would presumably mean that `time()` wasn't able to access `t`, let alone set its value to -1. This is why I'm wondering if perhaps `t` was set to -1 before the call to `time()`, somehow. – Jorge Israel Peña Dec 28 '15 at 06:40
  • `This is why I'm wondering if perhaps t was set to -1 before the call to time(), somehow`. I guess this is not the case. But then, don't know ;) – sjsam Dec 28 '15 at 07:21
  • Yeah, we need more feedback from the OP, but they're not communicating. – Jorge Israel Peña Dec 28 '15 at 07:22
-1

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

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • `&t` could not be outside the address space – M.M Dec 26 '15 at 22:06
  • @anand-krish : Could you try this. This may not be a solution but I am curious to know the output. – sjsam Dec 28 '15 at 06:53
  • @M.M : I might agree. I find no reason for `&t` to be outside the address space. The op's program initially gave me similar output to what he has mentioned. But this one gave me the right time.. – sjsam Dec 28 '15 at 07:00
  • @anand-krish : If you're on a linux machine you could try running your code as root. – sjsam Dec 28 '15 at 07:15