123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package com
- import (
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- func Date(ti int64, format string) string {
- t := time.Unix(int64(ti), 0)
- return DateT(t, format)
- }
- func DateS(ts string, format string) string {
- i, _ := strconv.ParseInt(ts, 10, 64)
- return Date(i, format)
- }
- func DateT(t time.Time, format string) string {
- res := strings.Replace(format, "MM", t.Format("01"), -1)
- res = strings.Replace(res, "M", t.Format("1"), -1)
- res = strings.Replace(res, "DD", t.Format("02"), -1)
- res = strings.Replace(res, "D", t.Format("2"), -1)
- res = strings.Replace(res, "YYYY", t.Format("2006"), -1)
- res = strings.Replace(res, "YY", t.Format("06"), -1)
- res = strings.Replace(res, "HH", fmt.Sprintf("%02d", t.Hour()), -1)
- res = strings.Replace(res, "H", fmt.Sprintf("%d", t.Hour()), -1)
- res = strings.Replace(res, "hh", t.Format("03"), -1)
- res = strings.Replace(res, "h", t.Format("3"), -1)
- res = strings.Replace(res, "mm", t.Format("04"), -1)
- res = strings.Replace(res, "m", t.Format("4"), -1)
- res = strings.Replace(res, "ss", t.Format("05"), -1)
- res = strings.Replace(res, "s", t.Format("5"), -1)
- return res
- }
- var datePatterns = []string{
-
- "Y", "2006",
- "y", "06",
-
- "m", "01",
- "n", "1",
- "M", "Jan",
- "F", "January",
-
- "d", "02",
- "j", "2",
-
- "D", "Mon",
- "l", "Monday",
-
- "g", "3",
- "G", "15",
- "h", "03",
- "H", "15",
- "a", "pm",
- "A", "PM",
- "i", "04",
- "s", "05",
-
- "T", "MST",
- "P", "-07:00",
- "O", "-0700",
-
- "r", time.RFC1123Z,
- }
- func DateParse(dateString, format string) (time.Time, error) {
- replacer := strings.NewReplacer(datePatterns...)
- format = replacer.Replace(format)
- return time.ParseInLocation(format, dateString, time.Local)
- }
|