-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildin_date.go
71 lines (59 loc) · 1.88 KB
/
buildin_date.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
60
61
62
63
64
65
66
67
68
69
70
71
package template
import (
"log"
"github.com/kaptinlin/filter"
)
func init() {
// Register all date filters
filtersToRegister := map[string]FilterFunc{
"date": dateFilter,
"day": dayFilter,
"month": monthFilter,
"month_full": monthFullFilter,
"year": yearFilter,
"week": weekFilter,
"weekday": weekdayFilter,
"timeago": timeAgoFilter,
}
for name, filterFunc := range filtersToRegister {
if err := RegisterFilter(name, filterFunc); err != nil {
log.Printf("Error registering filter %s: %v", name, err)
}
}
}
// dateFilter formats a timestamp into a specified format.
func dateFilter(value interface{}, args ...string) (interface{}, error) {
format := ""
if len(args) > 0 {
format = args[0]
}
return filter.Date(value, format)
}
// dayFilter extracts and returns the day of the month.
func dayFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Day(value)
}
// monthFilter extracts and returns the month number.
func monthFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Month(value)
}
// monthFullFilter returns the full month name.
func monthFullFilter(value interface{}, args ...string) (interface{}, error) {
return filter.MonthFull(value)
}
// yearFilter extracts and returns the year.
func yearFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Year(value)
}
// weekFilter returns the ISO week number.
func weekFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Week(value)
}
// weekdayFilter returns the day of the week.
func weekdayFilter(value interface{}, args ...string) (interface{}, error) {
return filter.Weekday(value)
}
// timeAgoFilter returns a human-readable string representing the time difference.
func timeAgoFilter(value interface{}, args ...string) (interface{}, error) {
return filter.TimeAgo(value)
}