19short IsLeapYear(
short year) {
22 year = year < 100 ? (year > 95 ? year + 1900 : year + 2000) : year;
23 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
29static short DaysInMonths[2][12] = {
60short JulianDayFromMonthDay(
short month,
short day,
short year,
short *julianDay) {
61 short leapYear, jday, i, daysInMonth;
63 if (year <= 0 || month < 1 || month > 12 || day < 1 || !julianDay)
66 leapYear = IsLeapYear(year);
67 daysInMonth = DaysInMonths[leapYear][month - 1];
68 if (day > daysInMonth)
72 for (i = 1; i < month; i++)
73 jday += DaysInMonths[leapYear][i - 1];
78short MonthDayFromJulianDay(
short julianDay,
short year,
short *month,
short *day) {
79 short leapYear, sum, i, days;
81 if (julianDay < 0 || julianDay > 366 || year <= 0 || !month || !day)
83 leapYear = IsLeapYear(year);
84 if ((leapYear == 0 && julianDay >= 365) || julianDay >= 366) {
91 for (i = 1; i <= 12; i++) {
92 days = DaysInMonths[leapYear][i - 1];
93 if (sum + days < julianDay) {
99 *day = julianDay - sum;
117short TimeEpochToBreakdown(
short *year,
short *jDay,
short *month,
short *day,
double *hour,
double epochTime) {
118 struct tm *timeBreakdown;
120 short lyear, ljDay, lhour;
123 if (!(timeBreakdown = localtime(&theTime)))
125 lyear = timeBreakdown->tm_year + 1900;
126 ljDay = timeBreakdown->tm_yday + 1;
127 lhour = timeBreakdown->tm_hour;
133 *month = timeBreakdown->tm_mon + 1;
135 *day = timeBreakdown->tm_mday;
139 *hour = (epochTime - dayStartTime) / 3600;
140 if (((
short)*hour) != lhour) {
142 *hour = *hour + lhour - ((short)*hour);
158 short year, jDay, month, day, hr, min;
163 min = 60 * (dayTime - hr);
164 sec = 3600.0 * dayTime - (3600.0 * hr + 60.0 * min);
165 sprintf(text,
"%04hd/%02hd/%02hd %02hd:%02hd:%07.4f",
166 year, month, day, hr, min, sec);
185 struct tm timeBreakdown;
191 memset((
char *)&timeBreakdown, 0,
sizeof(timeBreakdown));
193 timeBreakdown.tm_year = year - 1900;
195 timeBreakdown.tm_year = year;
198 if (!MonthDayFromJulianDay(jDay, year, &imonth, &iday)) {
201 timeBreakdown.tm_mday = iday;
202 timeBreakdown.tm_mon = imonth - 1;
204 timeBreakdown.tm_mday = day;
205 timeBreakdown.tm_mon = month - 1;
208 ihour = timeBreakdown.tm_hour = hour;
209 imin = timeBreakdown.tm_min = fmin = 60 * (hour - ihour);
210 timeBreakdown.tm_sec = fsec = 60 * (fmin - imin);
211 fsec -= timeBreakdown.tm_sec;
212 timeBreakdown.tm_isdst = -1;
214 *epochTime = timelocal(&timeBreakdown) + fsec;
216 *epochTime = mktime(&timeBreakdown) + fsec;
short TimeEpochToBreakdown(short *year, short *jDay, short *month, short *day, double *hour, double epochTime)
Breaks down epoch time into its constituent components.
short TimeBreakdownToEpoch(short year, short jDay, short month, short day, double hour, double *epochTime)
Converts a broken-down time into epoch time.
short TimeEpochToText(char *text, double epochTime)
Converts epoch time to a formatted text string.