bmk_time_t clock_ymdhms_to_secs(struct bmk_clock_ymdhms *dt) { uint64_t secs, i, year, days; year = dt->dt_year; /* * Compute days since start of time * First from years, then from months. */ if (year < POSIX_BASE_YEAR) return 0; days = 0; if (is_leap_year(year) && dt->dt_mon > FEBRUARY) days++; if (year < 2000) { /* simple way for early years */ for (i = POSIX_BASE_YEAR; i < year; i++) days += days_per_year(i); } else { /* years are properly aligned */ days += DAYSTO2000; year -= 2000; i = year / 400; days += i * DAYS400YEARS; year -= i * 400; i = year / 100; days += i * DAYS100YEARS; year -= i * 100; i = year / 4; days += i * DAYS4YEARS; year -= i * 4; for (i = dt->dt_year-year; i < dt->dt_year; i++) days += days_per_year(i); } /* Months */ for (i = 1; i < dt->dt_mon; i++) days += days_in_month(i); days += (dt->dt_day - 1); /* Add hours, minutes, seconds. */ secs = (((uint64_t)days * 24 + dt->dt_hour) * 60 + dt->dt_min) * 60 + dt->dt_sec; return secs; }
double join_time(int yyyy, int mm, int dd, int hh, int mi, int ss, int msec) { int year,month; double day,time; /* Switch debug messages from main on/off (1/0). */ int diagnostic = 0; if (yyyy == NULLVAL) return NULLVAL; if (diagnostic){ fprintf(logfp,"join_time: %d %d %d ",yyyy,mm,dd); fprintf(logfp,"%d %d %d %d\n",hh,mi,ss,msec); } day = 0; if (yyyy > EPOCH_YEAR) for (year=EPOCH_YEAR; year<yyyy; year++) day += days_per_year(year); else for (year=yyyy; year<EPOCH_YEAR; year++) day -= days_per_year(year); for (month=1; month<mm; month++) day += days_per_month(month,yyyy); day += dd-1; time = day*86400; time += hh*3600; time += mi*60; time += ss; if (msec != NULLVAL) time += msec*0.001; if (diagnostic) fprintf(logfp,"join_time: %f\n",time); return time; }
/* Convert a Julian date back to YEAR, MONTH and DAY. Return day of the year or 0 on error. This function uses some more or less arbitrary limits, most important is that days before 1582-10-15 are not supported. */ static int jd2date (unsigned long jd, int *year, int *month, int *day) { int y, m, d; long delta; if (!jd) return 0 ; if (jd < 1721425 || jd > 2843085) return 0; y = (jd - JD_DIFF) / 366; d = m = 1; while ((delta = jd - date2jd (y, m, d)) > days_per_year (y)) y++; m = (delta / 31) + 1; while( (delta = jd - date2jd (y, m, d)) > days_per_month (y,m)) if (++m > 12) { m = 1; y++; } d = delta + 1 ; if (d > days_per_month (y, m)) { d = 1; m++; } if (m > 12) { m = 1; y++; } if (year) *year = y; if (month) *month = m; if (day) *day = d ; return (jd - date2jd (y, 1, 1)) + 1; }
static int leitch_get_date( struct recvbuf *rbufp, struct leitchunit *leitch ) { int i; if (rbufp->recv_length < 6) return(0); #undef BAD /* confict: defined as (-1) in AIX sys/param.h */ #define BAD(A) (rbufp->recv_buffer[A] < '0') || (rbufp->recv_buffer[A] > '9') if (BAD(0)||BAD(1)||BAD(2)||BAD(3)||BAD(4)||BAD(5)) return(0); #define ATOB(A) ((rbufp->recv_buffer[A])-'0') leitch->year = ATOB(0)*10 + ATOB(1); leitch->month = ATOB(2)*10 + ATOB(3); leitch->day = ATOB(4)*10 + ATOB(5); /* sanity checks */ if (leitch->month > 12) return(0); if (leitch->day > days_in_month[leitch->month-1]) return(0); /* calculate yearday */ i = 0; leitch->yearday = leitch->day; while ( i < (leitch->month-1) ) leitch->yearday += days_in_month[i++]; if ((days_per_year((leitch->year>90?1900:2000)+leitch->year)==365) && leitch->month > 2) leitch->yearday--; return(1); }
static bool clock_to_gmt(satime_t *timbuf) { int i; satime_t tmp; int year, month, day, hour, min, sec; if (machineid == HP_425 && mmuid == MMUID_425_E) { /* 425e uses mcclock on the frodo utility chip */ while ((mc_read(MC_REGA) & MC_REGA_UIP) != 0) continue; sec = mc_read(MC_SEC); min = mc_read(MC_MIN); hour = mc_read(MC_HOUR); day = mc_read(MC_DOM); month = mc_read(MC_MONTH); year = mc_read(MC_YEAR) + 1900; } else { /* Use the traditional HIL bbc for all other models */ read_bbc(); sec = bbc_to_decimal(1, 0); min = bbc_to_decimal(3, 2); /* * Hours are different for some reason. Makes no sense really. */ hour = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4]; day = bbc_to_decimal(8, 7); month = bbc_to_decimal(10, 9); year = bbc_to_decimal(12, 11) + 1900; } if (year < POSIX_BASE_YEAR) year += 100; #ifdef CLOCK_DEBUG printf("clock todr: %u/%u/%u %u:%u:%u\n", year, month, day, hour, min, sec); #endif range_test(hour, 0, 23); range_test(day, 1, 31); range_test(month, 1, 12); tmp = 0; for (i = POSIX_BASE_YEAR; i < year; i++) tmp += days_per_year(i); if (is_leap_year(year) && month > FEBRUARY) tmp++; for (i = 1; i < month; i++) tmp += days_in_month(i); tmp += (day - 1); tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec; *timbuf = tmp; return true; }
int split_time(double time, int *yyyy, int *mm, int *dd,int *hh, int *mi, int *ss, int *msec) { int year,month,hour,min; double day,sec; /* Switch debug messages from main on/off (1/0). */ int diagnostic = 0; if (time == NULLVAL) return 1; if (diagnostic) fprintf(logfp,"split_time: time=%f ",time); /* Split into days before/after epoch and seconds since start of day. */ /* Get date from day. */ day = (int)(time/86400); year= EPOCH_YEAR; if (day >= 0){ sec = time - day*86400; day++; while (day > days_per_year(year)){ day -= days_per_year(year); year++; } } else{ sec = time - day*86400; if (sec != 0){ day--; sec+=86400; } while (day < 0 ){ day += days_per_year(year); year--; } } for (month=1; month<13; month++){ if (day <= days_per_month(month,year)){ break; } day -= days_per_month(month,year); } if (diagnostic) fprintf(logfp,"split_time: year=%d month=%d day=%f ",year,month,day); /* Get time from sec */ min = sec/60; sec -= min*60; hour = min/60; min -= hour*60; if (diagnostic) fprintf(logfp,"split_time: hour=%d min=%d sec=%f\n",hour,min,sec); *yyyy = year; *mm = month; *dd = day; *hh = hour; *mi = min; *ss = (int)sec; *msec = (sec-*ss)*1000; *msec = (int)(*msec+0.5); return 0; }
time_t parse_to_unixtime( register clocktime_t *clock_time, register unsigned long *cvtrtc ) { #define SETRTC(_X_) { if (cvtrtc) *cvtrtc = (_X_); } static int days_of_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; register int i; time_t t; if (clock_time->utctime) return clock_time->utctime; /* if the conversion routine gets it right away - why not */ if ( clock_time->year < YEAR_PIVOT ) /* Y2KFixes [ */ clock_time->year += 100; /* convert 20xx%100 to 20xx-1900 */ if ( clock_time->year < YEAR_BREAK ) /* expand to full four-digits */ clock_time->year += 1900; if (clock_time->year < 1970 ) /* Y2KFixes ] */ { SETRTC(CVT_FAIL|CVT_BADDATE); return -1; } /* * sorry, slow section here - but it's not time critical anyway */ t = julian0(clock_time->year) - julian0(1970); /* Y2kFixes */ /* month */ if (clock_time->month <= 0 || clock_time->month > 12) { SETRTC(CVT_FAIL|CVT_BADDATE); return -1; /* bad month */ } #if 0 /* Y2KFixes */ /* adjust leap year */ if (clock_time->month < 3 && days_per_year(clock_time->year) == 366) t--; #else /* Y2KFixes [ */ if ( clock_time->month >= 3 && isleap_4(clock_time->year) ) t++; /* add one more if within leap year */ #endif /* Y2KFixes ] */ for (i = 1; i < clock_time->month; i++) { t += days_of_month[i]; } /* day */ if (clock_time->day < 1 || ((clock_time->month == 2 && days_per_year(clock_time->year) == 366) ? clock_time->day > 29 : clock_time->day > days_of_month[clock_time->month])) { SETRTC(CVT_FAIL|CVT_BADDATE); return -1; /* bad day */ } t += clock_time->day - 1; /* hour */ if (clock_time->hour < 0 || clock_time->hour >= 24) { SETRTC(CVT_FAIL|CVT_BADTIME); return -1; /* bad hour */ } t = t*24 + clock_time->hour; /* min */ if (clock_time->minute < 0 || clock_time->minute > 59) { SETRTC(CVT_FAIL|CVT_BADTIME); return -1; /* bad min */ } t = t*60 + clock_time->minute; /* sec */ if (clock_time->second < 0 || clock_time->second > 60) /* allow for LEAPs */ { SETRTC(CVT_FAIL|CVT_BADTIME); return -1; /* bad sec */ } t = t*60 + clock_time->second; t += clock_time->utcoffset; /* warp to UTC */ /* done */ clock_time->utctime = t; /* documentray only */ return t; }