Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
Use current time in location consistently (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
grubbins authored and scottleedavis committed May 28, 2019
1 parent f2fc596 commit 10bd4ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
33 changes: 16 additions & 17 deletions server/occurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ func (p *Plugin) onEN(when string, user *model.User) (times []time.Time, err err
return []time.Time{}, pErr
}

todayWeekDayNum := int(time.Now().Weekday())
todayWeekDayNum := int(time.Now().In(location).Weekday())
weekDayNum := p.weekDayNumber(dateUnit, user)
day := 0

Expand Down Expand Up @@ -1003,7 +1003,7 @@ func (p *Plugin) everyEN(when string, user *model.User) (times []time.Time, err
T("friday"),
T("saturday"):

todayWeekDayNum := int(time.Now().Weekday())
todayWeekDayNum := int(time.Now().In(location).Weekday())
weekDayNum := p.weekDayNumber(dateUnit, user)
day := 0

Expand Down Expand Up @@ -1195,14 +1195,16 @@ func (p *Plugin) formatWhenEN(username string, when string, occurrence string, s
return ""
}
T, _ := p.translation(user)
location := p.location(user)
now := time.Now().In(location)
t, _ := time.Parse(time.RFC3339, occurrence)

if strings.HasPrefix(when, T("in")) {

t, _ := time.Parse(time.RFC3339, occurrence)
endDate := ""
if time.Now().YearDay() == t.YearDay() {
if now.YearDay() == t.YearDay() {
endDate = T("today")
} else if time.Now().YearDay() == t.YearDay()-1 {
} else if now.YearDay() == t.YearDay()-1 {
endDate = T("tomorrow")
} else {
endDate = t.Weekday().String() + ", " + t.Month().String() + " " + p.daySuffixFromInt(user, t.Day())
Expand All @@ -1216,11 +1218,10 @@ func (p *Plugin) formatWhenEN(username string, when string, occurrence string, s

if strings.HasPrefix(when, T("at")) {

t, _ := time.Parse(time.RFC3339, occurrence)
endDate := ""
if time.Now().YearDay() == t.YearDay() {
if now.YearDay() == t.YearDay() {
endDate = T("today")
} else if time.Now().YearDay() == t.YearDay()-1 {
} else if now.YearDay() == t.YearDay()-1 {
endDate = T("tomorrow")
} else {
endDate = t.Weekday().String() + ", " + t.Month().String() + " " + p.daySuffixFromInt(user, t.Day())
Expand All @@ -1235,11 +1236,10 @@ func (p *Plugin) formatWhenEN(username string, when string, occurrence string, s

if strings.HasPrefix(when, T("on")) {

t, _ := time.Parse(time.RFC3339, occurrence)
endDate := ""
if time.Now().YearDay() == t.YearDay() {
if now.YearDay() == t.YearDay() {
endDate = T("today")
} else if time.Now().YearDay() == t.YearDay()-1 {
} else if now.YearDay() == t.YearDay()-1 {
endDate = T("tomorrow")
} else {
endDate = t.Weekday().String() + ", " + t.Month().String() + " " + p.daySuffixFromInt(user, t.Day())
Expand All @@ -1254,7 +1254,6 @@ func (p *Plugin) formatWhenEN(username string, when string, occurrence string, s

if strings.HasPrefix(when, T("every")) {

t, _ := time.Parse(time.RFC3339, occurrence)
repeatDate := strings.Trim(strings.Split(when, T("at"))[0], " ")
repeatDate = strings.Replace(repeatDate, T("every"), "", -1)
repeatDate = strings.Title(strings.ToLower(repeatDate))
Expand All @@ -1267,11 +1266,10 @@ func (p *Plugin) formatWhenEN(username string, when string, occurrence string, s

}

t, _ := time.Parse(time.RFC3339, occurrence)
endDate := ""
if time.Now().YearDay() == t.YearDay() {
if now.YearDay() == t.YearDay() {
endDate = T("today")
} else if time.Now().YearDay() == t.YearDay()-1 {
} else if now.YearDay() == t.YearDay()-1 {
endDate = T("tomorrow")
} else {
endDate = t.Weekday().String() + ", " + t.Month().String() + " " + p.daySuffixFromInt(user, t.Day())
Expand All @@ -1287,16 +1285,17 @@ func (p *Plugin) formatWhenEN(username string, when string, occurrence string, s
func (p *Plugin) chooseClosest(user *model.User, chosen *time.Time, dayInterval bool) time.Time {

location := p.location(user)
now := time.Now().In(location)

if dayInterval {
if chosen.Before(time.Now().In(location)) {
if chosen.Before(now) {
return chosen.In(location).Round(time.Second).Add(time.Hour * 24 * time.Duration(1))
} else {
return *chosen
}
} else {
if chosen.Before(time.Now().In(location)) {
if chosen.Add(time.Hour * 12 * time.Duration(1)).Before(time.Now()) {
if chosen.Add(time.Hour * 12 * time.Duration(1)).Before(now) {
return chosen.In(location).Round(time.Second).Add(time.Hour * 24 * time.Duration(1))
} else {
return chosen.In(location).Round(time.Second).Add(time.Hour * 12 * time.Duration(1))
Expand Down
10 changes: 5 additions & 5 deletions server/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ func (p *Plugin) normalizeDate(text string, user *model.User) (string, error) {

switch len(date) {
case 2:
year := time.Now().Year()
year := time.Now().In(location).Year()
month, mErr := strconv.Atoi(date[0])
if mErr != nil {
return "", mErr
Expand Down Expand Up @@ -686,7 +686,7 @@ func (p *Plugin) normalizeDate(text string, user *model.User) (string, error) {

switch len(date) {
case 2:
year := time.Now().Year()
year := time.Now().In(location).Year()
month, mErr := strconv.Atoi(date[1])
if mErr != nil {
return "", mErr
Expand Down Expand Up @@ -737,11 +737,11 @@ func (p *Plugin) normalizeDate(text string, user *model.User) (string, error) {
dayInt = d
}

month := time.Now().Month()
year := time.Now().Year()
month := time.Now().In(location).Month()
year := time.Now().In(location).Year()
t := time.Date(year, month, dayInt, 0, 0, 0, 0, location)

if t.Before(time.Now()) {
if t.Before(time.Now().In(location)) {
t = t.AddDate(0, 1, 0)
}

Expand Down

0 comments on commit 10bd4ba

Please sign in to comment.