Skip to content

Commit 74f96ae

Browse files
committed
Introduce timezone parameter to the range description formatting
1 parent ca97bf1 commit 74f96ae

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

Networking/Networking/Extensions/DateFormatter+Woo.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,31 @@ public extension DateFormatter {
107107
/// Date formatter used for creating the properly-formatted date range info. Typically
108108
/// used when setting the end date on `AnalyticsHubTimeRangeGenerator`.
109109
///
110-
public static let analyticsHubDayMonthYearFormatter: DateFormatter = {
110+
public static func createAnalyticsHubDayMonthYearFormatter(timezone: TimeZone) -> DateFormatter {
111111
let formatter = DateFormatter()
112-
formatter.locale = Locale(identifier: "en_US_POSIX")
112+
formatter.timeZone = timezone
113113
formatter.dateFormat = "MMM d, yyyy"
114114
return formatter
115-
}()
115+
}
116116

117117
/// Date formatter used for creating the properly-formatted date range info. Typically
118118
/// used when setting the end date of a same-month range on `AnalyticsHubTimeRangeGenerator`.
119119
///
120-
public static let analyticsHubDayYearFormatter: DateFormatter = {
120+
public static func createAnalyticsHubDayYearFormatter(timezone: TimeZone) -> DateFormatter {
121121
let formatter = DateFormatter()
122-
formatter.locale = Locale(identifier: "en_US_POSIX")
122+
formatter.timeZone = timezone
123123
formatter.dateFormat = "d, yyyy"
124124
return formatter
125-
}()
125+
}
126126

127127
/// Date formatter used for creating the properly-formatted date range info. Typically
128128
/// used when setting the start date on `AnalyticsHubTimeRangeGenerator`.
129129
///
130-
public static let analyticsHubDayMonthFormatter: DateFormatter = {
130+
public static func createAnalyticsHubDayMonthFormatter(timezone: TimeZone) -> DateFormatter {
131131
let formatter = DateFormatter()
132-
formatter.locale = Locale(identifier: "en_US_POSIX")
132+
formatter.timeZone = timezone
133133
formatter.dateFormat = "MMM d"
134134
return formatter
135-
}()
135+
}
136136
}
137137
}

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/Time Range/AnalyticsHubTimeRange.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ struct AnalyticsHubTimeRange {
44
let start: Date
55
let end: Date
66

7-
func formatToString(simplified: Bool, calendar: Calendar) -> String {
7+
func formatToString(simplified: Bool, timezone: TimeZone, calendar: Calendar) -> String {
88
if simplified {
9-
return DateFormatter.Stats.analyticsHubDayMonthYearFormatter.string(from: start)
9+
return DateFormatter.Stats.createAnalyticsHubDayMonthYearFormatter(timezone: timezone).string(from: start)
1010
}
1111

12-
let startDateDescription = DateFormatter.Stats.analyticsHubDayMonthFormatter.string(from: start)
12+
let startDateDescription = DateFormatter.Stats.createAnalyticsHubDayMonthFormatter(timezone: timezone).string(from: start)
1313

14+
var endDateDescription: String
1415
if start.isSameMonth(as: end, using: calendar) {
15-
return "\(startDateDescription) - \(DateFormatter.Stats.analyticsHubDayYearFormatter.string(from: end))"
16+
endDateDescription = DateFormatter.Stats.createAnalyticsHubDayYearFormatter(timezone: timezone).string(from: end)
1617
} else {
17-
return "\(startDateDescription) - \(DateFormatter.Stats.analyticsHubDayMonthYearFormatter.string(from: end))"
18+
endDateDescription = DateFormatter.Stats.createAnalyticsHubDayMonthYearFormatter(timezone: timezone).string(from: end)
1819
}
20+
return "\(startDateDescription) - \(endDateDescription)"
1921
}
2022
}

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/Time Range/AnalyticsHubTimeRangeSelection.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ public class AnalyticsHubTimeRangeSelection {
4444
self.previousTimeRange = previousTimeRange
4545
self.rangeSelectionDescription = selectionType.description
4646
self.formattedCurrentRangeText = currentTimeRange?.formatToString(simplified: simplifiedDescription,
47+
timezone: timezone,
4748
calendar: calendar)
4849
self.formattedPreviousRangeText = previousTimeRange?.formatToString(simplified: simplifiedDescription,
50+
timezone: timezone,
4951
calendar: calendar)
5052
}
5153

0 commit comments

Comments
 (0)