Skip to content

Commit

Permalink
Merge pull request #38 from micampe/develop
Browse files Browse the repository at this point in the history
generate default text values for current date, time, and timestamp
  • Loading branch information
helje5 authored Nov 4, 2024
2 parents b74efa0 + c28e92a commit 27c3e64
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
25 changes: 25 additions & 0 deletions Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
}
}
Expand Down

0 comments on commit 27c3e64

Please sign in to comment.