MCPcopy Index your code
hub / github.com/apache/devlake / GetTimeFieldFromMap

Function GetTimeFieldFromMap

backend/helpers/utils/field.go:36–63  ·  view source on GitHub ↗

GetTimeFieldFromMap retrieves a time field from a map. allFields: A map containing all fields. fieldName: The name of the field to retrieve. loc: The timezone location. Returns: *time.Time: A pointer to the time.Time if the field exists and can be converted to time.Time. error: An error if the fi

(allFields map[string]interface{}, fieldName string, loc *time.Location)

Source from the content-addressed store, hash-verified

34// *time.Time: A pointer to the time.Time if the field exists and can be converted to time.Time.
35// error: An error if the field does not exist or an error occurs.
36func GetTimeFieldFromMap(allFields map[string]interface{}, fieldName string, loc *time.Location) (*time.Time, error) {
37 val, ok := allFields[fieldName]
38 if !ok {
39 return nil, fmt.Errorf("Field %s not found", fieldName)
40 }
41 var temp time.Time
42 switch v := val.(type) {
43 case string:
44 if v == "" || v == "null" || v == "{}" {
45 // In Zentao, the field `deadline`'s value may be "{}".
46 return nil, nil
47 }
48 // If value is a string with the format yyyy-MM-dd, use loc to parse it
49 isDateFormat, _ := regexp.Match(`\d{4}-\d{2}-\d{2}`, []byte(v))
50 if isDateFormat {
51 temp, _ = common.ConvertStringToTimeInLoc(v, loc)
52 } else {
53 temp, _ = common.ConvertStringToTime(v)
54 }
55
56 default:
57 temp, _ = v.(time.Time)
58 }
59 if temp.IsZero() {
60 return nil, nil
61 }
62 return &temp, nil
63}

Callers 1

TestGetTimeFeildFromMapFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestGetTimeFeildFromMapFunction · 0.68