forked from merlos/iOS-Open-GPX-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultDateFormat.swift
64 lines (55 loc) · 2.46 KB
/
DefaultDateFormat.swift
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
63
64
//
// DefaultDateFormat.swift
// OpenGpxTracker
//
// Created by Vincent on 4/3/20.
//
import Foundation
/// Handles processing of 'unprocessed' user input date format, processing of sample date format, etc
class DefaultDateFormat {
/// DateFormatter for use in each instance.
let dateFormatter = DateFormatter()
/// returns a 'processed', `DateFormatter`-friendly date format.
func getDateFormat(unprocessed: String) -> String {
var newText = ""
// prevents acknowledging unterminated date formats as valid
if (unprocessed.countInstances(of: "{") != unprocessed.countInstances(of: "}"))
|| unprocessed.countInstances(of: "{}") > 0 {
newText = "'invalid'"
}
else {
let arr = unprocessed.components(separatedBy: CharacterSet(charactersIn: "{}"))
var lastField: String?
let arrCount = arr.count
for i in 0...arrCount - 1 {
if let lastField = lastField, lastField.countInstances(of: String(arr[i].last ?? Character(" "))) > 0 {
newText = "'invalid: { ... } must not consecutively repeat'"
break
}
if arr.count == 1 {
newText += "'invalid'"
}
else if arrCount > 1 && !arr[i].isEmpty {
newText += (i % 2 == 0) ? "'\(arr[i])'" : arr[i]
lastField = (i % 2 != 0) ? arr[i] : nil
}
}
}
return newText
}
/// Returns sample date time based on user input.
func getDate(processedFormat dateFormat: String, useUTC: Bool = false, useENLocale: Bool = false) -> String {
//processedDateFormat = DefaultDateFormat.getDateFormat(unprocessed: self.cellTextField.text!)
dateFormatter.dateFormat = dateFormat
dateFormatter.timeZone = useUTC ? TimeZone(secondsFromGMT: 0) : TimeZone.current
dateFormatter.locale = useENLocale ? Locale(identifier: "en_US_POSIX") : Locale.current
return dateFormatter.string(from: Date())
}
/// Returns Preference stored date format and its settings.
func getDateFromPrefs() -> String {
let dateFormat = Preferences.shared.dateFormat
let useUTC = Preferences.shared.dateFormatUseUTC
let useEN = Preferences.shared.dateFormatUseEN
return getDate(processedFormat: dateFormat, useUTC: useUTC, useENLocale: useEN)
}
}