-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patht3.c
153 lines (126 loc) · 2.55 KB
/
t3.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
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "print.h"
Fmtconv date_tab[FMT_MAXCONV];
// XXX - expose these things from print.c?
static int
badconv(Format *format, int c)
{
eprint("dateconv: bad conversion char '%c'\n", c);
format->error = 1;
return FMT_verb;
}
static int
litconv(Format *format, int c)
{
fmtputc(format, c);
return FMT_verb;
}
// XXX - expose intconv() instead?
static void
put_int02(Format *format, int i)
{
Fmtconv *save = format->fmttab;
format->fmttab = 0;
fmtprint(format, "%02d", i);
format->fmttab = save;
}
static int
dconv(Format *format, int c)
{
struct tm *tm = (struct tm *)format->client_data;
put_int02(format, tm->tm_mday);
return FMT_verb;
}
static int
mconv(Format *format, int c)
{
struct tm *tm = (struct tm *)format->client_data;
put_int02(format, tm->tm_mon + 1);
return FMT_verb;
}
static int
yconv(Format *format, int c)
{
struct tm *tm = (struct tm *)format->client_data;
put_int02(format, tm->tm_year);
return FMT_verb;
}
static int
Dconv(Format *format, int c)
{
fmtprint(format, "%m/%d/%y");
return FMT_verb;
}
static int
Hconv(Format *format, int c)
{
struct tm *tm = (struct tm *)format->client_data;
put_int02(format, tm->tm_hour);
return FMT_verb;
}
static int
Mconv(Format *format, int c)
{
struct tm *tm = (struct tm *)format->client_data;
put_int02(format, tm->tm_min);
return FMT_verb;
}
static int
Sconv(Format *format, int c)
{
struct tm *tm = (struct tm *)format->client_data;
put_int02(format, tm->tm_sec);
return FMT_verb;
}
static int
Tconv(Format *format, int c)
{
fmtprint(format, "%H:%M:%S");
return FMT_verb;
}
static void
init_date_tab()
{
int i;
Fmtconv *fmttab = date_tab;
for (i = 0; i < FMT_MAXCONV; i++)
fmttab[i] = badconv;
fmttab['%'] = litconv;
fmttab['d'] = dconv; // day of month
fmttab['m'] = mconv; // month
fmttab['y'] = yconv; // year
fmttab['D'] = Dconv; // %m/%d/%y
fmttab['H'] = Hconv; // hour
fmttab['M'] = Mconv; // min
fmttab['S'] = Sconv; // sec
fmttab['T'] = Tconv; // %H:%M:%S
}
char *
format_date(time_t clock, char *fmt)
{
struct tm tm = *gmtime(&clock);
Format format;
memset(&format, 0, sizeof(format));
format.buf = 0;
format.bufbegin = 0;
format.bufend = 0;
format.flushed = 0;
format.error = 0;
format.u.p = (void *)palloc;
format.grow = fmt_memprint_grow;
format.fmttab = date_tab;
format.client_data = &tm;
if (date_tab[0] == 0)
init_date_tab();
return fmt_memprint(&format, fmt, 0);
}
int
main()
{
char *ds = format_date(time(0), "%% the date is %D at time %T");
print("%s\n", ds);
free(ds);
return 0;
}