SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
timeconvert.c File Reference

Detailed Description

Provides functions for converting and manipulating time representations, including leap year calculations, Julian day conversions, and epoch time breakdowns.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, R. Soliday

Definition in file timeconvert.c.

#include "mdb.h"
#include "time_utils.h"
#include <time.h>

Go to the source code of this file.

Functions

short IsLeapYear (short year)
 Determines whether a given year is a leap year.
 
short JulianDayFromMonthDay (short month, short day, short year, short *julianDay)
 Computes the Julian day number from a calendar date.
 
short MonthDayFromJulianDay (short julianDay, short year, short *month, short *day)
 Converts a Julian day number to month and day components.
 
short TimeEpochToBreakdown (short *year, short *jDay, short *month, short *day, double *hour, double epochTime)
 Breaks down epoch time into its constituent components.
 
short TimeEpochToText (char *text, double epochTime)
 Converts epoch time to a formatted text string.
 
short TimeBreakdownToEpoch (short year, short jDay, short month, short day, double hour, double *epochTime)
 Converts a broken-down time into epoch time.
 

Function Documentation

◆ IsLeapYear()

short IsLeapYear ( short year)

Determines whether a given year is a leap year.

Years below 100 are interpreted as 19xx if greater than 95, otherwise 20xx.

Parameters
yearThe year to evaluate.
Returns
1 if the year is a leap year, 0 if not, and -1 for invalid input.

Definition at line 28 of file timeconvert.c.

28 {
29 if (year < 0)
30 return -1;
31 year = year < 100 ? (year > 95 ? year + 1900 : year + 2000) : year;
32 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
33 return 1;
34 return 0;
35}

◆ JulianDayFromMonthDay()

short JulianDayFromMonthDay ( short month,
short day,
short year,
short * julianDay )

Computes the Julian day number from a calendar date.

Parameters
monthMonth of the year (1-12).
dayDay of the month.
yearYear value.
julianDayPointer to store the resulting Julian day number.
Returns
1 on success, 0 on failure.

Definition at line 78 of file timeconvert.c.

78 {
79 short leapYear, jday, i, daysInMonth;
80
81 if (year <= 0 || month < 1 || month > 12 || day < 1 || !julianDay)
82 return 0;
83
84 leapYear = IsLeapYear(year);
85 daysInMonth = DaysInMonths[leapYear][month - 1];
86 if (day > daysInMonth)
87 return 0;
88
89 jday = day;
90 for (i = 1; i < month; i++)
91 jday += DaysInMonths[leapYear][i - 1];
92 *julianDay = jday;
93 return 1;
94}
short IsLeapYear(short year)
Determines whether a given year is a leap year.
Definition timeconvert.c:28

◆ MonthDayFromJulianDay()

short MonthDayFromJulianDay ( short julianDay,
short year,
short * month,
short * day )

Converts a Julian day number to month and day components.

Parameters
julianDayThe Julian day number to convert.
yearYear corresponding to the Julian day.
monthPointer to store the resulting month (1-12).
dayPointer to store the resulting day of the month.
Returns
1 on success, 0 on failure.

Definition at line 105 of file timeconvert.c.

105 {
106 short leapYear, sum, i, days;
107
108 if (julianDay < 0 || julianDay > 366 || year <= 0 || !month || !day)
109 return 0;
110 leapYear = IsLeapYear(year);
111 if ((leapYear == 0 && julianDay >= 365) || julianDay >= 366) {
112 *month = 12;
113 *day = 31;
114 return 1;
115 }
116
117 sum = 0;
118 for (i = 1; i <= 12; i++) {
119 days = DaysInMonths[leapYear][i - 1];
120 if (sum + days < julianDay) {
121 sum += days;
122 } else
123 break;
124 }
125 *month = i;
126 *day = julianDay - sum;
127 return 1;
128}

◆ TimeBreakdownToEpoch()

short TimeBreakdownToEpoch ( short year,
short jDay,
short month,
short day,
double hour,
double * epochTime )

Converts a broken-down time into epoch time.

This function takes individual time components and converts them into epoch time. It handles both Julian day and standard month/day formats based on the input.

Parameters
yearThe year.
jDayThe Julian day number.
monthThe month.
dayThe day of the month.
hourThe fractional hour.
epochTimePointer to store the resulting epoch time.
Returns
Returns 1 on success, 0 on failure.

Definition at line 210 of file timeconvert.c.

