mirror of
https://github.com/Luzifer/worktime.git
synced 2024-12-22 22:11:16 +00:00
Allow time format without seconds
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
a968824a21
commit
b0750851c9
1 changed files with 19 additions and 10 deletions
|
@ -187,25 +187,34 @@ func (w *Time) migrate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Time) validate() error {
|
func (t *Time) validate() error {
|
||||||
now := time.Now().Format("15:04:05")
|
var err error
|
||||||
if t.Start == "now" {
|
if t.Start, err = t.sanitizeTime(t.Start); err != nil {
|
||||||
t.Start = now
|
|
||||||
}
|
|
||||||
if t.End == "now" {
|
|
||||||
t.End = now
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := time.Parse("15:04:05", t.Start); err != nil {
|
|
||||||
return fmt.Errorf("Time %.7s has invalid start date: %s", t.ID, err)
|
return fmt.Errorf("Time %.7s has invalid start date: %s", t.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := time.Parse("15:04:05", t.End); err != nil {
|
if t.End, err = t.sanitizeTime(t.End); err != nil {
|
||||||
return fmt.Errorf("Time %.7s has invalid end date: %s", t.ID, err)
|
return fmt.Errorf("Time %.7s has invalid end date: %s", t.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Time) sanitizeTime(in string) (string, error) {
|
||||||
|
if in == "now" {
|
||||||
|
return time.Now().Format("15:04:05"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if t, err := time.Parse("15:04", in); err == nil {
|
||||||
|
return t.Format("15:04:05"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := time.Parse("15:04:05", in); err == nil {
|
||||||
|
return in, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("Date does not comply format 15:04 or 15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
type Overtime struct {
|
type Overtime struct {
|
||||||
Value float64 `json:"value"`
|
Value float64 `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue