Skip to content

Commit

Permalink
Implement Date.description in pure Swift
Browse files Browse the repository at this point in the history
# Motivation

The current implementation of `Date.description` is depending on the what platform we are running on. On Darwin it used the CFFoundation backed implementation and on Linux/Windows it used libc APIs to do the date calculations. This caused build issues on Windows and the libc APIs are not 32bit clean.

# Changes

This PR changes the implementation by using the methods implemented in swift-certificates that provide timestamp to UTC time calculations. These were added in swift-certificates for the exact same reason. Furthermore, it removes the conditional usage of CFFoundation since the output is the same.

# Result

Swift only implementation that works cross platform for `Date.description` which also should perform better than any libc API calls.

Align the tests

Code review

Code review

Fix compiler error
  • Loading branch information
FranzBusch committed Oct 5, 2023
1 parent 3d52b25 commit 05da61e
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 52 deletions.
43 changes: 43 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

The SwiftFoundation Project
=====================

Please visit the SwiftFoundation web site for more information:

* https://github.com/apple/swift-foundation

Copyright 2023 The SwiftFoundation Project

The SwiftFoundation Project licenses this file to you under the Apache License,
version 2.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at:

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.

Also, please refer to each LICENSE.txt file, which is located in
the 'license' directory of the distribution file, for the license terms of the
components that this product depends on.

---

This product contains derivations of the time calculations from SwiftCertificates.

* LICENSE (Apache License 2.0):
* https://www.apache.org/licenses/LICENSE-2.0
* HOMEPAGE:
* https://github.com/apple/swift-certificates

---

This product contains code to calculate and decompose UNIX timestamps derived from musl libc.

* LICENSE (MIT):
* https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
* HOMEPAGE:
* https://musl.libc.org
58 changes: 18 additions & 40 deletions Sources/FoundationEssentials/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,14 @@ extension Date {

@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension Date : CustomDebugStringConvertible, CustomStringConvertible, CustomReflectable {
// For backwards compatibility, the Darwin version of this method is left alone
// because it uses `NSDateFormatter` and may behave slightly differently.
#if !FOUNDATION_FRAMEWORK
/// A string representation of the date object (read-only).
/// The representation is useful for debugging only.
/// The representation is useful for debugging only and might change over time. It currently returns
/// a representation in UTC time with a Gregorian calendar.
/// There are a number of options to acquire a formatted string for a date including: date formatters
/// (see [NSDateFormatter](//apple_ref/occ/cl/NSDateFormatter) and
/// [Data Formatting Guide](//apple_ref/doc/uid/10000029i)), and the `Date`
/// function `description(locale:)`.
public var description: String {
// NSDate uses the constant format `uuuu-MM-dd HH:mm:ss '+0000'`

// Glibc needs a non-standard format option to pad %Y to 4 digits
#if canImport(Glibc)
let format = "%4Y-%m-%d %H:%M:%S +0000"
#else
let format = "%Y-%m-%d %H:%M:%S +0000"
#endif
let unavailable = "<description unavailable>"

guard self >= Date.distantPast else {
Expand All @@ -260,36 +250,12 @@ extension Date : CustomDebugStringConvertible, CustomStringConvertible, CustomRe
return unavailable
}

var info = tm()
#if os(Windows)
var time = __time64_t(self.timeIntervalSince1970)
let errno: errno_t = _gmtime64_s(&info, &time)
guard errno == 0 else { return unavailable }
#else
var time = time_t(self.timeIntervalSince1970)
gmtime_r(&time, &info)
#endif

// This allocates stack space for range of 10^102 years
// That's more than Date currently supports.
let bufferSize = 128
return withUnsafeTemporaryAllocation(of: CChar.self, capacity: bufferSize) { buffer in
guard let ptr = buffer.baseAddress else {
return unavailable
}
// We are casting to Int64 here which means we are rounding down.
// This is fine since we only need second granularity here for the description
let (year, month, day, hours, minutes, seconds) = Int64(self.timeIntervalSince1970).utcDateFromTimestamp

guard strftime(ptr, bufferSize, format, &info) != 0 else {
return unavailable
}

guard let result = String(validatingUTF8: ptr) else {
return unavailable
}

return result
}
return "\(_zeroPad: year, _toWidth: 4)-\(_zeroPad: month, _toWidth: 2)-\(_zeroPad: day, _toWidth: 2) \(_zeroPad: hours, _toWidth: 2):\(_zeroPad: minutes, _toWidth: 2):\(_zeroPad: seconds, _toWidth: 2) +0000"
}
#endif // !FOUNDATION_FRAMEWORK

public var debugDescription: String {
return description
Expand Down Expand Up @@ -388,3 +354,15 @@ extension Date {
Date.validCalendarRange.contains(self)
}
}
// MARK: - String interpolation helper for description property
extension DefaultStringInterpolation {
fileprivate mutating func appendInterpolation(_zeroPad value: Int, _toWidth width: Int) {
precondition(width > 0)
let representation = String(value)
let padding = width &- representation.utf8.count
if padding > 0 {
appendLiteral(String(repeating: "0", count: padding))
}
appendLiteral(representation)
}
}
Loading

0 comments on commit 05da61e

Please sign in to comment.