4
4
5
5
⏰🇬🇧 Recognize British English time and try converting it to ` Date `
6
6
7
+ This package contains two libraries that converts a British spoken time like ` "It's sevent o'clock." ` to an optional ` Date ` from ** epoch** where you can easily extract hour and minute ` DateComponents ` if you want.
8
+
9
+ * ` SwiftToTen ` The main library and has a static function with this signature: ` live(time: String, calendar: Calendar) -> Date? ` where you simply providing a time like in the example above, see examples below
10
+ * ` SwiftToTenDependency ` A wrapper around the library above facilitating the integration with [ Point-Free Dependencies] ( https://github.com/pointfreeco/swift-dependencies ) library or a project made with The Composable Architecture (TCA).
11
+
7
12
## Usage
8
13
9
14
``` swift
@@ -16,44 +21,85 @@ calendar.timeZone = TimeZone(secondsFromGMT: 0) ?? calendar.timeZone
16
21
var recognizedTime: Date?
17
22
18
23
// Classic time with HH:mm format
19
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's 12:34" , calendar : calendar)
24
+ recognizedTime = SwiftToTen.live ( time : " It's 12:34" , calendar : calendar)
20
25
print (recognizedTime) // Optional(1970-01-01 12:34:00 +0000)
21
26
22
27
// In the afternoon HH:mm pm format
23
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's 1:37 pm" , calendar : calendar)
28
+ recognizedTime = SwiftToTen.live ( time : " It's 1:37 pm" , calendar : calendar)
24
29
print (recognizedTime) // Optional(1970-01-01 13:37:00 +0000)
25
30
26
31
// With o'clock format
27
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's 7 o'clock" , calendar : calendar)
32
+ recognizedTime = SwiftToTen.live ( time : " It's 7 o'clock" , calendar : calendar)
28
33
print (recognizedTime) // Optional(1970-01-01 07:00:00 +0000)
29
34
30
35
// With o'clock format in the afternoon
31
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's 2 o'clock in the afternoon" , calendar : calendar)
36
+ recognizedTime = SwiftToTen.live ( time : " It's 2 o'clock in the afternoon" , calendar : calendar)
32
37
print (recognizedTime) // Optional(1970-01-01 14:00:00 +0000)
33
38
34
39
// Midnight, ... to Midnight and ... past Midnight
35
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's midnight" , calendar : calendar)
40
+ recognizedTime = SwiftToTen.live ( time : " It's midnight" , calendar : calendar)
36
41
print (recognizedTime) // Optional(1970-01-01 00:00:00 +0000)
37
42
38
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's quarter to midnight" , calendar : calendar)
43
+ recognizedTime = SwiftToTen.live ( time : " It's quarter to midnight" , calendar : calendar)
39
44
print (recognizedTime) // Optional(1970-01-01 23:45:00 +0000)
40
45
41
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's 10 past midnight" , calendar : calendar)
46
+ recognizedTime = SwiftToTen.live ( time : " It's 10 past midnight" , calendar : calendar)
42
47
print (recognizedTime) // Optional(1970-01-01 00:10:00 +0000)
43
48
44
49
// If the string doesn't contain a recognizable time, it returns `nil`
45
- recognizedTime = SwiftToTen.recognizeTime ( in : " It's show time!" , calendar : calendar)
50
+ recognizedTime = SwiftToTen.live ( time : " It's show time!" , calendar : calendar)
46
51
print (recognizedTime) // nil
47
52
```
48
53
54
+ ## [ Point-Free Dependencies] ( https://github.com/pointfreeco/swift-dependencies ) usage
55
+
56
+ Add ` @Dependency(\.recognizeTime) var recognizeTime ` in your ` Reducer ` , you will have access to all functions mentioned above.
57
+
58
+ ### Example
59
+
60
+ ``` swift
61
+ import ComposableArchitecture
62
+ import Foundation
63
+ import SwiftToTenDependency
64
+
65
+ public struct BritishTime : ReducerProtocol {
66
+ public struct State : Equatable {
67
+ public var date: Date?
68
+
69
+ public init (date : Date? = nil ) {
70
+ self .date = date
71
+ }
72
+ }
73
+
74
+ public enum Action : Equatable {
75
+ case utteranceChanged (String )
76
+ }
77
+
78
+ @Dependency (\.calendar ) var calendar
79
+ @Dependency (\.recognizeTime ) var recognizeTime
80
+
81
+ public init () {}
82
+
83
+ public var body: some ReducerProtocol<State, Action> {
84
+ Reduce { state, action in
85
+ switch action {
86
+ case let .utteranceChanged (utterance):
87
+ state.date = recognizeTime (time : utterance, calendar : calendar)
88
+ return .none
89
+ }
90
+ }
91
+ }
92
+ ```
93
+
49
94
## Installation
50
95
51
96
### Xcode
52
97
53
98
You can add SwiftToTen to an Xcode project by adding it as a package dependency.
54
99
55
100
1 . From the ** File** menu, select ** Swift Packages › Add Package Dependency...**
56
- 2 . Enter "https://github.com/renaudjenny/swift-to-ten " into the package repository URL test field
101
+ 2 . Enter " https://github.com/renaudjenny/swift-to-ten" into the package repository URL text field
102
+ 3 . Select one of the library that you are interested in . See [above](#swifttoten )
57
103
58
104
### As package dependency
59
105
@@ -63,13 +109,16 @@ Edit your `Package.swift` to add this library.
63
109
let package = Package (
64
110
...
65
111
dependencies: [
66
- .package (url : " https://github.com/renaudjenny/swift-to-ten" , from : " 1.1 .0" ),
112
+ .package (url : " https://github.com/renaudjenny/swift-to-ten" , from : " 1.2 .0" ),
67
113
...
68
114
],
69
115
targets : [
70
116
.target (
71
117
name : " <Your project name>" ,
72
- dependencies : [.product (name : " SwiftToTen" , package : " swift-to-ten" )]
118
+ dependencies : [
119
+ .product (name : " SwiftToTen" , package : " swift-to-ten" ), // <-- Basic version
120
+ .product (name : " SwiftToTenDependency" , package : " swift-to-ten" ), // <-- Point-Free Dependencies library wrapper
121
+ ]
73
122
),
74
123
...
75
124
]
0 commit comments