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

Commit 50d47e6

Browse files
authored
Merge pull request #240 from wordpress-mobile/fix/post-timezone
Allow adjustment of Date display strings by Time Zone
2 parents 7656d0b + 0c8be0e commit 50d47e6

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

WordPressShared.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 = "WordPressShared"
3-
s.version = "1.8.12-beta.1"
3+
s.version = "1.8.12-beta.2"
44
s.summary = "Shared components used in building the WordPress iOS apps and other library components."
55

66
s.description = <<-DESC

WordPressShared/Core/Utility/NSDate+Helpers.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,21 @@ extension Date {
119119

120120
/// Formats the current date as relative date if it's within a week of
121121
/// today, or with DateFormatter.Style.medium otherwise.
122+
/// - Parameter timeZone: An optional time zone used to adjust the date formatters. **NOTE**: This has no affect on relative time stamps.
122123
///
123124
/// - Example: 22 hours from now
124125
/// - Example: 5 minutes ago
125126
/// - Example: 8 hours ago
126127
/// - Example: 2 days ago
127128
/// - Example: Jan 22, 2017
128129
///
129-
public func mediumString() -> String {
130+
public func mediumString(timeZone: TimeZone? = nil) -> String {
130131
let relativeFormatter = TTTTimeIntervalFormatter()
131132
let absoluteFormatter = DateFormatters.mediumDate
133+
134+
if let timeZone = timeZone {
135+
absoluteFormatter.timeZone = timeZone
136+
}
132137

133138
let components = Calendar.current.dateComponents([.day], from: self, to: Date())
134139
if let days = components.day, abs(days) < 7 {
@@ -139,15 +144,20 @@ extension Date {
139144
}
140145

141146
/// Formats the current date as a medium relative date/time.
147+
/// - Parameter timeZone: An optional time zone used to adjust the date formatters.
142148
///
143149
/// - Example: Tomorrow, 6:45 AM
144150
/// - Example: Today, 8:09 AM
145151
/// - Example: Yesterday, 11:36 PM
146152
/// - Example: Jan 28, 2017, 1:51 PM
147153
/// - Example: Jan 22, 2017, 2:18 AM
148154
///
149-
public func mediumStringWithTime() -> String {
150-
return DateFormatters.mediumDateTime.string(from: self)
155+
public func mediumStringWithTime(timeZone: TimeZone? = nil) -> String {
156+
let formatter = DateFormatters.mediumDateTime
157+
if let timeZone = timeZone {
158+
formatter.timeZone = timeZone
159+
}
160+
return formatter.string(from: self)
151161
}
152162

153163
/// Formats the current date as (non relative) long date (no time) in UTC.

WordPressSharedTests/NSDateHelperTest.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,33 @@ class NSDateHelperTest: XCTestCase {
3333
XCTAssertEqual(components.month, data.month)
3434
XCTAssertEqual(components.day, data.day)
3535
}
36+
37+
/// Verifies that `mediumString` produces relative format strings when less than 7 days have elapsed.
38+
func testMediumStringRelativeString() {
39+
let date = Date()
40+
XCTAssertEqual(date.mediumString(), "just now")
41+
XCTAssertEqual(date.addingTimeInterval(-60*5).mediumString(), "5 minutes ago")
42+
XCTAssertEqual(date.addingTimeInterval(-60*60*2).mediumString(), "2 hours ago")
43+
XCTAssertEqual(date.addingTimeInterval(-60*60*24).mediumString(), "1 day ago")
44+
XCTAssertEqual(date.addingTimeInterval(-60*60*24*6).mediumString(), "6 days ago")
45+
}
46+
47+
/// Verifies that `mediumStringWithTime` takes into account the time zone adjustment
48+
func testMediumStringTimeZoneAdjust() {
49+
let date = Date()
50+
let timeZone = TimeZone(secondsFromGMT: Calendar.current.timeZone.secondsFromGMT() - (60 * 60))
51+
XCTAssertEqual(date.mediumString(timeZone: timeZone), "just now")
52+
53+
let timeFormatter = DateFormatter()
54+
timeFormatter.dateStyle = .none
55+
timeFormatter.timeStyle = .short
56+
let withoutTimeZoneAdjust = timeFormatter.string(from: date)
57+
58+
XCTAssertEqual(date.mediumStringWithTime(), "Today at \(withoutTimeZoneAdjust)")
59+
60+
timeFormatter.timeZone = timeZone
61+
let withTimeZoneAdjust = timeFormatter.string(from: date)
62+
63+
XCTAssertEqual(date.mediumStringWithTime(timeZone: timeZone), "Today at \(withTimeZoneAdjust)")
64+
}
3665
}

0 commit comments

Comments
 (0)