From fc92609c9e7bef9cfeef0002404c220c8f4f431e Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sun, 31 Mar 2024 16:03:30 -0400 Subject: [PATCH] Only mutate date formatter if neccesary. --- .../Overall App/DateFormatterManager.swift | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Clocker/Overall App/DateFormatterManager.swift b/Clocker/Overall App/DateFormatterManager.swift index f5d8bfab..6d2f6c9f 100644 --- a/Clocker/Overall App/DateFormatterManager.swift +++ b/Clocker/Overall App/DateFormatterManager.swift @@ -20,12 +20,32 @@ class DateFormatterManager: NSObject { return dateFormatter } - class func dateFormatterWithFormat(with style: DateFormatter.Style, format: String, timezoneIdentifier: String, locale: Locale = Locale(identifier: "en_US")) -> DateFormatter { - specializedFormatter.dateStyle = style - specializedFormatter.timeStyle = style - specializedFormatter.dateFormat = format - specializedFormatter.timeZone = TimeZone(identifier: timezoneIdentifier) - specializedFormatter.locale = locale + class func dateFormatterWithFormat(with style: DateFormatter.Style, + format: String, + timezoneIdentifier: String, + locale: + Locale = Locale(identifier: "en_US")) -> DateFormatter { + if (specializedFormatter.dateStyle != style) { + specializedFormatter.dateStyle = style + } + + if (specializedFormatter.timeStyle != style) { + specializedFormatter.timeStyle = style + } + + if (specializedFormatter.dateFormat != format) { + specializedFormatter.dateFormat = format + } + + if (specializedFormatter.timeZone.identifier != timezoneIdentifier) { + specializedFormatter.timeZone = TimeZone(identifier: timezoneIdentifier) + } + + if (specializedFormatter.locale.identifier != locale.identifier) { + specializedFormatter.locale = locale + } + + return specializedFormatter }