22# include <sys/locking.h>
23# include <sys/types.h>
24# include <sys/timeb.h>
25# if defined(__BORLANDC__)
26# define lockf(handle, mode, nbytes) locking(handle, mode, nbytes)
28# define lockf(handle, mode, nbytes) _locking(handle, mode, nbytes)
30# define usleep(usecs) Sleep(usecs / 1000)
31# if !defined(__MINGW32__)
32# define popen(x, y) _popen(x, y)
37# if defined(__APPLE__)
40int lockf(
int filedes,
int function, off_t size);
44# if defined(BELOW_SOLARIS_56)
48typedef void (*OLD_SIG_FUNC)(int);
49static void usleep_alarm(
int x) {
53void usleep(
long usecs) {
61 v.it_value.tv_sec = usecs / 1000000;
62 v.it_value.tv_usec = usecs % 1000000;
64 v.it_interval.tv_sec = 0;
65 v.it_interval.tv_usec = 0;
67 old = signal(SIGALRM, usleep_alarm);
68 if (setitimer(ITIMER_REAL, &v, (
struct itimerval *)NULL) < 0)
88 TimeStamp = ctime(&intTime);
89 TimeStamp[strlen(TimeStamp) - 1] = 0;
104 double *ptrJulianDay,
double *ptrMonth,
105 double *ptrYear,
char **ptrTimeStamp) {
109 ptrMonth, ptrYear, ptrTimeStamp);
117 double theTime, hour;
123static long daysInMonth[12] = {
150 double *ptrJulianDay,
double *ptrMonth,
double *ptrYear,
151 char **ptrTimeStamp) {
153 double Day, Hour, JulianDay, Year;
154 struct tm *tmBreakdown;
164 integerTime = (time_t)Time;
165 SubSeconds = Time - integerTime;
166 tmBreakdown = localtime(&integerTime);
169 Hour = (tmBreakdown->tm_min + (tmBreakdown->tm_sec + SubSeconds) / 60.0) / 60.0 + tmBreakdown->tm_hour;
174 Day = tmBreakdown->tm_mday + Hour / 24.0;
179 JulianDay = tmBreakdown->tm_yday + Hour / 24.0 + 1;
181 *ptrJulianDay = JulianDay;
185 tmBreakdown->tm_year += 1900;
186 if ((tmBreakdown->tm_year % 4 == 0 && tmBreakdown->tm_year % 100 != 0) || tmBreakdown->tm_year % 400 == 0)
188 Year = tmBreakdown->tm_year + (JulianDay - 1) / (365.0 + isLeap);
194 *ptrMonth = tmBreakdown->tm_mon + 1 +
195 +(1.0 * tmBreakdown->tm_mday - 1) / (daysInMonth[tmBreakdown->tm_mon] +
196 (isLeap && tmBreakdown->tm_mon == 1 ? 1 : 0));
206 struct tm *YearStart;
210 YearStart = localtime(&intTime);
211 YearStart->tm_sec = 0;
212 YearStart->tm_min = 0;
213 YearStart->tm_hour = 0;
214 YearStart->tm_mday = 1;
215 YearStart->tm_mon = 0;
217 return (
double)timelocal(YearStart);
219 return (
double)mktime(YearStart);
228# if defined(__MINGW32__)
229# define exp7 10000000LL
230# define exp9 1000000000LL
231# define w2ux 116444736000000000LL
233# define exp7 10000000i64
234# define exp9 1000000000i64
235# define w2ux 116444736000000000i64
238void unix_time(
struct timespecoag *spec) {
240 GetSystemTimeAsFileTime((FILETIME *)&wintime);
242 spec->tv_sec = wintime / exp7;
243 spec->tv_nsec = wintime % exp7 * 100;
246int clock_gettime_oag(
struct timespecoag *spec) {
247 static struct timespecoag startspec;
248 static double ticks2nano;
249 static __int64 startticks, tps = 0;
250 __int64 tmp, curticks;
251 QueryPerformanceFrequency((LARGE_INTEGER *)&tmp);
254 QueryPerformanceCounter((LARGE_INTEGER *)&startticks);
255 unix_time(&startspec);
256 ticks2nano = (double)exp9 / tps;
258 QueryPerformanceCounter((LARGE_INTEGER *)&curticks);
259 curticks -= startticks;
260 spec->tv_sec = startspec.tv_sec + (curticks / tps);
261 spec->tv_nsec = startspec.tv_nsec + (double)(curticks % tps) * ticks2nano;
262 if (!(spec->tv_nsec < exp9)) {
264 spec->tv_nsec -= exp9;
276 struct timespecoag tp;
277 clock_gettime_oag(&tp);
278 return ((
double)tp.tv_sec) + ((double)tp.tv_nsec) * 1e-9;
281#if defined(__APPLE__)
282# include <sys/time.h>
284 gettimeofday(&tv, NULL);
285 return ((
double)tv.tv_sec) + ((double)tv.tv_usec) * 1e-6;
288#if !defined(_WIN32) && !defined(__APPLE__)
291 clock_gettime(CLOCK_REALTIME, &tp);
292 return ((
double)tp.tv_sec) + ((double)tp.tv_nsec) * 1e-9;
302 struct timespecoag tp;
303 clock_gettime_oag(&tp);
304 return ((
long double)tp.tv_sec) + ((
long double)tp.tv_nsec) * 1e-9L;
307#if defined(__APPLE__)
308# include <sys/time.h>
310 gettimeofday(&tv, NULL);
311 return ((
long double)tv.tv_sec) + ((
long double)tv.tv_usec) * 1e-6L;
314#if !defined(_WIN32) && !defined(__APPLE__)
317 clock_gettime(CLOCK_REALTIME, &tp);
318 return ((
long double)tp.tv_sec) + ((
long double)tp.tv_nsec) * 1e-9L;
331 char format[100], filename[1024], buffer[1024], *name, *ptr, *ptr1;
335 if (!rootname || strlen(rootname) == 0)
338 digits = DEFAULT_GENERATIONS_DIGITS;
340 if (lastFile && strlen(lastFile)) {
343 while ((ptr1 = strstr(ptr1, delimiter))) {
348 ptr += strlen(delimiter);
351 if (sscanf(ptr,
"%ld", &index) != 1) {
352 sprintf(buffer,
"Error scanning name of last file: %s", lastFile);
353 fprintf(stderr,
"Error: %s\n", buffer);
357 sprintf(buffer,
"Error scanning name of last file: %s", lastFile);
358 fprintf(stderr,
"Error: %s\n", buffer);
365 sprintf(format,
"%%s%s%%0%ldld", delimiter, digits);
368 sprintf(filename, format, rootname, index);
370 if (!(fp = fopen(filename,
"r")))
372#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
373 if (lockf(fileno(fp), F_TEST, 0) == -1) {
376 sprintf(buffer,
"aborting--previous generation of %s (%s) is still active", rootname,
378 fprintf(stderr,
"Warning: %s\n", buffer);
384 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
385 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
388 return strcpy(name, filename);
405 fprintf(stderr,
"The rootname is not provided.\n");
411 now1 = localtime(&now);
412 strftime(buffer, 1024,
"%Y-%j-%m%d-%H%M%S", now1);
413 sprintf(filename,
"%s%s", rootname, buffer);
414 fp = fopen(filename,
"r");
415 }
while (fp != NULL);
417 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
418 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
421 return strcpy(name, filename);
436 char format[100], filename[1024], *name, *hourNow, match_date[1024];
439 double dayDbl, jDayDbl, monthDbl, yearDbl, theTime;
440 long day, jDay, month, year;
443 digits = DEFAULT_GENERATIONS_DIGITS;
445 makeTimeBreakdown(theTime, NULL, &dayDbl, NULL, &jDayDbl, &monthDbl, &yearDbl, NULL);
453 if (rootname && strlen(rootname) > 0)
454 sprintf(match_date,
"%s-%4ld-%03ld-%02ld%02ld", rootname, year, jDay, month, day);
456 sprintf(match_date,
"%4ld-%03ld-%02ld%02ld", year, jDay, month, day);
458 if (rootname && strlen(rootname) > 0)
459 sprintf(filename,
"%s-%4ld-%03ld-%02ld%02ld.%s", rootname,
460 year, jDay, month, day, hourNow);
462 sprintf(filename,
"%4ld-%03ld-%02ld%02ld.%s", year, jDay, month, day, hourNow);
465 if (rootname && strlen(rootname) > 0)
466 sprintf(filename,
"%s-%4ld-%03ld-%02ld%02ld", rootname, year, jDay, month, day);
468 sprintf(filename,
"%4ld-%03ld-%02ld%02ld", year, jDay, month, day);
469 if ((fp = fopen(filename,
"r"))) {
470#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
471 if (lockf(fileno(fp), F_TEST, 0) == -1) {
474 sprintf(buffer,
"aborting--previous generation of %s is still active", filename);
475 fprintf(stderr,
"Warning: %s\n", buffer);
481 if (!delimiter || strlen(delimiter) == 0)
483 if (rootname && strlen(rootname) > 0)
484 sprintf(format,
"%s-%%4ld-%%03ld-%%02ld%%02ld%s%%0%ldld", rootname, delimiter, digits);
486 sprintf(format,
"%%4ld-%%03ld-%%02ld%%02ld%s%%0%ldld", delimiter, digits);
488 sprintf(filename, format, year, jDay, month, day, index);
490 if (!(fp = fopen(filename,
"r")))
493 makeTimeBreakdown(theTime, NULL, &dayDbl, NULL, &jDayDbl, &monthDbl, &yearDbl, NULL);
498#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
499 if (lockf(fileno(fp), F_TEST, 0) == -1) {
502 sprintf(buffer,
"aborting--previous generation of %s (%s) is still active", rootname,
504 fprintf(stderr,
"Warning: %s\n", buffer);
512 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
513 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
516 return strcpy(name, filename);
531 char format[100], filename[1024], *name, *hourNow, match_date[1024];
534 double dayDbl, jDayDbl, monthDbl, yearDbl, theTime;
538 digits = DEFAULT_GENERATIONS_DIGITS;
540 makeTimeBreakdown(theTime, NULL, &dayDbl, NULL, &jDayDbl, &monthDbl, &yearDbl, NULL);
546 if (rootname && strlen(rootname) > 0)
547 sprintf(match_date,
"%s-%4ld-%02ld", rootname, year, month);
549 sprintf(match_date,
"%4ld-%02ld", year, month);
551 if (rootname && strlen(rootname) > 0)
552 sprintf(filename,
"%s-%4ld-%02ld.%s", rootname,
553 year, month, hourNow);
555 sprintf(filename,
"%4ld-%02ld.%s", year, month, hourNow);
558 if (rootname && strlen(rootname) > 0)
559 sprintf(filename,
"%s-%4ld-%02ld", rootname, year, month);
561 sprintf(filename,
"%4ld-%02ld", year, month);
562 if ((fp = fopen(filename,
"r"))) {
563#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
564 if (lockf(fileno(fp), F_TEST, 0) == -1) {
567 sprintf(buffer,
"aborting--previous generation of %s is still active", filename);
568 fprintf(stderr,
"Warning: %s\n", buffer);
574 if (!delimiter || strlen(delimiter) == 0)
576 if (rootname && strlen(rootname) > 0)
577 sprintf(format,
"%s-%%4ld-%%02ld%s%%0%ldld", rootname, delimiter, digits);
579 sprintf(format,
"%%4ld-%%02ld%s%%0%ldld", delimiter, digits);
581 sprintf(filename, format, year, month, index);
583 if (!(fp = fopen(filename,
"r")))
589#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
590 if (lockf(fileno(fp), F_TEST, 0) == -1) {
593 sprintf(buffer,
"aborting--previous generation of %s (%s) is still active", rootname,
595 fprintf(stderr,
"Warning: %s\n", buffer);
603 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
604 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
607 return strcpy(name, filename);
615#if defined(vxWorks) && !defined(__rtems__)
616 struct timespec rqtp;
617 rqtp.tv_sec = (long)(usec / 1000000);
618 rqtp.tv_nsec = usec % 1000000 * 1000;
619 nanosleep(&rqtp, NULL);
636 ptr = strtok(Hour,
" ");
638 while (ptr != NULL) {
641 ptr = strtok(NULL,
" ");
652#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
653 char comm[1024], filename[2048], buffer[1024];
656 sprintf(comm,
"ls %s* 2> /dev/stdout", match_date);
657 if (!(handle = popen(comm,
"r"))) {
658 fprintf(stderr,
"Error: no data returned from popen call\n");
661 while (!feof(handle)) {
662 if (fgets(filename,
sizeof(filename), handle)) {
663 if (filename[strlen(filename) - 1] ==
'\n') {
664 filename[strlen(filename) - 1] = 0;
666 if (!(fp = fopen(filename,
"r")))
668 if (lockf(fileno(fp), F_TEST, 0) == -1) {
671 sprintf(buffer,
"aborting--previous generation %s is still active",
673 fprintf(stderr,
"Warning: %s\n", buffer);
687#if !defined(vxWorks) && !defined(__rtems__)
689 static FILE *fsh = NULL;
692 if (!(fsh = popen(
"csh",
"w"))) {
693 fprintf(stderr,
"Error: unable to launch csh for touchFile operations\n");
698 if ((fp = fopen(filename,
"r"))) {
700 fprintf(fsh,
"touch %s\n", filename);
long fexists(const char *filename)
Checks if a file exists.
double computeYearStartTime(double StartTime)
Compute the start time of the year for the given time.
char * makeTimeStamp(double Time)
Create a human-readable timestamp from the given time in seconds since the Epoch.
void makeTimeBreakdown(double Time, double *ptrTime, double *ptrDay, double *ptrHour, double *ptrJulianDay, double *ptrMonth, double *ptrYear, char **ptrTimeStamp)
Break down a given time into multiple components (time, day, hour, Julian day, month,...
void checkGenerationFileLocks(char *match_date)
Check for matching date-based generation files and ensure none are locked.
char * MakeDailyGenerationFilename(char *rootname, long digits, char *delimiter, long timetag)
Generate a new daily filename. Can include a time tag or a numeric index.
void usleepSystemIndependent(long usec)
Sleep for a given number of microseconds, system-independently.
double getHourOfDay()
Get the current hour of the day (0-23, possibly fractional).
char * getHourMinuteSecond()
Get the current hour, minute, and second as a string.
double getTimeInSecs()
Get the current time in seconds since the Epoch with high resolution.
char * MakeGenerationFilename(char *rootname, long digits, char *delimiter, char *lastFile)
Generate a new filename with an incremented index based on a root name and delimiter.
char * MakeSCRDailyTimeGenerationFilename(char *rootname)
Generate a new daily filename with a timestamp based on the current date and time.
void TouchFile(char *filename)
Update the modification timestamp of a file if it exists.
void getTimeBreakdown(double *ptrTime, double *ptrDay, double *ptrHour, double *ptrJulianDay, double *ptrMonth, double *ptrYear, char **ptrTimeStamp)
Retrieve detailed time breakdown (day, hour, julian day, month, year, and timestamp).
char * MakeMonthlyGenerationFilename(char *rootname, long digits, char *delimiter, long timetag)
Generate a new monthly filename. Can include a time tag or a numeric index.
long double getLongDoubleTimeInSecs()
Get the current time in seconds since the Epoch as a long double for higher precision.