Skip to content
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

fix timezone bug #36

Closed
wants to merge 5 commits into from
Closed
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
9 changes: 6 additions & 3 deletions internal/service/handlers/daily_question_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ func CreateDailyQuestion(w http.ResponseWriter, r *http.Request) {
})...)
return
}

nowTime := time.Now().UTC()
if !timeReq.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, DailyQuestions(r).Location)) {
Log(r).Errorf("Arg start_at must be more or equal tomorow midnoght noe: %s", timeReq.String())
TimeDeadline := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, location).UTC()

if TimeDeadline.After(timeReq.UTC()) {
Log(r).Errorf("Error %s", timeReq.UTC().String())
ape.RenderErr(w, problems.BadRequest(validation.Errors{
"starts_at": fmt.Errorf("argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.String()),
"starts_at": fmt.Errorf("argument start_at must be more or equal tomorow mid night now: %s", timeReq.UTC().String()),
})...)
return
}
Expand Down
26 changes: 19 additions & 7 deletions internal/service/handlers/daily_question_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/go-chi/chi"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/rarimo/geo-auth-svc/pkg/auth"
"github.com/rarimo/geo-points-svc/internal/data"
"github.com/rarimo/geo-points-svc/resources"
Expand Down Expand Up @@ -42,13 +43,24 @@ func DeleteDailyQuestion(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.NotFound())
return
}
deletedQuestion := *question

timeReq := question.StartsAt
location := DailyQuestions(r).Location
timeReq, err := time.ParseInLocation("2006-01-02", question.StartsAt.Format("2006-01-02"), location)
if err != nil {
Log(r).WithError(err).Error("Failed to parse start time")
ape.RenderErr(w, problems.BadRequest(validation.Errors{
"starts_at": fmt.Errorf("failed to parse start time %s err: %s", question.StartsAt.String(), err),
})...)
return
}

nowTime := time.Now().UTC()
if !timeReq.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, DailyQuestions(r).Location)) {
Log(r).Errorf("Only questions that start tomorrow or later can be delete: %s", timeReq.String())
ape.RenderErr(w, problems.BadRequest(err)...)

if timeReq.UTC().Before(nowTime.AddDate(0, 0, -2)) {
Log(r).Errorf("Error %s", timeReq.UTC().String())
ape.RenderErr(w, problems.BadRequest(validation.Errors{
"starts_at": fmt.Errorf("argument start_at must be more or equal tomorow mid night now: %s", timeReq.UTC().String()),
})...)
return
}

Expand All @@ -58,8 +70,8 @@ func DeleteDailyQuestion(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.InternalError())
return
}
loc := DailyQuestions(r).Location
response, err := NewDailyQuestionDelete(ID, deletedQuestion, loc)

response, err := NewDailyQuestionDelete(ID, *question, location)
if err != nil {
Log(r).WithError(err).Error("Error deleting daily question")
ape.RenderErr(w, problems.InternalError())
Expand Down
15 changes: 8 additions & 7 deletions internal/service/handlers/daily_question_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) {
return
}

location := DailyQuestions(r).Location
nowTime := time.Now().UTC()
if !question.StartsAt.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, DailyQuestions(r).Location)) {

if question.StartsAt.UTC().Before(nowTime.AddDate(0, 0, -1)) {
Log(r).Errorf("Cannot change a question id: %v that is available today or in the past", ID)
ape.RenderErr(w, problems.BadRequest(validation.Errors{
"starts_at": fmt.Errorf("cannot change a question id: %v that is available today or in the past", ID),
Expand All @@ -69,7 +71,6 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) {
}

if attributes.StartsAt != nil {
location := DailyQuestions(r).Location
timeReq, err := time.ParseInLocation("2006-01-02", *attributes.StartsAt, location)
if err != nil {
Log(r).WithError(err).Error("Failed to parse start time")
Expand All @@ -78,16 +79,16 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) {
})...)
return
}
nowTime := time.Now().UTC()
if !timeReq.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, DailyQuestions(r).Location)) {
Log(r).Errorf("Argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.String())

if !timeReq.After(nowTime.UTC().AddDate(0, 0, -1)) {
Log(r).Errorf("Argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.UTC().String())
ape.RenderErr(w, problems.BadRequest(validation.Errors{
"starts_at": fmt.Errorf("argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.String()),
"starts_at": fmt.Errorf("argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.UTC().String()),
})...)
return
}

question, err := DailyQuestionsQ(r).FilterDayQuestions(timeReq).Get()
question, err := DailyQuestionsQ(r).FilterDayQuestions(timeReq.UTC()).Get()
if err != nil {
Log(r).WithError(err).Error("Error on this day")
ape.RenderErr(w, problems.InternalError())
Expand Down
Loading