-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use time.Time not string for LastModified
Signed-off-by: Anders F Björklund <[email protected]>
- Loading branch information
1 parent
a5a885f
commit ceb657a
Showing
3 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package timeutil | ||
|
||
import ( | ||
"os" | ||
"time" | ||
) | ||
|
||
// same format as HTTP headers | ||
const timeFormat = time.RFC1123 | ||
|
||
func ReadTime(name string) (time.Time, error) { | ||
data, err := os.ReadFile(name) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
return time.Parse(timeFormat, string(data)) | ||
} | ||
|
||
func WriteTime(name string, tm time.Time, perm os.FileMode) error { | ||
data := tm.Format(timeFormat) | ||
return os.WriteFile(name, []byte(data), perm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package timeutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
var ( | ||
testTime time.Time | ||
testData = testTime.Format(timeFormat) | ||
) | ||
|
||
func TestReadTime(t *testing.T) { | ||
tmpFile := filepath.Join(t.TempDir(), "time") | ||
err := os.WriteFile(tmpFile, []byte(testData), 0o644) | ||
assert.NilError(t, err) | ||
|
||
tm, err := ReadTime(tmpFile) | ||
assert.NilError(t, err) | ||
assert.Equal(t, tm, testTime) | ||
} | ||
|
||
func TestWriteTime(t *testing.T) { | ||
tmpFile := filepath.Join(t.TempDir(), "time") | ||
err := WriteTime(tmpFile, testTime, 0o644) | ||
assert.NilError(t, err) | ||
|
||
b, err := os.ReadFile(tmpFile) | ||
assert.NilError(t, err) | ||
assert.Equal(t, string(b), testData) | ||
} |