Skip to content

Commit 059d384

Browse files
committed
Issue #31: Times in backup.control is in UTC by default
1 parent 188bb11 commit 059d384

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

src/backup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ pg_stop_backup(pgBackup *backup)
18361836
elog(ERROR,
18371837
"result of txid_snapshot_xmax() is invalid: %s",
18381838
PQgetvalue(res, 0, 0));
1839-
if (!parse_time(PQgetvalue(res, 0, 1), &recovery_time))
1839+
if (!parse_time(PQgetvalue(res, 0, 1), &recovery_time, true))
18401840
elog(ERROR,
18411841
"result of current_timestamp is invalid: %s",
18421842
PQgetvalue(res, 0, 1));

src/restore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ parseRecoveryTargetOptions(const char *target_time,
10361036
rt->time_specified = true;
10371037
rt->target_time_string = target_time;
10381038

1039-
if (parse_time(target_time, &dummy_time))
1039+
if (parse_time(target_time, &dummy_time, false))
10401040
rt->recovery_target_time = dummy_time;
10411041
else
10421042
elog(ERROR, "Invalid value of --time option %s", target_time);

src/util.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,33 +191,31 @@ get_data_checksum_version(bool safe)
191191

192192

193193
/*
194-
* Convert time_t value to ISO-8601 format string
194+
* Convert time_t value to ISO-8601 format string. Always set timezone offset.
195195
*/
196196
void
197197
time2iso(char *buf, size_t len, time_t time)
198198
{
199199
struct tm *ptm = gmtime(&time);
200200
time_t gmt = mktime(ptm);
201201
time_t offset;
202+
char *ptr = buf;
202203

203204
ptm = localtime(&time);
204205
offset = time - gmt + (ptm->tm_isdst ? 3600 : 0);
205206

206-
strftime(buf, len, "%Y-%m-%d %H:%M:%S", ptm);
207+
strftime(ptr, len, "%Y-%m-%d %H:%M:%S", ptm);
207208

208-
if (offset != 0)
209-
{
210-
buf += strlen(buf);
211-
sprintf(buf, "%c%02d",
212-
(offset >= 0) ? '+' : '-',
213-
abs((int) offset) / SECS_PER_HOUR);
209+
ptr += strlen(ptr);
210+
snprintf(ptr, len - (ptr - buf), "%c%02d",
211+
(offset >= 0) ? '+' : '-',
212+
abs((int) offset) / SECS_PER_HOUR);
214213

215-
if (abs((int) offset) % SECS_PER_HOUR != 0)
216-
{
217-
buf += strlen(buf);
218-
sprintf(buf, ":%02d",
219-
abs((int) offset % SECS_PER_HOUR) / SECS_PER_MINUTE);
220-
}
214+
if (abs((int) offset) % SECS_PER_HOUR != 0)
215+
{
216+
ptr += strlen(ptr);
217+
snprintf(ptr, len - (ptr - buf), ":%02d",
218+
abs((int) offset % SECS_PER_HOUR) / SECS_PER_MINUTE);
221219
}
222220
}
223221

src/utils/pgut.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ assign_option(pgut_option *opt, const char *optarg, pgut_optsrc src)
256256
message = "a valid string. But provided: ";
257257
break;
258258
case 't':
259-
if (parse_time(optarg, opt->var))
259+
if (parse_time(optarg, opt->var,
260+
opt->source == SOURCE_FILE))
260261
return;
261262
message = "a time";
262263
break;
@@ -746,9 +747,12 @@ parse_uint64(const char *value, uint64 *result, int flags)
746747

747748
/*
748749
* Convert ISO-8601 format string to time_t value.
750+
*
751+
* If utc_default is true, then if timezone offset isn't specified tz will be
752+
* +00:00.
749753
*/
750754
bool
751-
parse_time(const char *value, time_t *result)
755+
parse_time(const char *value, time_t *result, bool utc_default)
752756
{
753757
size_t len;
754758
int fields_num,
@@ -870,7 +874,7 @@ parse_time(const char *value, time_t *result)
870874
*result = mktime(&tm);
871875

872876
/* adjust time zone */
873-
if (tz_set)
877+
if (tz_set || utc_default)
874878
{
875879
time_t ltime = time(NULL);
876880
struct tm *ptm = gmtime(&ltime);

src/utils/pgut.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ extern bool parse_int32(const char *value, int32 *result, int flags);
204204
extern bool parse_uint32(const char *value, uint32 *result, int flags);
205205
extern bool parse_int64(const char *value, int64 *result, int flags);
206206
extern bool parse_uint64(const char *value, uint64 *result, int flags);
207-
extern bool parse_time(const char *value, time_t *result);
207+
extern bool parse_time(const char *value, time_t *result, bool utc_default);
208208
extern bool parse_int(const char *value, int *result, int flags,
209209
const char **hintmsg);
210210

0 commit comments

Comments
 (0)