Skip to content

Commit 34e6486

Browse files
committed
Refactor t_final parsing into shared function
Extract duplicate code from hgridshift/vgridshift into pj_parse_t_final()
1 parent 40690e7 commit 34e6486

File tree

4 files changed

+39
-54
lines changed

4 files changed

+39
-54
lines changed

src/param.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,39 @@ PROJVALUE pj_param(PJ_CONTEXT *ctx, paralist *pl, const char *opt) {
215215
}
216216
return value;
217217
}
218+
219+
/* Parse +t_final parameter with support for "now" keyword */
220+
double pj_parse_t_final(PJ *P) {
221+
if (!pj_param(P->ctx, P->params, "tt_final").i) {
222+
return 0.0;
223+
}
224+
225+
double t_final = pj_param(P->ctx, P->params, "dt_final").f;
226+
if (t_final != 0.0) {
227+
return t_final;
228+
}
229+
230+
/* Check if "now" was specified instead of a numeric value */
231+
if (strcmp("now", pj_param(P->ctx, P->params, "st_final").s) != 0) {
232+
return 0.0;
233+
}
234+
235+
/* Calculate t_final from current time */
236+
time_t now;
237+
struct tm date;
238+
time(&now);
239+
#ifdef _WIN32
240+
localtime_s(&date, &now);
241+
#else
242+
localtime_r(&now, &date);
243+
#endif
244+
245+
const int days_in_year = ((date.tm_year + 1900) % 4 == 0 &&
246+
((date.tm_year + 1900) % 100 != 0 ||
247+
(date.tm_year + 1900) % 400 == 0))
248+
? 366
249+
: 365;
250+
251+
return 1900.0 + date.tm_year +
252+
date.tm_yday / static_cast<double>(days_in_year);
253+
}

src/proj_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ PROJVALUE PROJ_DLL pj_param(PJ_CONTEXT *ctx, paralist *, const char *);
911911
paralist PROJ_DLL *pj_param_exists(paralist *list, const char *parameter);
912912
paralist PROJ_DLL *pj_mkparam(const char *);
913913
paralist *pj_mkparam_ws(const char *str, const char **next_str);
914+
double pj_parse_t_final(PJ *P);
914915

915916
int PROJ_DLL pj_ell_set(PJ_CONTEXT *ctx, paralist *, double *, double *);
916917
int pj_datum_set(PJ_CONTEXT *, paralist *, PJ *);

src/transformations/hgridshift.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <mutex>
55
#include <stddef.h>
66
#include <string.h>
7-
#include <time.h>
87

98
#include "grids.hpp"
109
#include "proj_internal.h"
@@ -162,32 +161,7 @@ PJ *PJ_TRANSFORMATION(hgridshift, 0) {
162161
return pj_hgridshift_destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG);
163162
}
164163

165-
/* TODO: Refactor into shared function that can be used */
166-
/* by both vgridshift and hgridshift */
167-
if (pj_param(P->ctx, P->params, "tt_final").i) {
168-
Q->t_final = pj_param(P->ctx, P->params, "dt_final").f;
169-
if (Q->t_final == 0) {
170-
/* a number wasn't passed to +t_final, let's see if it was "now" */
171-
/* and set the time accordingly. */
172-
if (!strcmp("now", pj_param(P->ctx, P->params, "st_final").s)) {
173-
time_t now;
174-
struct tm date;
175-
time(&now);
176-
#ifdef _WIN32
177-
localtime_s(&date, &now);
178-
#else
179-
localtime_r(&now, &date);
180-
#endif
181-
const int days_in_year = ((date.tm_year + 1900) % 4 == 0 &&
182-
((date.tm_year + 1900) % 100 != 0 ||
183-
(date.tm_year + 1900) % 400 == 0))
184-
? 366
185-
: 365;
186-
Q->t_final = 1900.0 + date.tm_year +
187-
date.tm_yday / static_cast<double>(days_in_year);
188-
}
189-
}
190-
}
164+
Q->t_final = pj_parse_t_final(P);
191165

192166
if (pj_param(P->ctx, P->params, "tt_epoch").i)
193167
Q->t_epoch = pj_param(P->ctx, P->params, "dt_epoch").f;

src/transformations/vgridshift.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <mutex>
55
#include <stddef.h>
66
#include <string.h>
7-
#include <time.h>
87

98
#include "grids.hpp"
109
#include "proj_internal.h"
@@ -182,32 +181,7 @@ PJ *PJ_TRANSFORMATION(vgridshift, 0) {
182181
return pj_vgridshift_destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG);
183182
}
184183

185-
/* TODO: Refactor into shared function that can be used */
186-
/* by both vgridshift and hgridshift */
187-
if (pj_param(P->ctx, P->params, "tt_final").i) {
188-
Q->t_final = pj_param(P->ctx, P->params, "dt_final").f;
189-
if (Q->t_final == 0) {
190-
/* a number wasn't passed to +t_final, let's see if it was "now" */
191-
/* and set the time accordingly. */
192-
if (!strcmp("now", pj_param(P->ctx, P->params, "st_final").s)) {
193-
time_t now;
194-
struct tm date;
195-
time(&now);
196-
#ifdef _WIN32
197-
localtime_s(&date, &now);
198-
#else
199-
localtime_r(&now, &date);
200-
#endif
201-
const int days_in_year = ((date.tm_year + 1900) % 4 == 0 &&
202-
((date.tm_year + 1900) % 100 != 0 ||
203-
(date.tm_year + 1900) % 400 == 0))
204-
? 366
205-
: 365;
206-
Q->t_final = 1900.0 + date.tm_year +
207-
date.tm_yday / static_cast<double>(days_in_year);
208-
}
209-
}
210-
}
184+
Q->t_final = pj_parse_t_final(P);
211185

212186
if (pj_param(P->ctx, P->params, "tt_epoch").i)
213187
Q->t_epoch = pj_param(P->ctx, P->params, "dt_epoch").f;

0 commit comments

Comments
 (0)