Skip to content

Commit

Permalink
Merge pull request #240 from wordpress-mobile/fix/post-timezone
Browse files Browse the repository at this point in the history
Allow adjustment of Date display strings by Time Zone
  • Loading branch information
bjtitus authored Jan 23, 2020
2 parents 7656d0b + 0c8be0e commit 50d47e6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion WordPressShared.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "WordPressShared"
s.version = "1.8.12-beta.1"
s.version = "1.8.12-beta.2"
s.summary = "Shared components used in building the WordPress iOS apps and other library components."

s.description = <<-DESC
Expand Down
16 changes: 13 additions & 3 deletions WordPressShared/Core/Utility/NSDate+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,21 @@ extension Date {

/// Formats the current date as relative date if it's within a week of
/// today, or with DateFormatter.Style.medium otherwise.
/// - Parameter timeZone: An optional time zone used to adjust the date formatters. **NOTE**: This has no affect on relative time stamps.
///
/// - Example: 22 hours from now
/// - Example: 5 minutes ago
/// - Example: 8 hours ago
/// - Example: 2 days ago
/// - Example: Jan 22, 2017
///
public func mediumString() -> String {
public func mediumString(timeZone: TimeZone? = nil) -> String {
let relativeFormatter = TTTTimeIntervalFormatter()
let absoluteFormatter = DateFormatters.mediumDate

if let timeZone = timeZone {
absoluteFormatter.timeZone = timeZone
}

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

/// Formats the current date as a medium relative date/time.
/// - Parameter timeZone: An optional time zone used to adjust the date formatters.
///
/// - Example: Tomorrow, 6:45 AM
/// - Example: Today, 8:09 AM
/// - Example: Yesterday, 11:36 PM
/// - Example: Jan 28, 2017, 1:51 PM
/// - Example: Jan 22, 2017, 2:18 AM
///
public func mediumStringWithTime() -> String {
return DateFormatters.mediumDateTime.string(from: self)
public func mediumStringWithTime(timeZone: TimeZone? = nil) -> String {
let formatter = DateFormatters.mediumDateTime
if let timeZone = timeZone {
formatter.timeZone = timeZone
}
return formatter.string(from: self)
}

/// Formats the current date as (non relative) long date (no time) in UTC.
Expand Down
29 changes: 29 additions & 0 deletions WordPressSharedTests/NSDateHelperTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,33 @@ class NSDateHelperTest: XCTestCase {
XCTAssertEqual(components.month, data.month)
XCTAssertEqual(components.day, data.day)
}

/// Verifies that `mediumString` produces relative format strings when less than 7 days have elapsed.
func testMediumStringRelativeString() {
let date = Date()
XCTAssertEqual(date.mediumString(), "just now")
XCTAssertEqual(date.addingTimeInterval(-60*5).mediumString(), "5 minutes ago")
XCTAssertEqual(date.addingTimeInterval(-60*60*2).mediumString(), "2 hours ago")
XCTAssertEqual(date.addingTimeInterval(-60*60*24).mediumString(), "1 day ago")
XCTAssertEqual(date.addingTimeInterval(-60*60*24*6).mediumString(), "6 days ago")
}

/// Verifies that `mediumStringWithTime` takes into account the time zone adjustment
func testMediumStringTimeZoneAdjust() {
let date = Date()
let timeZone = TimeZone(secondsFromGMT: Calendar.current.timeZone.secondsFromGMT() - (60 * 60))
XCTAssertEqual(date.mediumString(timeZone: timeZone), "just now")

let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
let withoutTimeZoneAdjust = timeFormatter.string(from: date)

XCTAssertEqual(date.mediumStringWithTime(), "Today at \(withoutTimeZoneAdjust)")

timeFormatter.timeZone = timeZone
let withTimeZoneAdjust = timeFormatter.string(from: date)

XCTAssertEqual(date.mediumStringWithTime(timeZone: timeZone), "Today at \(withTimeZoneAdjust)")
}
}

0 comments on commit 50d47e6

Please sign in to comment.