util.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "time"
  8. )
  9. const (
  10. Date_Full_Layout = "2006-01-02 15:04:05"
  11. Date_Short_Layout = "2006-01-02"
  12. Date_Small_Layout = "01-02"
  13. Date_Time_Layout = "15:04"
  14. Date_yyyyMMdd = "20060102"
  15. Date_yyyyMMdd_Point = "2006.01.02"
  16. )
  17. func ReadConfig(config ...interface{}) {
  18. var r *os.File
  19. if len(config) > 1 {
  20. filepath, _ := config[0].(string)
  21. r, _ = os.Open(filepath)
  22. defer r.Close()
  23. bs, _ := ioutil.ReadAll(r)
  24. json.Unmarshal(bs, config[1])
  25. } else {
  26. r, _ = os.Open("./config.json")
  27. defer r.Close()
  28. bs, _ := ioutil.ReadAll(r)
  29. json.Unmarshal(bs, config[0])
  30. }
  31. }
  32. func GetTime(day int) int64 {
  33. nowTime := time.Now().AddDate(0, 0, day)
  34. timeStr := FormatDate(&nowTime, Date_Short_Layout)
  35. t, _ := time.ParseInLocation(Date_Short_Layout, timeStr, time.Local)
  36. return t.Unix()
  37. }
  38. func FormatDateByInt64(src *int64, layout string) string {
  39. var tmp int64
  40. if *src > 0 {
  41. if len(fmt.Sprint(*src)) >= 12 {
  42. tmp = (*src) / 1000
  43. } else {
  44. tmp = (*src)
  45. }
  46. } else {
  47. if len(fmt.Sprint(*src)) >= 13 {
  48. tmp = (*src) / 1000
  49. } else {
  50. tmp = (*src)
  51. }
  52. }
  53. date := time.Unix(tmp, 0)
  54. return FormatDate(&date, layout)
  55. }
  56. // 日期格式化
  57. func FormatDate(src *time.Time, layout string) string {
  58. return (*src).Local().Format(layout)
  59. }