-
Notifications
You must be signed in to change notification settings - Fork 0
/
strtime.c
46 lines (37 loc) · 1.35 KB
/
strtime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* strtime.c
Demonstrate the use of strptime() and strftime().
Calls strptime() using the given "format" to process the "input-date+time".
The conversion is then reversed by calling strftime() with the given
"out-format" (or a default format if this argument is omitted).
*/
#if ! defined(__sun)
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#endif
#include <time.h>
#include <locale.h>
#include "tlpi_hdr.h"
#define SBUF_SIZE 1000
int
main(int argc, char *argv[])
{
struct tm tm;
char sbuf[SBUF_SIZE];
char *ofmt;
if (argc < 3 || strcmp(argv[1], "--help") == 0)
usageErr("%s input-date-time in-format [out-format]\n", argv[0]);
if (setlocale(LC_ALL, "") == NULL)
errExit("setlocale"); /* Use locale settings in conversions */
memset(&tm, 0, sizeof(struct tm)); /* Initialize 'tm' */
if (strptime(argv[1], argv[2], &tm) == NULL)
fatal("strptime");
tm.tm_isdst = -1; /* Not set by strptime(); tells mktime()
to determine if DST is in effect */
printf("calendar time (seconds since Epoch): %ld\n", (long) mktime(&tm));
ofmt = (argc > 3) ? argv[3] : "%H:%M:%S %A, %d %B %Y %Z";
if (strftime(sbuf, SBUF_SIZE, ofmt, &tm) == 0)
fatal("strftime returned 0");
printf("strftime() yields: %s\n", sbuf);
exit(EXIT_SUCCESS);
}