-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.c
349 lines (284 loc) · 6.99 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
date.c -- date utility operations
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <utime.h>
#include <sys/time.h>
#include "constants.h"
char *get_month();
char *get_day();
char *get_year();
char *get_date();
/**************************** DATE MODULES **********************************/
#define NUM_MONTHS 12
static char *date,timestring[9],month[4],day[3],year[3];
static char months[NUM_MONTHS][4]=
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
static int date_init=0;
/**************************** INIT_DATE() **********************************/
void init_date()
{
time_t clock;
struct timeval *tp;
struct timezone *tzp;
struct tm *time;
tp = (struct timeval *)malloc(sizeof(struct timeval));
tzp = (struct timezone *)malloc(sizeof(struct timezone));
gettimeofday(tp,tzp);
clock=(long)tp->tv_sec;
time=localtime(&clock);
date=asctime(time);
/* put leading zero on days 1 through 9
*/
if (date[8]==' ')
date[8]='0';
date_init=1;
(void)free(tp);
(void)free(tzp);
}
/**************************** GET_MONTH() **********************************/
char *
get_month()
{
if (!(date_init))
init_date();
(void)strncpy(month,&date[4],3);
return (month);
}
/*****************************************************************************/
int GetMonthNum(char *month)
{
int m;
for (m=0; m<NUM_MONTHS; m++)
if (!(strcmp(month,months[m])))
break;
return m;
}
/**************************** GET_MONTH_NUM() ******************************/
int get_month_num()
{
int m;
char *month;
month=get_month();
for (m=0; m<NUM_MONTHS; m++)
if (!(strcmp(month,months[m])))
break;
return m+1;
}
/**************************** GET_DAY() **********************************/
char *
get_day()
{
if (!(date_init))
init_date();
(void)strncpy(day,&date[8],2);
return (day);
}
/**************************** GET_YEAR() **********************************/
char *
get_year()
{
if (!(date_init))
init_date();
(void)strncpy(year,&date[22],2);
return (year);
}
/**************************** GetTime() **********************************/
char *
GetTime()
{
if (!(date_init))
init_date();
(void)strncpy(timestring,&date[11],8);
return (timestring);
}
/**************************** GET_DATE() **********************************/
char *
get_date()
/* since this returns the current time, always call init_date() first to
get current time
*/
{
init_date();
return (date);
}
/************************* GetYYMMDDfromBase() ****************************/
char *GetYYMMDDfromBase(time_t basetime)
/*
given a base time, return a string formatted as YYMMDD representing the
year-month-day, with leading zero if needed
*/
{
struct tm *time_info;
static char base_date[16];
time_info=(struct tm *)gmtime(&basetime);
(void)snprintf(base_date,16,"%02d%02d%02d",
time_info->tm_mon+1,time_info->tm_mday,time_info->tm_year);
return base_date;
}
/************************ GET_DATE_FROM_BASE() ******************************/
char *
get_date_from_base(time_t base)
/*
given a base time since the epoch, initialize a tm struct with
integers representing date and time and return a string with formatted date
*/
{
static char base_date[16];
time_t basetime;
struct tm *time_info;
basetime=base;
time_info=(struct tm *)gmtime(&basetime);
(void)snprintf(base_date,16,"%s-%02d-%02d",
months[time_info->tm_mon],time_info->tm_mday,time_info->tm_year);
return base_date;
}
/************************ GET_TIME_FROM_BASE() ******************************/
char *
get_time_from_base(time_t base)
/*
given a base time since the epoch, initialize a tm struct with
integers representing date and time and return a string with formatted time
*/
{
static char time[16];
time_t basetime;
struct tm *time_info;
basetime=base;
time_info=(struct tm *)gmtime(&basetime);
(void)snprintf(time,16,"%02d:%02d:%02d",
time_info->tm_hour,time_info->tm_min,time_info->tm_sec);
return time;
}
/*******************************************************************/
int GetTimeIntFromHMS(int hr,int min,int sec)
{
return hr*10000+min*100+sec;
}
/*******************************************************************/
time_t GetTimeIntFromBase(time_t base)
{
time_t basetime;
struct tm *time_info;
basetime=base;
time_info=(struct tm *)gmtime(&basetime);
return time_info->tm_hour*10000+time_info->tm_min*100+time_info->tm_sec;
}
static char TZstring[16];
/*******************************************************************/
time_t GetBaseFromMDYHMS(int month,int day,int year,int hr,int min,int sec)
{
struct tm time_info;
time_t basetime;
time_info.tm_mon=month;
time_info.tm_mday=day;
time_info.tm_year=year;
time_info.tm_hour=hr;
time_info.tm_min=min;
time_info.tm_sec=sec;
(void)snprintf(TZstring,16,"%s","TZ=GMT");
(void)putenv(TZstring);
basetime=(int)mktime(&time_info);
return basetime;
}
/************************ HMS2SEC() ******************************/
int
hms2sec(int hr,int minut,int sec)
/* convert integer hour,minutes,seconds to total seconds
*/
{
if (hr<0 || minut<0 || sec<0)
return ERROR;
if (hr>23 || minut>59 || sec>59)
return ERROR;
return hr*3600 + minut*60 + sec;
}
void
sec2hms(int total,int *hr,int *minut,int *sec)
/* convert integer total seconds to hour, minutes, and seconds
*/
{
if (total<0)
/* midnite crossover
*/
total+=24*3600;
*hr=total/3600;
*minut=(int)(((float)total/3600-*hr)*60);
*sec=total%60;
/* midnite crossover
*/
*hr=*hr%24;
}
int
next_end_period(int total,int interval)
/* given a total seconds (from midnite) and desired interval, return the next
total seconds that lands "evenly" at the end of a period as prescribed
by interval.
*/
{
if (total<0 || interval <=0)
return ERROR;
return (total+(interval-(total%interval)));
}
int last_start_period(int total,int rate,int interval)
/* given a total seconds (from midnite), an update rate, and interval, return
previous total seconds that lands "evenly" at the start of a period as
prescribed by rate and interval.
*/
{
if (total<0 || interval <=0 || rate <=0)
return ERROR;
if (!(total%interval))
return total-(rate*interval)+1;
else {
int temp;
temp=total-interval*rate;
if (temp < 0)
/* midnite crossover
*/
temp+=24*3600;
return next_end_period(temp,interval)+1;
}
}
/***
main(int argc, char **argv)
{
int basetime;
***/
/*** uncomment this, make date.o, then use gcc date.o -o gethhmmss to create
that binary
***/
/***
basetime=atoi(argv[1]);
printf("%s",get_time_from_base(basetime));
}
***/
/**
while (1) {
printf("enter basetime: ");
scanf("%d",&basetime);
printf("%s (%s)",
GetYYMMDDfromBase((long)basetime),get_date_from_base((long)basetime));
}
***/
/***
uncomment this part, make (date.o), then use
gcc date.o -o getmmyydd
...to create getmmyydd binary
basetime=atoi(argv[1]);
printf("%s",GetYYMMDDfromBase((long)basetime));
}
***/
/***
uncomment this part, make (date.o), then use
gcc date.c -o getMMM-dd-yy
...to create getMMM-dd-yy binary
***/
/***
basetime=atoi(argv[1]);
printf("%s",get_date_from_base((long)basetime));
}
***/