Skip to content

Instantly share code, notes, and snippets.

@brettohland
Last active June 10, 2022 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettohland/fcda4acd2c80d04de866e4ed332b8483 to your computer and use it in GitHub Desktop.
Save brettohland/fcda4acd2c80d04de866e4ed332b8483 to your computer and use it in GitHub Desktop.
import Foundation
// MARK: - URL
// https://developer.apple.com/documentation/foundation/formatstyle/4013379-url
let appleURL = URL(string: "https://apple.com")!
appleURL.formatted() // "https://apple.com"
appleURL.formatted(.url) // "https://apple.com"
appleURL.formatted(.url.locale(Locale(identifier: "fr_FR"))) // "https://apple.com"
var httpComponents = URLComponents(url: appleURL, resolvingAgainstBaseURL: false)!
httpComponents.scheme = "https"
httpComponents.user = "jAppleseed"
httpComponents.password = "Test1234"
httpComponents.host = "apple.com"
httpComponents.port = 80
httpComponents.path = "/macbook-pro"
httpComponents.query = "get-free"
httpComponents.fragment = "someFragmentOfSomething"
let complexURL = httpComponents.url!
let everythingStyle = URL.FormatStyle(
scheme: .always,
user: .always,
password: .always,
host: .always,
port: .always,
path: .always,
query: .always,
fragment: .always
)
everythingStyle.format(complexURL) // "https://jAppleseed:Test1234@apple.com:80/macbook-pro?get-free#someFragmentOfSomething"
let omitStyle = URL.FormatStyle(
scheme: .omitIfHTTPFamily,
user: .omitIfHTTPFamily,
password: .omitIfHTTPFamily,
host: .omitIfHTTPFamily,
port: .omitIfHTTPFamily,
path: .omitIfHTTPFamily,
query: .omitIfHTTPFamily,
fragment: .omitIfHTTPFamily
)
var httpsComponent = httpComponents
httpsComponent.scheme = "https"
let httpsURL = httpsComponent.url!
var ftpComponents = httpComponents
ftpComponents.scheme = "ftp"
let ftpURL = ftpComponents.url!
omitStyle.format(complexURL) // ""
omitStyle.format(httpsURL) // ""
omitStyle.format(ftpURL) // "ftp://jAppleseed@apple.com:80/macbook-pro?get-free#someFragmentOfSomething"
let localhostURL = URL(string: "https://localhost:80/macbook-pro")!
let displayWhen = URL.FormatStyle(
scheme: .always,
user: .never,
password: .never,
host: .displayWhen(.host, matches: ["localhost"]),
port: .always,
path: .always,
query: .never,
fragment: .never
)
displayWhen.format(complexURL) // "https://:80/macbook-pro"
displayWhen.format(localhostURL) // "https://localhost:80/macbook-pro"
let omitWhen = URL.FormatStyle(
scheme: .always,
user: .never,
password: .never,
host: .omitWhen(.host, matches: ["localhost"]),
port: .always,
path: .always,
query: .never,
fragment: .never
)
omitWhen.format(complexURL) // "https://apple.com:80/macbook-pro"
omitWhen.format(localhostURL) // "https://:80/macbook-pro"
let omitSpecificWhen = URL.FormatStyle(
scheme: .always,
user: .never,
password: .never,
host: .omitSpecificSubdomains(["secret"], includeMultiLevelSubdomains: false),
port: .always,
path: .always,
query: .never,
fragment: .never
)
var secretAppleURL = URL(string: "https://secret.apple.com/macbook-pro")!
omitSpecificWhen.format(complexURL) // "https://apple.com:80/macbook-pro"
omitSpecificWhen.format(secretAppleURL) // "https://apple.com/macbook-pro"
let omitSpecificWhenWhere = URL.FormatStyle(
scheme: .always,
user: .never,
password: .never,
host: .omitSpecificSubdomains(["secret"], includeMultiLevelSubdomains: false, when: .user, matches: ["jAppleseed"]),
port: .always,
path: .always,
query: .never,
fragment: .never
)
let complexSecretURL = URL(string: "https://jAppleseed:Test1234@secret.apple.com:80/macbook-pro?get-free#someFragmentOfSomething")!
omitSpecificWhenWhere.format(complexSecretURL) // "https://apple.com:80/macbook-pro"
omitSpecificWhenWhere.format(secretAppleURL) // "https://secret.apple.com/macbook-pro"
// MARK: - ByteCount
// https://developer.apple.com/documentation/foundation/formatstyle/3931407-bytecount
let severalTerabytes = Measurement(value: 3, unit: UnitInformationStorage.terabytes)
severalTerabytes.formatted() // "3 TB"
severalTerabytes.formatted(.byteCount(style: .binary)) // "2.73 TB"
severalTerabytes.formatted(.byteCount(style: .decimal)) // "3 TB"
severalTerabytes.formatted(.byteCount(style: .file)) // "3 TB"
severalTerabytes.formatted(
.byteCount(
style: .binary,
allowedUnits: .tb,
spellsOutZero: true,
includesActualByteCount: true
)
) // "2.73 TB (3,000,000,000,000 bytes)"
severalTerabytes.formatted(
.byteCount(
style: .binary,
allowedUnits: .tb,
spellsOutZero: true,
includesActualByteCount: false
)
) // "2.73 TB"
severalTerabytes.formatted(
.byteCount(
style: .binary,
allowedUnits: .tb,
spellsOutZero: true,
includesActualByteCount: true
)
.locale(Locale(identifier: "fr_FR"))
) // "2,73 To (3 000 000 000 000 octets)"
severalTerabytes.formatted(
.byteCount(
style: .binary,
allowedUnits: .tb,
spellsOutZero: true,
includesActualByteCount: true
)
.attributed
)
let byteCountMeasurementStyle = Measurement<UnitInformationStorage>.FormatStyle.ByteCount(
style: .binary,
allowedUnits: .mb,
spellsOutZero: true,
includesActualByteCount: true,
locale: Locale(identifier: "fr_FR")
)
severalTerabytes.formatted(byteCountMeasurementStyle) // "2 861 022,9 Mo (3 000 000 000 000 octets)"
// This no longer results in a crash.
let threeTerabytes = Int64(3_000_000_000_000)
threeTerabytes.formatted(
.byteCount(
style: .binary,
allowedUnits: .tb,
spellsOutZero: false,
includesActualByteCount: true
)
) // "2.73 TB (3,000,000,000,000 bytes)"
// MARK: - Duration
// https://developer.apple.com/documentation/foundation/formatstyle/3988407-time
let thousandSeconds: Duration = .seconds(1000)
thousandSeconds.formatted() // "0:16:40"
thousandSeconds.formatted(.time(pattern: .hourMinute)) // "0:17"
thousandSeconds.formatted(.time(pattern: .hourMinute(padHourToLength: 10, roundSeconds: .awayFromZero))) // "0,000,000,000:17"
thousandSeconds.formatted(.time(pattern: .hourMinuteSecond)) // "0:16:40"
thousandSeconds.formatted(.time(pattern: .hourMinuteSecond(padHourToLength: 3, fractionalSecondsLength: 3, roundFractionalSeconds: .awayFromZero))) // "000:16:40.000"
thousandSeconds.formatted(.time(pattern: .minuteSecond)) // "16:40"
thousandSeconds.formatted(.time(pattern: .minuteSecond(padMinuteToLength: 3, fractionalSecondsLength: 3, roundFractionalSeconds: .awayFromZero))) // "016:40.000"
thousandSeconds.formatted(.time(pattern: .hourMinute).locale(Locale(identifier: "fr_FR"))) // "0:17"
let frenchTimeFormatStyle = Duration.TimeFormatStyle(pattern: .minuteSecond, locale: Locale(identifier: "fr_FR"))
frenchTimeFormatStyle.format(thousandSeconds) // "16:40"
// MARK: - Duration
// https://developer.apple.com/documentation/foundation/formatstyle/3988408-units
let halfSecond: Duration = .milliseconds(500)
halfSecond.formatted(.units()) // "0 sec"
halfSecond.formatted(
.units(allowed: [.milliseconds])
) // "500 ms"
halfSecond.formatted(
.units(
allowed: [.milliseconds],
width: .abbreviated
)
) // "500 ms"
halfSecond.formatted(
.units(
allowed: [.milliseconds],
width: .condensedAbbreviated
)
) // "500ms"
halfSecond.formatted(
.units(
allowed: [.milliseconds],
width: .narrow
)
)
halfSecond.formatted(
.units(
allowed: [.milliseconds],
width: .wide
)
) // "500 milliseconds"
halfSecond.formatted(
.units(
allowed: [.milliseconds],
width: .wide,
maximumUnitCount: 1
)
)
halfSecond.formatted(
.units(
allowed: [.milliseconds],
width: .wide,
maximumUnitCount: 1,
zeroValueUnits: .hide
)
) // "500 milliseconds"
halfSecond.formatted(
.units(
allowed: [.seconds, .milliseconds],
width: .wide,
maximumUnitCount: 2,
zeroValueUnits: .show(length: 2)
)
) // "00 seconds, 500 milliseconds"
halfSecond.formatted(
.units(
allowed: [.seconds, .milliseconds],
width: .wide,
maximumUnitCount: 2,
zeroValueUnits: .show(length: 2),
valueLength: 5
)
) // "00,000 seconds, 00,500 milliseconds"
halfSecond.formatted(
.units(
allowed: [.seconds, .milliseconds],
width: .wide,
maximumUnitCount: 2,
zeroValueUnits: .show(length: 2),
valueLength: 5,
fractionalPart: .hide
)
)
halfSecond.formatted(
.units(
allowed: [.seconds, .milliseconds],
width: .wide,
maximumUnitCount: 2,
zeroValueUnits: .show(length: 2),
valueLength: 5,
fractionalPart: .hide(rounded: .awayFromZero)
)
)
halfSecond.formatted(
.units(
allowed: [.seconds, .milliseconds],
width: .wide,
maximumUnitCount: 2,
zeroValueUnits: .show(length: 2),
valueLength: 5,
fractionalPart: .show(length: 5)
)
)
halfSecond.formatted(
.units(
allowed: [.seconds, .milliseconds],
width: .wide,
maximumUnitCount: 2,
zeroValueUnits: .show(length: 2),
valueLength: 5,
fractionalPart: .show(length: 2, rounded: .awayFromZero, increment: 0.000025)
)
) // "00,000.000000 seconds, 00,500.000000 milliseconds"
let durationUnitsStyle = Duration.UnitsFormatStyle(
allowedUnits: [.milliseconds],
width: .abbreviated,
maximumUnitCount: 5,
zeroValueUnits: .show(length: 3),
valueLength: 3,
fractionalPart: .show(length: 10, rounded: .awayFromZero, increment: 0.1)
)
Duration.microseconds(1234).formatted(durationUnitsStyle) // "001.3000000000 ms"
let durationValueLenghLimitsStyle = Duration.UnitsFormatStyle(
allowedUnits: [.milliseconds],
width: .abbreviated,
maximumUnitCount: 5,
zeroValueUnits: .show(length: 3),
valueLengthLimits: 0 ... 1,
fractionalPart: .show(length: 3)
)
Duration.microseconds(1234).formatted(durationValueLenghLimitsStyle) // "01.234 ms"
// Verbatim
// https://developer.apple.com/documentation/foundation/formatstyle/3996606-verbatim
let twosdayDateComponents = DateComponents(
year: 2022,
month: 2,
day: 22,
hour: 2,
minute: 22,
second: 22,
nanosecond: 22
)
let twosday = Calendar(identifier: .gregorian).date(from: twosdayDateComponents)!
twosday.formatted(
.verbatim(
"\(hour: .defaultDigits(clock: .twentyFourHour, hourCycle: .oneBased)):\(minute: .defaultDigits):\(minute: .defaultDigits) \(dayPeriod: .standard(.wide))",
locale: Locale(identifier: "zh_CN"),
timeZone: .current,
calendar: .current
)
) // "2:22:22 上午"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment