Merge pull request #2216 from semaphoreui/always_use_utc_time

Always use utc time
This commit is contained in:
Denis Gukov 2024-07-22 17:19:28 +05:00 committed by GitHub
commit 74863501dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package db
import (
"fmt"
"github.com/go-gorp/gorp/v3"
"time"
"github.com/ansible-semaphore/semaphore/pkg/task_logger"
@ -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

View File

@ -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 (?, ?, ?, ?, ?, ?)",
@ -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)
@ -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)

View File

@ -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",

View File

@ -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
}
@ -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
}

View File

@ -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,
@ -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
}
@ -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)

View File

@ -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)
@ -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)

View File

@ -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));