SDDSlib
Loading...
Searching...
No Matches
time.c
Go to the documentation of this file.
1/**
2 * @file time.c
3 * @brief Contains time-related functions: mtime(), convert_date_time(), mtimes().
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, C. Saunders, R. Soliday
14 */
15#include "mdb.h"
16#include <ctype.h>
17#include <time.h>
18
19#ifdef VAX_VMS
20
21static char *month[12] = {
22 "january", "february", "march",
23 "april", "may", "june", "july",
24 "august", "september", "october",
25 "november", "december"};
26
27/* routine: convert_date_time()
28 * purpose: convert time string, as from mtime(), to DATE_TIME structure.
29 * assumed format of input string: day month year hours:minutes
30 * The month must be written out (e.g., "June", not 6).
31 * Michael Borland, 1987
32 */
33
34convert_date_time(dt, ct0)
35 DATE_TIME *dt;
36char *ct0;
37{
38 register char *ptr, *ct;
39 register long i, l;
40
41 /* copy the string */
42 cp_str(&ct, ct0);
43
44 /* shift everything to lower case */
45 ptr = ct;
46 while (*ptr) {
47 *ptr = tolower(*ptr);
48 ptr++;
49 }
50
51 /* scan the day */
52 if (!get_long(&(dt->day), ct))
53 return (0);
54
55 /* scan the month */
56 ptr = get_token(ct);
57 l = strlen(ptr);
58 for (i = 0; i < 12; i++)
59 if (strncmp(ptr, month[i], l) == 0)
60 break;
61 if (i == 12)
62 return (0);
63 dt->month = i + 1;
64
65 if (!get_long(&(dt->year), ct))
66 return (0);
67 if (dt->year < 100)
68 dt->year += 1900;
69
70 dt->hrs = 0;
71 dt->mins = 0;
72 dt->secs = 0;
73 while (*ct == ' ' && *ct)
74 ct++;
75 if (sscanf(ct, "%ld:%ld", &(dt->hrs), &(dt->mins)) != 2)
76 return (NO_TIME);
77
78 return (1);
79}
80#endif
81
82/**
83 * @brief Generates a formatted time string.
84 *
85 * This function returns a more user-friendly time string compared to `ctime()`.
86 *
87 * **Format Comparison:**
88 * - `ctime`: "wkd mmm dd hh:mm:ss 19yy\n"
89 * - `mtime`: "dd mmm yy hh:mm"
90 *
91 * @return A pointer to the newly allocated formatted time string.
92 */
93char *mtime(void) {
94 char *ct, *mt;
95 char *month, *day, *t, *ptr;
96 time_t i;
97 time_t time();
98
99 while ((mt = tmalloc((unsigned)30 * sizeof(*mt))) == NULL)
100 puts("allocation failure in mtime()");
101 time(&i);
102 ct = ctime(&i) + 4;
103 *(ct + strlen(ct) - 1) = 0;
104
105 month = ct;
106 ct = strchr(ct, ' ');
107 while (*ct == ' ')
108 *ct++ = 0;
109
110 day = ct;
111 ct = strchr(ct, ' ');
112 while (*ct == ' ')
113 *ct++ = 0;
114
115 t = ct;
116 ct = strchr(ct, ' ');
117 while (*ct == ' ')
118 *ct++ = 0;
119 ptr = strrchr(t, ':');
120 *ptr = 0;
121
122 sprintf(mt, "%s %s %s %s", day, month, ct + 2, t);
123 return (mt);
124}
125
126/**
127 * @brief Generates a detailed formatted time string.
128 *
129 * This function returns a more detailed and user-friendly time string compared to `ctime()`.
130 *
131 * **Format Comparison:**
132 * - `ctime`: "wkd mmm dd hh:mm:ss 19yy\n"
133 * - `mtimes`: "dd mmm yy hh:mm:ss"
134 *
135 * @return A pointer to the newly allocated detailed formatted time string.
136 */
137char *mtimes(void) {
138 char *ct, *mt;
139 char *month, *day, *t;
140 time_t i;
141 time_t time();
142
143 while ((mt = tmalloc((unsigned)30 * sizeof(*mt))) == NULL)
144 puts("allocation failure in mtime()");
145 time(&i);
146 ct = ctime(&i) + 4;
147 *(ct + strlen(ct) - 1) = 0;
148
149 month = ct;
150 ct = strchr(ct, ' ');
151 while (*ct == ' ')
152 *ct++ = 0;
153
154 day = ct;
155 ct = strchr(ct, ' ');
156 while (*ct == ' ')
157 *ct++ = 0;
158
159 t = ct;
160 ct = strchr(ct, ' ');
161 while (*ct == ' ')
162 *ct++ = 0;
163
164 sprintf(mt, "%s %s %s %s", day, month, ct + 2, t);
165 return (mt);
166}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28
int get_long(long *iptr, char *s)
Parses a long integer value from the given string.
Definition data_scan.c:255
char * get_token(char *s)
Extracts the next token from the input string.
Definition data_scan.c:413
char * mtimes(void)
Generates a detailed formatted time string.
Definition time.c:137
char * mtime(void)
Generates a formatted time string.
Definition time.c:93