-
Notifications
You must be signed in to change notification settings - Fork 63
/
formatdate.go
62 lines (50 loc) · 1.48 KB
/
formatdate.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
package datetime
import (
"fmt"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
"github.com/project-flogo/core/support/log"
"strings"
)
// Deprecated
type FormatDate struct {
}
func init() {
function.Register(&FormatDate{})
}
func (s *FormatDate) Name() string {
return "formatDate"
}
func (s *FormatDate) GetCategory() string {
return "datetime"
}
func (s *FormatDate) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeDateTime, data.TypeString}, false
}
func (s *FormatDate) Eval(params ...interface{}) (interface{}, error) {
date, err := coerce.ToDateTime(params[0])
if err != nil {
return nil, fmt.Errorf("Format date first argument must be string")
}
format, err := coerce.ToString(params[1])
if err != nil {
return nil, fmt.Errorf("Format date second argument must be string")
}
format = convertDateFormater(format)
log.RootLogger().Debugf("Format date %s to format %s", date, format)
return date.Format(format), nil
}
func convertDateFormater(format string) string {
lowerFormat := strings.ToLower(format)
if strings.Contains(lowerFormat, "yyyy") {
lowerFormat = strings.Replace(lowerFormat, "yyyy", "2006", -1)
}
if strings.Contains(lowerFormat, "mm") {
lowerFormat = strings.Replace(lowerFormat, "mm", "01", -1)
}
if strings.Contains(lowerFormat, "dd") {
lowerFormat = strings.Replace(lowerFormat, "dd", "02", -1)
}
return lowerFormat
}