Skip to content

Commit 87e6289

Browse files
add time.h functions
1 parent f1dd861 commit 87e6289

File tree

9 files changed

+293
-0
lines changed

9 files changed

+293
-0
lines changed

src/std/shared/asctime.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <time.h>
2+
#include <stdio.h>
3+
4+
char *asctime(const struct tm *timeptr)
5+
{
6+
static const char wday_name[][4] =
7+
{
8+
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
9+
};
10+
static const char mon_name[][4] =
11+
{
12+
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
13+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
14+
};
15+
static char result[26];
16+
17+
sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
18+
wday_name[timeptr->tm_wday],
19+
mon_name[timeptr->tm_mon],
20+
timeptr->tm_mday,
21+
timeptr->tm_hour,
22+
timeptr->tm_min,
23+
timeptr->tm_sec,
24+
1900 + timeptr->tm_year);
25+
26+
return result;
27+
}

src/std/shared/ctime.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <tice.h>
2+
#include <time.h>
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
6+
char *ctime(const time_t *timer)
7+
{
8+
return asctime(localtime(timer));
9+
}

src/std/shared/difftime.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <time.h>
2+
#include <stdio.h>
3+
4+
double difftime(time_t end, time_t beginning)
5+
{
6+
return end - beginning;
7+
}

src/std/shared/gmtime.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <tice.h>
2+
#include <time.h>
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
6+
#define SECS_PER_DAY 86400UL
7+
#define SECS_PER_HOUR 3600UL
8+
#define SECS_PER_MIN 60UL
9+
#define SECS_PER_YEAR (365UL * SECS_PER_DAY)
10+
#define SECS_PER_LEAP (SECS_PER_YEAR + SECS_PER_DAY)
11+
12+
extern bool __isleap(int year);
13+
14+
static bool istmleap(int year)
15+
{
16+
year += 1900;
17+
18+
return __isleap(year);
19+
}
20+
21+
struct tm *gmtime(const time_t *tp)
22+
{
23+
static uint8_t dpm[] =
24+
{
25+
31,28,31,30,31,30,31,31,30,31,30,31
26+
};
27+
static struct tm tm2;
28+
time_t secs_this_year;
29+
time_t t = *tp;
30+
31+
tm2.tm_sec = 0;
32+
tm2.tm_min = 0;
33+
tm2.tm_hour = 0;
34+
tm2.tm_mday = 1;
35+
tm2.tm_mon = 0;
36+
tm2.tm_year = 70;
37+
tm2.tm_wday = (t / SECS_PER_DAY + 4 ) % 7;
38+
tm2.tm_isdst = -1;
39+
40+
while (t >= (secs_this_year = istmleap(tm2.tm_year) ? SECS_PER_LEAP : SECS_PER_YEAR ))
41+
{
42+
t -= secs_this_year;
43+
tm2.tm_year++;
44+
}
45+
46+
while (t < 0)
47+
{
48+
t += istmleap(--tm2.tm_year) ? SECS_PER_LEAP : SECS_PER_YEAR;
49+
}
50+
51+
tm2.tm_yday = t / SECS_PER_DAY;
52+
53+
if (istmleap(tm2.tm_year))
54+
{
55+
dpm[1] = 29;
56+
}
57+
58+
while (t >= dpm[tm2.tm_mon] * SECS_PER_DAY)
59+
{
60+
t -= dpm[tm2.tm_mon++] * SECS_PER_DAY;
61+
}
62+
63+
dpm[1] = 28;
64+
65+
while (t >= SECS_PER_DAY)
66+
{
67+
t -= SECS_PER_DAY;
68+
tm2.tm_mday++;
69+
}
70+
71+
while (t >= SECS_PER_HOUR)
72+
{
73+
t -= SECS_PER_HOUR;
74+
tm2.tm_hour++;
75+
}
76+
77+
while (t >= SECS_PER_MIN)
78+
{
79+
t -= SECS_PER_MIN;
80+
tm2.tm_min++;
81+
}
82+
83+
tm2.tm_sec = t;
84+
85+
return &tm2;
86+
}

src/std/shared/isleap.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdbool.h>
2+
#include <stdint.h>
3+
4+
extern bool __isleap(int year)
5+
{
6+
if( year % 100 == 0 )
7+
{
8+
return year % 400 == 0;
9+
}
10+
11+
return year % 4 == 0;
12+
}

