Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8973] DateComponents returns incorrect value #3618

Closed
swift-ci opened this issue Oct 11, 2018 · 4 comments
Closed

[SR-8973] DateComponents returns incorrect value #3618

swift-ci opened this issue Oct 11, 2018 · 4 comments
Assignees

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-8973
Radar rdar://problem/45407970
Original Reporter johnbona (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Using the Swift 4.2 Docker image:

Ubuntu 16.04

Swift 4.2

Additional Detail from JIRA
Votes 1
Component/s Foundation
Labels Bug
Assignee @millenomi
Priority Medium

md5: ff7988c73bd10692c9cf2616cfde9eb1

Issue Description:

When obtaining the difference between two dates on Linux, Calendar's .dateComponents(_:from:to: ) method will return nil instead of the correct value. This seemingly only occurs when the two dates are off by one minute.

import Foundation

let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

let now = dateFormatter.date(from: "2018-10-11 19:01:39 +0000")!
let start = dateFormatter.date(from: "2018-10-11 19:00:00 +0000")!

Calendar(identifier: .iso8601).dateComponents([.minute], from: now, to: start).minute
// Linux: nil (incorrect)
// macOS: -1 (correct)

However, when using the .values(for: ) method and the same dates, it returns NSDateComponentUndefined (or Int.max). Might be related to SR-2671.

Calendar(identifier: .iso8601).dateComponents([.minute], from: now, to: start).value(for: .minute)
// Linux: Optional(9223372036854775807) (incorrect)
// macOS: -1 (correct)
@weissi
Copy link
Member

weissi commented Oct 19, 2018

@swift-ci create

@spevans
Copy link
Collaborator

spevans commented Oct 19, 2018

This is fixed in master probably by #1658

$ cat sr_8973.swift 
import Foundation

let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

let now = dateFormatter.date(from: "2018-10-11 19:01:39 +0000")!
let start = dateFormatter.date(from: "2018-10-11 19:00:00 +0000")!

let c1 = Calendar(identifier: .iso8601).dateComponents([.minute], from: now, to: start).minute
print(c1 == -1)
let c2 = Calendar(identifier: .iso8601).dateComponents([.minute], from: now, to: start).value(for: .minute)
print(c2 == -1)

$ ~/swift-4.2-RELEASE-ubuntu16.04/usr/bin/swift sr_8973.swift 
false
false
$ ~/swift-DEVELOPMENT-SNAPSHOT-2018-10-03-a-ubuntu16.04/usr/bin/swift sr_8973.swift 
true
true
 

@millenomi
Copy link
Contributor

Using this to track a 4.2 cherry-pick.

@spevans
Copy link
Collaborator

spevans commented Apr 24, 2019

This was fixed in swift-4.2.3

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants