summaryrefslogtreecommitdiff
path: root/support/dktools/dk4grpdt.c
blob: 3a52787552ceee8895eff2f3d072fadcf95491bd (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Copyright (C) 2017-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4grpdt.ctr
*/

/**	@file dk4grpdt.c The dk4grpdt module.
*/


#line 9 "dk4grpdt.ctr"

#include "dk4conf.h"

#if DK4_TIME_WITH_SYS_TIME
#ifndef SYS_TIME_H_INCLUDED
#include <sys/time.h>
#define SYS_TIME_H_INCLUDED 1
#endif
#ifndef TIME_H_INCLUDED
#include <time.h>
#define TIME_H_INCLUDED 1
#endif
#else
#if DK4_HAVE_TIME_H
#ifndef TIME_H_INCLUDED
#include <time.h>
#define TIME_H_INCLUDED 1
#endif
#else
#if DK4_HAVE_SYS_TIME_H
#ifndef SYS_TIME_H_INCLUDED
#include <sys/time.h>
#define SYS_TIME_H_INCLUDED 1
#endif
#endif
#endif
#endif

#include <stdio.h>

#ifndef	GRA_H_INCLUDED
#include "gra.h"
#endif

#if DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif



/**	Conversion format string.
*/
static const char dk4gra_pdf_ts_format[] = { "%04d%02d%02d%02d%02d%02d" };



/**	Convert timestamp to text.
	@param	buf		Destination buffer.
	@param	szbuf	Size of destination buffer.
	@param	plct	Local time splitted into components.
*/
static
void
dk4gra_pdf_convert_timestamp(char *buf, size_t szbuf, const struct tm *plct)
{
#if DK4_HAVE__SNPRINTF_S
	_snprintf_s(
		buf, szbuf, _TRUNCATE, dk4gra_pdf_ts_format,
		(1900 + plct->tm_year),
		(1 + plct->tm_mon),
		plct->tm_mday,
		plct->tm_hour,
		plct->tm_min,
		plct->tm_sec
	);
#else
#if DK4_HAVE__SNPRINTF
	_snprintf(
		buf, szbuf, dk4gra_pdf_ts_format,
		(1900 + plct->tm_year),
		(1 + plct->tm_mon),
		plct->tm_mday,
		plct->tm_hour,
		plct->tm_min,
		plct->tm_sec
	);
#else
#if DK4_HAVE_SNPRINTF
	snprintf(
		buf, szbuf, dk4gra_pdf_ts_format,
		(1900 + plct->tm_year),
		(1 + plct->tm_mon),
		plct->tm_mday,
		plct->tm_hour,
		plct->tm_min,
		plct->tm_sec
	);
#else
	sprintf(
		buf, dk4gra_pdf_ts_format,
		(1900 + plct->tm_year),
		(1 + plct->tm_mon),
		plct->tm_mday,
		plct->tm_hour,
		plct->tm_min,
		plct->tm_sec
	);
#endif
#endif
#endif
}



int
dk4gra_pdf_timestamp(char *buf, size_t szbuf, dk4_er_t *erp)
{
#if	DK4_ON_WINDOWS
/* +++ Windows +++ */
	struct tm	 lct;
	__time64_t	 timer;
/* --- Windows --- */
#else
/* +++ POSIX +++ */
	struct tm	 lct;
#if !DK4_HAVE_LOCALTIME_R
	struct tm	*plct;
#endif
	time_t		 timer;
/* --- POSIX --- */
#endif

	int		 back = 0;
#if	DK4_USE_ASSERT
	assert(NULL != buf);
	assert(14 < szbuf);
#endif
	if ((NULL == buf) || (15 > szbuf)) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}
	buf[0] = '\0';
#if	DK4_ON_WINDOWS
/* +++ Windows +++ */
	(void)_time64(&timer);
	if (0 == _localtime64_s(&lct, &timer)) {
		back = 1;
		dk4gra_pdf_convert_timestamp(buf, szbuf, &lct);
	}
/* --- Windows --- */
#else
/* +++ POSIX +++ */
	(void)time(&timer);
#if	DK4_HAVE_LOCALTIME_R
	if (NULL != localtime_r(&timer, &lct)) {
		back = 1;
		dk4gra_pdf_convert_timestamp(buf, szbuf, &lct);
	}
#else
	dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
/* --- POSIX --- */
#endif

	finished:
	return back;
}




/* vim: set ai sw=4 ts=4 : */