Skip to content

Commit

Permalink
[perf] only create date formatters once
Browse files Browse the repository at this point in the history
  • Loading branch information
jessesquires committed Jan 26, 2024
1 parent 93e50d9 commit c85342a
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,25 @@ import Foundation

#if os(OSX)

extension Date {
/// - Returns: The date 9:41 AM on Tuesday January 9, 2007
/// in the current local and current time zone.
static func statusBarDateTime() -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
dateFormatter.locale = Locale.current
return dateFormatter.date(from: "2007-01-09T09:41:00")!
}

/// - Returns: An ISO date/time string for 9:41 AM on Tuesday January 9, 2007
/// in the current local and current time zone.
static func simulatorDateTimeText() -> String {
let isoFormatter = ISO8601DateFormatter()
isoFormatter.formatOptions = [
.withFullDate,
.withDashSeparatorInDate,
.withFullTime,
.withColonSeparatorInTime,
.withFractionalSeconds,
.withTimeZone
]
return isoFormatter.string(from: Date.statusBarDateTime())
}
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
dateFormatter.locale = Locale.current

let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.formatOptions = [
.withFullDate,
.withDashSeparatorInDate,
.withFullTime,
.withColonSeparatorInTime,
.withFractionalSeconds,
.withTimeZone
]

/// An ISO date/time string for 9:41 AM on Tuesday January 9, 2007
/// in the current local and current time zone.
let date = dateFormatter.date(from: "2007-01-09T09:41:00")!
let simulatorDateTimeText = isoDateFormatter.string(from: date)

extension Process {
/// Creates a process to execute `xcrun`.
Expand All @@ -63,14 +56,13 @@ extension Process {
self.xcrun("simctl", "list", "devices", "-j")
}

/// Executes `xcrun simctl status_bar` on the specified device.
/// Executes `xcrun simctl status_bar` on the specified device to set overrides.
///
/// - Parameter device: The device for which status bar values should be overridden.
func xcrun_fix_status_bar(_ device: String) {
let dateTimeText = Date.simulatorDateTimeText()
self.xcrun(
"simctl", "status_bar", device, "override",
"--time", "\(dateTimeText)",
"--time", "\(simulatorDateTimeText)",
"--dataNetwork", "wifi",
"--wifiMode", "active",
"--wifiBars", "3",
Expand Down

0 comments on commit c85342a

Please sign in to comment.