Skip to content

Commit 134c7de

Browse files
litaocdlmnencia
andauthored
fix: use RFC3339 format to parse ISO times (#55)
Fix for cloudnative-pg/cloudnative-pg#6345 Signed-off-by: Tao Li <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]>
1 parent adf4e51 commit 134c7de

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

pkg/catalog/catalog.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ func NewBackupFromBarmanCloudBackupShow(rawJSON string) (*BarmanBackup, error) {
319319
// barmanTimeLayout is the format that is being used to parse
320320
// the backupInfo from barman-cloud-backup-list
321321
const (
322-
barmanTimeLayout = "Mon Jan 2 15:04:05 2006"
323-
barmanTimeLayoutISO = "2006-01-02 15:04:05Z07:00"
322+
barmanTimeLayout = "Mon Jan 2 15:04:05 2006"
324323
)
325324

326325
func (b *BarmanBackup) deserializeBackupTimeStrings() error {
@@ -340,14 +339,14 @@ func (b *BarmanBackup) deserializeBackupTimeStrings() error {
340339

341340
func tryParseISOOrCtimeTime(isoValue, ctimeOrISOValue string) (time.Time, error) {
342341
if isoValue != "" {
343-
return time.Parse(barmanTimeLayoutISO, isoValue)
342+
return time.Parse(time.RFC3339, isoValue)
344343
}
345344

346345
if ctimeOrISOValue != "" {
347346
// Barman 3.12.0 incorrectly puts an ISO-formatted time in the ctime-formatted field.
348347
// So in case of parsing failure we try again parsing it as an ISO time,
349348
// discarding an eventual failure
350-
return parseTimeWithFallbackLayout(ctimeOrISOValue, barmanTimeLayout, barmanTimeLayoutISO)
349+
return parseTimeWithFallbackLayout(ctimeOrISOValue, barmanTimeLayout, time.RFC3339)
351350
}
352351

353352
return time.Time{}, nil

pkg/catalog/catalog_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ var _ = Describe("barman-cloud-backup-list parsing", func() {
180180
})
181181
})
182182

183-
var _ = Describe("barman-cloud-backup-list parsing with ISO times", func() {
183+
var _ = Describe("barman-cloud-backup-list parsing with RFC3339 times", func() {
184184
const barmanCloudListOutput = `{
185185
"backups_list": [
186186
{
187187
"backup_label": "'START WAL LOCATION:[...]",
188188
"begin_offset": 40,
189189
"begin_time": "Tue Oct 20 11:52:31 2020",
190-
"begin_time_iso": "2020-10-20 11:52:31+02:00",
190+
"begin_time_iso": "2020-10-20T11:52:31+02:00",
191191
"begin_wal": "000000010000000000000006",
192192
"begin_xlog": "0/6000028",
193193
"config_file": "/var/lib/postgresql/data/pgdata/postgresql.conf",
@@ -210,7 +210,7 @@ var _ = Describe("barman-cloud-backup-list parsing with ISO times", func() {
210210
"deduplicated_size": null,
211211
"end_offset": 312,
212212
"end_time": "Tue Oct 20 11:52:34 2020",
213-
"end_time_iso": "2020-10-20 11:52:34+02:00",
213+
"end_time_iso": "2020-10-20T11:52:34+02:00",
214214
"end_wal": "000000010000000000000006",
215215
"end_xlog": "0/6000138",
216216
"error": null,
@@ -245,8 +245,8 @@ var _ = Describe("barman-cloud-backup-list parsing with ISO times", func() {
245245
Expect(result.List[0].SystemID).To(Equal("6885668674852188181"))
246246
Expect(result.List[0].BeginTimeString).To(Equal("Tue Oct 20 11:52:31 2020"))
247247
Expect(result.List[0].EndTimeString).To(Equal("Tue Oct 20 11:52:34 2020"))
248-
Expect(result.List[0].BeginTimeISOString).To(Equal("2020-10-20 11:52:31+02:00"))
249-
Expect(result.List[0].EndTimeISOString).To(Equal("2020-10-20 11:52:34+02:00"))
248+
Expect(result.List[0].BeginTimeISOString).To(Equal("2020-10-20T11:52:31+02:00"))
249+
Expect(result.List[0].EndTimeISOString).To(Equal("2020-10-20T11:52:34+02:00"))
250250
Expect(result.List[0].BeginTime).To(BeTemporally("==", time.Date(
251251
2020, 10, 20,
252252
11, 52, 31,
@@ -268,13 +268,13 @@ var _ = Describe("barman-cloud-backup-list parsing with ISO times", func() {
268268
})
269269
})
270270

271-
var _ = Describe("barman-cloud-backup-list parsing with ISO times in the wrong place", func() {
271+
var _ = Describe("barman-cloud-backup-list parsing with RFC3339 times in the wrong place", func() {
272272
const barmanCloudListOutput = `{
273273
"backups_list": [
274274
{
275275
"backup_label": "'START WAL LOCATION:[...]",
276276
"begin_offset": 40,
277-
"begin_time": "2020-10-20 11:52:31+02:00",
277+
"begin_time": "2020-10-20T11:52:31+02:00",
278278
"begin_wal": "000000010000000000000006",
279279
"begin_xlog": "0/6000028",
280280
"config_file": "/var/lib/postgresql/data/pgdata/postgresql.conf",
@@ -296,7 +296,7 @@ var _ = Describe("barman-cloud-backup-list parsing with ISO times in the wrong p
296296
},
297297
"deduplicated_size": null,
298298
"end_offset": 312,
299-
"end_time": "2020-10-20 11:52:34+02:00",
299+
"end_time": "2020-10-20T11:52:34+02:00",
300300
"end_wal": "000000010000000000000006",
301301
"end_xlog": "0/6000138",
302302
"error": null,
@@ -329,8 +329,8 @@ var _ = Describe("barman-cloud-backup-list parsing with ISO times in the wrong p
329329
Expect(result.List).To(HaveLen(2))
330330
Expect(result.List[0].ID).To(Equal("20201020T115231"))
331331
Expect(result.List[0].SystemID).To(Equal("6885668674852188181"))
332-
Expect(result.List[0].BeginTimeString).To(Equal("2020-10-20 11:52:31+02:00"))
333-
Expect(result.List[0].EndTimeString).To(Equal("2020-10-20 11:52:34+02:00"))
332+
Expect(result.List[0].BeginTimeString).To(Equal("2020-10-20T11:52:31+02:00"))
333+
Expect(result.List[0].EndTimeString).To(Equal("2020-10-20T11:52:34+02:00"))
334334
Expect(result.List[0].BeginTime).To(BeTemporally("==", time.Date(
335335
2020, 10, 20,
336336
11, 52, 31,
@@ -442,13 +442,13 @@ var _ = Describe("barman-cloud-backup-show parsing", func() {
442442
})
443443
})
444444

445-
var _ = Describe("barman-cloud-backup-show parsing with ISO times", func() {
445+
var _ = Describe("barman-cloud-backup-show parsing with RFC3339 times", func() {
446446
const barmanCloudShowOutput = `{
447447
"cloud":{
448448
"backup_label": null,
449449
"begin_offset": 40,
450450
"begin_time": "Tue Jan 19 03:14:08 2038",
451-
"begin_time_iso": "2038-01-19 03:14:08+05:30",
451+
"begin_time_iso": "2038-01-19T03:14:08+05:30",
452452
"begin_wal": "000000010000000000000002",
453453
"begin_xlog": "0/2000028",
454454
"compression": null,
@@ -457,7 +457,7 @@ var _ = Describe("barman-cloud-backup-show parsing with ISO times", func() {
457457
"deduplicated_size": null,
458458
"end_offset": 184,
459459
"end_time": "Tue Jan 19 04:14:08 2038",
460-
"end_time_iso": "2038-01-19 04:14:08+05:30",
460+
"end_time_iso": "2038-01-19T04:14:08+05:30",
461461
"end_wal": "000000010000000000000004",
462462
"end_xlog": "0/20000B8",
463463
"error": null,
@@ -519,8 +519,8 @@ var _ = Describe("barman-cloud-backup-show parsing with ISO times", func() {
519519
Expect(result.SystemID).To(Equal("6885668674852188181"))
520520
Expect(result.BeginTimeString).To(Equal("Tue Jan 19 03:14:08 2038"))
521521
Expect(result.EndTimeString).To(Equal("Tue Jan 19 04:14:08 2038"))
522-
Expect(result.BeginTimeISOString).To(Equal("2038-01-19 03:14:08+05:30"))
523-
Expect(result.EndTimeISOString).To(Equal("2038-01-19 04:14:08+05:30"))
522+
Expect(result.BeginTimeISOString).To(Equal("2038-01-19T03:14:08+05:30"))
523+
Expect(result.EndTimeISOString).To(Equal("2038-01-19T04:14:08+05:30"))
524524
Expect(result.BeginTime).To(BeTemporally("==", time.Date(
525525
2038, 1, 19,
526526
3, 14, 8,
@@ -536,20 +536,20 @@ var _ = Describe("barman-cloud-backup-show parsing with ISO times", func() {
536536
})
537537
})
538538

539-
var _ = Describe("barman-cloud-backup-show parsing with ISO times in the wrong place", func() {
539+
var _ = Describe("barman-cloud-backup-show parsing with RFC3339 times in the wrong place", func() {
540540
const barmanCloudShowOutput = `{
541541
"cloud":{
542542
"backup_label": null,
543543
"begin_offset": 40,
544-
"begin_time": "2038-01-19 03:14:08+05:30",
544+
"begin_time": "2038-01-19T03:14:08+05:30",
545545
"begin_wal": "000000010000000000000002",
546546
"begin_xlog": "0/2000028",
547547
"compression": null,
548548
"config_file": "/pgdata/location/postgresql.conf",
549549
"copy_stats": null,
550550
"deduplicated_size": null,
551551
"end_offset": 184,
552-
"end_time": "2038-01-19 04:14:08+05:30",
552+
"end_time": "2038-01-19T04:14:08+05:30",
553553
"end_wal": "000000010000000000000004",
554554
"end_xlog": "0/20000B8",
555555
"error": null,
@@ -609,8 +609,8 @@ var _ = Describe("barman-cloud-backup-show parsing with ISO times in the wrong p
609609
Expect(result).ToNot(BeNil())
610610
Expect(result.ID).To(Equal("20201020T115231"))
611611
Expect(result.SystemID).To(Equal("6885668674852188181"))
612-
Expect(result.BeginTimeString).To(Equal("2038-01-19 03:14:08+05:30"))
613-
Expect(result.EndTimeString).To(Equal("2038-01-19 04:14:08+05:30"))
612+
Expect(result.BeginTimeString).To(Equal("2038-01-19T03:14:08+05:30"))
613+
Expect(result.EndTimeString).To(Equal("2038-01-19T04:14:08+05:30"))
614614
Expect(result.BeginTime).To(BeTemporally("==", time.Date(
615615
2038, 1, 19,
616616
3, 14, 8,

0 commit comments

Comments
 (0)