github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/time.go (about) 1 // Copyright 2013 com authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package com 16 17 import ( 18 "fmt" 19 "strconv" 20 "strings" 21 "time" 22 ) 23 24 // Format unix time int64 to string 25 func Date(ti int64, format string) string { 26 t := time.Unix(int64(ti), 0) 27 return DateT(t, format) 28 } 29 30 // Format unix time string to string 31 func DateS(ts string, format string) string { 32 i, _ := strconv.ParseInt(ts, 10, 64) 33 return Date(i, format) 34 } 35 36 // Format time.Time struct to string 37 // MM - month - 01 38 // M - month - 1, single bit 39 // DD - day - 02 40 // D - day 2 41 // YYYY - year - 2006 42 // YY - year - 06 43 // HH - 24 hours - 03 44 // H - 24 hours - 3 45 // hh - 12 hours - 03 46 // h - 12 hours - 3 47 // mm - minute - 04 48 // m - minute - 4 49 // ss - second - 05 50 // s - second = 5 51 func DateT(t time.Time, format string) string { 52 res := strings.Replace(format, "MM", t.Format("01"), -1) 53 res = strings.Replace(res, "M", t.Format("1"), -1) 54 res = strings.Replace(res, "DD", t.Format("02"), -1) 55 res = strings.Replace(res, "D", t.Format("2"), -1) 56 res = strings.Replace(res, "YYYY", t.Format("2006"), -1) 57 res = strings.Replace(res, "YY", t.Format("06"), -1) 58 res = strings.Replace(res, "HH", fmt.Sprintf("%02d", t.Hour()), -1) 59 res = strings.Replace(res, "H", fmt.Sprintf("%d", t.Hour()), -1) 60 res = strings.Replace(res, "hh", t.Format("03"), -1) 61 res = strings.Replace(res, "h", t.Format("3"), -1) 62 res = strings.Replace(res, "mm", t.Format("04"), -1) 63 res = strings.Replace(res, "m", t.Format("4"), -1) 64 res = strings.Replace(res, "ss", t.Format("05"), -1) 65 res = strings.Replace(res, "s", t.Format("5"), -1) 66 return res 67 } 68 69 // DateFormat pattern rules. 70 var datePatterns = []string{ 71 // year 72 "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 73 "y", "06", //A two digit representation of a year Examples: 99 or 03 74 75 // month 76 "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 77 "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 78 "M", "Jan", // A short textual representation of a month, three letters Jan through Dec 79 "F", "January", // A full textual representation of a month, such as January or March January through December 80 81 // day 82 "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 83 "j", "2", // Day of the month without leading zeros 1 to 31 84 85 // week 86 "D", "Mon", // A textual representation of a day, three letters Mon through Sun 87 "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday 88 89 // time 90 "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 91 "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 92 "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 93 "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 94 95 "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm 96 "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM 97 98 "i", "04", // Minutes with leading zeros 00 to 59 99 "s", "05", // Seconds, with leading zeros 00 through 59 100 101 // time zone 102 "T", "MST", 103 "P", "-07:00", 104 "O", "-0700", 105 106 // RFC 2822 107 "r", time.RFC1123Z, 108 } 109 110 // Parse Date use PHP time format. 111 func DateParse(dateString, format string) (time.Time, error) { 112 replacer := strings.NewReplacer(datePatterns...) 113 format = replacer.Replace(format) 114 return time.ParseInLocation(format, dateString, time.Local) 115 }