12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package main
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "os"
- "time"
- )
- const (
- Date_Full_Layout = "2006-01-02 15:04:05"
- Date_Short_Layout = "2006-01-02"
- Date_Small_Layout = "01-02"
- Date_Time_Layout = "15:04"
- Date_yyyyMMdd = "20060102"
- Date_yyyyMMdd_Point = "2006.01.02"
- )
- func ReadConfig(config ...interface{}) {
- var r *os.File
- if len(config) > 1 {
- filepath, _ := config[0].(string)
- r, _ = os.Open(filepath)
- defer r.Close()
- bs, _ := ioutil.ReadAll(r)
- json.Unmarshal(bs, config[1])
- } else {
- r, _ = os.Open("./config.json")
- defer r.Close()
- bs, _ := ioutil.ReadAll(r)
- json.Unmarshal(bs, config[0])
- }
- }
- func GetTime(day int) int64 {
- nowTime := time.Now().AddDate(0, 0, day)
- timeStr := FormatDate(&nowTime, Date_Short_Layout)
- t, _ := time.ParseInLocation(Date_Short_Layout, timeStr, time.Local)
- return t.Unix()
- }
- func FormatDateByInt64(src *int64, layout string) string {
- var tmp int64
- if *src > 0 {
- if len(fmt.Sprint(*src)) >= 12 {
- tmp = (*src) / 1000
- } else {
- tmp = (*src)
- }
- } else {
- if len(fmt.Sprint(*src)) >= 13 {
- tmp = (*src) / 1000
- } else {
- tmp = (*src)
- }
- }
- date := time.Unix(tmp, 0)
- return FormatDate(&date, layout)
- }
- // 日期格式化
- func FormatDate(src *time.Time, layout string) string {
- return (*src).Local().Format(layout)
- }
|