]> andersk Git - moira.git/blob - gen/util.c
Initial revision
[moira.git] / gen / util.c
1 /* $Header$
2  *
3  * Utility routines used by the SMS extraction programs.
4  */
5
6
7 #include <stdio.h>
8 #include <sys/time.h>
9
10 char *malloc();
11
12
13 /* Trim trailing spaces from a string by replacing one of them with a null.
14  */
15
16 trim(s)
17 register char *s;
18 {
19     register char *p;
20
21     for (p = s; *s; s++)
22       if (*s != ' ')
23         p = s;
24     if (p != s) {
25         if (*p == ' ')
26           *p = 0;
27         else
28           p[1] = 0;
29     }
30 }
31
32
33 /* return a "saved" copy of the string */
34
35 char *strsave(s)
36 register char *s;
37 {
38     register char *r;
39
40     r = malloc(strlen(s) + 1);
41     strcpy(r, s);
42     return(r);
43 }
44
45
46
47 /* ingres_date_and_time: passed a unix time_t, returns a string that ingres
48  * can parse to obtain that time.
49  */
50
51 static char *month_name[] = {
52     "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
53     "nov", "dec"
54   };
55
56
57 char *ingres_date(), *ingres_time();
58
59 char *ingres_date_and_time(l)
60 long l;
61 {
62         char *ans = NULL, *date, *time;
63       
64         if ((date = ingres_date(l)) && (time = ingres_time(l))) {
65                 char buf[BUFSIZ];
66                 sprintf(buf, "%s %s", date, time);
67                 ans = strsave(buf);
68         }
69         if (date)
70                 free(date);
71         if (time)
72                 free(time);
73         return ans;
74 }
75
76 char *ingres_time(t)
77         long t;
78 {
79         struct tm *tm;
80
81         if (t == (long) 0)
82                 (void) time(&t);
83
84         if ((tm = localtime(&t)) == (struct tm *) NULL) {
85                 return NULL;
86         } else {
87                 char buf[BUFSIZ];
88
89                 sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
90                         tm->tm_sec);
91                 return strsave(buf);
92         }
93 }
94
95 char *ingres_date(t)
96         long t;
97 {
98         struct tm *tm;
99
100         if (t == (long) 0)
101                 (void) time(&t);
102
103         if ((tm = localtime(&t)) == (struct tm *) NULL) {
104                 return NULL;
105         } else {
106                 char buf[BUFSIZ];
107
108                 sprintf(buf, "%02d-%3.3s-%04d", tm->tm_mday,
109                         month_name[tm->tm_mon], 1900 + tm->tm_year);
110                 return strsave(buf);
111         }
112 }
113
This page took 0.284924 seconds and 5 git commands to generate.