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-7269] Explicitly declaring import CDispatch is required to use NSEC_PER_SEC on Linux. #659

Open
norio-nomura opened this issue Mar 24, 2018 · 4 comments

Comments

@norio-nomura
Copy link

Previous ID SR-7269
Radar None
Original Reporter @norio-nomura
Type Bug
Environment

swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-22-a

Additional Detail from JIRA
Votes 0
Component/s libdispatch
Labels Bug
Assignee None
Priority Medium

md5: 771ca6a4f852f7ce29492a2d0a307f75

Issue Description:

Since swift-corelibs-libdispatch does not have @_exported import CDispatch, explicitly declaring import CDispatch is required to use NSEC_PER_SEC on Linux.
Since CDispatch does not exist on Darwin, we need to use #if !_runtime(_ObjC) or #if os(Linux) on importing CDispatch. This is inconvenient.

@belkadan
Copy link

cc phabouzit (JIRA User)

@swift-ci
Copy link

Comment by Pierre Habouzit (JIRA)

FWIW I disagree with that, the fact that CDispatch vends anything is IMO a bug.

Swift should have a very good standard library for time and allow to convert versus all units. Dispatch should not be in the business of doing that.

@belkadan do you agree?

@belkadan
Copy link

That seems fair, but I don't know when such a library is going to be added.

@norio-nomura, what was your use case for NSEC_PER_SEC? (as opposed to something like DispatchTimeInterval)

@norio-nomura
Copy link
Author

I'm using NSEC_PER_SEC as following:

func timestamp(_ timeZoneHour: Int = 0,
               _ year: Int? = nil,
               _ month: Int? = nil,
               _ day: Int? = nil,
               _ hour: Int? = nil,
               _ minute: Int? = nil,
               _ second: Int? = nil,
               _ fraction: Double? = nil ) -> Date {
    let calendar = Calendar(identifier: .gregorian)
    let timeZone = TimeZone(secondsFromGMT: timeZoneHour * 60 * 60)
    let nanosecond = fraction.map { Int($0 * Double(NSEC_PER_SEC)) }
    let datecomponents = DateComponents(calendar: calendar, timeZone: timeZone,
                          year: year, month: month, day: day,
                          hour: hour, minute: minute, second: second, nanosecond: nanosecond)
    // Using `DateComponents.date` causes crash on Linux
    guard let date = NSCalendar(identifier: .gregorian)?.date(from: datecomponents) else {
        fatalError("Never happen this")
    }
    return date
}

@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
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

3 participants