You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.2 KiB

2 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. )
  7. func unmarshalTime(s []byte) (time.Time, error) {
  8. r := string(s)
  9. q, err := strconv.ParseInt(r, 10, 64)
  10. if err != nil {
  11. return time.Now(), err
  12. }
  13. return time.Unix(q, 0), nil
  14. }
  15. type CurrentDate time.Time
  16. func (t *CurrentDate) UnmarshalJSON(s []byte) (err error) {
  17. *(*time.Time)(t), err = unmarshalTime(s)
  18. return err
  19. }
  20. func (t CurrentDate) String() string {
  21. return time.Time(t).Format("2006-01-02, 15h")
  22. }
  23. type HourOnly time.Time
  24. func (t *HourOnly) UnmarshalJSON(s []byte) (err error) {
  25. *(*time.Time)(t), err = unmarshalTime(s)
  26. return err
  27. }
  28. func (t HourOnly) String() string {
  29. return time.Time(t).Format("15h")
  30. }
  31. type Day time.Time
  32. func (t *Day) UnmarshalJSON(s []byte) (err error) {
  33. *(*time.Time)(t), err = unmarshalTime(s)
  34. return err
  35. }
  36. func (t Day) String() string {
  37. return time.Time(t).Format("Mon")
  38. }
  39. type Hour time.Time
  40. func (t *Hour) UnmarshalJSON(s []byte) (err error) {
  41. *(*time.Time)(t), err = unmarshalTime(s)
  42. return err
  43. }
  44. func (t Hour) String() string {
  45. return time.Time(t).Format("15:04:05")
  46. }
  47. type Temperature float64
  48. func (t Temperature) String() string {
  49. return fmt.Sprintf("%.0f", t)
  50. }