1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/*
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
Copyright (C) 2005- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#include "timestamp.h"
#include <sys/time.h>
#include <sys/select.h>
#include <sys/stat.h>
time_t timestamp_file(const char *filename)
{
struct stat buf;
if(stat(filename, &buf) == 0) {
return buf.st_mtime;
} else {
return 0;
}
}
timestamp_t timestamp_get()
{
struct timeval current;
timestamp_t stamp;
gettimeofday(¤t, 0);
stamp = ((timestamp_t) current.tv_sec) * 1000000 + current.tv_usec;
return stamp;
}
int timestamp_fmt(char *buf, size_t size, const char *fmt, timestamp_t ts)
{
time_t tv_sec;
struct tm *tp;
if(buf == NULL) return 0;
tv_sec = ts / 1000000;
#if defined (_XOPEN_SOURCE) || defined (_BSD_SOURCE) || defined (_SVID_SOURCE) || defined (_POSIX_SOURCE) || _POSIX_C_SOURCE >= 1
struct tm t;
tp = localtime_r(&tv_sec, &t);
#else
tp = localtime(&tv_sec);
#endif
if(tp != NULL) {
return strftime(buf, size, fmt, tp);
}
return 0;
}
void timestamp_sleep(timestamp_t interval)
{
struct timeval t;
t.tv_sec = interval / 1000000;
t.tv_usec = interval % 1000000;
select(0, 0, 0, 0, &t);
}
/* vim: set noexpandtab tabstop=4: */
|