Skip to content

Commit 4e6302f

Browse files
committed
added secondsOfDay option for time format
1 parent c01f679 commit 4e6302f

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

data/map.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ func (s *Map) Replace(key, val string) {
7676

7777
// Has returns true if the provided key is present
7878
func (s *Map) Has(key string) bool {
79+
if index := strings.LastIndex(key, "("); index != -1 {
80+
key = key[:index]
81+
}
7982
_, found := (*s)[key]
8083
return found
8184
}
8285

8386
// Get returns a value for provided key
8487
func (s *Map) Get(key string) interface{} {
88+
if index := strings.LastIndex(key, "("); index != -1 {
89+
key = key[:index]
90+
}
8591
if result, found := (*s)[key]; found {
8692
return result
8793
}
@@ -145,19 +151,15 @@ func (s *Map) GetValue(expr string) (interface{}, bool) {
145151
}
146152
}
147153
isLast := i+1 == len(fragments)
148-
149154
hasKey := state.Has(fragment)
150155
if !hasKey {
151156
return nil, false
152157
}
153-
154158
var candidate = state.Get(fragment)
155159
if !isLast && candidate == nil {
156160
return nil, false
157161
}
158-
159162
if index != nil {
160-
161163
if intIndex, err := toolbox.ToInt(index); err == nil {
162164
if !toolbox.IsSlice(candidate) {
163165
return nil, false

data/udf/time.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"fmt"
55
"github.com/viant/toolbox"
66
"github.com/viant/toolbox/data"
7+
"strings"
78
"time"
89
)
910

10-
//FormatTime return formatted time, it takes an array of arguments, the first is time express, or now followed by java style time format, optional timezone and truncate format .
11+
// FormatTime return formatted time, it takes an array of arguments, the first is time express, or now followed by java style time format, optional timezone and truncate format .
1112
func FormatTime(source interface{}, state data.Map) (interface{}, error) {
1213
if !toolbox.IsSlice(source) {
1314
return nil, fmt.Errorf("unable to run FormatTime: expected %T, but had: %T", []interface{}{}, source)
@@ -18,16 +19,18 @@ func FormatTime(source interface{}, state data.Map) (interface{}, error) {
1819
}
1920
var err error
2021
var timeText = toolbox.AsString(aSlice[0])
21-
var timeFormat = toolbox.AsString(aSlice[1])
22-
var timeLayout = toolbox.DateFormatToLayout(timeFormat)
2322
var timeValue *time.Time
2423
timeValue, err = toolbox.TimeAt(timeText)
24+
var timeLayout string
2525
if err != nil {
26+
timeFormat := toolbox.AsString(aSlice[1])
27+
timeLayout = toolbox.DateFormatToLayout(timeFormat)
2628
timeValue, err = toolbox.ToTime(aSlice[0], timeLayout)
2729
}
2830
if err != nil {
2931
return nil, err
3032
}
33+
3134
if len(aSlice) > 2 && aSlice[2] != "" {
3235
timeLocation, err := time.LoadLocation(toolbox.AsString(aSlice[2]))
3336
if err != nil {
@@ -37,13 +40,25 @@ func FormatTime(source interface{}, state data.Map) (interface{}, error) {
3740
timeValue = &timeInLocation
3841
}
3942

43+
if len(aSlice) > 1 {
44+
formatArg := toolbox.AsString(aSlice[1])
45+
switch strings.Trim(formatArg, `" ,`) {
46+
case "secondsOfDay":
47+
return timeValue.Hour()*60 + timeValue.Second(), nil
48+
}
49+
if timeLayout == "" {
50+
timeFormat := toolbox.AsString(aSlice[1])
51+
timeLayout = toolbox.DateFormatToLayout(timeFormat)
52+
}
53+
}
54+
4055
if len(aSlice) > 3 {
4156
switch aSlice[3] {
4257
case "weekday":
4358
return timeValue.Weekday(), nil
4459
default:
4560
truncFromat := toolbox.DateFormatToLayout(toolbox.AsString(aSlice[3]))
46-
if ts, err := time.Parse(truncFromat, timeValue.Format(truncFromat));err == nil {
61+
if ts, err := time.Parse(truncFromat, timeValue.Format(truncFromat)); err == nil {
4762
timeValue = &ts
4863
}
4964
}
@@ -52,7 +67,7 @@ func FormatTime(source interface{}, state data.Map) (interface{}, error) {
5267
return timeValue.Format(timeLayout), nil
5368
}
5469

55-
//Elapsed returns elapsed time
70+
// Elapsed returns elapsed time
5671
func Elapsed(source interface{}, state data.Map) (interface{}, error) {
5772
inThePast, err := toolbox.ToTime(source, time.RFC3339)
5873
if err != nil {

data/udf/time_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func Test_FormatTime(t *testing.T) {
4141
now := time.Now()
4242
assert.Equal(t, now.Year(), toolbox.AsInt(value))
4343
}
44+
4445
{
4546
aMap := data.NewMap()
4647
aMap.Put("ts", "2015-02-11")

time_format.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"time"
66
)
77

8-
//DateFormatKeyword constant 'dateFormat' key
8+
// DateFormatKeyword constant 'dateFormat' key
99
var DateFormatKeyword = "dateFormat"
1010

11-
//DateLayoutKeyword constant 'dateLayout' key
11+
// DateLayoutKeyword constant 'dateLayout' key
1212
var DateLayoutKeyword = "dateLayout"
1313

14-
//DateFormatToLayout converts java date format https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#rfc822timezone into go date layout
14+
// DateFormatToLayout converts java date format https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#rfc822timezone into go date layout
1515
func DateFormatToLayout(dateFormat string) string {
1616

1717
dateFormat = strings.Replace(dateFormat, "ddd", "_2", 1)
@@ -53,12 +53,11 @@ func DateFormatToLayout(dateFormat string) string {
5353

5454
dateFormat = strings.Replace(dateFormat, "EEEE", "Monday", 1)
5555
dateFormat = strings.Replace(dateFormat, "E", "Mon", 1)
56-
5756
return dateFormat
5857
}
5958

60-
//GetTimeLayout returns time laout from passed in map, first it check if DateLayoutKeyword is defined is so it returns it, otherwise it check DateFormatKeyword and if exists converts it to dateLayout
61-
//If neithers keys exists it panics, please use HasTimeLayout to avoid panic
59+
// GetTimeLayout returns time laout from passed in map, first it check if DateLayoutKeyword is defined is so it returns it, otherwise it check DateFormatKeyword and if exists converts it to dateLayout
60+
// If neithers keys exists it panics, please use HasTimeLayout to avoid panic
6261
func GetTimeLayout(input interface{}) string {
6362
switch settings := input.(type) {
6463
case map[string]string:
@@ -83,7 +82,7 @@ func GetTimeLayout(input interface{}) string {
8382
return ""
8483
}
8584

86-
//HasTimeLayout checks if dateLayout can be taken from the passed in setting map
85+
// HasTimeLayout checks if dateLayout can be taken from the passed in setting map
8786
func HasTimeLayout(input interface{}) bool {
8887
switch settings := input.(type) {
8988
case map[string]string:
@@ -106,7 +105,7 @@ func HasTimeLayout(input interface{}) bool {
106105
return false
107106
}
108107

109-
//TimestampToString formats timestamp to passed in java style date format
108+
// TimestampToString formats timestamp to passed in java style date format
110109
func TimestampToString(dateFormat string, unixTimestamp, unixNanoTimestamp int64) string {
111110
t := time.Unix(unixTimestamp, unixNanoTimestamp)
112111
dateLayout := DateFormatToLayout(dateFormat)

0 commit comments

Comments
 (0)