add an DateParse function, parse datestring to time.Time use php date format
Showing
1 changed file
with
33 additions
and
18 deletions
| ... | @@ -66,18 +66,16 @@ func DateFormat(t time.Time, layout string) (datestring string) { | ... | @@ -66,18 +66,16 @@ func DateFormat(t time.Time, layout string) (datestring string) { |
| 66 | return | 66 | return |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | // Date takes a PHP like date func to Go's time fomate | 69 | var DatePatterns = []string{ |
| 70 | func Date(t time.Time, format string) (datestring string) { | ||
| 71 | patterns := []string{ | ||
| 72 | // year | 70 | // year |
| 73 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 | 71 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 |
| 74 | "y", "06", //A two digit representation of a year Examples: 99 or 03 | 72 | "y", "06", //A two digit representation of a year Examples: 99 or 03 |
| 75 | 73 | ||
| 76 | // month | 74 | // month |
| 77 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 | 75 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 |
| 78 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 | 76 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 |
| 79 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec | 77 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec |
| 80 | "F", "January", // A full textual representation of a month, such as January or March January through December | 78 | "F", "January", // A full textual representation of a month, such as January or March January through December |
| 81 | 79 | ||
| 82 | // day | 80 | // day |
| 83 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 | 81 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 |
| ... | @@ -85,24 +83,41 @@ func Date(t time.Time, format string) (datestring string) { | ... | @@ -85,24 +83,41 @@ func Date(t time.Time, format string) (datestring string) { |
| 85 | 83 | ||
| 86 | // week | 84 | // week |
| 87 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun | 85 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun |
| 88 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday | 86 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday |
| 89 | 87 | ||
| 90 | // time | 88 | // time |
| 91 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 | 89 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 |
| 92 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 | 90 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 |
| 93 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 | 91 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 |
| 94 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 | 92 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 |
| 95 | 93 | ||
| 96 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm | 94 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm |
| 97 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM | 95 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM |
| 98 | 96 | ||
| 99 | "i", "04", // Minutes with leading zeros 00 to 59 | 97 | "i", "04", // Minutes with leading zeros 00 to 59 |
| 100 | "s", "05", // Seconds, with leading zeros 00 through 59 | 98 | "s", "05", // Seconds, with leading zeros 00 through 59 |
| 101 | } | 99 | |
| 102 | replacer := strings.NewReplacer(patterns...) | 100 | // time zone |
| 101 | "T", "MST", | ||
| 102 | "P", "-07:00", | ||
| 103 | "O", "-0700", | ||
| 104 | |||
| 105 | // RFC 2822 | ||
| 106 | "r", time.RFC1123Z, | ||
| 107 | } | ||
| 108 | |||
| 109 | // Parse Date use PHP time format | ||
| 110 | func DateParse(dateString, format string) (time.Time, error) { | ||
| 111 | replacer := strings.NewReplacer(DatePatterns...) | ||
| 103 | format = replacer.Replace(format) | 112 | format = replacer.Replace(format) |
| 104 | datestring = t.Format(format) | 113 | return time.ParseInLocation(format, dateString, time.Local) |
| 105 | return | 114 | } |
| 115 | |||
| 116 | // Date takes a PHP like date func to Go's time format | ||
| 117 | func Date(t time.Time, format string) string { | ||
| 118 | replacer := strings.NewReplacer(DatePatterns...) | ||
| 119 | format = replacer.Replace(format) | ||
| 120 | return t.Format(format) | ||
| 106 | } | 121 | } |
| 107 | 122 | ||
| 108 | // Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal. | 123 | // Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal. | ... | ... |
-
Please register or sign in to post a comment