8000 Fix Daylight Savings Behavior by subtlepseudonym · Pull Request #189 · aptible/supercronic · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix Daylight Savings Behavior #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cronexpr/cronexpr_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func (expr *Expression) nextHour(t time.Time) time.Time {
return expr.nextDayOfMonth(t)
}

name, offset := t.Zone()
zone := time.FixedZone(name, offset)
return time.Date(
t.Year(),
t.Month(),
Expand All @@ -137,7 +139,8 @@ func (expr *Expression) nextHour(t time.Time) time.Time {
expr.minuteList[0],
expr.secondList[0],
0,
t.Location())
zone,
).In(t.Location())
}

/******************************************************************************/
Expand Down
132 changes: 106 additions & 26 deletions cronexpr/cronexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (

/******************************************************************************/

var (
PST8PDT, _ = time.LoadLocation("PST8PDT")
Anchorage, _ = time.LoadLocation("America/Anchorage")
Adelaide, _ = time.LoadLocation("Australia/Adelaide")
)

type crontimes struct {
from string
next string
Expand All @@ -30,6 +36,7 @@ type crontest struct {
expr string
layout string
times []crontimes
location *time.Location
}

var crontests = []crontest{
Expand All @@ -46,6 +53,7 @@ var crontests = []crontest{
{"2016-02-28 23:59:59", "2016-02-29 00:00:00"},
{"2012-12-31 23:59:59", "2013-01-01 00:00:00"},
},
nil,
},

// every 5 Second
Expand All @@ -61,6 +69,7 @@ var crontests = []crontest{
{"2016-02-28 23:59:59", "2016-02-29 00:00:00"},
{"2012-12-31 23:59:59", "2013-01-01 00:00:00"},
},
nil,
},

// Minutes
Expand All @@ -76,6 +85,7 @@ var crontests = []crontest{
{"2016-02-28 23:59:00", "2016-02-29 00:00:00"},
{"2012-12-31 23:59:00", "2013-01-01 00:00:00"},
},
nil,
},

// Minutes with interval
Expand All @@ -92,6 +102,7 @@ var crontests = []crontest{
{"2016-02-28 23:50:00", "2016-02-29 00:17:00"},
{"2012-12-31 23:50:00", "2013-01-01 00:17:00"},
},
nil,
},

// Minutes interval, list
Expand All @@ -109,108 +120,177 @@ var crontests = []crontest{
{"2012-12-31 23:54:00", "2012-12-31 23:55:00"},
{"2012-12-31 23:55:00", "2013-01-01 00:15:00"},
},
nil,
},

// Days of week
{
"0 0 * * MON",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-01-01 00:00:00", "Mon 2013-01-07 00:00"},
{"2013-01-28 00:00:00", "Mon 2013-02-04 00:00"},
{"2013-12-30 00:30:00", "Mon 2014-01-06 00:00"},
{"Mon 2013-01-01 00:00", "Mon 2013-01-07 00:00"},
{"Mon 2013-01-28 00:00", "Mon 2013-02-04 00:00"},
{"Mon 2013-12-30 00:30", "Mon 2014-01-06 00:00"},
},
nil,
},
{
"0 0 * * friday",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-01-01 00:00:00", "Fri 2013-01-04 00:00"},
{"2013-01-28 00:00:00", "Fri 2013-02-01 00:00"},
{"2013-12-30 00:30:00", "Fri 2014-01-03 00:00"},
{"Mon 2013-01-01 00:00", "Fri 2013-01-04 00:00"},
{"Mon 2013-01-28 00:00", "Fri 2013-02-01 00:00"},
{"Mon 2013-12-30 00:30", "Fri 2014-01-03 00:00"},
},
nil,
},
{
"0 0 * * 6,7",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-01-01 00:00:00", "Sat 2013-01-05 00:00"},
{"2013-01-28 00:00:00", "Sat 2013-02-02 00:00"},
{"2013-12-30 00:30:00", "Sat 2014-01-04 00:00"},
{"Mon 2013-01-01 00:00", "Sat 2013-01-05 00:00"},
{"Mon 2013-01-28 00:00", "Sat 2013-02-02 00:00"},
{"Mon 2013-12-30 00:30", "Sat 2014-01-04 00:00"},
},
nil,
},

// Specific days of week
{
"0 0 * * 6#5",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-09-02 00:00:00", "Sat 2013-11-30 00:00"},
{"Mon 2013-09-02 00:00", "Sat 2013-11-30 00:00"},
},
nil,
},

// Work day of month
{
"0 0 14W * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-03-31 00:00:00", "Mon 2013-04-15 00:00"},
{"2013-08-31 00:00:00", "Fri 2013-09-13 00:00"},
{"Sun 2013-03-31 00:00", "Mon 2013-04-15 00:00"},
{"Sat 2013-08-31 00:00", "Fri 2013-09-13 00:00"},
},
nil,
},

