-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.c
executable file
·47 lines (38 loc) · 1.05 KB
/
date.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
// The code for dayofweek was obtained at:
// https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
#ifdef CS333_P1
#include "types.h"
#include "user.h"
#include "date.h"
#define STDOUT 1
static char *months[] = {"NULL", "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static char *days[] = {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"};
static int
dayofweek(int y, int m, int d)
{
return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;
}
int
main(int argc, char *argv[])
{
int day;
struct rtcdate r;
if (date(&r)) {
printf(2,"Error: date call failed. %s at line %d\n",
__FILE__, __LINE__);
exit();
}
day = dayofweek(r.year, r.month, r.day);
printf(STDOUT, "%s %s %d", days[day], months[r.month], r.day);
printf(STDOUT, " ");
if (r.hour < 10) printf(1, "0");
printf(STDOUT, "%d:", r.hour);
if (r.minute < 10) printf(1, "0");
printf(STDOUT, "%d:", r.minute);
if (r.second < 10) printf(1, "0");
printf(STDOUT, "%d UTC %d\n", r.second, r.year);
exit();
}
#endif