parseTaskTime converts a flexible time string into the Task API due/start object format.
(timeStr string)
| 34 | |
| 35 | // parseTaskTime converts a flexible time string into the Task API due/start object format. |
| 36 | func parseTaskTime(timeStr string) (map[string]interface{}, error) { |
| 37 | var msTs string |
| 38 | timeStr = strings.TrimSpace(timeStr) |
| 39 | |
| 40 | // snapDay aligns to start-of-day or end-of-day based on hint. |
| 41 | snapDay := func(t time.Time) time.Time { |
| 42 | return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) |
| 43 | } |
| 44 | |
| 45 | if isRelativeTime(timeStr) { |
| 46 | t, err := parseRelativeTime(timeStr) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | if strings.HasSuffix(timeStr, "d") || strings.HasSuffix(timeStr, "w") { |
| 51 | msTs = fmt.Sprintf("%d", snapDay(t).Unix()*1000) |
| 52 | } else { |
| 53 | msTs = fmt.Sprintf("%d", t.Unix()*1000) |
| 54 | } |
| 55 | } else { |
| 56 | parsedTs, err := common.ParseTime(timeStr) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | var sec int64 |
| 61 | fmt.Sscanf(parsedTs, "%d", &sec) |
| 62 | msTs = fmt.Sprintf("%d", sec*1000) |
| 63 | } |
| 64 | |
| 65 | // Determine if it's an all-day event based on the input format |
| 66 | isAllDay := false |
| 67 | // YYYY-MM-DD or relative like +2d typically mean all-day |
| 68 | if len(timeStr) == 10 && strings.Count(timeStr, "-") == 2 { |
| 69 | isAllDay = true |
| 70 | } else if strings.HasPrefix(timeStr, "+") && (strings.HasSuffix(timeStr, "d") || strings.HasSuffix(timeStr, "w")) { |
| 71 | isAllDay = true |
| 72 | } |
| 73 | |
| 74 | return map[string]interface{}{ |
| 75 | "timestamp": msTs, |
| 76 | "is_all_day": isAllDay, |
| 77 | }, nil |
| 78 | } |
| 79 | |
| 80 | // extractTasklistGuid extracts the GUID from an applink URL or returns the string if it's already an ID. |
| 81 | func extractTasklistGuid(input string) string { |
no test coverage detected