Skip to content

Commit 4877f6b

Browse files
committed
Issue #31: Use UTC time to make backup ID
1 parent f0864a2 commit 4877f6b

File tree

8 files changed

+34
-26
lines changed

8 files changed

+34
-26
lines changed

src/backup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ do_backup_instance(void)
553553
pg_ptrack_clear();
554554

555555
/* notify start of backup to PostgreSQL server */
556-
time2iso(label, lengthof(label), current.start_time);
556+
time2iso(label, lengthof(label), current.start_time, true);
557557
strncat(label, " with pg_probackup", lengthof(label) -
558558
strlen(" with pg_probackup"));
559559
pg_start_backup(label, smooth_checkpoint, &current);

src/catalog.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,17 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
409409
(uint32) (backup->stop_lsn >> 32),
410410
(uint32) backup->stop_lsn);
411411

412-
time2iso(timestamp, lengthof(timestamp), backup->start_time);
412+
time2iso(timestamp, lengthof(timestamp), backup->start_time, false);
413413
fprintf(out, "start-time = '%s'\n", timestamp);
414414
if (backup->end_time > 0)
415415
{
416-
time2iso(timestamp, lengthof(timestamp), backup->end_time);
416+
time2iso(timestamp, lengthof(timestamp), backup->end_time, false);
417417
fprintf(out, "end-time = '%s'\n", timestamp);
418418
}
419419
fprintf(out, "recovery-xid = " XID_FMT "\n", backup->recovery_xid);
420420
if (backup->recovery_time > 0)
421421
{
422-
time2iso(timestamp, lengthof(timestamp), backup->recovery_time);
422+
time2iso(timestamp, lengthof(timestamp), backup->recovery_time, false);
423423
fprintf(out, "recovery-time = '%s'\n", timestamp);
424424
}
425425

src/delete.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pgBackupDeleteFiles(pgBackup *backup)
257257
if (backup->status == BACKUP_STATUS_DELETED)
258258
return 0;
259259

260-
time2iso(timestamp, lengthof(timestamp), backup->recovery_time);
260+
time2iso(timestamp, lengthof(timestamp), backup->recovery_time, true);
261261

262262
elog(INFO, "delete: %s %s", base36enc(backup->start_time), timestamp);
263263

