package common import ( "fmt" "log" "strconv" "strings" "time" "errors" ) // DateFormat pattern rules. var datePatterns = []string{ // year "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 "y", "06", //A two digit representation of a year Examples: 99 or 03 // month "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 "M", "Jan", // A short textual representation of a month, three letters Jan through Dec "F", "January", // A full textual representation of a month, such as January or March January through December // day "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 "j", "2", // Day of the month without leading zeros 1 to 31 // week "D", "Mon", // A textual representation of a day, three letters Mon through Sun "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday // time "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM "i", "04", // Minutes with leading zeros 00 to 59 "s", "05", // Seconds, with leading zeros 00 through 59 // time zone "T", "MST", "P", "-07:00", "O", "-0700", // RFC 2822 "r", time.RFC1123Z, } // Format time.Time struct to string // MM - month - 01 // M - month - 1, single bit // DD - day - 02 // D - day 2 // YYYY - year - 2006 // YY - year - 06 // HH - 24 hours - 03 // H - 24 hours - 3 // hh - 12 hours - 03 // h - 12 hours - 3 // mm - minute - 04 // m - minute - 4 // ss - second - 05 // s - second = 5 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 } // Parse Date use PHP time format. func DateParse(dateString, format string) (time.Time, error) { replacer := strings.NewReplacer(datePatterns...) format = replacer.Replace(format) return time.ParseInLocation(format, dateString, time.Local) } // Format unix time int64 to string func Date(ti int64, format string) string { t := time.Unix(int64(ti), 0) return DateT(t, format) } // Format unix time string to string func DateS(ts string, format string) string { i, _ := strconv.ParseInt(ts, 10, 64) return Date(i, format) } func GetTimeFormatByType(timeType int) (format string){ switch timeType{ case 203: format = "YmdHi" case 204: format = "YmdHis" case 102: format = "Ymd" default: format = "YmdHi" } return } //format time like java, such as: yyyy-MM-dd HH:mm:ss func TimeFormat(t time.Time, format string) string { //year if strings.ContainsAny(format, "y") { year := strconv.Itoa(t.Year()) if strings.Count(format, "yy") == 1 && strings.Count(format, "y") == 2 { format = strings.Replace(format, "yy", year[2:], 1) } else if strings.Count(format, "yyyy") == 1 && strings.Count(format, "y") == 4 { format = strings.Replace(format, "yyyy", year, 1) } else { log.Fatalln("format year error! please 'yyyy' or 'yy'") } } //month if strings.ContainsAny(format, "M") { var month string if int(t.Month()) < 10 { month = "0" + strconv.Itoa(int(t.Month())) } else { month = strconv.Itoa(int(t.Month())) } if strings.Count(format, "MM") == 1 && strings.Count(format, "M") == 2 { format = strings.Replace(format, "MM", month, 1) } else { log.Fatalln("format month error! please 'MM'") } } //day if strings.ContainsAny(format, "d") { var day string if t.Day() < 10 { day = "0" + strconv.Itoa(t.Day()) } else { day = strconv.Itoa(t.Day()) } if strings.Count(format, "dd") == 1 && strings.Count(format, "d") == 2 { format = strings.Replace(format, "dd", day, 1) } else { log.Fatalln("format day error! please 'dd'") } } //hour if strings.ContainsAny(format, "H") { var hour string if t.Hour() < 10 { hour = "0" + strconv.Itoa(t.Hour()) } else { hour = strconv.Itoa(t.Hour()) } if strings.Count(format, "HH") == 1 && strings.Count(format, "H") == 2 { format = strings.Replace(format, "HH", hour, 1) } else { log.Fatalln("format hour error! please 'HH'") } } //minute if strings.ContainsAny(format, "m") { var minute string if t.Minute() < 10 { minute = "0" + strconv.Itoa(t.Minute()) } else { minute = strconv.Itoa(t.Minute()) } if strings.Count(format, "mm") == 1 && strings.Count(format, "m") == 2 { format = strings.Replace(format, "mm", minute, 1) } else { log.Fatalln("format minute error! please 'mm'") } } //second if strings.ContainsAny(format, "s") { var second string if t.Second() < 10 { second = "0" + strconv.Itoa(t.Second()) } else { second = strconv.Itoa(t.Second()) } if strings.Count(format, "ss") == 1 && strings.Count(format, "s") == 2 { format = strings.Replace(format, "ss", second, 1) } else { log.Fatalln("format second error! please 'ss'") } } return format } //2007-11-23 10:02:14/20071123100214 func TimeParse(str string) (time.Time, error) { ll := len(str) if ll == 19 { loc, _ := time.LoadLocation("Local") return time.ParseInLocation("2006-01-02 15:04:05", str, loc) } else if ll == 14 { loc, _ := time.LoadLocation("Local") return time.ParseInLocation("20060102150405", str, loc) } return time.Time{}, errors.New("str len is error") } //17:02:03/170203 func TimeParseHHmmss(str string) (time.Time, error) { ll := len(str) if ll != 8 && ll != 6 { return time.Time{}, errors.New("input str is error") } if ll == 8 { return TimeParse(TimeFormat(time.Now(), "yyyy-MM-dd "+str)) } return TimeParse(TimeFormat(time.Now(), "yyyyMMdd"+str)) } //17:02/1702 func TimeParseHHmm(str string) (time.Time, error) { ll := len(str) if ll != 5 && ll != 4 { return time.Time{}, errors.New("input str is error") } if ll == 5 { return TimeParse(TimeFormat(time.Now(), "yyyy-MM-dd "+str+":00")) } return TimeParse(TimeFormat(time.Now(), "yyyyMMdd"+str+"00")) } //2007-05-69/20070569 func TimeParseyyyyMMdd(str string) (time.Time, error) { ll := len(str) if ll != 10 && ll != 8 { return time.Time{}, errors.New("input str is error") } if ll == 10 { return TimeParse(TimeFormat(time.Now(), str+" 00:00:00")) } return TimeParse(TimeFormat(time.Now(), str+"000000")) }