source: buchla-68k/orig/DATE/GTIME.C@ 4810254

Last change on this file since 4810254 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 8 years ago

Imported original source code.

  • Property mode set to 100755
File size: 2.0 KB
Line 
1/*
2 =============================================================================
3 gtime.c -- get the time from a string
4 Version 1 -- 1988-01-14
5 =============================================================================
6*/
7
8#include "stddefs.h"
9#include "time.h"
10
11#define dysize(A) (((A) % 4) ? 365 : 366)
12
13extern long timbuf;
14
15static char *cbp;
16static int dmsize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
17
18/*
19 =============================================================================
20 gpair() -- get 2 digits
21 =============================================================================
22*/
23
24static
25int
26gpair()
27{
28 register int c, d;
29 register char *cp;
30
31 cp = cbp;
32
33 if(*cp EQ 0)
34 return(-1);
35
36 c = (*cp++ - '0') * 10;
37
38 if (c < 0 OR c > 100)
39 return(-1);
40
41 if (*cp EQ 0)
42 return(-1);
43
44 if ((d = *cp++ - '0') < 0 OR d > 9)
45 return(-1);
46
47 cbp = cp;
48
49 return (c + d);
50}
51
52/*
53 */
54
55/*
56 =============================================================================
57 gtime() -- get time
58 =============================================================================
59*/
60
61int
62gtime(cp)
63char *cp;
64{
65 register int i;
66 register int y, t;
67 int d, h, m;
68 long nt;
69 struct tm ntm;
70
71 tzset();
72
73 cbp = cp;
74
75 t = gpair();
76
77 if (t < 1 OR t > 12)
78 return(1);
79
80 d = gpair();
81
82 if(d < 1 OR d > 31)
83 return(1);
84
85 h = gpair();
86
87 if (h EQ 24) {
88
89 h = 0;
90 d++;
91 }
92
93 m = gpair();
94
95 if (m < 0 OR m > 59)
96 return(1);
97
98 y = gpair();
99
100 if (y < 0) {
101
102 time(&nt);
103 y = lcltime(&nt, &ntm)->tm_year;
104 }
105
106 if (*cbp EQ 'p')
107 h += 12;
108
109 if (h < 0 OR h > 23)
110 return(1);
111
112 timbuf = 0;
113 y += 1900;
114
115 for(i = 1970; i < y; i++)
116 timbuf += dysize(i);
117
118 /* Leap year */
119
120 if (dysize(y) EQ 366 AND t GE 3)
121 timbuf += 1;
122
123 while (--t)
124 timbuf += dmsize[t - 1];
125
126 timbuf += (d - 1);
127 timbuf *= 24;
128 timbuf += h;
129 timbuf *= 60;
130 timbuf += m;
131 timbuf *= 60;
132
133 return(0);
134}
Note: See TracBrowser for help on using the repository browser.