Skip to content

Commit

Permalink
Handle fractional dates
Browse files Browse the repository at this point in the history
Navidrome returns these, but other servers may not.
NSISO8601DateFormatter looks like it can parse one or the other, but not
both with the same instance. So, have two.
  • Loading branch information
NattyNarwhal committed Jun 19, 2024
1 parent 398a3c9 commit ef8bbe0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Doing so isn't fatal (it's not a secret), but it is annoying for other contribut
* The playlist model has been corrected to handle multiple of the same track. (GH-192)
* Lower case artist names are properly sorted in the artist list.
* Favouriting items is available from the menu bar, or Cmd+E.
* Fix parsing fractional dates returned by some servers.
* Fix the null cover being used for system now playing information.
* Upgrading from Submariner 1.x is no longer supported.

Expand Down
10 changes: 9 additions & 1 deletion Submariner/String+Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import Cocoa

extension String {
fileprivate static let iso8601Formatter = ISO8601DateFormatter()
// Navidrome returns fractional time, but ISO8601DateFormatter will reject it.
// The .withFractionalSeconds option is what we want, but it mandates fractional seconds then.
// Have two formatters to try to use. To have one is blocking on FB13972481.
fileprivate static let iso8601FractionalFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter
}()
fileprivate static let rfc3339DateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
Expand All @@ -26,7 +34,7 @@ extension String {
}()

func dateTimeFromISO() -> Date? {
return String.iso8601Formatter.date(from: self as String)
return String.iso8601Formatter.date(from: self as String) ?? String.iso8601FractionalFormatter.date(from: self as String)
}

func dateTimeFromRFC3339() -> Date? {
Expand Down

0 comments on commit ef8bbe0

Please sign in to comment.