1 | /* arpatime.c - routines to do ``ARPA-style'' time structures */
|
---|
2 | /* requires the "notorious" timeb.h ... */
|
---|
3 |
|
---|
4 | #include "tws.h"
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <sys/types.h>
|
---|
7 |
|
---|
8 | #include "timeb.h"
|
---|
9 |
|
---|
10 | #ifndef BSD42
|
---|
11 | #include <time.h>
|
---|
12 | #else BSD42
|
---|
13 | #include <sys/time.h>
|
---|
14 | #endif BSD42
|
---|
15 |
|
---|
16 | #ifdef MAIN
|
---|
17 | main()
|
---|
18 | {
|
---|
19 |
|
---|
20 | printf("%s\n",dtimenow());
|
---|
21 |
|
---|
22 | }
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | #define abs(a) (a >= 0 ? a : -a)
|
---|
26 |
|
---|
27 | static char *month[] = {
|
---|
28 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
29 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
30 | };
|
---|
31 |
|
---|
32 | static char *day[] = {
|
---|
33 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
---|
34 | };
|
---|
35 |
|
---|
36 | struct keywd {
|
---|
37 | char *key;
|
---|
38 | int value;
|
---|
39 | };
|
---|
40 |
|
---|
41 | #define NZONES (sizeof (zones)/sizeof (struct keywd))
|
---|
42 |
|
---|
43 | static struct keywd zones[] = { /* order sensitive */
|
---|
44 | "UT", 0,
|
---|
45 | "EST", -5, "CST", -6, "MST", -7, "PST", -8, "PDT", -9,
|
---|
46 | "A", -1, "B", -2, "C", -3, "D", -4,
|
---|
47 | "E", -5, "F", -6, "G", -7, "H", -8,
|
---|
48 | "I", -9, "K", -10, "L", -11, "M", -12,
|
---|
49 | "N", 1, "O", 2, "P", 3, "Q", 4,
|
---|
50 | "R", 5, "S", 6, "T", 7, "U", 8,
|
---|
51 | "V", 9, "W", 10, "X", 11, "Y", 12
|
---|
52 | };
|
---|
53 |
|
---|
54 |
|
---|
55 | long time ();
|
---|
56 | struct tm *localtime ();
|
---|
57 |
|
---|
58 | char *dtime (clock)
|
---|
59 | long *clock;
|
---|
60 | {
|
---|
61 | return dasctime (dlocaltime (clock));
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | char *dtimenow()
|
---|
66 | {
|
---|
67 | long clock;
|
---|
68 |
|
---|
69 | time (&clock);
|
---|
70 | return dtime (&clock);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | char *dctime (tw)
|
---|
75 | struct tws *tw;
|
---|
76 | {
|
---|
77 | static char buffer[25];
|
---|
78 |
|
---|
79 | if (!tw)
|
---|
80 | return NULL;
|
---|
81 |
|
---|
82 | sprintf (buffer, "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
|
---|
83 | day[tw -> tw_wday], month[tw -> tw_mon], tw -> tw_mday,
|
---|
84 | tw -> tw_hour, tw -> tw_min, tw -> tw_sec,
|
---|
85 | tw -> tw_year >= 100 ? tw -> tw_year : 1900 + tw -> tw_year);
|
---|
86 |
|
---|
87 | return buffer;
|
---|
88 | }
|
---|
89 |
|
---|
90 | struct tws *dlocaltime (clock)
|
---|
91 | long *clock;
|
---|
92 | {
|
---|
93 | struct tm *tm;
|
---|
94 | struct timeb tb;
|
---|
95 | static struct tws tw;
|
---|
96 |
|
---|
97 | if (!clock)
|
---|
98 | return NULL;
|
---|
99 |
|
---|
100 | tm = localtime (clock);
|
---|
101 | tw.tw_sec = tm -> tm_sec;
|
---|
102 | tw.tw_min = tm -> tm_min;
|
---|
103 | tw.tw_hour = tm -> tm_hour;
|
---|
104 | tw.tw_mday = tm -> tm_mday;
|
---|
105 | tw.tw_mon = tm -> tm_mon;
|
---|
106 | tw.tw_year = tm -> tm_year;
|
---|
107 | tw.tw_wday = tm -> tm_wday;
|
---|
108 | tw.tw_yday = tm -> tm_yday;
|
---|
109 | tw.tw_isdst = tm -> tm_isdst;
|
---|
110 |
|
---|
111 | ftime (&tb);
|
---|
112 | tw.tw_zone = -tb.timezone;
|
---|
113 | tw.tw_sday = 0;
|
---|
114 |
|
---|
115 | return (&tw);
|
---|
116 | }
|
---|
117 |
|
---|
118 | char *dasctime (tw)
|
---|
119 | struct tws *tw;
|
---|
120 | {
|
---|
121 | static char buffer[80],
|
---|
122 | result[80];
|
---|
123 |
|
---|
124 | if (!tw)
|
---|
125 | return NULL;
|
---|
126 |
|
---|
127 | sprintf (buffer, "%02d %s %02d %02d:%02d:%02d %s",
|
---|
128 | tw -> tw_mday, month[tw -> tw_mon], tw -> tw_year,
|
---|
129 | tw -> tw_hour, tw -> tw_min, tw -> tw_sec,
|
---|
130 | dtimezone (tw -> tw_zone, tw -> tw_isdst));
|
---|
131 |
|
---|
132 | if (tw -> tw_sday > 0)
|
---|
133 | sprintf (result, "%s, %s", day[tw -> tw_wday], buffer);
|
---|
134 | else
|
---|
135 | if (tw -> tw_sday < 0)
|
---|
136 | strcpy (result, buffer);
|
---|
137 | else
|
---|
138 | /* sprintf (result, "%s (%s)", buffer, day[tw -> tw_wday]);*/
|
---|
139 | sprintf (result, "%s, %s", day[tw -> tw_wday], buffer);
|
---|
140 | return result;
|
---|
141 | }
|
---|
142 |
|
---|
143 | char *dtimezone (zone, dst)
|
---|
144 | int zone,
|
---|
145 | dst;
|
---|
146 | {
|
---|
147 | int i,
|
---|
148 | j,
|
---|
149 | hours,
|
---|
150 | mins;
|
---|
151 | static char buffer[10];
|
---|
152 |
|
---|
153 | if (zone < 0) {
|
---|
154 | mins = -((-zone) % 60);
|
---|
155 | hours = -((-zone) / 60);
|
---|
156 | }
|
---|
157 | else {
|
---|
158 | mins = zone % 60;
|
---|
159 | hours = zone / 60;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (dst)
|
---|
163 | hours -= 1;
|
---|
164 |
|
---|
165 | if (mins == 0)
|
---|
166 | for (i = 0, j = NZONES; i < j; i++)
|
---|
167 | if (zones[i].value == hours) {
|
---|
168 | strcpy (buffer, zones[i].key);
|
---|
169 | if (dst && buffer[1] == 'S')
|
---|
170 | buffer[1] = 'D';
|
---|
171 | return buffer;
|
---|
172 | }
|
---|
173 |
|
---|
174 | sprintf (buffer, "%s%02d%02d",
|
---|
175 | zone < 0 ? "-" : "+", abs (hours), abs (mins));
|
---|
176 | return buffer;
|
---|
177 | }
|
---|
178 | |
---|