Skip to content

Calendar.Component.day ignores calendar month differences #104

Description

@joshuakcockrell

In Swift, this code shows a 31 day difference

let today = Date()
let future = Calendar.current.date(
  byAdding: .day,
  value: 31,
  to: today
) ?? Date()
print(Calendar.current.dateComponents([.day], from: today, to: future).day ?? 0) // ~31 days

But when transpiled through Skip it only shows a 0 or 1 day difference between the two dates.

This is because this line in DateComponents.swift incorrectly maps the .day Swift case to java.util.Calendar.DAY_OF_MONTH, which only looks at "day of month" and ignores differences between months. Jan 1 and Feb 1 are treated the same.

Here's a workaround till this is fixed.

    #if SKIP
    let startDate = startDate(nocopy: true).toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
    let endDate = endDate.kotlin(nocopy: true).toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
    return ChronoUnit.DAYS.between(startDate, endDate).toInt()
    #else
    return Calendar.current.dateComponents([.day], from: startDate, to: endDate).day ?? 0
    #endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions