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)
| 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. |
| 36 | func 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 | } |
no outgoing calls