Add convertToTimeExpression function to convert numbers into time expressions

* Add logic to handle 3-digit and 4-digit numbers
* Check if the number is 3 digits with the first digit between 0-9 and the last two digits between 00-60
* Check if the number is 4 digits with the first two digits between 00-24 and the last two digits between 00-59
* Convert the number into a time expression in the format 'HH:MM' if conditions are met
This commit is contained in:
Miwa
2025-03-30 17:04:30 +09:00
parent e8f9eb2f80
commit 38b84fe3e8

View File

@@ -4,8 +4,6 @@ extension KanaKanjiConverter {
func convertToTimeExpression(_ inputData: ComposingText) -> [Candidate] {
var candidates: [Candidate] = []
let numberString = inputData.convertTarget
let firstPart = Int(numberString.prefix(2))!
let secondPart = Int(numberString.suffix(2))!
if numberString.count == 3 {
let firstDigit = Int(numberString.prefix(1))!
@@ -22,8 +20,10 @@ extension KanaKanjiConverter {
candidates.append(candidate)
}
} else if numberString.count == 4 {
if (0...24).contains(firstPart) && (0...59).contains(secondPart) {
let timeExpression = "\(String(format: "%02d", firstPart)):\(String(format: "%02d", secondPart))"
let firstTwoDigits = Int(numberString.prefix(2))!
let lastTwoDigits = Int(numberString.suffix(2))!
if (0...24).contains(firstTwoDigits) && (0...59).contains(lastTwoDigits) {
let timeExpression = "\(String(format: "%02d", firstTwoDigits)):\(String(format: "%02d", lastTwoDigits))"
let candidate = Candidate(
text: timeExpression,
value: -10,