src/parsexlog.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ validate_wal(pgBackup *backup,
380380
xlogfpath[0] = '\0';
381381

382382
/* We can restore at least up to the backup end */
383-
time2iso(last_timestamp, lengthof(last_timestamp), backup->recovery_time);
383+
time2iso(last_timestamp, lengthof(last_timestamp), backup->recovery_time,
384+
true);
384385
last_xid = backup->recovery_xid;
385386

386387
if ((TransactionIdIsValid(target_xid) && target_xid == last_xid)
@@ -430,7 +431,7 @@ validate_wal(pgBackup *backup,
430431

431432
if (last_time > 0)
432433
time2iso(last_timestamp, lengthof(last_timestamp),
433-
timestamptz_to_time_t(last_time));
434+
timestamptz_to_time_t(last_time), true);
434435

435436
/* There are all needed WAL records */
436437
if (all_wal)
@@ -459,8 +460,8 @@ validate_wal(pgBackup *backup,
459460
last_timestamp, last_xid);
460461

461462
if (target_time > 0)
462-
time2iso(target_timestamp, lengthof(target_timestamp),
463-
target_time);
463+
time2iso(target_timestamp, lengthof(target_timestamp), target_time,
464+
true);
464465
if (TransactionIdIsValid(target_xid) && target_time != 0)
465466
elog(ERROR, "not enough WAL records to time %s and xid " XID_FMT,
466467
target_timestamp, target_xid);

src/pg_probackup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ extern bool wal_contains_lsn(const char *archivedir, XLogRecPtr target_lsn,
521521
/* in util.c */
522522
extern TimeLineID get_current_timeline(bool safe);
523523
extern void sanityChecks(void);
524-
extern void time2iso(char *buf, size_t len, time_t time);
524+
extern void time2iso(char *buf, size_t len, time_t time, bool to_local);
525525
extern const char *status2str(BackupStatus status);
526526
extern void remove_trailing_space(char *buf, int comment_mark);
527527
extern void remove_not_digit(char *buf, size_t len, const char *str);

src/restore.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ do_restore_or_validate(time_t target_backup_id, pgRecoveryTarget *rt,
122122
current_backup = (pgBackup *) parray_get(backups, i);
123123

124124
/* Skip all backups which started after target backup */
125-
if (target_backup_id && current_backup->start_time > target_backup_id)
125+
if (target_backup_id != INVALID_BACKUP_ID &&
126+
current_backup->start_time > target_backup_id)
126127
continue;
127128

128129
/*
@@ -381,7 +382,7 @@ restore_backup(pgBackup *backup)
381382
"XLOG_BLCKSZ(%d) is not compatible(%d expected)",
382383
backup->wal_block_size, XLOG_BLCKSZ);
383384

384-
time2iso(timestamp, lengthof(timestamp), backup->start_time);
385+
time2iso(timestamp, lengthof(timestamp), backup->start_time, true);
385386
elog(LOG, "restoring database from backup %s", timestamp);
386387

387388
/*

src/show.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ show_instance_plain(parray *backup_list, bool show_name)
324324
char data_bytes_str[10] = "----";
325325

326326
if (backup->recovery_time != (time_t) 0)
327-
time2iso(timestamp, lengthof(timestamp), backup->recovery_time);
327+
time2iso(timestamp, lengthof(timestamp), backup->recovery_time,
328+
true);
328329
if (backup->end_time != (time_t) 0)
329330
snprintf(duration, lengthof(duration), "%.*lfs", 0,
330331
difftime(backup->end_time, backup->start_time));
@@ -452,12 +453,12 @@ show_instance_json(parray *backup_list)
452453
(uint32) (backup->stop_lsn >> 32), (uint32) backup->stop_lsn);
453454
json_add_value(buf, "stop-lsn", lsn, json_level, true);
454455

455-
time2iso(timestamp, lengthof(timestamp), backup->start_time);
456+
time2iso(timestamp, lengthof(timestamp), backup->start_time, true);
456457
json_add_value(buf, "start-time", timestamp, json_level, true);
457458

458459
if (backup->end_time)
459460
{
460-
time2iso(timestamp, lengthof(timestamp), backup->end_time);
461+
time2iso(timestamp, lengthof(timestamp), backup->end_time, true);
461462
json_add_value(buf, "end-time", timestamp, json_level, true);
462463
}
463464

@@ -466,7 +467,8 @@ show_instance_json(parray *backup_list)
466467

467468
if (backup->recovery_time > 0)
468469
{
469-
time2iso(timestamp, lengthof(timestamp), backup->recovery_time);
470+
time2iso(timestamp, lengthof(timestamp), backup->recovery_time,
471+
true);
470472
json_add_value(buf, "recovery-time", timestamp, json_level, true);
471473
}
472474

src/util.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,31 +194,35 @@ get_data_checksum_version(bool safe)
194194
* Convert time_t value to ISO-8601 format string
195195
*/
196196
void
197-
time2iso(char *buf, size_t len, time_t time)
197+
time2iso(char *buf, size_t len, time_t time, bool to_local)
198198
{
199199
struct tm *ptm = gmtime(&time);
200200
time_t gmt = mktime(ptm);
201201
time_t offset;
202202

203-
ptm = localtime(&time);
204-
offset = time - gmt + (ptm->tm_isdst ? 3600 : 0);
203+
if (to_local)
204+
{
205+
char *ptr = buf;
205206

206-
strftime(buf, len, "%Y-%m-%d %H:%M:%S", ptm);
207+
ptm = localtime(&time);
208+
offset = time - gmt + (ptm->tm_isdst ? 3600 : 0);
207209

208-
if (offset != 0)
209-
{
210-
buf += strlen(buf);
211-
sprintf(buf, "%c%02d",
210+
strftime(ptr, len, "%Y-%m-%d %H:%M:%S", ptm);
211+
212+
ptr += strlen(ptr);
213+
snprintf(ptr, len - (ptr - buf), "%c%02d",
212214
(offset >= 0) ? '+' : '-',
213215
abs((int) offset) / SECS_PER_HOUR);
214216

215217
if (abs((int) offset) % SECS_PER_HOUR != 0)
216218
{
217-
buf += strlen(buf);
218-
sprintf(buf, ":%02d",
219+
ptr += strlen(ptr);
220+
snprintf(ptr, len - (ptr - buf), ":%02d",
219221
abs((int) offset % SECS_PER_HOUR) / SECS_PER_MINUTE);
220222
}
221223
}
224+
else
225+
strftime(buf, len, "%Y-%m-%d %H:%M:%S+00", ptm);
222226
}
223227

224228
/* copied from timestamp.c */

0 commit comments

Comments
 (0)