210 {
211 struct tm timeBreakdown;
212 short imin, ihour;
213 double fsec, fmin;
214
215 if (!epochTime)
216 return 0;
217 memset((char *)&timeBreakdown, 0, sizeof(timeBreakdown));
218 if (year > 100)
219 timeBreakdown.tm_year = year - 1900;
220 else
221 timeBreakdown.tm_year = year;
222 if (jDay) {
223 short iday, imonth;
224 if (!MonthDayFromJulianDay(jDay, year, &imonth, &iday)) {
225 return 0;
226 }
227 timeBreakdown.tm_mday = iday;
228 timeBreakdown.tm_mon = imonth - 1;
229 } else {
230 timeBreakdown.tm_mday = day;
231 timeBreakdown.tm_mon = month - 1;
232 }
233 /* Break floating-point hours into integer H:M:S plus fractional seconds */
234 ihour = timeBreakdown.tm_hour = hour;
235 imin = timeBreakdown.tm_min = fmin = 60 * (hour - ihour);
236 timeBreakdown.tm_sec = fsec = 60 * (fmin - imin);
237 fsec -= timeBreakdown.tm_sec;
238 timeBreakdown.tm_isdst = -1;
239#if defined(SUNOS4)
240 *epochTime = timelocal(&timeBreakdown) + fsec;
241#else
242 *epochTime = mktime(&timeBreakdown) + fsec;
243#endif
244 return 1;
245}
short MonthDayFromJulianDay(short julianDay, short year, short *month, short *day)
Converts a Julian day number to month and day components.

◆ TimeEpochToBreakdown()

short TimeEpochToBreakdown ( short * year,
short * jDay,
short * month,
short * day,
double * hour,
double epochTime )

Breaks down epoch time into its constituent components.

This function decomposes epoch time into year, Julian day, month, day, and fractional hour components.

Parameters
yearPointer to store the year.
jDayPointer to store the Julian day.
monthPointer to store the month.
dayPointer to store the day.
hourPointer to store the fractional hour.
epochTimeThe epoch time to be broken down.
Returns
Returns 1 on success, 0 on failure.

Definition at line 143 of file timeconvert.c.

143 {
144 struct tm timeBreakdown;
145 double dayStartTime;
146 short lyear, ljDay, lhour;
147 time_t theTime;
148 theTime = epochTime;
149 if (!mdb_localtime_copy(&theTime, &timeBreakdown))
150 return 0;
151 lyear = timeBreakdown.tm_year + 1900;
152 ljDay = timeBreakdown.tm_yday + 1;
153 lhour = timeBreakdown.tm_hour;
154 if (year)
155 *year = lyear;
156 if (jDay)
157 *jDay = ljDay;
158 if (month)
159 *month = timeBreakdown.tm_mon + 1;
160 if (day)
161 *day = timeBreakdown.tm_mday;
162 if (hour) {
163 /* go through some contortions to preserve fractional seconds */
164 TimeBreakdownToEpoch(lyear, ljDay, (short)0, (short)0, (double)0.0, &dayStartTime);
165 *hour = (epochTime - dayStartTime) / 3600;
166 if (((short)*hour) != lhour) {
167 /* daylight savings time problem? */
168 *hour = *hour + lhour - ((short)*hour);
169 }
170 }
171 return 1;
172}
short TimeBreakdownToEpoch(short year, short jDay, short month, short day, double hour, double *epochTime)
Converts a broken-down time into epoch time.

◆ TimeEpochToText()

short TimeEpochToText ( char * text,
double epochTime )

Converts epoch time to a formatted text string.

This function formats epoch time into a human-readable string in the format "YYYY/MM/DD HH:MM:SS.FFFF".

Parameters
textBuffer to store the formatted time string.
epochTimeThe epoch time to be converted.
Returns
Returns 1 on success, 0 on failure.

Definition at line 183 of file timeconvert.c.

183 {
184 short year, jDay, month, day, hr, min;
185 double dayTime, sec;
186 if (!TimeEpochToBreakdown(&year, &jDay, &month, &day, &dayTime, epochTime))
187 return 0;
188 hr = dayTime;
189 min = 60 * (dayTime - hr);
190 sec = 3600.0 * dayTime - (3600.0 * hr + 60.0 * min);
191 sprintf(text, "%04hd/%02hd/%02hd %02hd:%02hd:%07.4f",
192 year, month, day, hr, min, sec);
193 return 1;
194}
short TimeEpochToBreakdown(short *year, short *jDay, short *month, short *day, double *hour, double epochTime)
Breaks down epoch time into its constituent components.