-
Notifications
You must be signed in to change notification settings - Fork 1
/
datetime.go
59 lines (51 loc) · 1.64 KB
/
datetime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package goease
import "time"
// ParseRFC3339Date parses a date string in RFC3339 format.
//
// Parameters:
// - dateStr: string - The date string to parse.
//
// Returns:
// - time.Time: The parsed time if successful, otherwise a zero time.
func ParseRFC3339Date(dateStr string) time.Time {
if dateStr == "" {
return time.Time{} // Return a zero time for empty string
}
parsedTime, err := time.Parse(time.RFC3339, dateStr)
if err != nil {
// Handle the error here, depending on your application logic
// For example, log the error or return a default value
return time.Time{} // Return a zero time for invalid input
}
return parsedTime
}
// ParseCustomDate parses a date string in a custom format.
//
// Parameters:
// - dateStr: string - The date string to parse.
// - layout: string - The layout to use for parsing.
//
// Returns:
// - time.Time: The parsed time if successful, otherwise a zero time.
func ParseCustomDate(dateStr, layout string) time.Time {
if dateStr == "" {
return time.Time{} // Return a zero time for empty string
}
parsedTime, err := time.Parse(layout, dateStr)
if err != nil {
// Handle the error here, depending on your application logic
// For example, log the error or return a default value
return time.Time{} // Return a zero time for invalid input
}
return parsedTime
}
// ParseISO8601Date parses a date string in ISO8601 format.
//
// Parameters:
// - dateStr: string - The date string to parse.
//
// Returns:
// - time.Time: The parsed time if successful, otherwise a zero time.
func ParseISO8601Date(dateStr string) time.Time {
return ParseCustomDate(dateStr, "2006-01-02T15:04:05Z07:00")
}