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)
34# if defined(__APPLE__)
37int lockf(
int filedes,
int function, off_t size);
41# if defined(BELOW_SOLARIS_56)
45typedef void (*OLD_SIG_FUNC)(int);
46static void usleep_alarm(
int x) {
50void usleep(
long usecs) {
58 v.it_value.tv_sec = usecs / 1000000;
59 v.it_value.tv_usec = usecs % 1000000;
61 v.it_interval.tv_sec = 0;
62 v.it_interval.tv_usec = 0;
64 old = signal(SIGALRM, usleep_alarm);
65 if (setitimer(ITIMER_REAL, &v, (
struct itimerval *)NULL) < 0)
85 TimeStamp = ctime(&intTime);
86 TimeStamp[strlen(TimeStamp) - 1] = 0;
101 double *ptrJulianDay,
double *ptrMonth,
102 double *ptrYear,
char **ptrTimeStamp) {
106 ptrMonth, ptrYear, ptrTimeStamp);
114 double theTime, hour;
120static long daysInMonth[12] = {
147 double *ptrJulianDay,
double *ptrMonth,
double *ptrYear,
148 char **ptrTimeStamp) {
150 double Day, Hour, JulianDay, Year;
151 struct tm *tmBreakdown;
161 integerTime = (time_t)Time;
162 SubSeconds = Time - integerTime;
163 tmBreakdown = localtime(&integerTime);
166 Hour = (tmBreakdown->tm_min + (tmBreakdown->tm_sec + SubSeconds) / 60.0) / 60.0 + tmBreakdown->tm_hour;
171 Day = tmBreakdown->tm_mday + Hour / 24.0;
176 JulianDay = tmBreakdown->tm_yday + Hour / 24.0 + 1;
178 *ptrJulianDay = JulianDay;
182 tmBreakdown->tm_year += 1900;
183 if ((tmBreakdown->tm_year % 4 == 0 && tmBreakdown->tm_year % 100 != 0) || tmBreakdown->tm_year % 400 == 0)
185 Year = tmBreakdown->tm_year + (JulianDay - 1) / (365.0 + isLeap);
191 *ptrMonth = tmBreakdown->tm_mon + 1 +
192 +(1.0 * tmBreakdown->tm_mday - 1) / (daysInMonth[tmBreakdown->tm_mon] +
193 (isLeap && tmBreakdown->tm_mon == 1 ? 1 : 0));
203 struct tm *YearStart;
207 YearStart = localtime(&intTime);
208 YearStart->tm_sec = 0;
209 YearStart->tm_min = 0;
210 YearStart->tm_hour = 0;
211 YearStart->tm_mday = 1;
212 YearStart->tm_mon = 0;
214 return (
double)timelocal(YearStart);
216 return (
double)mktime(YearStart);
225# if defined(__MINGW32__)
226# define exp7 10000000LL
227# define exp9 1000000000LL
228# define w2ux 116444736000000000LL
230# define exp7 10000000i64
231# define exp9 1000000000i64
232# define w2ux 116444736000000000i64
235void unix_time(
struct timespecoag *spec) {
237 GetSystemTimeAsFileTime((FILETIME *)&wintime);
239 spec->tv_sec = wintime / exp7;
240 spec->tv_nsec = wintime % exp7 * 100;
243int clock_gettime_oag(
struct timespecoag *spec) {
244 static struct timespecoag startspec;
245 static double ticks2nano;
246 static __int64 startticks, tps = 0;
247 __int64 tmp, curticks;
248 QueryPerformanceFrequency((LARGE_INTEGER *)&tmp);
251 QueryPerformanceCounter((LARGE_INTEGER *)&startticks);
252 unix_time(&startspec);
253 ticks2nano = (double)exp9 / tps;
255 QueryPerformanceCounter((LARGE_INTEGER *)&curticks);
256 curticks -= startticks;
257 spec->tv_sec = startspec.tv_sec + (curticks / tps);
258 spec->tv_nsec = startspec.tv_nsec + (double)(curticks % tps) * ticks2nano;
259 if (!(spec->tv_nsec < exp9)) {
261 spec->tv_nsec -= exp9;
273 struct timespecoag tp;
274 clock_gettime_oag(&tp);
275 return ((
double)tp.tv_sec) + ((double)tp.tv_nsec) * 1e-9;
278#if defined(__APPLE__)
279# include <sys/time.h>
281 gettimeofday(&tv, NULL);
282 return ((
double)tv.tv_sec) + ((double)tv.tv_usec) * 1e-6;
285#if !defined(_WIN32) && !defined(__APPLE__)
288 clock_gettime(CLOCK_REALTIME, &tp);
289 return ((
double)tp.tv_sec) + ((double)tp.tv_nsec) * 1e-9;
299 struct timespecoag tp;
300 clock_gettime_oag(&tp);
301 return ((
long double)tp.tv_sec) + ((
long double)tp.tv_nsec) * 1e-9L;
304#if defined(__APPLE__)
305# include <sys/time.h>
307 gettimeofday(&tv, NULL);
308 return ((
long double)tv.tv_sec) + ((
long double)tv.tv_usec) * 1e-6L;
311#if !defined(_WIN32) && !defined(__APPLE__)
314 clock_gettime(CLOCK_REALTIME, &tp);
315 return ((
long double)tp.tv_sec) + ((
long double)tp.tv_nsec) * 1e-9L;
328 char format[100], filename[1024], buffer[1100], *name, *ptr, *ptr1;
332 if (!rootname || strlen(rootname) == 0)
335 digits = DEFAULT_GENERATIONS_DIGITS;
337 if (lastFile && strlen(lastFile)) {
340 while ((ptr1 = strstr(ptr1, delimiter))) {
345 ptr += strlen(delimiter);
348 if (sscanf(ptr,
"%ld", &index) != 1) {
349 sprintf(buffer,
"Error scanning name of last file: %s", lastFile);
350 fprintf(stderr,
"Error: %s\n", buffer);
354 sprintf(buffer,
"Error scanning name of last file: %s", lastFile);
355 fprintf(stderr,
"Error: %s\n", buffer);
362 sprintf(format,
"%%s%s%%0%ldld", delimiter, digits);
365 sprintf(filename, format, rootname, index);
367 if (!(fp = fopen(filename,
"r")))
369#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
370 if (lockf(fileno(fp), F_TEST, 0) == -1) {
373 sprintf(buffer,
"aborting--previous generation of %s (%s) is still active", rootname,
375 fprintf(stderr,
"Warning: %s\n", buffer);
381 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
382 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
385 return strcpy(name, filename);
402 fprintf(stderr,
"The rootname is not provided.\n");
408 now1 = localtime(&now);
409 strftime(buffer, 1024,
"%Y-%j-%m%d-%H%M%S", now1);
410 sprintf(filename,
"%s%s", rootname, buffer);
411 fp = fopen(filename,
"r");
412 }
while (fp != NULL);
414 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
415 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
418 return strcpy(name, filename);
433 char format[100], filename[1024], *name, *hourNow, match_date[1024];
436 double dayDbl, jDayDbl, monthDbl, yearDbl, theTime;
437 long day, jDay, month, year;
440 digits = DEFAULT_GENERATIONS_DIGITS;
442 makeTimeBreakdown(theTime, NULL, &dayDbl, NULL, &jDayDbl, &monthDbl, &yearDbl, NULL);
450 if (rootname && strlen(rootname) > 0)
451 sprintf(match_date,
"%s-%4ld-%03ld-%02ld%02ld", rootname, year, jDay, month, day);
453 sprintf(match_date,
"%4ld-%03ld-%02ld%02ld", year, jDay, month, day);
455 if (rootname && strlen(rootname) > 0)
456 sprintf(filename,
"%s-%4ld-%03ld-%02ld%02ld.%s", rootname,
457 year, jDay, month, day, hourNow);
459 sprintf(filename,
"%4ld-%03ld-%02ld%02ld.%s", year, jDay, month, day, hourNow);
462 if (rootname && strlen(rootname) > 0)
463 sprintf(filename,
"%s-%4ld-%03ld-%02ld%02ld", rootname, year, jDay, month, day);
465 sprintf(filename,
"%4ld-%03ld-%02ld%02ld", year, jDay, month, day);
466 if ((fp = fopen(filename,
"r"))) {
467#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
468 if (lockf(fileno(fp), F_TEST, 0) == -1) {
471 sprintf(buffer,
"aborting--previous generation of %s is still active", filename);
472 fprintf(stderr,
"Warning: %s\n", buffer);
478 if (!delimiter || strlen(delimiter) == 0)
480 if (rootname && strlen(rootname) > 0)
481 sprintf(format,
"%s-%%4ld-%%03ld-%%02ld%%02ld%s%%0%ldld", rootname, delimiter, digits);
483 sprintf(format,
"%%4ld-%%03ld-%%02ld%%02ld%s%%0%ldld", delimiter, digits);
485 sprintf(filename, format, year, jDay, month, day, index);
487 if (!(fp = fopen(filename,
"r")))
490 makeTimeBreakdown(theTime, NULL, &dayDbl, NULL, &jDayDbl, &monthDbl, &yearDbl, NULL);
495#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
496 if (lockf(fileno(fp), F_TEST, 0) == -1) {
499 sprintf(buffer,
"aborting--previous generation of %s (%s) is still active", rootname,
501 fprintf(stderr,
"Warning: %s\n", buffer);
509 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
510 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
513 return strcpy(name, filename);
528 char format[100], filename[1024], *name, *hourNow, match_date[1024];
531 double dayDbl, jDayDbl, monthDbl, yearDbl, theTime;
535 digits = DEFAULT_GENERATIONS_DIGITS;
537 makeTimeBreakdown(theTime, NULL, &dayDbl, NULL, &jDayDbl, &monthDbl, &yearDbl, NULL);
543 if (rootname && strlen(rootname) > 0)
544 sprintf(match_date,
"%s-%4ld-%02ld", rootname, year, month);
546 sprintf(match_date,
"%4ld-%02ld", year, month);
548 if (rootname && strlen(rootname) > 0)
549 sprintf(filename,
"%s-%4ld-%02ld.%s", rootname,
550 year, month, hourNow);
552 sprintf(filename,
"%4ld-%02ld.%s", year, month, hourNow);
555 if (rootname && strlen(rootname) > 0)
556 sprintf(filename,
"%s-%4ld-%02ld", rootname, year, month);
558 sprintf(filename,
"%4ld-%02ld", year, month);
559 if ((fp = fopen(filename,
"r"))) {
560#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
561 if (lockf(fileno(fp), F_TEST, 0) == -1) {
564 sprintf(buffer,
"aborting--previous generation of %s is still active", filename);
565 fprintf(stderr,
"Warning: %s\n", buffer);
571 if (!delimiter || strlen(delimiter) == 0)
573 if (rootname && strlen(rootname) > 0)
574 sprintf(format,
"%s-%%4ld-%%02ld%s%%0%ldld", rootname, delimiter, digits);
576 sprintf(format,
"%%4ld-%%02ld%s%%0%ldld", delimiter, digits);
578 sprintf(filename, format, year, month, index);
580 if (!(fp = fopen(filename,
"r")))
586#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
587 if (lockf(fileno(fp), F_TEST, 0) == -1) {
590 sprintf(buffer,
"aborting--previous generation of %s (%s) is still active", rootname,
592 fprintf(stderr,
"Warning: %s\n", buffer);
600 if (!(name = malloc(
sizeof(*name) * (strlen(filename) + 1)))) {
601 fprintf(stderr,
"Error: memory allocation failure making generation filename\n");
604 return strcpy(name, filename);
612#if defined(vxWorks) && !defined(__rtems__)
613 struct timespec rqtp;
614 rqtp.tv_sec = (long)(usec / 1000000);
615 rqtp.tv_nsec = usec % 1000000 * 1000;
616 nanosleep(&rqtp, NULL);
633 ptr = strtok(Hour,
" ");
635 while (ptr != NULL) {
638 ptr = strtok(NULL,
" ");
649#if !defined(vxWorks) && !defined(__rtems__) && !defined(__CYGWIN__)
650 char comm[1024], filename[2048], buffer[1024];
653 sprintf(comm,
"ls %s* 2> /dev/stdout", match_date);
654 if (!(handle = popen(comm,
"r"))) {
655 fprintf(stderr,
"Error: no data returned from popen call\n");
658 while (!feof(handle)) {
659 if (fgets(filename,
sizeof(filename), handle)) {
660 if (filename[strlen(filename) - 1] ==
'\n') {
661 filename[strlen(filename) - 1] = 0;
663 if (!(fp = fopen(filename,
"r")))
665 if (lockf(fileno(fp), F_TEST, 0) == -1) {
668 sprintf(buffer,
"aborting--previous generation %s is still active",
670 fprintf(stderr,
"Warning: %s\n", buffer);
684#if !defined(vxWorks) && !defined(__rtems__)
686 static FILE *fsh = NULL;
689 if (!(fsh = popen(
"csh",
"w"))) {
690 fprintf(stderr,
"Error: unable to launch csh for touchFile operations\n");
695 if ((fp = fopen(filename,
"r"))) {
697 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.