Tina Liu 25014fcaa0
Add Duration FormatStyle(s) to swift-foundation (#278)
* Add Duration formatting and utility files to FoundationInternationalization.

This commit moves existing files as-is to FoundationInternationalization. We'll address compatibility fixes in the following commits.

* Add partial implementation for Measurement.FormatStyle

`Duration.FormatStyle` uses `Measurement.FormatStyle.UnitWidth` internally. This PR adds a partial implementation for `Measurement.FormatStyle` to maintain source compatibility for `Duration.FormatStyle`.

`Measurement.FormatStyle` is built on top of `struct Measurement<Dimension>`, which isn't available in the package yet. To unblock ourselves, add stubs for relevant types if they're not available for now.

* Addressing compatibility issues post file move

- Add functions to generate ICU skeleton to `ICUMeasurementNumberFormatter`: The function was originally declared as a `static func` in `Measurement.FormatStyle`. Re-work this into `ICUMeasurementNumberFormatter` since this function is only relevant when used with this class.

- Use Glibc for `pow` when Darwin isn't available

* Move test files

* Move test files

* Compatibility fix: typealias Duration.TimeFormatStyle and Duration.UnitsFormatStyle

XCTest explicitly exports Foundation, so referring to `Duration.UnitsFormatStyle` and `Duration.TimeFormatStyle` raises amibiguity errors on Darwin since the compiler doesn't know if they refer to the ones defined in the package or the one in Foundation.

We've been working around this issue by referring common types with prefix of package name (see TestSupport.swift). However we cannot do that for `Duration.UnitsFormatStyle` since this type is declared as `extension Duration`, and there's no way to disambiguate or typealias extensions on stdlib types. Workaround this by declaring `Duration._UnitsFormatStyle` as a typealias of `Duration.FormatStyle`, and use the former in the test. This way, when Darwin is imported, `_UnitsFormatStyle` would refer to the `Duration.UnitsFormatStyle` declared in Foundation, and the one in the package otherwise.
2023-10-05 15:01:48 -07:00

22 lines
713 B
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if canImport(FoundationEssentials)
import FoundationEssentials
#endif
// MARK: Duration Compatibility
extension TimeInterval {
init(_ duration: Duration) {
self = Double(duration.components.seconds) + 1e-18 * Double(duration.components.attoseconds)
}
}