1 /*
2 Copyright (C) 2017-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4grpdt.ctr
12 */
13 
14 /**	@file dk4grpdt.c The dk4grpdt module.
15 */
16 
17 
18 #include "dk4conf.h"
19 
20 #if DK4_TIME_WITH_SYS_TIME
21 #ifndef SYS_TIME_H_INCLUDED
22 #include <sys/time.h>
23 #define SYS_TIME_H_INCLUDED 1
24 #endif
25 #ifndef TIME_H_INCLUDED
26 #include <time.h>
27 #define TIME_H_INCLUDED 1
28 #endif
29 #else
30 #if DK4_HAVE_TIME_H
31 #ifndef TIME_H_INCLUDED
32 #include <time.h>
33 #define TIME_H_INCLUDED 1
34 #endif
35 #else
36 #if DK4_HAVE_SYS_TIME_H
37 #ifndef SYS_TIME_H_INCLUDED
38 #include <sys/time.h>
39 #define SYS_TIME_H_INCLUDED 1
40 #endif
41 #endif
42 #endif
43 #endif
44 
45 #include <stdio.h>
46 
47 #ifndef	GRA_H_INCLUDED
48 #include <libdk4gra/gra.h>
49 #endif
50 
51 #if DK4_HAVE_ASSERT_H
52 #ifndef	ASSERT_H_INCLUDED
53 #include <assert.h>
54 #define	ASSERT_H_INCLUDED 1
55 #endif
56 #endif
57 
58 
59 
60 /**	Conversion format string.
61 */
62 static const char dk4gra_pdf_ts_format[] = { "%04d%02d%02d%02d%02d%02d" };
63 
64 
65 
66 /**	Convert timestamp to text.
67 	@param	buf		Destination buffer.
68 	@param	szbuf	Size of destination buffer.
69 	@param	plct	Local time splitted into components.
70 */
71 static
72 void
dk4gra_pdf_convert_timestamp(char * buf,size_t szbuf,const struct tm * plct)73 dk4gra_pdf_convert_timestamp(char *buf, size_t szbuf, const struct tm *plct)
74 {
75 #if DK4_HAVE__SNPRINTF_S
76 	_snprintf_s(
77 		buf, szbuf, _TRUNCATE, dk4gra_pdf_ts_format,
78 		(1900 + plct->tm_year),
79 		(1 + plct->tm_mon),
80 		plct->tm_mday,
81 		plct->tm_hour,
82 		plct->tm_min,
83 		plct->tm_sec
84 	);
85 #else
86 #if DK4_HAVE__SNPRINTF
87 	_snprintf(
88 		buf, szbuf, dk4gra_pdf_ts_format,
89 		(1900 + plct->tm_year),
90 		(1 + plct->tm_mon),
91 		plct->tm_mday,
92 		plct->tm_hour,
93 		plct->tm_min,
94 		plct->tm_sec
95 	);
96 #else
97 #if DK4_HAVE_SNPRINTF
98 	snprintf(
99 		buf, szbuf, dk4gra_pdf_ts_format,
100 		(1900 + plct->tm_year),
101 		(1 + plct->tm_mon),
102 		plct->tm_mday,
103 		plct->tm_hour,
104 		plct->tm_min,
105 		plct->tm_sec
106 	);
107 #else
108 	sprintf(
109 		buf, dk4gra_pdf_ts_format,
110 		(1900 + plct->tm_year),
111 		(1 + plct->tm_mon),
112 		plct->tm_mday,
113 		plct->tm_hour,
114 		plct->tm_min,
115 		plct->tm_sec
116 	);
117 #endif
118 #endif
119 #endif
120 }
121 
122 
123 
124 int
dk4gra_pdf_timestamp(char * buf,size_t szbuf,dk4_er_t * erp)125 dk4gra_pdf_timestamp(char *buf, size_t szbuf, dk4_er_t *erp)
126 {
127 #if	DK4_ON_WINDOWS
128 /* +++ Windows +++ */
129 	struct tm	 lct;
130 	__time64_t	 timer;
131 /* --- Windows --- */
132 #else
133 /* +++ POSIX +++ */
134 	struct tm	 lct;
135 #if !DK4_HAVE_LOCALTIME_R
136 	struct tm	*plct;
137 #endif
138 	time_t		 timer;
139 /* --- POSIX --- */
140 #endif
141 
142 	int		 back = 0;
143 #if	DK4_USE_ASSERT
144 	assert(NULL != buf);
145 	assert(14 < szbuf);
146 #endif
147 	if ((NULL == buf) || (15 > szbuf)) {
148 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
149 		goto finished;
150 	}
151 	buf[0] = '\0';
152 #if	DK4_ON_WINDOWS
153 /* +++ Windows +++ */
154 	(void)_time64(&timer);
155 	if (0 == _localtime64_s(&lct, &timer)) {
156 		back = 1;
157 		dk4gra_pdf_convert_timestamp(buf, szbuf, &lct);
158 	}
159 /* --- Windows --- */
160 #else
161 /* +++ POSIX +++ */
162 	(void)time(&timer);
163 #if	DK4_HAVE_LOCALTIME_R
164 	if (NULL != localtime_r(&timer, &lct)) {
165 		back = 1;
166 		dk4gra_pdf_convert_timestamp(buf, szbuf, &lct);
167 	}
168 #else
169 	dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
170 #endif
171 /* --- POSIX --- */
172 #endif
173 
174 	finished:
175 	return back;
176 }
177 
178 
179 
180 
181 /* vim: set ai sw=4 ts=4 : */
182 
183