Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
e788fb72
authored
2013-09-11 23:47:01 +0800
by
slene
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
add an DateParse function, parse datestring to time.Time use php date format
1 parent
72f5961d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
18 deletions
utils.go
utils.go
View file @
e788fb7
...
...
@@ -66,18 +66,16 @@ func DateFormat(t time.Time, layout string) (datestring string) {
return
}
// Date takes a PHP like date func to Go's time fomate
func
Date
(
t
time
.
Time
,
format
string
)
(
datestring
string
)
{
patterns
:=
[]
string
{
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
"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
"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
"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
...
...
@@ -85,24 +83,41 @@ func Date(t time.Time, format string) (datestring string) {
// 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
"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
"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
}
replacer
:=
strings
.
NewReplacer
(
patterns
...
)
"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
,
}
// Parse Date use PHP time format
func
DateParse
(
dateString
,
format
string
)
(
time
.
Time
,
error
)
{
replacer
:=
strings
.
NewReplacer
(
DatePatterns
...
)
format
=
replacer
.
Replace
(
format
)
datestring
=
t
.
Format
(
format
)
return
return
time
.
ParseInLocation
(
format
,
dateString
,
time
.
Local
)
}
// Date takes a PHP like date func to Go's time format
func
Date
(
t
time
.
Time
,
format
string
)
string
{
replacer
:=
strings
.
NewReplacer
(
DatePatterns
...
)
format
=
replacer
.
Replace
(
format
)
return
t
.
Format
(
format
)
}
// 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.
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment