I have never programmed in C and therefore it is being a real challenge to write a small piece of software which returns the difference between two dates in seconds.
Just as background information, I implemented a kind of heart beat in a Python application with updates a txt file every 15 seconds. Now I would like to create a C application that checks this txt constantly and in case the difference is greater than 30 seconds, it restarts my computer.
That is how far I went so far:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
//Returns the number seconds since the last time the files has been updated
int lastUpdate() {
struct stat attr;
stat("watcher.txt", &attr);
//Gets the last update in the following format: Mon Aug 13 08:23:14 2012
//ctime(&attr.st_mtime);
time_t lastChange = &attr.st_mtime
time_t now = time(0)
//No idea how to implement it :-(
//int seconds = timediff(now, lastChange)
return seconds;
}
//Constantly checks if application is sending heart beats in a 30 seconds time frame
main()
{
while(1 == 1)
{
if(lastUpdate() > 30)
{
sprintf(cmd, "sudo reboot -i -p");
system(cmd);
}
}
}
Would anyone be so nice and give some hints on how to get this working?
Thank you very much!
EDITED:
Final code working without issues:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
//Returns the number seconds since the last time the files has been updated
int lastUpdate() {
struct stat attr;
stat("/home/pi/watcher.txt", &attr);
time_t lastChange = attr.st_mtime;
time_t now = time(0);
int seconds = now - lastChange;
return seconds;
}
//Constantly checks if application is sending heart beats in a 30 seconds time frame
main()
{
sleep(120);
while(1 == 1)
{
sleep(1);
if(lastUpdate() > 30)
{
system("sudo reboot -i -p");
}
}
}
You're in luck! time_t is a number, and the timestamps are the time since the beginning of 1970 in seconds. So:
int seconds = now - lastChange;
Oh, and
time_t lastChange = &attr.st_mtime
should be
time_t lastChange = attr.st_mtime
time_t is typically the number of seconds since Jan 1, 1970 0:00:00 UTC (GMT ). So a simply subtraction will work.
time_t lastChange = &attr.st_mtime
time_t now = time(0)
time_t seconds = now - lastChange;
printf("%ll\n", (long long) seconds);
Stricly speaking, time_t may be some other scalar.
A portable solution is to use double difftime(time_t time1, time_t time0);
#include <time.h>
double seconds = difftime(now, lastChange);
printf("%f\n", seconds);
You just need to subtract the time_t's but you have a problem in this line
time_t lastChange = &attr.st_mtime
because stat.st_mtime is of type time_t not time_t *, so you should change it to
time_t lastChange = attr.st_mtime
and then
return new - lastChange;
and also, check stat("watcher.txt", &attr) != -1
Related
I need the fastest way to get the localtime (thus considering the current timezone) at least in milliseconds precision, if is possible to get in tenths of milliseconds it would be better.
I would like to avoid the use of gettimeofday() as it is now an obsolete function.
So, there seems that I'll need to use the clock_gettime(CLOCK_REALTIME, ...) and adjust the hour to the current timezone, but how ? And where is the best point to do that ? Before to store the timestamp got with clock_gettime, or before to convert it in a gregorian calendar in the current timezone?
EDIT: My original sample joining get_clock and localtime - There are better ways to reach that?
#include <time.h>
#include <stdio.h>
int main() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
struct tm* ptm;
ptm = localtime(&(ts.tv_sec));
// Tenths of milliseconds (4 decimal digits)
int tenths_ms = ts.tv_nsec / (100000L);
printf("%04d-%02d-%02d %02d:%02d:%02d.%04d\n",
1900 + ptm->tm_year, ptm->tm_mon + 1, ptm->tm_mday,
ptm->tm_hour, ptm->tm_min, ptm->tm_sec, tenths_ms);
}
I don't think there's a better way than with clock_gettime() and localtime(). However you need to round the returned nanoseconds correctly and consider the case when the time is rounded up to the next second. To format the time you can use strftime() instead of formatting the tm struct manually:
#include <time.h>
#include <stdio.h>
int main(void) {
struct timespec ts;
long msec;
int err = clock_gettime(CLOCK_REALTIME, &ts);
if (err) {
perror("clock_gettime");
return 1;
}
// round nanoseconds to milliseconds
if (ts.tv_nsec >= 999500000) {
ts.tv_sec++;
msec = 0;
} else {
msec = (ts.tv_nsec + 500000) / 1000000;
}
struct tm* ptm = localtime(&ts.tv_sec);
if (ptm == NULL) {
perror("localtime");
return 1;
}
char time_str[sizeof("1900-01-01 23:59:59")];
time_str[strftime(time_str, sizeof(time_str),
"%Y-%m-%d %H:%M:%S", ptm)] = '\0';
printf("%s.%03li\n", time_str, msec);
}
Yes, this can be achieved using the clock_gettime() function. In the current version of POSIX, gettimeofday() is marked obsolete. This means it may be removed from a future version of the specification. Application writers are encouraged to use the clock_gettime() function instead of gettimeofday().
Long story short, here is an example of how to use clock_gettime():
#define _POSIX_C_SOURCE 200809L
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
void print_current_time_in_ms (void)
{
long ms; // Milliseconds
time_t s; // Seconds
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
s = spec.tv_sec;
ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds
printf("Current time: %"PRIdMAX".%03ld seconds since the Epoch\n",
(intmax_t)s, ms);
}
If your goal is to measure elapsed time, and your system supports the "monotonic clock" option, then you should consider using CLOCK_MONOTONIC instead of CLOCK_REALTIME.
One more point, remember to include -lm flag when trying to compile the code.
To get the timezone, just do the following:
#define _GNU_SOURCE /* for tm_gmtoff and tm_zone */
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t = time(NULL);
struct tm lt = {0};
localtime_r(&t, <);
printf("Offset to GMT is %lds.\n", lt.tm_gmtoff);
printf("The time zone is '%s'.\n", lt.tm_zone);
return 0;
}
Note: The seconds since epoch returned by time() are measured as if in GMT (Greenwich Mean Time).
i need to convet current time in milliseconds to human readable time format. I have following code
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <time.h>
int Cnvrt_To_Time_Frmt(char *Epochval)
{
unsigned long epoch = 0;
time_t tt = 0;
char timestamp[64],usec_buf[20];
if (!sscanf(Epochval, "%lu", &epoch))
{
return 1;
}
tt = epoch;
strftime(timestamp, 64, "%c", localtime(&tt));
printf("%s\n",timestamp);
return 0;
}
int main()
{
uint64_t Epoch_time=1468496250207;
char str_ms[256];
sprintf(str_ms, "%llu", (Epoch_time/1000));
Cnvrt_To_Time_Frmt(str_ms);
}
It produce result : Thu Jul 14 17:07:30 2016.
But i need to print result with milli seconds. like Thu Jul 14 17:07:30:40 2016.(17 hour,07 minute, 30 second, 40 milliSecond)
How it will be possible?
Type time_t by its definition doesn't represent time with milliseconds resolution, function localtime returns pointer to struct tm which does not include milliseconds, function strftime is not designed to produce strings with milliseconds.
If you need time with milliseconds you can use timeb stucture with its associated ftime function if those are supported by your tool-chain.
Use this as format string:
strftime(timestamp, 64, "%a %b %d %H:%M:%S.XXX %Y", localtime(&tt));
The XXX will be copied as-is into the time string.
Then in main, you can overwrite the Xs with the millisecond count.
sprintf(×tamp[20], "%03u", (unsigned)Epoch_time%1000);
timestamp[23] = ' '; // restore the NUL to space again
After that, refactor your code so the divisions and remainder operations are done inside Cnvrt_To_Time_Frmt. You could use this as prototype:
int msecs_tostr(char *buffer, const char *msecs_since_epoch);
I don't have 50 reps yet so I can't comment so I will write my suggestion as an answer here.
You can use the other guys suggestions they are pretty good or you can make your own struct and a function that converts the mili seconds into time , by using basic math functions.
Make a struct that contains dayOfWeek , month , dayOfMonth , hour, minute, second , milliSecond , year.
Make a convertFunction that will receive a value of milliSeconds that need to be converted to your struct format.
Maybe its not the best way to do it , but if you don't find a way of using existing libraries , make your own .
... need to print result with milli seconds. ... How it will be possible?
Take it step by step
uint64_t Epoch_time=1468496250207;
// Break time into a whole number of seconds and ms fraction
time_t t_unix = Epoch_time/1000;
unsigned t_ms = Epoch_time%1000;
// Convert to YMD HMS structure
struct tm tm = *localtime(&t_unix);
// Form left portion of string
char left[64];
strftime(left, sizeof left, "%a %b %d %H:%M", &tm);
// Form right portion of string
char right[20];
strftime(right, sizeof right, "%Y", &tm);
// Put together with ms
char timestamp[64];
snprintf(timestamp, sizeof timestamp, "%s:%u %s", left, t_ms, right);
// Thu Jul 14 17:07:30:40 2016
// Print as needed
puts(timestamp);
Robust code would add error checking with each function's return value.
[edit]
Evidently OP's time stamp's last 3 digits are a fraction / 512.
unsigned t_fraction = Epoch_time%1000;
...
snprintf(timestamp, sizeof timestamp, "%s:%02u %s", left, t_fraction*100/512, right);
This example program will both retrieve the current timestamp from they system OS, and print it out in human readable format. It is similar to #user:2410359 answer, but a little more concise.
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
/*
* timestamp - read and print the current timestamp
* Wade Ryan 2020-09-27
* compile using: g++ timestamp.cpp -o timestamp
*/
int main(int argc, char **argv) {
char timestamp[24];
struct timeval currentTime;
struct tm ts;
gettimeofday(¤tTime, NULL);
long long epoch = (unsigned long long)(currentTime.tv_sec) * 1000 +
(unsigned long long)(currentTime.tv_usec) / 1000;
strftime(timestamp, sizeof(timestamp), "%F %T", localtime(¤tTime.tv_sec));
printf("epoch %lld ms :: %s.%03ld\n", epoch, timestamp, currentTime.tv_usec/1000);
}
Example output:
epoch 1601259041504 ms :: 2020-09-27 21:10:41.504
I need to be able to count the seconds between now and a specific time (like the next time it's 3pm). I saw some similar questions but I wasn't able to use any of these.
C standard library has two representation of time: time_t is a seconds since Unix Epoch and struct tm where you can individially set seconds, minutes, etc.
So to get next moment in time when wall clock will show 3 p.m. you'll need take current time in seconds time(NULL), convert it to struct tm, advance time to 3 p.m. by setting structure fields, convert it back to time_t and calculate the difference:
#include <time.h>
#include <string.h>
#include <stdio.h>
int main() {
time_t now, next3pm;
struct tm threepm;
// Get current time (now)
now = time(NULL);
// Copy current date to a `threepm`, and set time
memcpy(&threepm, gmtime(&now), sizeof(struct tm));
if(threepm.tm_hour > 15) {
// Advance to a next day
++threepm.tm_mday;
}
threepm.tm_hour = 15;
threepm.tm_min = threepm.tm_sec = 0;
printf("%.f seconds till 3:00 PM\n", difftime(mktime(&threepm), now));
return 0;
}
I used UTC conversion functions gmtime()/mktime(). Since there is no timezoned version of mktime() you may need to convert time on your own. Using UTC time may cause trouble with advancing to a next day when it shouldn't do that (because it is already 15:00 according to UTC, but not yet 15:00 according to a local time).
Or, a classic joke version:
int main() {
time_t t;
struct tm* tm;
do {
t = time(NULL);
tm = gmtime(&t);
usleep(1000000);
} while(tm->tm_hour != 15 && tm->tm_min != 0);
puts("0 seconds till 3:00 PM");
}
I have this program which prints the time difference between 2 different instances, but it prints in accuracy of seconds. I want to print it in milliseconds and another in nanoseconds difference.
//Prints in accuracy of seconds
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now, later;
double seconds;
time(&now);
sleep(2);
time(&later);
seconds = difftime(later, now);
printf("%.f seconds difference", seconds);
}
How can I accomplish that?
Read first the time(7) man page.
Then, you can use clock_gettime(2) syscall (you may need to link -lrt to get it).
So you could try
struct timespec tstart={0,0}, tend={0,0};
clock_gettime(CLOCK_MONOTONIC, &tstart);
some_long_computation();
clock_gettime(CLOCK_MONOTONIC, &tend);
printf("some_long_computation took about %.5f seconds\n",
((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));
Don't expect the hardware timers to have a nanosecond accuracy, even if they give a nanosecond resolution. And don't try to measure time durations less than several milliseconds: the hardware is not faithful enough. You may also want to use clock_getres to query the resolution of some clock.
timespec_get from C11
This function returns up to nanoseconds, rounded to the resolution of the implementation.
Example from: http://en.cppreference.com/w/c/chrono/timespec_get :
#include <stdio.h>
#include <time.h>
int main(void)
{
struct timespec ts;
timespec_get(&ts, TIME_UTC);
char buff[100];
strftime(buff, sizeof buff, "%D %T", gmtime(&ts.tv_sec));
printf("Current time: %s.%09ld UTC\n", buff, ts.tv_nsec);
}
Output:
Current time: 02/18/15 14:34:03.048508855 UTC
More details here: https://stackoverflow.com/a/36095407/895245
here is a simple macro to deal with it
#include <time.h>
#define CHECK_TIME(x) {\
struct timespec start,end;\
clock_gettime(CLOCK_REALTIME, &start);\
x;\
clock_gettime(CLOCK_REALTIME, &end);\
double f = ((double)end.tv_sec*1e9 + end.tv_nsec) - ((double)start.tv_sec*1e9 + start.tv_nsec); \
printf("time %f ms\n",f/1000000); \
}
and here is how to use it:
CHECK_TIME(foo(a,b,c))
How do I get the current time on Linux in milliseconds?
This can be achieved using the POSIX clock_gettime function.
In the current version of POSIX, gettimeofday is marked obsolete. This means it may be removed from a future version of the specification. Application writers are encouraged to use the clock_gettime function instead of gettimeofday.
Here is an example of how to use clock_gettime:
#define _POSIX_C_SOURCE 200809L
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
void print_current_time_with_ms (void)
{
long ms; // Milliseconds
time_t s; // Seconds
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
s = spec.tv_sec;
ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds
if (ms > 999) {
s++;
ms = 0;
}
printf("Current time: %"PRIdMAX".%03ld seconds since the Epoch\n",
(intmax_t)s, ms);
}
If your goal is to measure elapsed time, and your system supports the "monotonic clock" option, then you should consider using CLOCK_MONOTONIC instead of CLOCK_REALTIME.
You have to do something like this:
struct timeval tv;
gettimeofday(&tv, NULL);
double time_in_mill =
(tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ; // convert tv_sec & tv_usec to millisecond
Following is the util function to get current timestamp in milliseconds:
#include <sys/time.h>
long long current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
// printf("milliseconds: %lld\n", milliseconds);
return milliseconds;
}
About timezone:
gettimeofday() support to specify timezone,
I use NULL, which ignore the timezone, but you can specify a timezone, if need.
#Update - timezone
Since the long representation of time is not relevant to or effected by timezone itself, so setting tz param of gettimeofday() is not necessary, since it won't make any difference.
And, according to man page of gettimeofday(), the use of the timezone structure is obsolete, thus the tz argument should normally be specified as NULL, for details please check the man page.
Use gettimeofday() to get the time in seconds and microseconds. Combining and rounding to milliseconds is left as an exercise.
C11 timespec_get
It returns up to nanoseconds, rounded to the resolution of the implementation.
It is already implemented in Ubuntu 15.10. API looks the same as the POSIX clock_gettime.
#include <time.h>
struct timespec ts;
timespec_get(&ts, TIME_UTC);
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
More details here: https://stackoverflow.com/a/36095407/895245
This version need not math library and checked the return value of clock_gettime().
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
/**
* #return milliseconds
*/
uint64_t get_now_time() {
struct timespec spec;
if (clock_gettime(1, &spec) == -1) { /* 1 is CLOCK_MONOTONIC */
abort();
}
return spec.tv_sec * 1000 + spec.tv_nsec / 1e6;
}
Derived from Dan Moulding's POSIX answer, this should work :
#include <time.h>
#include <math.h>
long millis(){
struct timespec _t;
clock_gettime(CLOCK_REALTIME, &_t);
return _t.tv_sec*1000 + lround(_t.tv_nsec/1e6);
}
Also as pointed out by David Guyon: compile with -lm
Jirka Justra's answer returns a long, which is usually 32 bits. The number of milliseconds since unix time 0 in 1970 requires more bits, so the data type
should be long long or unsigned long long, which is usually 64 bits. Also, as Kevin Thibedeau commented, rounding can be done without converting to floating point or using math.h.
#include <time.h>
long long millis () {
struct timespec t ;
clock_gettime ( CLOCK_REALTIME , & t ) ;
return t.tv_sec * 1000 + ( t.tv_nsec + 500000 ) / 1000000 ;
}
If you are trying to measure time less than 50 days, 32 bits is enough. Data type int is 32 bits or 64 bits on most computers, so the data type can be unsigned int.