-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.c
197 lines (136 loc) · 5.23 KB
/
time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
Copyright 2012-2014 Infinitycoding all rights reserved
This file is part of the mercury c-library.
The mercury c-library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
The mercury c-library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the mercury c-library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
@author Peter Hösch aka. BlitzBasic <[email protected]>
*/
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <atoi.h>
const char weekday_name_short[WEEKDAY_NUMBER][MAX_WEEKDAY_NAME_SHORT_LENGTH] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const char weekday_name_long[WEEKDAY_NUMBER][MAX_WEEKDAY_NAME_LONG_LENGTH] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const char month_name_short[MONTHS_NUMBER][MAX_MONTH_NAME_SHORT_LENGTH] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
const char month_name_long[MONTHS_NUMBER][MAX_MONTH_NAME_LONG_LENGTH] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char static_string[STATIC_TIME_STRING_LENGTH];
struct tm static_tm;
time_t time(time_t *timer)
{
// TODO
time_t t = 0;//TODO(time_t)linux_syscall(SYS_TIME, 0, 0, 0, 0, 0);
if(timer != NULL)
*timer = t;
return t;
}
struct tm *localtime(const time_t *timer)
{
return gmtolocal(gmtime(timer));
}
struct tm *gmtime(const time_t *timer)
{
if(validtimestamp(timer) == INVALID_TIME)
return NULL;
int days = *timer / SECONDS_PER_DAY;
int remaining_seconds = *timer % SECONDS_PER_DAY;
// TODO
static_tm.tm_sec = (remaining_seconds % SECONDS_PER_MINUTE);
static_tm.tm_min = ((remaining_seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
static_tm.tm_hour = (remaining_seconds / SECONDS_PER_HOUR);
static_tm.tm_wday = (thursday + (days % DAYS_PER_WEEK));
return &static_tm;
}
time_t mktime(struct tm *timer)
{
if(validtm(timer) == INVALID_TIME)
return -1;
int year = 1900 + timer->tm_year;
int leap_years = ((year - 1) - 1968) / 4 - ((year - 1) - 1900) / 100 + ((year - 1) - 1600) / 400;
time_t timestamp = timer->tm_sec + (timer->tm_min * SECONDS_PER_MINUTE) + (timer->tm_hour * SECONDS_PER_HOUR) + (timer->tm_yday * SECONDS_PER_DAY) + (((year-1970)*DAYS_PER_YEAR+leap_years)*SECONDS_PER_DAY);
if((timer->tm_mon >2) && (year%4==0 && (year%100!=0 || year%400==0)))
timestamp += SECONDS_PER_DAY;
return timestamp;
}
char *asctime(struct tm *timer)
{
if(validtm(timer) == INVALID_TIME)
return NULL;
memset(static_string, ' ', sizeof(char) * STATIC_TIME_STRING_LENGTH);
strncpy(&static_string[0], weekday_name_short[timer->tm_wday], 4);
strncpy(&static_string[5], month_name_short[timer->tm_mon], 4);
itoa(timer->tm_mday, &static_string[10], 10);
itoa(timer->tm_hour, &static_string[13], 10);
static_string[15] = ':';
itoa(timer->tm_min, &static_string[16], 10);
static_string[18] = ':';
itoa(timer->tm_sec, &static_string[19], 10);
itoa(timer->tm_year + 1900, &static_string[23], 10);
static_string[27] = '\n';
static_string[28] = '\0';
return static_string;
}
char *ctime(const time_t *timer)
{
return asctime(localtime(timer));
}
double difftime(time_t time1, time_t time0)
{
return ((double)(time1 - time0));
}
clock_t clock()
{
// TODO
return -1;
}
size_t strftime(char *buffer, int maxchars, const char *format, struct tm *timer)
{
// TODO
return 0;
}
int validtm(const struct tm *timer)
{
if(timer == NULL)
return INVALID_TIME;
if(timer->tm_sec < 0 || timer->tm_sec > 59 || timer->tm_min < 0 || timer->tm_min > 59 || timer->tm_hour < 0 || timer->tm_hour > 23 || timer->tm_mday < 1 || timer->tm_mday > 31 || timer->tm_year < 70 || timer->tm_wday < 0 || timer->tm_wday > 6 || timer->tm_yday < 0 || timer->tm_yday > 365 || (timer->tm_yday > 364 && (timer->tm_yday % 4 != 0 || (timer->tm_yday % 100 == 0 && (timer->tm_yday + 1900) % 400 != 0))))
return INVALID_TIME;
return VALID_TIME;
}
int validtimestamp(const time_t *timer)
{
if(timer == NULL)
return INVALID_TIME;
if(timer < 0)
return INVALID_TIME;
return VALID_TIME;
}
struct tm *gmtolocal(struct tm *timer)
{
if(validtm(timer) == INVALID_TIME)
return NULL;
// TODO
return timer;
}
int gettimeofday(struct timeval *tp, struct timezone *tzp)
{
if(tp)
{
tp->tv_sec = 0;//TODO//linux_syscall(SYS_TIME, 0, 0, 0, 0, 0);
tp->tv_usec = 0; // kernel can't log usecs at the time of implementation
}
if(tzp)
{
tzp->tz_minuteswest = 23*60; // timeshift westwards from GMT -> timezone of middle europe
tzp->tz_dsttime = 4; //
}
return 0;
}