} else if (stv->type == 'd' || stv->type == 'D') {
// Date
stvd = (SSaveToVarDate*)start;
// The value at stvd->dVal is a julian day number representing the date
iComputeDateFromJulianDayNumber((u32)stvd->dVal, buffer);
iAppendToTemplate(builder, buffer, strlen(buffer), NULL, NULL);
// Indicate where the next one will go after this entry
stv = (SSaveToVar*)(start + sizeof(SSaveToVarDate));
} else if (stv->type == 't' || stv->type == 'T') {
// Datetime
stvt = (SSaveToVarDatetime*)start;
// The integer portion at stvt->dVal is a julian day number representing the date
iComputeDateFromJulianDayNumber((u32)stvt->dVal, buffer);
iAppendToTemplate(builder, buffer, strlen(buffer), NULL, NULL);
// The fractional number portion at stvt->dVal relates to the seconds, which backs into the time.
iAppendToTemplate(builder, " ", 1, NULL, NULL);
// Compute the time from the fractional part
iComputeTimeFromFraction((stvt->dVal - (u32)stvt->dVal) * 24.0 * 60.0 * 60.0, buffer);
iAppendToTemplate(builder, buffer, strlen(buffer), NULL, NULL);
// Indicate where the next one will go after this entry
stv = (SSaveToVar*)(start + sizeof(SSaveToVarDatetime));
....
// Taken from [URL unfurl="true"]http://stason.org/TULARC/society/calendars/2-15-1-Is-there-a-formula-for-calculating-the-Julian-day-nu.html[/URL]
// Stores: mm/dd/yyyy
void iComputeDateFromJulianDayNumber(u32 tnJulianDayNumber, s8* tcDate11)
{
u32 a, b, c, d, e, m, day, month, year;
a = tnJulianDayNumber + 32044;
b = ((4 * a) + 3) / 146097;
c = a - ((b * 146097) / 4);
d = ((4 * c) + 3) / 1461;
e = c - ((1461 * d) / 4);
m = ((5 * e) + 2) / 153;
day = e - (((153 * m) + 2) / 5) + 1;
month = m + 3 - (12 * (m / 10));
year = (b * 100) + d - 4800 + (m / 10);
sprintf(tcDate11, "%02u/%02u/%04u\000", month, day, year);
}
// Takes the number of seconds elapsed since midnight and computes time
// hh:mm:ss.mls
void iComputeTimeFromFraction(f32 tfSeconds, s8* tcTime13)
{
u32 lnHour, lnMinute, lnSecond, lnMillisecond;
// Compute hour
lnHour = (u32)tfSeconds / (60 * 60);
tfSeconds = tfSeconds - (f32)(lnHour * 60 * 60);
// Compute minute
lnMinute = (u32)tfSeconds / 60;
tfSeconds = tfSeconds - (f32)(lnMinute * 60);
// Compute seconds
lnSecond = (u32)tfSeconds;
tfSeconds = tfSeconds - (f32)lnSecond;
// Note: Does not seem to be any value other than zero, though it could be as
// 64-bit floating point values have about 15 significant digits.
// Julian date numbers are only 7, leaving 7 or so significant digits
// for the milliseconds position. And even if it wasn't 100% accurate
// for milliseconds, having a partial would still be nice.
// Compute milliseconds
lnMillisecond = (u32)(tfSeconds * 999.0);
// Build the time
sprintf(tcTime13, "%02u:%02u:%02u.%03u\000", lnHour, lnMinute, lnSecond, lnMillisecond);
}