File: time.c

package info (click to toggle)
moxftp 2.2-18.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,160 kB
  • ctags: 3,837
  • sloc: ansic: 43,420; makefile: 353; perl: 262; sh: 192
file content (374 lines) | stat: -rw-r--r-- 7,873 bytes parent folder | download | duplicates (4)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * Copyright (c) 1992 The Regents of the University of Texas System.
 * Copyright (c) 1993 The Regents of the University of Texas System.
 * Copyright (c) 1994 The Regents of the University of Texas System.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that the above copyright notice and this paragraph are duplicated in all
 * such forms and that any documentation, advertising materials,  and other
 * materials  related to such  distribution  and use  acknowledge  that the
 * software  was  developed  by the  University of Texas.  The  name of the
 * University may not be  used to endorse or promote  products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1993 The Regents of the University of Texas System.\n\
 All rights reserved.\n";
static char rcsid[] =
"$Id: time.c,v 1.1 1994/03/14 18:55:53 jones Exp $\n";
static char rcspath[] =
"$Source: /mintaka/u0/xx/ss/jones/jones.old/mftp.src/mftpnew/RCS/time.c,v $\n";
#endif

/* $Log: time.c,v $
 * Revision 1.1  1994/03/14  18:55:53  jones
 * Initial revision
 *
 */

#include "machine.h"
#include <time.h>

#if !defined(timemaker)
#if defined(sun)&&!defined(SVR4)
#define timemaker timegm
#else
#define timemaker mktime
#endif
#else
time_t timemaker();
#endif



static struct {
     char *month, number;
} months[] = {
	{ "january"  ,  0},
	{ "febuary"  ,  1},
	{ "march"    ,  2},
	{ "april"    ,  3},
	{ "may"      ,  4},
	{ "june"     ,  5},
	{ "july"     ,  6},
	{ "august"   ,  7},
	{ "september",  8},
	{ "october"  ,  9},
	{ "november" , 10},
	{ "december" , 11}
};
	
time_t 
get_unix_time(day, time_year, month)
char *day;
char *time_year;
char *month;
{
     struct tm tm, nowtm;
     time_t now;

     ZERO((char *)&tm, sizeof(tm));

     time(&now);
     nowtm = *localtime(&now);
#if defined(GMTOFF)
     tm.tm_gmtoff = nowtm.tm_gmtoff;
#endif
     
     /* 
      * Set month
      */
     get_month(&tm, &nowtm, month);
     /*
      * Set day.
      */
     get_day(&tm, &nowtm, day);
     /*
      * Set year or time;
      */
     get_year_time(&tm, &nowtm, time_year);
     now = timemaker(&tm);
     return now;
}

get_month(tm, nowtm, month)
struct tm *tm, *nowtm;
char *month;
{
     char temp[1000];
     int i,n;

     if (!month)  return;

     strcpy(temp, month);
     Lower(temp);
     n = strlen(temp);
     for (i=0; i<12; i++) {
	if (strncmp(temp, months[i].month, n) == 0) {
		tm->tm_mon   = months[i].number;
		if (tm->tm_mon <= nowtm->tm_mon) {
		    tm->tm_year  = nowtm->tm_year;
		} else {
		    tm->tm_year  = nowtm->tm_year - 1;
		}
		return;
	}
     }
}
	
get_day(tm, nowtm, day)
struct tm *tm, *nowtm;
char *day;
{
    int daynum;

    if (!day) return;
    daynum = atoi(day);
    if (daynum < 0 || daynum > 32) return;
    tm->tm_mday = daynum;
}

get_year_time(tm, nowtm, year_time)
struct tm *tm, *nowtm;
char *year_time;
{
     char *cp;
     char temp[8];
     int  year;

     strcpy(temp, year_time);
     if ((cp = INDEX(temp,':')) && (int)strlen(year_time) == 5) {
	/*
	 * Time of day.
	 */
	*cp = '\0';
	cp++;
	tm->tm_hour = atoi(temp);
	tm->tm_min  = atoi(cp);
     } else {
	/* 
	 * Must be year 
	 */
	year = atoi(year_time);
	year = year - 1900;
	if (year < 0) return;
	if (year > nowtm->tm_year) return;
	tm->tm_year = year;
    }
}

time_t 
get_remote_time(r_date, r_time)
char *r_date;
char *r_time;
{
    struct tm tm, nowtm;
    time_t now;

    ZERO((char *)&tm, sizeof(tm));

    time(&now);
    nowtm = *localtime(&now);
#if defined(SUN)
     tm.tm_gmtoff = nowtm.tm_gmtoff;
#endif
    /*
     * Parse date;
     */
    if(get_vms_date(&tm, &nowtm, r_date) == 0)  {
	get_vms_time(&tm, &nowtm, r_time);
    } else if (get_vm_date(&tm, &nowtm, r_date) == 0) {
	get_vm_time(&tm, &nowtm, r_time);
    }else if (get_tops20_date(&tm, &nowtm, r_date) == 0)  {
	get_tops20_time(&tm, &nowtm, r_time);
    } else {
	return 0;
    }

    now = timemaker(&tm);
    if (now >= 0) return now;
    return  0;
	
}

