diff options
| author | Sandro Jäckel <sandro.jaeckel@gmail.com> | 2026-11-10 18:03:37 +0100 |
|---|---|---|
| committer | Sandro Jäckel <sandro.jaeckel@gmail.com> | 2026-11-10 18:03:51 +0100 |
| commit | 0738cfbbdbbf898d9e864970924e59d0a2af45e3 (patch) | |
| tree | faf2d31bf4438fcf25c9f50f5b87b0d14e65902a | |
| parent | 537dbec6f63eff137a582dc4e8e7a06d20083237 (diff) | |
| download | go-schwift-0738cfbbdbbf898d9e864970924e59d0a2af45e3.tar.gz | |
Fix that Ceph Swift API emulation contains timezone
| -rw-r--r-- | container_iterator.go | 2 | ||||
| -rw-r--r-- | iterator.go | 10 | ||||
| -rw-r--r-- | iterator_test.go | 28 | ||||
| -rw-r--r-- | object_iterator.go | 2 |
4 files changed, 40 insertions, 2 deletions
diff --git a/container_iterator.go b/container_iterator.go index 64e02ef..0475fbf 100644 --- a/container_iterator.go +++ b/container_iterator.go @@ -109,7 +109,7 @@ func (i *ContainerIterator) NextPageDetailed(ctx context.Context, limit int) ([] result[idx].Container = i.Account.Container(data.Name) result[idx].BytesUsed = data.BytesUsed result[idx].ObjectCount = data.ObjectCount - result[idx].LastModified, err = time.Parse(time.RFC3339Nano, data.LastModifiedStr+"Z") + result[idx].LastModified, err = parseSwiftLastModified(data.LastModifiedStr) if err != nil { // this error is sufficiently obscure that we don't need to expose a type for it return nil, fmt.Errorf("bad field containers[%d].last_modified: %s", idx, err.Error()) diff --git a/iterator.go b/iterator.go index c566f59..9938404 100644 --- a/iterator.go +++ b/iterator.go @@ -9,8 +9,18 @@ import ( "net/http" "strconv" "strings" + "time" ) +func parseSwiftLastModified(value string) (time.Time, error) { + // Swift listings traditionally omit the timezone, which is implicitly UTC. + // Some Swift-compatible APIs include a timezone, so try the value as-is first. + if parsed, err := time.Parse(time.RFC3339Nano, value); err == nil { + return parsed, nil + } + return time.Parse(time.RFC3339Nano, value+"Z") +} + // iteratorInterface allows iteratorBase to access public attributes of // ContainerIterator/ObjectIterator. type iteratorInterface interface { diff --git a/iterator_test.go b/iterator_test.go new file mode 100644 index 0000000..6ea8599 --- /dev/null +++ b/iterator_test.go @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> +// SPDX-License-Identifier: Apache-2.0 + +package schwift + +import ( + "testing" + "time" +) + +func TestParseSwiftLastModified(t *testing.T) { + want := time.Date(2026, time.November, 10, 15, 37, 37, 707_000_000, time.UTC) + for _, input := range []string{ + "2026-11-10T15:37:37.707", // Swift's timezone-less format + "2026-11-10T15:37:37.707Z", // UTC timezone + "2026-11-10T16:37:37.707+01:00", // Explicit offset + } { + t.Run(input, func(t *testing.T) { + got, err := parseSwiftLastModified(input) + if err != nil { + t.Fatalf("parseSwiftLastModified(%q) returned an error: %v", input, err) + } + if !got.Equal(want) { + t.Errorf("parseSwiftLastModified(%q) = %s, want %s", input, got, want) + } + }) + } +} diff --git a/object_iterator.go b/object_iterator.go index ae4c896..5e5570b 100644 --- a/object_iterator.go +++ b/object_iterator.go @@ -138,7 +138,7 @@ func (i *ObjectIterator) NextPageDetailed(ctx context.Context, limit int) ([]Obj result[idx].ContentType = data.ContentType result[idx].Etag = data.Etag result[idx].SizeBytes = data.SizeBytes - result[idx].LastModified, err = time.Parse(time.RFC3339Nano, data.LastModifiedStr+"Z") + result[idx].LastModified, err = parseSwiftLastModified(data.LastModifiedStr) if err != nil { // this error is sufficiently obscure that we don't need to expose a type for it return nil, fmt.Errorf("bad field objects[%d].last_modified: %s", idx, err.Error()) |
