/* Low Level Interface Library Copyright 1983-2008 Green Hills Software,Inc. * This program is the property of Green Hills Software, Inc, * its contents are proprietary information and no part of it * is to be disclosed to anyone except employees of Green Hills * Software, Inc., or as agreed in writing signed by the President * of Green Hills Software, Inc. */ /* ind_time.c: ANSI time() and times() facilities. */ #include "indos.h" #include "cafe/os/OSTime.h" static int YearDays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; static int LeapYearDays[] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; /* Checks if the year is a leap year */ static BOOL IsLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } /* Gets days since January 1 to the month */ static int GetYearDays(int year, int month) { int* md; md = IsLeapYear(year) ? LeapYearDays : YearDays; return md[month]; } /* Gets extra days (Feb. 29) since 0000 */ static int GetLeapDays(int year) { return (year + 3) / 4 - (year - 1) / 100 + (year - 1) / 400; } #if !defined(ANYUNIX) && !defined(UNIXSYSCALLS) && !defined(SIMULATE) /******************************************************************************/ /* time_t time(time_t *tptr); */ /* */ /* Return the current time relative to the Epoch (truncated to the most */ /* recent second), Midnight 00:00 January 1, 1970 Greenwich Mean Time. */ /* If tptr is non-null also store the time into *tptr. */ /* */ /* If all you have is local time, not Greenwich Mean Time, then return the */ /* local time and return 0 from __gh_timezone(). */ /* This function is used for the C time_t and struct tm functions and the */ /* Fortran secnds, time(), idate(), date(), and fdate() functions. */ /* */ /* NO DEFAULT IMPLEMENTATION */ /******************************************************************************/ time_t time(time_t *tptr) { #if 1 // CAFE MOD OSCalendarTime cosTime, *pCosTime = &cosTime; u64 base_days, days, sec; OSTime t = OSGetTime(); OSTicksToCalendarTime(t, pCosTime); /* OS ticks and calendar time start from 0:00 AM January 1, 2000, need to convert to 0:00 AM January 1, 1970 Greenwich Mean Time */ base_days = 1970 * 365 + GetLeapDays(1970); days = pCosTime->year * 365 + GetLeapDays(pCosTime->year) + GetYearDays(pCosTime->year, pCosTime->mon) + pCosTime->mday - 1; sec = 86400 * (days - base_days) + (u64)3600 * pCosTime->hour + (u64) 60 * pCosTime->min + pCosTime->sec; if (tptr!=NULL) *tptr = (time_t)sec; return (time_t)sec; #endif #if 0 //CAFE GHS ORIG time_t the_time = -1; #if defined(EMBEDDED) #pragma ghs nowarning 1547 /* Syscall prototypes might not match */ the_time = __ghs_syscall(SYSCALL_MONTIME); #pragma ghs endnowarning 1547 #endif /* * If no other implementation provided, return time = -1 */ if (tptr!=NULL) *tptr= the_time; return(the_time); #endif } #endif /* !ANYUNIX and !UNIXSYSCALLS and !SIMULATE */ #if !defined(ANYUNIX) && !defined(UNIXSYSCALLS) /******************************************************************************/ /* #include */ /* #include */ /* int times(struct tms *buffer); */ /* */ /* Place the process execution time in user mode in buffer->tms_utime */ /* Place the process execution time in system mode in buffer->tms_stime */ /* Returns the elapsed time since EPOCH in clock ticks, or -1 on failure. */ /* All times are measured in clock ticks = 1/CLOCKS_PER_SEC second */ /* This function is used for the C clock() function and the Fortran */ /* dtime(), etime(), and mclock() functions. */ /* */ /* NO DEFAULT IMPLEMENTATION */ /******************************************************************************/ int times(struct tms *buffer) { /* * If no other implementation provided, return time = 0 */ buffer->tms_utime = 0; buffer->tms_stime = 0; buffer->tms_cutime = 0; buffer->tms_cstime = 0; return -1; } #else int _I_empty_file_illegal; #endif /* ANYUNIX or UNIXSYSCALLS */