get_vms_date(tm, nowtm, r_date)
struct tm *tm, *nowtm;
char *r_date;
{
    char temp[12];
    int day;
    int month;
    int year;
    int i, n;
    char *cp;
    char *cp1;

    if (!r_date) return 1;
    if ((int)strlen(r_date) < 10) return 1;
    if (!((int)	strlen(r_date) == 11 &&
	  r_date[2] == '-' && r_date[6] == '-') &&
        !((int)strlen(r_date) == 10 &&
  	  r_date[1] == '-' && r_date[5] == '-') ) {
	 return 1;
    }
    strcpy(temp, r_date);
    Lower(temp);
    cp = INDEX(temp, '-');
    *cp = 0;
    day = atoi(temp);
    if (day < 0 || day > 32) return 1;
    cp++;
    cp1 = cp;
    cp = INDEX(cp, '-');
    *cp = 0;
    n = strlen(cp1);
    for (i=0; i<12; i++) {
	if (strncmp(cp1, months[i].month, n) == 0) {
	    month   = months[i].number;
	    break;
	}
    }
    if (i == 12) return 1;
    cp++;
    year = atoi(cp) - 1900;
    if (year < 0 || year > nowtm->tm_year) return 1;
    tm->tm_mday = day;
    tm->tm_year = year;
    tm->tm_mon  = month;
    return 0;
}
   

get_vms_time(tm, nowtm, r_time)
struct tm *tm, *nowtm;
char *r_time;
{
    char temp[7];

    if (!r_time || (int)strlen(r_time) != 5 || r_time[2] != ':') return; 
    strcpy(temp, r_time);
    temp[2] = '\0';
    tm->tm_hour = atoi(temp);
    tm->tm_min  = atoi(&temp[3]);
}


get_vm_date(tm, nowtm, r_date)
struct tm *tm, *nowtm;
char *r_date;
{
    char temp[12];
    int day;
    int month;
    int year;
    int i, n;
    char *cp;
    char *cp1;

    if (!r_date) return 1;
    if ((int)strlen(r_date) < 7) return 1;
    if (!((int)strlen(r_date) == 8 &&
	  r_date[2] == '/' && r_date[5] == '/') &&
        !((int)strlen(r_date) == 7 && 
  	  r_date[1] == '/' && r_date[4] == '/') ) {
	 return 1;
    }
    strcpy(temp, r_date);
    Lower(temp);
    cp = INDEX(temp, '/');
    *cp = 0;
    month = atoi(temp) - 1;
    if (month < 0 || month > 11) return 1;
    cp++;
    day = atoi(cp);
    if (day < 0 || day > 32) return 1;
    cp = INDEX(cp, '/');
    cp++;
    year = atoi(cp);
    if (year < 0 || year > nowtm->tm_year) return 1;
    tm->tm_mday = day;
    tm->tm_year = year;
    tm->tm_mon  = month;
    return 0;
}
   

get_vm_time(tm, nowtm, r_time)
struct tm *tm, *nowtm;
char *r_time;
{
    char temp[7];

    if (!r_time || (int)strlen(r_time) != 8 || r_time[2] != ':' ||
	 r_time[5] != ':') return;
    strcpy(temp, r_time);
    temp[2] = '\0';
    temp[5] = '\0';
    tm->tm_hour = atoi(temp);
    tm->tm_min  = atoi(&temp[3]);
    tm->tm_sec  = atoi(&temp[6]);
}



get_tops20_date(tm, nowtm, r_date)
struct tm *tm, *nowtm;
char *r_date;
{
    char temp[12];
    int day;
    int month;
    int year;
    int i, n;
    char *cp;
    char *cp1;

    if (!r_date) return 1;
    if ((int)strlen(r_date) < 8) return 1;
    if (!((int)strlen(r_date) == 9 &&
	  r_date[2] == '-' && r_date[6] == '-') &&
        !((int)strlen(r_date) == 8 &&
  	  r_date[1] == '-' && r_date[5] == '-') ) {
	 return 1;
    }
    strcpy(temp, r_date);
    Lower(temp);
    cp = INDEX(temp, '-');
    *cp = 0;
    day = atoi(temp);
    if (day < 0 || day > 32) return 1;
    cp++;
    cp1 = cp;
    cp = INDEX(cp, '-');
    *cp = 0;
    n = strlen(cp1);
    for (i=0; i<12; i++) {
	if (strncmp(cp1, months[i].month, n) == 0) {
	    month   = months[i].number;
	    break;
	}
    }
    if (i == 12) return 1;
    cp++;
    year = atoi(cp);
    if (year < 0 || year > nowtm->tm_year) return 1;
    tm->tm_mday = day;
    tm->tm_year = year;
    tm->tm_mon  = month;
    return 0;
}
   

get_tops20_time(tm, nowtm, r_time)
struct tm *tm, *nowtm;
char *r_time;
{
    get_vm_time(tm, nowtm, r_time);
}