add an DateParse function, parse datestring to time.Time use php date format
Showing
1 changed file
with
51 additions
and
36 deletions
| ... | @@ -66,43 +66,58 @@ func DateFormat(t time.Time, layout string) (datestring string) { | ... | @@ -66,43 +66,58 @@ 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) { | 70 | // year |
| 71 | patterns := []string{ | 71 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 |
| 72 | // year | 72 | "y", "06", //A two digit representation of a year Examples: 99 or 03 |
| 73 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 | 73 | |
| 74 | "y", "06", //A two digit representation of a year Examples: 99 or 03 | 74 | // month |
| 75 | 75 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 | |
| 76 | // month | 76 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 |
| 77 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 | 77 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec |
| 78 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 | 78 | "F", "January", // A full textual representation of a month, such as January or March January through December |
| 79 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec | 79 | |
| 80 | "F", "January", // A full textual representation of a month, such as January or March January through December | 80 | // day |
| 81 | 81 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 | |
| 82 | // day | 82 | "j", "2", // Day of the month without leading zeros 1 to 31 |
| 83 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 | 83 | |
| 84 | "j", "2", // Day of the month without leading zeros 1 to 31 | 84 | // week |
| 85 | 85 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun | |
| 86 | // week | 86 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday |
| 87 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun | 87 | |
| 88 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday | 88 | // time |
| 89 | 89 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 | |
| 90 | // time | 90 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 |
| 91 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 | 91 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 |
| 92 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 | 92 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 |
| 93 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 | 93 | |
| 94 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 | 94 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm |
| 95 | 95 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM | |
| 96 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm | 96 | |
| 97 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM | 97 | "i", "04", // Minutes with leading zeros 00 to 59 |
| 98 | 98 | "s", "05", // Seconds, with leading zeros 00 through 59 | |
| 99 | "i", "04", // Minutes with leading zeros 00 to 59 | 99 | |
| 100 | "s", "05", // Seconds, with leading zeros 00 through 59 | 100 | // time zone |
| 101 | } | 101 | "T", "MST", |
| 102 | replacer := strings.NewReplacer(patterns...) | 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