src/std/shared/localtime.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <tice.h>
2+
#include <time.h>
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
6+
#define SECS_PER_MIN 60UL
7+
8+
struct tm *localtime(const time_t *timer)
9+
{
10+
time_t timer2 = *timer;
11+
timer2 -= LOCALTIME_GMT_OFFSET * SECS_PER_MIN;
12+
13+
return gmtime(&timer2);
14+
}

src/std/shared/mktime.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <tice.h>
2+
#include <time.h>
3+
#include <stdint.h>
4+
5+
#define SECS_PER_DAY 86400UL
6+
#define SECS_PER_HOUR 3600UL
7+
#define SECS_PER_MIN 60UL
8+
#define DAYS_PER_YEAR 365UL
9+
10+
extern bool __isleap(int year);
11+
12+
time_t mktime(struct tm *tp)
13+
{
14+
static const unsigned int dpmt[] =
15+
{
16+
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17+
};
18+
unsigned int i;
19+
time_t days;
20+
21+
if (tp->tm_year < (1970 - 1900))
22+
{
23+
return -1L;
24+
}
25+
26+
days = (tp->tm_year - (1970 - 1900)) * DAYS_PER_YEAR;
27+
28+
for (i = 1970; i < (unsigned int)tp->tm_year + 1900; ++i)
29+
{
30+
if (__isleap(i))
31+
{
32+
days++;
33+
}
34+
}
35+
36+
days += dpmt[tp->tm_mon];
37+
if (__isleap(tp->tm_year))
38+
{
39+
days++;
40+
}
41+
42+
days += tp->tm_mday - 1;
43+
44+
days *= SECS_PER_DAY;
45+
46+
days += (tp->tm_hour * SECS_PER_HOUR) + (tp->tm_min * SECS_PER_MIN) + tp->tm_sec;
47+
48+
return days;
49+
}

src/std/shared/time.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <tice.h>
2+
#include <time.h>
3+
#include <stdint.h>
4+
5+
time_t time(time_t *timer)
6+
{
7+
uint8_t sec, min, hrs;
8+
uint8_t day, month;
9+
uint16_t year;
10+
struct tm tm2;
11+
time_t res;
12+
13+
boot_GetDate(&day, &month, &year);
14+
boot_GetTime(&sec, &min, &hrs);
15+
16+
month--;
17+
18+
tm2.tm_sec = sec;
19+
tm2.tm_min = min;
20+
tm2.tm_hour = hrs;
21+
tm2.tm_mday = day;
22+
tm2.tm_mon = month;
23+
tm2.tm_year = (unsigned int)year - 1900;
24+
25+
res = mktime(&tm2);
26+
27+
if (timer != NULL)
28+
{
29+
*timer = res;
30+
}
31+
32+
return res;
33+
}

src/std/time.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef _TIME_H
2+
#define _TIME_H
3+
4+
#include <cdefs.h>
5+
#include <stdint.h>
6+
7+
#ifndef NULL
8+
#define NULL ((void*)0)
9+
#endif
10+
11+
#ifndef SIZE_T_DEFINED
12+
#define SIZE_T_DEFINED
13+
typedef __SIZE_TYPE__ size_t;
14+
#endif
15+
16+
#ifndef LOCALTIME_GMT_OFFSET
17+
#define LOCALTIME_GMT_OFFSET 0
18+
#endif
19+
20+
__BEGIN_DECLS
21+
22+
typedef uint32_t time_t;
23+
typedef uint32_t clock_t;
24+
25+
struct tm {
26+
int tm_sec;
27+
int tm_min;
28+
int tm_hour;
29+
int tm_mday;
30+
int tm_mon;
31+
int tm_year;
32+
int tm_wday;
33+
int tm_yday;
34+
int tm_isdst;
35+
};
36+
37+
clock_t clock(void);
38+
39+
double difftime(time_t time1, time_t time0)
40+
__attribute__((__const__));
41+
42+
time_t mktime(struct tm *tmp);
43+
44+
time_t time(time_t *timer);
45+
46+
struct tm *localtime(const time_t *timer);
47+
48+
struct tm *gmtime(const time_t *timer);
49+
50+
char *asctime(const struct tm *tmp);
51+
52+
char *ctime(const time_t *timer);
53+
54+
__END_DECLS
55+
56+
#endif /* _TIME_H */

0 commit comments

Comments
 (0)