Home

Awesome

SwiftToTen

Swift Unit Tests

⏰🇬🇧 Recognize British English time and try converting it to Date

This package contains two libraries that converts a British spoken time like "It's seven o'clock." to an optional Date from epoch where you can easily extract hour and minute DateComponents if you want.

Usage

import SwiftToTen

// Set the Calendar to your convenience
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(secondsFromGMT: 0) ?? calendar.timeZone

var recognizedTime: Date?

// Classic time with HH:mm format
recognizedTime = SwiftToTen.live(time: "It's 12:34", calendar: calendar)
print(recognizedTime) // Optional(1970-01-01 12:34:00 +0000)

// In the afternoon HH:mm pm format
recognizedTime = SwiftToTen.live(time: "It's 1:37 pm", calendar: calendar)
print(recognizedTime) // Optional(1970-01-01 13:37:00 +0000)

// With o'clock format
recognizedTime = SwiftToTen.live(time: "It's 7 o'clock", calendar: calendar)
print(recognizedTime) // Optional(1970-01-01 07:00:00 +0000)

// With o'clock format in the afternoon
recognizedTime = SwiftToTen.live(time: "It's 2 o'clock in the afternoon", calendar: calendar)
print(recognizedTime) // Optional(1970-01-01 14:00:00 +0000)

// Midnight, ... to Midnight and ... past Midnight
recognizedTime = SwiftToTen.live(time: "It's midnight", calendar: calendar)
print(recognizedTime) // Optional(1970-01-01 00:00:00 +0000)

recognizedTime = SwiftToTen.live(time: "It's quarter to midnight", calendar: calendar)
print(recognizedTime) // Optional(1970-01-01 23:45:00 +0000)

recognizedTime = SwiftToTen.live(time: "It's 10 past midnight", calendar: calendar)
print(recognizedTime) // Optional(1970-01-01 00:10:00 +0000)

// If the string doesn't contain a recognizable time, it returns `nil`
recognizedTime = SwiftToTen.live(time: "It's show time!", calendar: calendar)
print(recognizedTime) // nil

Point-Free Dependencies usage

Add @Dependency(\.recognizeTime) var recognizeTime in your Reducer, you will have access to all functions mentioned above.

Example

import ComposableArchitecture
import Foundation
import SwiftToTenDependency

public struct BritishTime: ReducerProtocol {
    public struct State: Equatable {
        public var date: Date?

        public init(date: Date? = nil) {
            self.date = date
        }
    }

    public enum Action: Equatable {
        case utteranceChanged(String)
    }

    @Dependency(\.calendar) var calendar
    @Dependency(\.recognizeTime) var recognizeTime

    public init() {}

    public var body: some ReducerProtocol<State, Action> {
        Reduce { state, action in
            switch action {
            case let .utteranceChanged(utterance):
                state.date = recognizeTime(time: utterance, calendar: calendar)
                return .none
        }
    }
}

Installation

Xcode

You can add SwiftToTen to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency...
  2. Enter "https://github.com/renaudjenny/swift-to-ten" into the package repository URL text field
  3. Select one of the library that you are interested in. See above

As package dependency

Edit your Package.swift to add this library.

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/renaudjenny/swift-to-ten", from: "1.2.0"),
        ...
    ],
    targets: [
        .target(
            name: "<Your project name>",
            dependencies: [
                .product(name: "SwiftToTen", package: "swift-to-ten"), // <-- Basic version
                .product(name: "SwiftToTenDependency", package: "swift-to-ten"), // <-- Point-Free Dependencies library wrapper
            ]
        ),
        ...
    ]
)

App using this library