From c28e92a9932622f44d283e86b049819794e500da Mon Sep 17 00:00:00 2001 From: Michele Campeotto Date: Mon, 28 Oct 2024 22:39:01 +0100 Subject: [PATCH] generate default text values for current date, time, and timestamp fixes #34 --- .../LighterCodeGenAST/Nodes/Expression.swift | 25 +++++++++++++++++++ .../GenerateDefaultValues.swift | 24 ++++-------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift b/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift index 1525a8c..552f718 100644 --- a/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift +++ b/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift @@ -291,6 +291,31 @@ public extension Expression { } } + +// MARK: - Date Formatting Convenience + +public extension Expression { + static func formattedCurrentDate(format: String) -> Self { + .inlineClosureCall([ + .let("fmt", is: .call(name: "DateFormatter")), + .set(instance: "fmt", "locale", + .call(name: "Locale", parameters: [ + ("identifier", .string("en_US_POSIX")) + ])), + .set(instance: "fmt", "timeZone", + .call(name: "TimeZone", parameters: [ + ("secondsFromGMT", .integer(0)) + ])), + .set(instance: "fmt", "dateFormat", .string(format)), + .return(.call(instance: "fmt", + name: "string", + parameters: [ + ("from", .call(name: "Date")) + ])) + ]) + } +} + #if swift(>=5.5) extension Expression : Sendable {} extension FunctionCall : Sendable {} diff --git a/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift b/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift index 9815d05..84e42a6 100644 --- a/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift +++ b/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift @@ -147,22 +147,16 @@ extension EnlighterASTGenerator { .data, .uuid, .custom: return cannotConvert() case .string: - /* TODO: https://github.com/Lighter-swift/Lighter/issues/34 - needs to generate this: - var fmt = DateFormatter() - fmt.locale = Locale(identifier: "en_US_POSIX") - fmt.dateFormat = "yyyy-MM-dd" // "HH:mm:ss" - fmt.timeZone = TimeZone(secondsFromGMT: 0) - Date().string(from: Date()) - */ - return cannotConvert() + return .formattedCurrentDate(format: (value == .currentDate + ? "yyyy-MM-dd" + : "HH:mm:ss")) } case .currentTimestamp: switch property.propertyType { case .decimal, .bool, .url, .uint8Array, .data, .uuid, .custom: return cannotConvert() case .date: - return .call(name: "Foundation.Date()") + return .call(name: "Foundation.Date") case .double: return .variableReference( instance: "Foundation.Date()", name: "timeIntervalSince1970") @@ -173,15 +167,7 @@ extension EnlighterASTGenerator { to: .int ) case .string: - /* TODO: https://github.com/Lighter-swift/Lighter/issues/34 - needs to generate this: - var fmt = DateFormatter() - fmt.locale = Locale(identifier: "en_US_POSIX") - fmt.dateFormat = "yyyy-MM-dd HH:mm:ss" - fmt.timeZone = TimeZone(secondsFromGMT: 0) - Date().string(from: Date()) - */ - return cannotConvert() + return .formattedCurrentDate(format: "yyyy-MM-dd HH:mm:ss") } } }