1 /*
2 		    Low Level Interface Library
3 
4           Copyright 1983-2008 Green Hills Software,Inc.
5 
6  *  This program is the property of Green Hills Software, Inc,
7  *  its contents are proprietary information and no part of it
8  *  is to be disclosed to anyone except employees of Green Hills
9  *  Software, Inc., or as agreed in writing signed by the President
10  *  of Green Hills Software, Inc.
11 */
12 /* ind_time.c: ANSI time() and times() facilities. */
13 
14 #include "indos.h"
15 #include "cafe/os/OSTime.h"
16 
17 static int YearDays[] =
18 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
19 
20 static int LeapYearDays[] =
21 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
22 
23 /* Checks if the year is a leap year */
IsLeapYear(int year)24 static BOOL IsLeapYear(int year)
25 {
26     return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
27 }
28 
29 /* Gets days since January 1 to the month */
GetYearDays(int year,int month)30 static int GetYearDays(int year, int month)
31 {
32     int* md;
33 
34     md = IsLeapYear(year) ? LeapYearDays : YearDays;
35     return md[month];
36 }
37 
38 /* Gets extra days (Feb. 29) since 0000 */
GetLeapDays(int year)39 static int GetLeapDays(int year)
40 {
41     return (year + 3) / 4 - (year - 1) / 100 + (year - 1) / 400;
42 }
43 
44 #if !defined(ANYUNIX) && !defined(UNIXSYSCALLS) && !defined(SIMULATE)
45 /******************************************************************************/
46 /*  time_t time(time_t *tptr);						      */
47 /*									      */
48 /*  Return the current time relative to the Epoch (truncated to the most      */
49 /*  recent second), Midnight 00:00 January 1, 1970 Greenwich Mean Time.       */
50 /*  If tptr is non-null also store the time into *tptr.			      */
51 /*									      */
52 /*  If all you have is local time, not Greenwich Mean Time, then return the   */
53 /*  local time and return 0 from __gh_timezone().			      */
54 /*  This function is used for the C time_t and struct tm functions and the    */
55 /*  Fortran secnds, time(), idate(), date(), and fdate() functions.	      */
56 /*									      */
57 /*  NO DEFAULT IMPLEMENTATION						      */
58 /******************************************************************************/
time(time_t * tptr)59 time_t time(time_t *tptr) {
60 #if 1 // CAFE MOD
61     OSCalendarTime cosTime, *pCosTime = &cosTime;
62     u64 base_days, days, sec;
63 
64     OSTime t = OSGetTime();
65     OSTicksToCalendarTime(t, pCosTime);
66 
67     /* OS ticks and calendar time start from 0:00 AM January 1, 2000,
68        need to convert to 0:00 AM January 1, 1970 Greenwich Mean Time */
69 
70     base_days = 1970 * 365  + GetLeapDays(1970);
71     days      = pCosTime->year * 365 + GetLeapDays(pCosTime->year) + GetYearDays(pCosTime->year, pCosTime->mon) + pCosTime->mday - 1;
72 
73     sec = 86400 * (days - base_days) +
74           (u64)3600 * pCosTime->hour +
75           (u64) 60 * pCosTime->min +
76           pCosTime->sec;
77 
78     if (tptr!=NULL)
79         *tptr = (time_t)sec;
80 
81     return (time_t)sec;
82 #endif
83 
84 #if 0 //CAFE GHS ORIG
85     time_t the_time = -1;
86 
87 #if defined(EMBEDDED)
88 #pragma ghs nowarning 1547	/* Syscall prototypes might not match */
89     the_time = __ghs_syscall(SYSCALL_MONTIME);
90 #pragma ghs endnowarning 1547
91 #endif
92 /*
93  *  If no other implementation provided, return time = -1
94  */
95     if (tptr!=NULL)
96 	*tptr= the_time;
97     return(the_time);
98 #endif
99 }
100 #endif	/* !ANYUNIX and !UNIXSYSCALLS and !SIMULATE */
101 
102 #if !defined(ANYUNIX) && !defined(UNIXSYSCALLS)
103 /******************************************************************************/
104 /* #include <sys/types.h>						      */
105 /* #include <sys/times.h>						      */
106 /* int times(struct tms *buffer);					      */
107 /*									      */
108 /*  Place the process execution time in user mode in buffer->tms_utime	      */
109 /*  Place the process execution time in system mode in buffer->tms_stime      */
110 /*  Returns the elapsed time since EPOCH in clock ticks, or -1 on failure.    */
111 /*  All times are measured in clock ticks = 1/CLOCKS_PER_SEC second	      */
112 /*  This function is used for the C clock() function and the Fortran	      */
113 /*     dtime(), etime(), and mclock() functions.			      */
114 /*									      */
115 /*  NO DEFAULT IMPLEMENTATION						      */
116 /******************************************************************************/
times(struct tms * buffer)117 int times(struct tms *buffer) {
118 /*
119  *  If no other implementation provided, return time = 0
120  */
121     buffer->tms_utime = 0;
122     buffer->tms_stime = 0;
123     buffer->tms_cutime = 0;
124     buffer->tms_cstime = 0;
125     return -1;
126 }
127 #else
128 int _I_empty_file_illegal;
129 #endif	/* ANYUNIX or UNIXSYSCALLS */
130