Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Calculate t using time zone offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed May 25, 2018
1 parent 0f8d75a commit 7539c08
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rotatelogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ func New(p string, options ...Option) (*RotateLogs, error) {

func (rl *RotateLogs) genFilename() string {
now := rl.clock.Now()
diff := time.Duration(now.UnixNano()) % rl.rotationTime
t := now.Add(time.Duration(-1 * diff))
_, offset := now.Zone()
base := now.Truncate(rl.rotationTime).Add(-1 * time.Duration(offset) * time.Second)
t := now.Add(base.Sub(now))
return rl.pattern.FormatString(t)
}

Expand Down
44 changes: 44 additions & 0 deletions rotatelogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/jonboulle/clockwork"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/lestrrat-go/strftime"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -358,3 +359,46 @@ func TestRotationGenerationalNames(t *testing.T) {
defer rl.Close()
})
}

func TestGHIssue23(t *testing.T) {
dir, err := ioutil.TempDir("", "file-rotatelogs-generational")
if !assert.NoError(t, err, `creating temporary directory should succeed`) {
return
}
defer os.RemoveAll(dir)

t.Run("Set location to Asia/Tokyo", func(t *testing.T) {
loc, _ := time.LoadLocation("Asia/Tokyo")
rl, err := rotatelogs.New(
filepath.Join(dir, "asia_tokyo.%Y%m%d%H%M.log"),
rotatelogs.WithLocation(loc),
)
if !assert.NoError(t, err, "rotatelogs.New should succeed") {
return
}

// Timing sensitive...

var now time.Time
for {
now = time.Now().In(loc)
if now.Hour() == 23 && now.Minute() >= 59 {
t.Logf("This test is sensitive to date changes. don't run this test after 23:59 Asia/Tokyo time")
time.Sleep(time.Minute)
continue
}
break
}
dt := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
expected, err := strftime.Format("asia_tokyo.%Y%m%d%H%M.log",dt)
if !assert.NoError(t, err, "strftime.Format should succeed") {
return
}
expected = filepath.Join(dir, expected)

rl.Rotate()
if !assert.Equal(t, expected, rl.CurrentFileName(), "file names should match") {
return
}
})
}

0 comments on commit 7539c08

Please sign in to comment.