CS350 COS
COS
Loading...
Searching...
No Matches
time.c
Go to the documentation of this file.
1
2#include <stdbool.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <time.h>
6
7#include <sys/time.h>
8
9#include <syscall.h>
10
11#define TZ_OFFSET_SECS 0
12
13static const char *dayOfWeek[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
14static const char *months[12] = {
15 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
16 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
17};
18
21{
22 uint64_t nsec = OSTime();
23 time_t sec = nsec / 1000000000;
24
25 if (t)
26 *t = sec;
27
28 return sec;
29}
30
31int
32gettimeofday(struct timeval *tv, struct timezone *tz)
33{
34 uint64_t nsec = OSTime();
35
36 tv->tv_sec = nsec / 1000000000;
37 tv->tv_usec = (nsec % 1000000000) / 1000;
38
39 return 0;
40}
41
42int
43settimeofday(const struct timeval *tv, const struct timezone *tz)
44{
45 // set errno
46 return -1;
47}
48
49char *
50asctime_r(const struct tm *tm, char *buf)
51{
52 // assert(tm->tm_wday < 7);
53 // assert(tm->tm_mon < 12);
54
55 snprintf(buf, 26, "%s %s %2d %2d:%02d:%02d %4d\n",
58 tm->tm_sec, tm->tm_year + 1900);
59
60 return buf;
61}
62
63char *
64asctime(const struct tm *tm)
65{
66 static char buf[26];
67 return asctime_r(tm, buf);
68}
69
70char *
71ctime_r(const time_t *timep, char *buf)
72{
73 struct tm tm;
74 return asctime_r(localtime_r(timep, &tm), buf);
75}
76
77char *
78ctime(const time_t *timep)
79{
80 return asctime(localtime(timep));
81}
82
83static bool
85{
86 if ((year % 4) != 0)
87 return false;
88 if ((year % 100) != 0)
89 return true;
90 if ((year % 400) != 0)
91 return false;
92 return true;
93}
94
95static int
97{
98 static const uint64_t days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
99
100 if ((month == 2) && Time_IsLeapYear(year))
101 return 29;
102 else
103 return days[month];
104}
105
106
107struct tm *
108gmtime_r(const time_t *timep, struct tm *tm)
109{
110 uint64_t secs, mins, hours;
111 uint64_t days;
112 uint64_t y, m;
113
114 // Compute seconds
115 secs = *timep % (60 * 60 * 24);
116 days = *timep / (60 * 60 * 24);
117 mins = secs / 60;
118 secs = secs % 60;
119
120 // Compute minutes
121 hours = mins / 60;
122 mins = mins % 60;
123
124 // Compute hours
125 hours = hours % 24;
126
127 tm->tm_sec = secs;
128 tm->tm_min = mins;
129 tm->tm_hour = hours;
130
131 tm->tm_wday = (days + 3) % 7;
132
133 for (y = 1970; ; y++) {
134 uint64_t daysOfYear;
135 if (Time_IsLeapYear(y)) {
136 daysOfYear = 366;
137 } else {
138 daysOfYear = 365;
139 }
140
141 if (days < daysOfYear) {
142 tm->tm_yday = days;
143 tm->tm_year = y - 1900;
144 break;
145 }
146 days -= daysOfYear;
147 }
148
149 for (m = 0; ; m++) {
150 uint64_t daysOfMonth = Time_DaysInMonth(tm->tm_year + 1900, m);
151
152 if (days < daysOfMonth) {
153 tm->tm_mday = days;
154 tm->tm_mon = m;
155 break;
156 }
157 days -= daysOfMonth;
158 }
159
160 return tm;
161}
162
163struct tm *
164gmtime(const time_t *timep)
165{
166 static struct tm tm;
167 return gmtime_r(timep, &tm);
168}
169
170struct tm *
171localtime_r(const time_t *timep, struct tm *result)
172{
173 time_t t = *timep - TZ_OFFSET_SECS;
174 return gmtime_r(&t, result);
175}
176
177struct tm *
178localtime(const time_t *timep)
179{
180 static struct tm tm;
181 return localtime_r(timep, &tm);
182}
183
184time_t
185mktime(struct tm *tm)
186{
187 uint64_t days = 0;
188 uint64_t secs = 0;
189 uint64_t y, m;
190
191 // Convert to UNIX epoch
192 for (y = 70; y < tm->tm_year; y++) {
193 if (Time_IsLeapYear(y))
194 days += 366;
195 else
196 days += 365;
197 }
198
199 uint64_t yday = 0;
200 for (m = 0; m < tm->tm_mon; m++) {
201 yday += Time_DaysInMonth(tm->tm_year + 1900, m);
202 }
203 yday += tm->tm_mday;
204 days += yday;
205
206 secs = 24 * days + tm->tm_hour;
207 secs = secs * 60 + tm->tm_min;
208 secs = secs * 60 + tm->tm_sec;
209
210 secs += TZ_OFFSET_SECS;
211
212 return secs;
213}
214
static char buf[4096]
Definition: ethdump.c:10
uint64_t OSTime()
Definition: syscall.c:11
int tm_mon
Definition: time.h:12
int tm_year
Definition: time.h:13
int tm_hour
Definition: time.h:10
int tm_sec
Definition: time.h:8
int tm_yday
Definition: time.h:15
int tm_mday
Definition: time.h:11
int tm_min
Definition: time.h:9
int tm_wday
Definition: time.h:14
Definition: time.h:7
char * asctime_r(const struct tm *tm, char *buf)
Definition: time.c:50
struct tm * localtime(const time_t *timep)
Definition: time.c:178
struct tm * gmtime(const time_t *timep)
Definition: time.c:164
char * ctime_r(const time_t *timep, char *buf)
Definition: time.c:71
int settimeofday(const struct timeval *tv, const struct timezone *tz)
Definition: time.c:43
struct tm * gmtime_r(const time_t *timep, struct tm *tm)
Definition: time.c:108
static const char * months[12]
Definition: time.c:14
int gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: time.c:32
struct tm * localtime_r(const time_t *timep, struct tm *result)
Definition: time.c:171
#define TZ_OFFSET_SECS
Definition: time.c:11
char * ctime(const time_t *timep)
Definition: time.c:78
static int Time_DaysInMonth(uint64_t year, uint64_t month)
Definition: time.c:96
static const char * dayOfWeek[7]
Definition: time.c:13
static bool Time_IsLeapYear(uint64_t year)
Definition: time.c:84
time_t time(time_t *t)
Definition: time.c:20
char * asctime(const struct tm *tm)
Definition: time.c:64
time_t mktime(struct tm *tm)
Definition: time.c:185
int snprintf(char *str, size_t size, const char *fmt,...) __printflike(3
suseconds_t tv_usec
Definition: time.h:10
time_t tv_sec
Definition: time.h:9
Definition: time.h:8
Definition: time.h:13
uint64_t time_t
Definition: types.h:25
unsigned long uint64_t
Definition: types.h:13