Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions modules/time/fsw/src/cfe_time_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,25 +565,63 @@ uint32 CFE_TIME_Micro2SubSecs(uint32 MicroSeconds)
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
static void CFE_TIME_UnixSecondsToTm(struct tm *Time, uint64 UnixSeconds)
{
uint64 Days;
uint32 SecondsOfDay;
uint32 Year;
uint32 DaysInYear;

Days = UnixSeconds / 86400;
SecondsOfDay = UnixSeconds % 86400;
Year = 1970;

while (true)
{
DaysInYear = 365;
if (((Year % 4) == 0) && (((Year % 100) != 0) || ((Year % 400) == 0)))
{
++DaysInYear;
}

if (Days < DaysInYear)
{
break;
}

Days -= DaysInYear;
++Year;
}

Time->tm_year = (int)Year - 1900;
Time->tm_yday = (int)Days;
Time->tm_hour = (int)(SecondsOfDay / 3600);
SecondsOfDay %= 3600;
Time->tm_min = (int)(SecondsOfDay / 60);
Time->tm_sec = (int)(SecondsOfDay % 60);
}

CFE_Status_t CFE_TIME_Print(char *PrintBuffer, CFE_TIME_SysTime_t TimeToPrint)
{
size_t FmtLen = 0;
uint32 Micros = (CFE_TIME_Sub2MicroSecs(TimeToPrint.Subseconds) + CFE_MISSION_TIME_EPOCH_MICROS) / 10;
struct tm tm = { 0 };
uint64 UnixSeconds;
struct tm tm = { 0 };

if (PrintBuffer == NULL)
{
return CFE_TIME_BAD_ARGUMENT;
}

time_t sec = TimeToPrint.Seconds + CFE_MISSION_TIME_EPOCH_SECONDS; // epoch is Jan 1, 1980
UnixSeconds = (uint64)TimeToPrint.Seconds + (uint64)CFE_MISSION_TIME_EPOCH_SECONDS;
CFE_TIME_UnixSecondsToTm(&tm, UnixSeconds);

if (gmtime_r(&sec, &tm) == NULL)
FmtLen = strftime(PrintBuffer, CFE_TIME_PRINTED_STRING_SIZE - 6, "%Y-%j-%H:%M:%S", &tm);
if (FmtLen == 0)
{
return CFE_TIME_BAD_ARGUMENT;
}

FmtLen = strftime(PrintBuffer, CFE_TIME_PRINTED_STRING_SIZE - 6, "%Y-%j-%H:%M:%S", &tm);
PrintBuffer += FmtLen;
*(PrintBuffer++) = '.';

Expand Down
17 changes: 17 additions & 0 deletions modules/time/ut-coverage/time_UT.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,23 @@ void Test_Print(void)
(unsigned int)time.Subseconds,
timeBuf);
}
/* Test the maximum seconds value, including the 2100 non-leap-century boundary */
time.Subseconds = 0;
time.Seconds = 0xffffffff;

CFE_UtAssert_SUCCESS(CFE_TIME_Print(timeBuf, time));
if (usingDefaultEpoch)
{
strcpy(expectedBuf, "2116-038-06:28:15.00000");
UtAssert_STRINGBUF_EQ(timeBuf, sizeof(timeBuf), expectedBuf, sizeof(expectedBuf));
}
else
{
UtAssert_MIR("Confirm adding seconds = %u, subseconds = %u to configured EPOCH results in time %s",
(unsigned int)time.Seconds,
(unsigned int)time.Subseconds,
timeBuf);
}
}

/*
Expand Down