Copilot commented on code in PR #33:
URL: https://github.com/apache/arrow-swift/pull/33#discussion_r2141364648
##########
Arrow/Sources/Arrow/ArrowArray.swift:
##########
@@ -233,6 +235,71 @@ public class Date64Array: ArrowArray<Date> {
public class Time32Array: FixedArray<Time32> {}
public class Time64Array: FixedArray<Time64> {}
+public class TimestampArray: FixedArray<Timestamp> {
+
+ public struct FormattingOptions {
+ public var dateFormat: String = "yyyy-MM-dd HH:mm:ss.SSS"
+ public var locale: Locale = .current
+ public var includeTimezone: Bool = true
+ public var fallbackToRaw: Bool = true
+
+ public init(dateFormat: String = "yyyy-MM-dd HH:mm:ss.SSS",
+ locale: Locale = .current,
+ includeTimezone: Bool = true,
+ fallbackToRaw: Bool = true) {
+ self.dateFormat = dateFormat
+ self.locale = locale
+ self.includeTimezone = includeTimezone
+ self.fallbackToRaw = fallbackToRaw
+ }
+ }
+
+ public func formattedDate(at index: UInt, options: FormattingOptions =
FormattingOptions()) -> String? {
+ guard let timestamp = self[index] else { return nil }
+
+ guard let timestampType = self.arrowData.type as? ArrowTypeTimestamp
else {
+ return options.fallbackToRaw ? "\(timestamp)" : nil
+ }
+
+ let date = dateFromTimestamp(timestamp, unit: timestampType.unit)
+
+ let formatter = DateFormatter()
+ formatter.dateFormat = options.dateFormat
+ formatter.locale = options.locale
+
+ if options.includeTimezone, let timezone = timestampType.timezone {
+ formatter.timeZone = TimeZone(identifier: timezone)
+ }
+
+ return formatter.string(from: date)
Review Comment:
Creating a new DateFormatter for each call of formattedDate(at:) may impact
performance in tight loops; consider caching a formatter instance if possible.
```suggestion
if cachedFormatter == nil || cachedOptions != options {
let formatter = DateFormatter()
formatter.dateFormat = options.dateFormat
formatter.locale = options.locale
if options.includeTimezone, let timezone =
timestampType.timezone {
formatter.timeZone = TimeZone(identifier: timezone)
}
cachedFormatter = formatter
cachedOptions = options
}
return cachedFormatter?.string(from: date)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]