Date - leacode/SwiftWings GitHub Wiki

Date Extensions

Source: Sources/Extensions/Foundation/Date

Tests: Tests/Extensions/Date

SwiftWings provides an extensive date toolkit that works across Apple platforms and Linux.

Date+Extensions.swift

  • Timestamp helpers (timeMillis, currentTimeMillis).
  • Calendar checks (isToday, isTomorrow, isInThisWeek, isInNextWeek).
  • Convenience accessors for adjacent days/weeks/months/years (e.g. lastMonday, nextMonth).

Date+Components.swift

  • Adds computed properties for every Calendar.Component (year, month, weekday, etc.) using Calendar.current.

Date+Utilities.swift

  • Start/end of component calculations, isSame(_:as:), difference(to:component:), isBetween, clamped, and general advanced(_:value:) arithmetic.
  • New DST-aware helpers (interval(of:timeZone:), distanceComponents(to:timeZone:)).

Date+TimeZone.swift

  • Wall-clock conversions between time zones, formatted output pinned to a TimeZone, and component extraction using injected calendars.

Date+Intervals.swift

  • Convenience wrappers around Calendar.dateInterval(of:for:) and dateComponents(_:from:to:) with time-zone overrides.

Example

var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(identifier: "America/Los_Angeles")!

let meeting = DateComponents(calendar: calendar,
                             year: 2024,
                             month: 3,
                             day: 10,
                             hour: 9).date!

print(meeting.isToday) // depends on current date
print(meeting.start(of: .day, calendar: calendar)!) // 2024-03-10 08:00:00 +0000
print(meeting.interval(of: .day, timeZone: calendar.timeZone)?.duration ?? 0) // 82800 (23h, DST day)

let tokyo = TimeZone(identifier: "Asia/Tokyo")!
print(meeting.converted(from: calendar.timeZone, to: tokyo)
               .formatted("yyyy-MM-dd HH:mm", timeZone: tokyo, locale: Locale(identifier: "en_US_POSIX")))
// "2024-03-11 01:00"
⚠️ **GitHub.com Fallback** ⚠️