Calendar.Component.day ignores calendar month differences

Open Beginner friendly
#104 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
java, swift
Domain
mobile-dev

Research direction

Start at the referenced mapping in Sources/SkipFoundation/DateComponents.swift and compare the Swift .day behavior with the java.util.Calendar field used there. Verify the Jan 1 to Feb 1 and 31-day examples, then confirm that dateComponents([.day], from:to:) reports the full elapsed day difference across month boundaries.

Written by the indexing model from the issue text.

Description

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
Dominant language
Swift
Stars
17
Forks
16
Avg merge
1h 50m
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from skiptools/skip-foundation

All issues in skiptools/skip-foundation

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.