#include #include #include #include "sys_util.h" #include "myc_time.h" int ftime (struct timeb *__timeptr); /* for some reason not defined in timeb.h with flag -std1 */ static time_t my_base=0; void initBase(void) { struct tm tim; time_t now; time(&now); tim = *localtime(&now); tim.tm_year = MYC_TIME_BASE / 10000 - 1900; tim.tm_mon = (MYC_TIME_BASE % 10000) / 100 - 1; tim.tm_mday = MYC_TIME_BASE % 100; tim.tm_hour = 12; tim.tm_min = 0; tim.tm_sec = 0; /* take daylight saving time flag and gmt offset from today */ my_base=mktime(&tim) - 12*3600; assert(tim.tm_wday == 1); /* time base must be a monday */ } int mycNow(void) { time_t now; if (my_base == 0) initBase(); time(&now); return (int)(now-my_base); } int mycMsecSince(int since) { struct timeb now; int msec; if (my_base == 0) initBase(); ftime(&now); msec = (now.time - my_base) % (24*3600) * 1000 + now.millitm - since; if (msec < 0) msec+=24*3600000; return msec; } int mycDate(int time) { struct tm tim; time_t t; if (my_base == 0) initBase(); t = time + my_base; tim=*localtime(&t); return (tim.tm_year + 1900) * 10000 + (tim.tm_mon + 1) * 100 + tim.tm_mday; } int mycTime(int date) { struct tm tim; time_t t, now; int y, m, d; if (my_base == 0) initBase(); time(&now); tim=*localtime(&now); tim.tm_hour=0; tim.tm_min=0; tim.tm_sec=0; if (date != 0) { d = date % 100; date = date / 100; m = date % 100; y = date / 100; if (y == 0) { if (m == 0) { if (d > tim.tm_mday) { if (tim.tm_mon == 0) { /* jan */ tim.tm_mon=11; /* dec */ tim.tm_year--; } else { tim.tm_mon--; /* last month */ } } } else { tim.tm_mon = m - 1; } } else { tim.tm_year = y - 1900; if (m == 0) { if (d == 0) { tim.tm_mon = 0; } } else { tim.tm_mon = m - 1; } } if (d == 0) d = 1; tim.tm_mday = d; } return (int)(mktime(&tim)-my_base); } /* fortran routines -------------------------------------------------- */ #ifdef F_CHAR /* compile only when fortran c interface stuff is defined */ #ifdef __VMS #define myc_date_ myc_date #define myc_time_ myc_time #define myc_now_ myc_now #endif int myc_now_(void) { return mycNow(); } int myc_date_(int *time) { return mycDate(*time); } int myc_time_(int *date) { return mycTime(*date); } #endif