deep-linking-sample/Apps/Wikipedia/WMF Framework/DateFormatter+WikipediaLanguage.swift
Javier Cicchelli 9bcdaa697b [Setup] Basic project structure (#1)
This PR contains all the work related to setting up this project as required to implement the [Assignment](https://repo.rock-n-code.com/rock-n-code/deep-linking-assignment/wiki/Assignment) on top, as intended.

To summarise this work:
- [x] created a new **Xcode** project;
- [x] cloned the `Wikipedia` app and inserted it into the **Xcode** project;
- [x] created the `Locations` app and also, its `Libraries` package;
- [x] created the `Shared` package to share dependencies between the apps;
- [x] added a `Makefile` file and implemented some **environment** and **help** commands.

Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Reviewed-on: rock-n-code/deep-linking-assignment#1
2023-04-08 18:37:13 +00:00

82 lines
3.7 KiB
Swift

import Foundation
extension DateFormatter {
// Returns year string - i.e. '1000' or '200 BC'. (Negative years are 'BC')
class func wmf_yearString(for year: Int, with wikipediaLanguageCode: String?) -> String? {
var components = DateComponents()
components.year = year
let calendar = NSCalendar.wmf_utcGregorian()
guard let date = calendar?.date(from: components) else {
return nil
}
let formatter = year < 0 ? DateFormatter.wmf_yearWithEraGMTDateFormatter(for: wikipediaLanguageCode) : DateFormatter.wmf_yearGMTDateFormatter(for: wikipediaLanguageCode)
return formatter.string(from: date)
}
private static var wmf_yearGMTDateFormatterCache: [String: DateFormatter] = [:]
public static func wmf_yearGMTDateFormatter(for wikipediaLanguageCode: String?) -> DateFormatter {
let wikipediaLanguageCode = wikipediaLanguageCode ?? "en"
if let formatter = wmf_yearGMTDateFormatterCache[wikipediaLanguageCode] {
return formatter
}
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale.wmf_locale(for: wikipediaLanguageCode)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.setLocalizedDateFormatFromTemplate("y")
wmf_yearGMTDateFormatterCache[wikipediaLanguageCode] = dateFormatter
return dateFormatter
}
private static var wmf_yearWithEraGMTDateFormatterCache: [String: DateFormatter] = [:]
public static func wmf_yearWithEraGMTDateFormatter(for wikipediaLanguageCode: String?) -> DateFormatter {
let wikipediaLanguageCode = wikipediaLanguageCode ?? "en"
if let formatter = wmf_yearWithEraGMTDateFormatterCache[wikipediaLanguageCode] {
return formatter
}
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale.wmf_locale(for: wikipediaLanguageCode)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.setLocalizedDateFormatFromTemplate("y G")
wmf_yearWithEraGMTDateFormatterCache[wikipediaLanguageCode] = dateFormatter
return dateFormatter
}
private static var wmf_monthNameDayNumberGMTFormatterCache: [String: DateFormatter] = [:]
public static func wmf_monthNameDayNumberGMTFormatter(for wikipediaLanguageCode: String?) -> DateFormatter {
let wikipediaLanguageCode = wikipediaLanguageCode ?? "en"
if let formatter = wmf_monthNameDayNumberGMTFormatterCache[wikipediaLanguageCode] {
return formatter
}
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale.wmf_locale(for: wikipediaLanguageCode)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.setLocalizedDateFormatFromTemplate("MMMM d")
wmf_monthNameDayNumberGMTFormatterCache[wikipediaLanguageCode] = dateFormatter
return dateFormatter
}
private static var wmf_longDateGMTFormatterCache: [String: DateFormatter] = [:]
public static func wmf_longDateGMTFormatter(for wikipediaLanguageCode: String?) -> DateFormatter {
let wikipediaLanguageCode = wikipediaLanguageCode ?? "en"
if let formatter = wmf_longDateGMTFormatterCache[wikipediaLanguageCode] {
return formatter
}
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale.wmf_locale(for: wikipediaLanguageCode)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.timeStyle = .none
dateFormatter.dateStyle = .long
wmf_longDateGMTFormatterCache[wikipediaLanguageCode] = dateFormatter
return dateFormatter
}
}