@@ -565,25 +565,63 @@ uint32 CFE_TIME_Micro2SubSecs(uint32 MicroSeconds)
565565 * See description in header file for argument/return detail
566566 *
567567 *-----------------------------------------------------------------*/
568+ static void CFE_TIME_UnixSecondsToTm (struct tm * Time , uint64 UnixSeconds )
569+ {
570+ uint64 Days ;
571+ uint32 SecondsOfDay ;
572+ uint32 Year ;
573+ uint32 DaysInYear ;
574+
575+ Days = UnixSeconds / 86400 ;
576+ SecondsOfDay = UnixSeconds % 86400 ;
577+ Year = 1970 ;
578+
579+ while (true)
580+ {
581+ DaysInYear = 365 ;
582+ if (((Year % 4 ) == 0 ) && (((Year % 100 ) != 0 ) || ((Year % 400 ) == 0 )))
583+ {
584+ ++ DaysInYear ;
585+ }
586+
587+ if (Days < DaysInYear )
588+ {
589+ break ;
590+ }
591+
592+ Days -= DaysInYear ;
593+ ++ Year ;
594+ }
595+
596+ Time -> tm_year = (int )Year - 1900 ;
597+ Time -> tm_yday = (int )Days ;
598+ Time -> tm_hour = (int )(SecondsOfDay / 3600 );
599+ SecondsOfDay %= 3600 ;
600+ Time -> tm_min = (int )(SecondsOfDay / 60 );
601+ Time -> tm_sec = (int )(SecondsOfDay % 60 );
602+ }
603+
568604CFE_Status_t CFE_TIME_Print (char * PrintBuffer , CFE_TIME_SysTime_t TimeToPrint )
569605{
570606 size_t FmtLen = 0 ;
571607 uint32 Micros = (CFE_TIME_Sub2MicroSecs (TimeToPrint .Subseconds ) + CFE_MISSION_TIME_EPOCH_MICROS ) / 10 ;
572- struct tm tm = { 0 };
608+ uint64 UnixSeconds ;
609+ struct tm tm = { 0 };
573610
574611 if (PrintBuffer == NULL )
575612 {
576613 return CFE_TIME_BAD_ARGUMENT ;
577614 }
578615
579- time_t sec = TimeToPrint .Seconds + CFE_MISSION_TIME_EPOCH_SECONDS ; // epoch is Jan 1, 1980
616+ UnixSeconds = (uint64 )TimeToPrint .Seconds + (uint64 )CFE_MISSION_TIME_EPOCH_SECONDS ;
617+ CFE_TIME_UnixSecondsToTm (& tm , UnixSeconds );
580618
581- if (gmtime_r (& sec , & tm ) == NULL )
619+ FmtLen = strftime (PrintBuffer , CFE_TIME_PRINTED_STRING_SIZE - 6 , "%Y-%j-%H:%M:%S" , & tm );
620+ if (FmtLen == 0 )
582621 {
583622 return CFE_TIME_BAD_ARGUMENT ;
584623 }
585624
586- FmtLen = strftime (PrintBuffer , CFE_TIME_PRINTED_STRING_SIZE - 6 , "%Y-%j-%H:%M:%S" , & tm );
587625 PrintBuffer += FmtLen ;
588626 * (PrintBuffer ++ ) = '.' ;
589627
0 commit comments