I have been trying to run a c function on my computer, which gives the number of seconds elapsed since Epoch, and it seems to work fine
#include <stdio.h>
#include <time.h>
int main () {
time_t seconds;
seconds = time(NULL);
printf("Number of seconds since January 1, 1970 = %ld\n", seconds);
return(0);
}
Now, I have a micro-controller on which I try to run the same code. It always gives me time since boot of the micro-controller, and not since EPOCH (January 1, 1970). So, I am bit confused why I dont get the usual result ? Do I need connection to internet so that some server sends out the timer since EPOCH to the time.h library ?
In other words if I run this code on a computer without internet connection, will I get the time since EPOCH. If not then why ?
No, time.h do not require an internet connection. On a computer it ask the OS for current time and make some conversion to return Epoch. All PC computers have a clock chip with a battery, keeping time when power is off. Most OS use Internet to get time an make sure the clock has correct time. If no internet, then the clock still gives a reasonably correct time.
On a micro-controller, there is usually no clock chip (RTC) that keeps the time when power is down. You can buy RTC to connect to the micro controller and read probably it instead of using standard time functions.
Here is an example of real time clock to connect to a micro-controller.