SDDS ToolKit Programs and Libraries for C and Python
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 "time_utils.h"
17#include <ctype.h>
18#include <time.h>
19
20#ifdef VAX_VMS
21
22static const char *const month[12] = {
23 "january", "february", "march",
24 "april", "may", "june", "july",
25 "august", "september", "october",
26 "november", "december"};
27
28/**
29 * @brief Converts a time string into a DATE_TIME structure.
30 *
31 * The input should have the format "day month year hours:minutes" with the
32 * month written out (e.g., "June" rather than "6").
33 *
34 * @param dt Pointer to the DATE_TIME structure to populate.
35 * @param ct0 String containing the time specification.
36 * @return 1 on success, 0 on failure, or NO_TIME if the format is invalid.
37 */
38
39convert_date_time(dt, ct0)
40 DATE_TIME *dt;
41 char *ct0;
42{
43 register char *ptr, *ct;
44 register long i, l;
45
46 /* copy the string */
47 cp_str(&ct, ct0);
48
49 /* shift everything to lower case */
50 ptr = ct;
51 while (*ptr) {
52 *ptr = tolower(*ptr);
53 ptr++;
54 }
55
56 /* scan the day */
57 if (!get_long(&(dt->day), ct))
58 return (0);
59
60 /* scan the month */
61 ptr = get_token(ct);
62 l = strlen(ptr);
63 for (i = 0; i < 12; i++)
64 if (strncmp(ptr, month[i], l) == 0)
65 break;
66 if (i == 12)
67 return (0);
68 dt->month = i + 1;
69
70 if (!get_long(&(dt->year), ct))
71 return (0);
72 if (dt->year < 100)
73 dt->year += 1900;
74
75 dt->hrs = 0;
76 dt->mins = 0;
77 dt->secs = 0;
78 while (*ct == ' ' && *ct)
79 ct++;
80 if (sscanf(ct, "%ld:%ld", &(dt->hrs), &(dt->mins)) != 2)
81 return (NO_TIME);
82
83 return (1);
84}
85#endif
86
87static const char *const MonthAbbreviation[12] = {
88 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
89 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
90
91/**
92 * @brief Generates a formatted time string.
93 *
94 * This function returns a compact local time string.
95 *
96 * **Format Comparison:**
97 * - `ctime`: "wkd mmm dd hh:mm:ss 19yy\n"
98 * - `mtime`: "dd mmm yy hh:mm"
99 *
100 * @return A pointer to the newly allocated formatted time string.
101 */
102char *mtime(void) {
103 char *mt;
104 struct tm timeBreakdown;
105 time_t i;
106
107 while ((mt = tmalloc((unsigned)30 * sizeof(*mt))) == NULL)
108 puts("allocation failure in mtime()");
109 time(&i);
110 if (!mdb_localtime_copy(&i, &timeBreakdown)) {
111 mt[0] = 0;
112 return mt;
113 }
114
115 sprintf(mt, "%d %s %02d %02d:%02d",
116 timeBreakdown.tm_mday,
117 MonthAbbreviation[timeBreakdown.tm_mon],
118 (timeBreakdown.tm_year + 1900) % 100,
119 timeBreakdown.tm_hour,
120 timeBreakdown.tm_min);
121 return (mt);
122}
123
124/**
125 * @brief Generates a detailed formatted time string.
126 *
127 * This function returns a compact local time string including seconds.
128 *
129 * **Format Comparison:**
130 * - `ctime`: "wkd mmm dd hh:mm:ss 19yy\n"
131 * - `mtimes`: "dd mmm yy hh:mm:ss"
132 *
133 * @return A pointer to the newly allocated detailed formatted time string.
134 */
135char *mtimes(void) {
136 char *mt;
137 struct tm timeBreakdown;
138 time_t i;
139
140 while ((mt = tmalloc((unsigned)30 * sizeof(*mt))) == NULL)
141 puts("allocation failure in mtime()");
142 time(&i);
143 if (!mdb_localtime_copy(&i, &timeBreakdown)) {
144 mt[0] = 0;
145 return mt;
146 }
147
148 sprintf(mt, "%d %s %02d %02d:%02d:%02d",
149 timeBreakdown.tm_mday,
150 MonthAbbreviation[timeBreakdown.tm_mon],
151 (timeBreakdown.tm_year + 1900) % 100,
152 timeBreakdown.tm_hour,
153 timeBreakdown.tm_min,
154 timeBreakdown.tm_sec);
155 return (mt);
156}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:65
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:135
char * mtime(void)
Generates a formatted time string.
Definition time.c:102