|
| 1 | +/* ------------------------------------------------------------------------------ |
| 2 | + * |
| 3 | + * File Name: |
| 4 | + * datelib.c |
| 5 | + * |
| 6 | + * Description: |
| 7 | + * This file contains functions for manipulating dates and times. |
| 8 | + * |
| 9 | + * Functions defined: |
| 10 | + * format_date_string |
| 11 | + * |
| 12 | + * Author: |
| 13 | + * Brian Rapp Jun 19, 2012 |
| 14 | + * |
| 15 | + * Modification History: |
| 16 | + * Modified by Date |
| 17 | + * Description |
| 18 | + * |
| 19 | + ------------------------------------------------------------------------------ */ |
| 20 | + |
| 21 | +#ifndef _XOPEN_SOURCE |
| 22 | +#define _XOPEN_SOURCE |
| 23 | +#endif |
| 24 | +#include <time.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <stdlib.h> |
| 27 | +#include <string.h> |
| 28 | +#include <unistd.h> |
| 29 | +#include <ctype.h> |
| 30 | +#include <libgen.h> |
| 31 | + |
| 32 | +#include "datelib.h" |
| 33 | + |
| 34 | +/* ------------------------------------------------------------------------------ |
| 35 | +
|
| 36 | +Function Name |
| 37 | + formatDateString |
| 38 | +Synopsis |
| 39 | + Creates custom-formatted date strings. |
| 40 | +
|
| 41 | +Prototype |
| 42 | + int formatDateString (char *in_format, char *out_format, long adjustment, |
| 43 | + char *in_date, char *out_date) |
| 44 | +
|
| 45 | +Arguments |
| 46 | +
|
| 47 | + Name: in_format |
| 48 | + Type: char * |
| 49 | + Access: read only |
| 50 | +
|
| 51 | + Character string containing the format specification for the input time. |
| 52 | + See the man page for strftime for details. This is only used when an |
| 53 | + absolute input date is provided. |
| 54 | +
|
| 55 | + Name: out_format |
| 56 | + Type: char * |
| 57 | + Access: read only |
| 58 | +
|
| 59 | + Character string containing the Format specification for the output time. |
| 60 | + See the man page for strftime for details. This argument is required. |
| 61 | +
|
| 62 | + Name: adjustment |
| 63 | + Type: long |
| 64 | + Access: read only |
| 65 | +
|
| 66 | + A long integer containing an adjustment (in seconds) to be applied to |
| 67 | + the input time. Negative values adjust the time prior to the input |
| 68 | + time. Positive values adjust the time after the input time. A value of |
| 69 | + zero indicates no adjustment is to be made to the input time. |
| 70 | +
|
| 71 | + Name: in_date |
| 72 | + Type: char * |
| 73 | + Access: read only |
| 74 | +
|
| 75 | + Character string containing the input time. Can be an absolute time in |
| 76 | + any format supported by the strftime function, or one of the following |
| 77 | + relative times: |
| 78 | +
|
| 79 | + NOW Current date and time |
| 80 | + TODAY Today at midnight |
| 81 | + YESTERDAY Yesterday at midnight |
| 82 | + TOMORROW Tomorrow at midnight |
| 83 | +
|
| 84 | + Name: out_date |
| 85 | + Type: char * |
| 86 | + Access: write only |
| 87 | +
|
| 88 | + A character string that is large enough to hold the output date provided |
| 89 | + in the out_format argument. The string must be allocated by the caller. |
| 90 | +
|
| 91 | + Name: max_out_size |
| 92 | + Type: int |
| 93 | + Access: read only |
| 94 | +
|
| 95 | + An integer containing the maximum size for out_date. This value must |
| 96 | + include the trailing nul character. |
| 97 | +
|
| 98 | +Description |
| 99 | + This function creates a formatted date string optionally adjusted by |
| 100 | + a specified amount. The output format defaults to "YYYY-MM-DD HH:MM:SS", |
| 101 | + but can be fully specified with the 'out_format' argument. See the |
| 102 | + man page for strftime for more information on formatting. |
| 103 | +
|
| 104 | + An input time string is required, which can either be an absolute time |
| 105 | + of the form "YYYY-MM-DD HH:MM:SS" or one of the relative time strings |
| 106 | + "NOW", "YESTERDAY", "TODAY", or "TOMORROW". |
| 107 | +
|
| 108 | + Another optional parameter provides the |
| 109 | + ability to adjust the time string by the specified number of seconds. |
| 110 | +
|
| 111 | +Return Values |
| 112 | + The number of characters placed in out_date, not including the terminating |
| 113 | + nul terminator, provided the string, including the terminating nul byte, |
| 114 | + fits. Otherwise, it returns 0, and the contents of the array is undefined. |
| 115 | +
|
| 116 | +------------------------------------------------------------------------------ */ |
| 117 | + |
| 118 | +int formatDateString (const char *in_format, const char *out_format, long adjustment, |
| 119 | + char *in_date, char *out_date, int max_out_size) { |
| 120 | + |
| 121 | + struct tm *tp; |
| 122 | + struct tm ts; |
| 123 | + time_t the_time; |
| 124 | + char *res; |
| 125 | + size_t stat; |
| 126 | + time_t time_adjust = 0; |
| 127 | + char new_out_format[MAX_DATE_FORMAT_LEN+1]; |
| 128 | + |
| 129 | + if ((out_format == NULL) || (strlen (out_format) == 0)) { |
| 130 | + strcpy (new_out_format, DEFAULT_OUT_FORMAT); |
| 131 | + } else { |
| 132 | + strncpy (new_out_format, out_format, MAX_DATE_FORMAT_LEN); |
| 133 | + } |
| 134 | + |
| 135 | + if (!strcmp (in_date, RELT_NOW)) { |
| 136 | + time (&the_time); |
| 137 | + } else { |
| 138 | + if (!strcmp (in_date, RELT_TODAY)) { |
| 139 | + time (&the_time); |
| 140 | + tp = localtime (&the_time); |
| 141 | + tp->tm_hour = tp->tm_min = tp->tm_sec = 0; |
| 142 | + the_time = mktime (tp); |
| 143 | + } else { |
| 144 | + if (!strcmp (in_date, RELT_YESTERDAY)) { |
| 145 | + time (&the_time); |
| 146 | + tp = localtime (&the_time); |
| 147 | + tp->tm_hour = tp->tm_min = tp->tm_sec = 0; |
| 148 | + the_time = mktime (tp); |
| 149 | + the_time -= 86400; |
| 150 | + } else { |
| 151 | + if (!strcmp (in_date, RELT_TOMORROW)) { |
| 152 | + time (&the_time); |
| 153 | + tp = localtime (&the_time); |
| 154 | + tp->tm_hour = tp->tm_min = tp->tm_sec = 0; |
| 155 | + the_time = mktime (tp); |
| 156 | + the_time += 86400; |
| 157 | + } else { |
| 158 | + memset (&ts, 0, sizeof (struct tm)); |
| 159 | + res = strptime (in_date, in_format, &ts); /* Convert time string to broken-down time -- dst may be wrong */ |
| 160 | + if ((res == 0) || (*res != 0)) { |
| 161 | + return 0; |
| 162 | + } |
| 163 | + |
| 164 | + /* Convert broken-down time back to calendar time so we have a proper value for tm_isdst */ |
| 165 | + the_time = mktime (&ts); /* Convert broken-down time to calendar time */ |
| 166 | + localtime_r (&the_time, &ts); /* Convert calendar time back to broken-down time -- dst will be set properly here */ |
| 167 | + |
| 168 | + res = strptime (in_date, in_format, &ts); |
| 169 | + |
| 170 | + if ((ts.tm_year == 0) || (ts.tm_mday == 0)) { |
| 171 | + return 0; |
| 172 | + } |
| 173 | + the_time = mktime (&ts); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + the_time += time_adjust; |
| 180 | + memset (&ts, 0, sizeof (struct tm)); |
| 181 | + tp = localtime_r (&the_time, &ts); /* This will set tm_isdst properly for the given time */ |
| 182 | + stat = strftime (out_date, max_out_size, new_out_format, &ts); |
| 183 | + |
| 184 | + return stat; |
| 185 | + |
| 186 | +} |
0 commit comments