Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit c8b0633

Browse files
authored
Merge pull request #170 from wordpress-mobile/issue/stats_timezone
Stats: Use site timezone for Published posts.
2 parents b70a073 + 0ee48df commit c8b0633

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

WordPressKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WordPressKit"
3-
s.version = "4.2.1-beta.2"
3+
s.version = "4.3.0-beta.1"
44
s.summary = "WordPressKit offers a clean and simple WordPress.com and WordPress.org API."
55

66
s.description = <<-DESC

WordPressKit/StatsServiceRemoteV2.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
2020
return df
2121
}()
2222

23+
private lazy var calendarForSite: Calendar = {
24+
var cal = Calendar(identifier: .iso8601)
25+
cal.timeZone = siteTimezone
26+
return cal
27+
}()
28+
2329
public init(wordPressComRestApi api: WordPressComRestApi, siteID: Int, siteTimezone: TimeZone) {
2430
self.siteID = siteID
2531
self.siteTimezone = siteTimezone
@@ -198,9 +204,7 @@ extension StatsServiceRemoteV2 {
198204
endingOn: Date,
199205
limit: Int = 10,
200206
completion: @escaping ((StatsPublishedPostsTimeIntervalData?, Error?) -> Void)) {
201-
202207
let pathComponent = StatsLastPostInsight.pathComponent
203-
204208
let path = self.path(forEndpoint: "sites/\(siteID)/\(pathComponent)", withVersion: ._1_1)
205209

206210
let properties = ["number": limit,
@@ -227,21 +231,19 @@ extension StatsServiceRemoteV2 {
227231
private func startDate(for period: StatsPeriodUnit, endDate: Date) -> Date {
228232
switch period {
229233
case .day:
230-
return Calendar.autoupdatingCurrent.startOfDay(for: endDate)
234+
return calendarForSite.startOfDay(for: endDate)
231235
case .week:
232-
let weekAgo = Calendar.autoupdatingCurrent.date(byAdding: .day, value: -6, to: endDate)!
233-
return Calendar.autoupdatingCurrent.startOfDay(for: weekAgo)
236+
let weekAgo = calendarForSite.date(byAdding: .day, value: -6, to: endDate)!
237+
return calendarForSite.startOfDay(for: weekAgo)
234238
case .month:
235-
let monthAgo = Calendar.autoupdatingCurrent.date(byAdding: .month, value: -1, to: endDate)!
236-
let firstOfMonth = Calendar.autoupdatingCurrent.date(bySetting: .day, value: 1, of: monthAgo)!
237-
238-
return Calendar.autoupdatingCurrent.startOfDay(for: firstOfMonth)
239+
let monthAgo = calendarForSite.date(byAdding: .month, value: -1, to: endDate)!
240+
let firstOfMonth = calendarForSite.date(bySetting: .day, value: 1, of: monthAgo)!
241+
return calendarForSite.startOfDay(for: firstOfMonth)
239242
case .year:
240-
let yearAgo = Calendar.autoupdatingCurrent.date(byAdding: .year, value: -1, to: endDate)!
241-
let january = Calendar.autoupdatingCurrent.date(bySetting: .month, value: 1, of: yearAgo)!
242-
let jan1 = Calendar.autoupdatingCurrent.date(bySetting: .day, value: 1, of: january)!
243-
244-
return Calendar.autoupdatingCurrent.startOfDay(for: jan1)
243+
let yearAgo = calendarForSite.date(byAdding: .year, value: -1, to: endDate)!
244+
let january = calendarForSite.date(bySetting: .month, value: 1, of: yearAgo)!
245+
let jan1 = calendarForSite.date(bySetting: .day, value: 1, of: january)!
246+
return calendarForSite.startOfDay(for: jan1)
245247
}
246248
}
247249

0 commit comments

Comments
 (0)