diff --git a/parser.go b/parser.go index 8da6547a..3fc4a964 100644 --- a/parser.go +++ b/parser.go @@ -135,12 +135,19 @@ func (p Parser) Parse(spec string) (Schedule, error) { hour = field(fields[2], hours) dayofmonth = field(fields[3], dom) month = field(fields[4], months) - dayofweek = field(fields[5], dow) + dayofweek = field(fields[5], extendedDOW) ) if err != nil { return nil, err } + if 1<<7&dayofweek > 0 { + // For compatibility with https://linux.die.net/man/5/crontab + // we consider 1 in the 7th bit as Sunday and move it to the first bit + dayofweek |= 1 + dayofweek ^= 1 << 7 + } + return &SpecSchedule{ Second: second, Minute: minute, diff --git a/parser_test.go b/parser_test.go index 41c8c520..749ce296 100644 --- a/parser_test.go +++ b/parser_test.go @@ -149,6 +149,9 @@ func TestParseSchedule(t *testing.T) { {secondParser, "CRON_TZ=UTC 0 5 * * * *", every5min(time.UTC)}, {standardParser, "CRON_TZ=UTC 5 * * * *", every5min(time.UTC)}, {secondParser, "CRON_TZ=Asia/Tokyo 0 5 * * * *", every5min(tokyo)}, + {standardParser, "CRON_TZ=Asia/Tokyo 0 0 * * 0", everySunday(tokyo)}, + {standardParser, "CRON_TZ=Asia/Tokyo 0 0 * * 7", everySunday(tokyo)}, + {standardParser, "CRON_TZ=Asia/Tokyo 0 0 * * Sun", everySunday(tokyo)}, {secondParser, "@every 5m", ConstantDelaySchedule{5 * time.Minute}}, {secondParser, "@midnight", midnight(time.Local)}, {secondParser, "TZ=UTC @midnight", midnight(time.UTC)}, @@ -370,6 +373,10 @@ func midnight(loc *time.Location) *SpecSchedule { return &SpecSchedule{1, 1, 1, all(dom), all(months), all(dow), loc} } +func everySunday(loc *time.Location) *SpecSchedule { + return &SpecSchedule{1, 1, 1, all(dom), all(months), 1 << uint(time.Sunday), loc} +} + func annual(loc *time.Location) *SpecSchedule { return &SpecSchedule{ Second: 1 << seconds.min, diff --git a/spec.go b/spec.go index fa1e241e..0c75fb0d 100644 --- a/spec.go +++ b/spec.go @@ -46,6 +46,9 @@ var ( "fri": 5, "sat": 6, }} + + // extendedDOW is only needed to parse "7" as Sunday + extendedDOW = bounds{dow.min, 7, dow.names} ) const (