Linux - Calendar date(byAdding:to:wrappingComponents:) returns nil when it shouldn't?


(Jason Ji) #1

Hello,

I'm having an issue with (NS)Calendar on Linux which I think is a bug, but
I just wanted to check first if it was just me or if this is indeed a bug.
I've filed a bug report here, just in case:
https://bugs.swift.org/browse/SR-2846

In short, Calendar has a method date(byAdding:to:wrappingComponents:) which
returns a new date which is the result of date arithmetic on the passed-in
date. It works fine on El Capitan, but doesn't seem to work properly on
Ubuntu 14.04. Below is some sample code:

import Foundation

let today = Date()
let diffComponents = DateComponents(day: -1)
let newDate = Calendar.current.date(byAdding: diffComponents, to:
today) //returns nil

I've tried this in the swift REPL on Ubuntu 14.04 with both Swift
3.0-RELEASE, and the latest snapshot (October 2).

If anyone else could try this out as a sanity check for me, that would be
great - I'd be happy to be embarrassed that I've done something wrong.

Thanks,

Jason


(Alex Blewitt) #2

It's worth explicitly specifying a calendar (so that it rules out any environmental setup) -- for example, Calendar(identifier:.gregorian). However, I see 'nil' as well as the return result for this operation, so it could be a bug.

Alex

···

On 4 Oct 2016, at 20:10, Jason Ji via swift-users <swift-users@swift.org> wrote:

Hello,

I'm having an issue with (NS)Calendar on Linux which I think is a bug, but I just wanted to check first if it was just me or if this is indeed a bug. I've filed a bug report here, just in case: https://bugs.swift.org/browse/SR-2846

In short, Calendar has a method date(byAdding:to:wrappingComponents:) which returns a new date which is the result of date arithmetic on the passed-in date. It works fine on El Capitan, but doesn't seem to work properly on Ubuntu 14.04. Below is some sample code:

import Foundation

let today = Date()
let diffComponents = DateComponents(day: -1)
let newDate = Calendar.current.date(byAdding: diffComponents, to: today) //returns nil

I've tried this in the swift REPL on Ubuntu 14.04 with both Swift 3.0-RELEASE, and the latest snapshot (October 2).

If anyone else could try this out as a sanity check for me, that would be great - I'd be happy to be embarrassed that I've done something wrong.


(Alex Blewitt) #3

I think the bug is here:

https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSCalendar.swift#L464

        _convert(comps.isLeapMonth, type: "L", vector: &vector, compDesc: &compDesc)

The leap month is defined with a capital letter here, but a lower-case letter when it's decoded:

https://github.com/apple/swift-corelibs-foundation/blob/338f4bf3a89c75a0420b49f5701466e106af02b5/CoreFoundation/Locale.subproj/CFCalendar.c#L423

    case 'l': return UCAL_IS_LEAP_MONTH;

The lower-case l should be the correct version so this probably needs to be changed here. In addition, there are optional-of-bool issues here; the 'comps.isLeapMonth' is a boolean value, which means it attempts to roll forward the leap month (which doesn't make sense). I think the error was introduced here, although it didn't help that the capitalisation was also wrong at the time:

https://github.com/apple/swift-corelibs-foundation/commit/d3300b7118924d6ad8ba411d317f33eade854bc5

Alex

···

On 5 Oct 2016, at 16:48, Alex Blewitt via swift-users <swift-users@swift.org> wrote:

On 4 Oct 2016, at 20:10, Jason Ji via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello,

I'm having an issue with (NS)Calendar on Linux which I think is a bug, but I just wanted to check first if it was just me or if this is indeed a bug. I've filed a bug report here, just in case: https://bugs.swift.org/browse/SR-2846

In short, Calendar has a method date(byAdding:to:wrappingComponents:) which returns a new date which is the result of date arithmetic on the passed-in date. It works fine on El Capitan, but doesn't seem to work properly on Ubuntu 14.04. Below is some sample code:

import Foundation

let today = Date()
let diffComponents = DateComponents(day: -1)
let newDate = Calendar.current.date(byAdding: diffComponents, to: today) //returns nil

I've tried this in the swift REPL on Ubuntu 14.04 with both Swift 3.0-RELEASE, and the latest snapshot (October 2).

If anyone else could try this out as a sanity check for me, that would be great - I'd be happy to be embarrassed that I've done something wrong.

It's worth explicitly specifying a calendar (so that it rules out any environmental setup) -- for example, Calendar(identifier:.gregorian). However, I see 'nil' as well as the return result for this operation, so it could be a bug.