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

Always use utc time #2216

Merged
merged 3 commits into from
Jul 22, 2024
Merged
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
19 changes: 19 additions & 0 deletions db/Task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"fmt"
"github.com/go-gorp/gorp/v3"
"time"

"github.com/ansible-semaphore/semaphore/pkg/task_logger"
Expand Down Expand Up @@ -51,6 +52,24 @@ type Task struct {
InventoryID *int `db:"inventory_id" json:"inventory_id"`
}

func (task *Task) PreInsert(gorp.SqlExecutor) error {
task.Created = task.Created.UTC()
return nil
}

func (task *Task) PreUpdate(gorp.SqlExecutor) error {
if task.Start != nil {
start := task.Start.UTC()
task.Start = &start
}

if task.End != nil {
end := task.End.UTC()
task.End = &end
}
return nil
}

func (task *Task) GetIncomingVersion(d Store) *string {
if task.BuildTaskID == nil {
return nil
Expand Down
6 changes: 3 additions & 3 deletions db/sql/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (d *SqlDb) getEvents(q squirrel.SelectBuilder, params db.RetrieveQueryParam
}

func (d *SqlDb) CreateEvent(evt db.Event) (newEvent db.Event, err error) {
var created = time.Now()
var created = time.Now().UTC()

_, err = d.exec(
"insert into event(user_id, project_id, object_id, object_type, description, created) values (?, ?, ?, ?, ?, ?)",
Expand All @@ -54,7 +54,7 @@ func (d *SqlDb) GetUserEvents(userID int, params db.RetrieveQueryParams) ([]db.E
q := squirrel.Select("event.*, p.name as project_name").
From("event").
LeftJoin("project as p on event.project_id=p.id").
OrderBy("created desc").
OrderBy("id desc").
LeftJoin("project__user as pu on pu.project_id=p.id").
Where("p.id IS NULL or pu.user_id=?", userID)

Expand All @@ -65,7 +65,7 @@ func (d *SqlDb) GetEvents(projectID int, params db.RetrieveQueryParams) ([]db.Ev
q := squirrel.Select("event.*, p.name as project_name").
From("event").
LeftJoin("project as p on event.project_id=p.id").
OrderBy("created desc").
OrderBy("id desc").
Where("event.project_id=?", projectID)

return d.getEvents(q, params)
Expand Down
2 changes: 1 addition & 1 deletion db/sql/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (d *SqlDb) CreateProject(project db.Project) (newProject db.Project, err error) {
project.Created = time.Now()
project.Created = time.Now().UTC()

insertId, err := d.insert(
"id",
Expand Down
4 changes: 2 additions & 2 deletions db/sql/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (d *SqlDb) CreateSession(session db.Session) (db.Session, error) {
}

func (d *SqlDb) CreateAPIToken(token db.APIToken) (db.APIToken, error) {
token.Created = db.GetParsedTime(time.Now())
token.Created = db.GetParsedTime(time.Now().UTC())
err := d.sql.Insert(&token)
return token, err
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func (d *SqlDb) ExpireSession(userID int, sessionID int) error {
}

func (d *SqlDb) TouchSession(userID int, sessionID int) error {
_, err := d.exec("update session set last_active=? where id=? and user_id=?", time.Now(), sessionID, userID)
_, err := d.exec("update session set last_active=? where id=? and user_id=?", time.Now().UTC(), sessionID, userID)

return err
}
Expand Down
11 changes: 8 additions & 3 deletions db/sql/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ func (d *SqlDb) CreateTask(task db.Task, maxTasks int) (newTask db.Task, err err
}

func (d *SqlDb) UpdateTask(task db.Task) error {
_, err := d.exec(
err := task.PreUpdate(d.sql)
if err != nil {
return err
}

_, err = d.exec(
"update task set status=?, start=?, `end`=? where id=?",
task.Status,
task.Start,
Expand All @@ -107,7 +112,7 @@ func (d *SqlDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) {
"insert into task__output (task_id, task, output, time) VALUES (?, '', ?, ?)",
output.TaskID,
output.Output,
output.Time)
output.Time.UTC())
return output, err
}

Expand All @@ -123,7 +128,7 @@ func (d *SqlDb) getTasks(projectID int, templateID *int, taskIDs []int, params d
From("task").
Join("project__template as tpl on task.template_id=tpl.id").
LeftJoin("`user` on task.user_id=`user`.id").
OrderBy("task.created desc, id desc")
OrderBy("id desc")

if templateID == nil {
q = q.Where("tpl.project_id=?", projectID)
Expand Down
4 changes: 2 additions & 2 deletions db/sql/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (d *SqlDb) CreateUserWithoutPassword(user db.User) (newUser db.User, err er
}

user.Password = ""
user.Created = db.GetParsedTime(time.Now())
user.Created = db.GetParsedTime(time.Now().UTC())

err = d.sql.Insert(&user)

Expand All @@ -42,7 +42,7 @@ func (d *SqlDb) CreateUser(user db.UserWithPwd) (newUser db.User, err error) {
}

user.Password = string(pwdHash)
user.Created = db.GetParsedTime(time.Now())
user.Created = db.GetParsedTime(time.Now().UTC())

err = d.sql.Insert(&user.User)

Expand Down
13 changes: 12 additions & 1 deletion web/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ const convert = new Convert();
axios.defaults.baseURL = document.baseURI;
Vue.config.productionTip = false;

Vue.filter('formatDate', (value) => (value ? moment(String(value)).fromNow() : '—'));
Vue.filter('formatDate', (value) => {
if (!value) {
return '—';
}
const date = moment(value);
const now = moment();

if (now.isSame(date, 'day')) {
return `${date.fromNow()} (${date.format('LT')})`; // Display only time if today
}
return date.format('L LT'); // Display only date otherwise
});
Vue.filter('formatTime', (value) => (value ? moment(String(value)).format('LTS') : '—'));
Vue.filter('formatLog', (value) => (value ? convert.toHtml(String(value)) : value));

Expand Down