// Work day of month -- end of month
{
"0 0 30W * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-03-02 00:00:00", "Fri 2013-03-29 00:00"},
{"2013-06-02 00:00:00", "Fri 2013-06-28 00:00"},
{"2013-09-02 00:00:00", "Mon 2013-09-30 00:00"},
{"2013-11-02 00:00:00", "Fri 2013-11-29 00:00"},
{"Sat 2013-03-02 00:00", "Fri 2013-03-29 00:00"},
{"Sun 2013-06-02 00:00", "Fri 2013-06-28 00:00"},
{"Mon 2013-09-02 00:00", "Mon 2013-09-30 00:00"},
{"Sat 2013-11-02 00:00", "Fri 2013-11-29 00:00"},
},
nil,
},

// Last day of month
{
"0 0 L * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-09-02 00:00:00", "Mon 2013-09-30 00:00"},
{"2014-01-01 00:00:00", "Fri 2014-01-31 00:00"},
{"2014-02-01 00:00:00", "Fri 2014-02-28 00:00"},
{"2016-02-15 00:00:00", "Mon 2016-02-29 00:00"},
{"Mon 2013-09-02 00:00", "Mon 2013-09-30 00:00"},
{"Wed 2014-01-01 00:00", "Fri 2014-01-31 00:00"},
{"Sat 2014-02-01 00:00", "Fri 2014-02-28 00:00"},
{"Mon 2016-02-15 00:00", "Mon 2016-02-29 00:00"},
},
nil,
},

// Last work day of month
{
"0 0 LW * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-09-02 00:00:00", "Mon 2013-09-30 00:00"},
{"2013-11-02 00:00:00", "Fri 2013-11-29 00:00"},
{"2014-08-15 00:00:00", "Fri 2014-08-29 00:00"},
{"Mon 2013-09-02 00:00", "Mon 2013-09-30 00:00"},
{"Sat 2013-11-02 00:00", "Fri 2013-11-29 00:00"},
{"Fri 2014-08-15 00:00", "Fri 2014-08-29 00:00"},
},
nil,
},

// Check that month is allowed a zero prefix (should correctly land in february in subsequent year)
{
"0 0 28 02 * *",
"Mon 2006-01-02 15:04",
[]crontimes{
{"2013-09-02 00:00:00", "Fri 2014-02-28 00:00"},
{"2014-08-15 00:00:00", "Sat 2015-02-28 00:00"},
{"Mon 20 8000 13-09-02 00:00", "Fri 2014-02-28 00:00"},
{"Fri 2014-08-15 00:00", "Sat 2015-02-28 00:00"},
},
nil,
},

// Daylight savings time
{
"*/30 * * * * * *",
"Mon 2006-01-02 15:04:05 MST",
[]crontimes{
{"Sun 2021-04-04 02:59:31 ACDT", "Sun 2021-04-04 02:00:00 ACST"},
{"Sun 2021-10-03 01:59:31 ACST", "Sun 2021-10-03 03:00:00 ACDT"},
},
Adelaide,
},
{
"*/30 * * * * * *",
"Mon 2006-01-02 15:04:05 MST",
[]crontimes{
{"Sun 2024-03-10 01:59:45 AKST", "Sun 2024-03-10 03:00:00 AKDT"},
{"Sun 2024-11-03 01:59:45 AKDT", "Sun 2024-11-03 01:00:00 AKST"},
},
Anchorage,
},
{
"*/30 * * * * * *",
"Mon 2006-01-02 15:04:05 MST",
[]crontimes{
{"Sun 2024-03-10 01:59:40 PST", "Sun 2024-03-10 03:00:00 PDT"},
{"Sun 2024-11-03 01:59:40 PDT", "Sun 2024-11-03 01:00:00 PST"},
},
PST8PDT,
},
{
"0 2-16/7 * * * * *",
"Mon 2006-01-02 15:04:05 MST",
[]crontimes{
{"Sun 2024-03-10 01:59:00 PST", "Sun 2024-03-10 03:02:00 PDT"},
{"Sun 2024-11-03 01:59:00 PDT", "Sun 2024-11-03 01:02:00 PST"},
},
PST8PDT,
},
{
"0 0 */2 * * * *",
"Mon 2006-01-02 15:04:05 MST",
[]crontimes{
{"Sun 2024-03-10 01:59:00 PST", "Sun 2024-03-10 03:00:00 PDT"},
{"Sun 2024-03-10 03:00:00 PDT", "Sun 2024-03-10 04:00:00 PDT"},
{"Sun 2024-03-10 04:01:00 PDT", "Sun 2024-03-10 06:00:00 PDT"},
{"Sun 2024-11-03 01:59:00 PDT", "Sun 2024-11-03 01:00:00 PST"},
{"Sun 2024-11-03 01:00:00 PST", "Sun 2024-11-03 02:00:00 PST"},
{"Sun 2024-11-03 02:01:00 PST", "Sun 2024-11-03 04:00:00 PST"},
},
PST8PDT,
},

// TODO: more tests
}

func TestExpressions(t *testing.T) {
for _, test := range crontests {
location := time.UTC
if test.location != nil {
location = test.location
}

for _, times := range test.times {
from, _ := time.Parse("2006-01-02 15:04:05", times.from)
from, err := time.ParseInLocation(test.layout, times.from, location)
if err != nil {
panic(err)
}
expr, err := Parse(test.expr)
if err != nil {
t.Errorf(`Parse("%s") returned "%s"`, test.expr, err.Error())
Expand Down
0