Skip to content

Commit

Permalink
Fix for dates belonging to previous year (#34)
Browse files Browse the repository at this point in the history
* Update year

* Fix for dates belonging to previous year

* Update year
  • Loading branch information
sbooth authored Jan 18, 2024
1 parent 97b17c1 commit 17820a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2021-2023 Stephen F. Booth <[email protected]>
Copyright (c) 2021-2024 Stephen F. Booth <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions Sources/JulianDayNumber/ISOCalendar.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright © 2021-2023 Stephen F. Booth <[email protected]>
// Copyright © 2021-2024 Stephen F. Booth <[email protected]>
// Part of https://github.com/sbooth/JulianDayNumber
// MIT license
//
Expand Down Expand Up @@ -64,7 +64,7 @@ public struct ISOCalendar {
let weekday = isoWeekdayFrom(year: Y, month: M, day: D)
let w = (10 + N - weekday) / 7
if w == 0 {
return (Y - 1, isoWeeksInYear(Y), weekday)
return (Y - 1, isoWeeksInYear(Y - 1), weekday)
} else if w > isoWeeksInYear(Y) {
return (Y + 1, 1, weekday)
}
Expand Down
22 changes: 21 additions & 1 deletion Tests/JulianDayNumberTests/ISOCalendarTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright © 2021-2023 Stephen F. Booth <[email protected]>
// Copyright © 2021-2024 Stephen F. Booth <[email protected]>
// Part of https://github.com/sbooth/JulianDayNumber
// MIT license
//
Expand Down Expand Up @@ -27,6 +27,26 @@ final class ISOCalendarTests: XCTestCase {
func testDateFrom() {
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2023, month: 11, day: 16) == (2023, 46, 4))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2023, week: 46, weekday: 4) == (2023, 11, 16))

XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2020, month: 12, day: 27) == (2020, 52, 7))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2020, month: 12, day: 28) == (2020, 53, 1))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2020, month: 12, day: 29) == (2020, 53, 2))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2020, month: 12, day: 30) == (2020, 53, 3))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2020, month: 12, day: 31) == (2020, 53, 4))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2021, month: 1, day: 1) == (2020, 53, 5))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2021, month: 1, day: 2) == (2020, 53, 6))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2021, month: 1, day: 3) == (2020, 53, 7))
XCTAssertTrue(ISOCalendar.isoDateFrom(year: 2021, month: 1, day: 4) == (2021, 1, 1))

XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 52, weekday: 7) == (2020, 12, 27))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 53, weekday: 1) == (2020, 12, 28))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 53, weekday: 2) == (2020, 12, 29))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 53, weekday: 3) == (2020, 12, 30))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 53, weekday: 4) == (2020, 12, 31))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 53, weekday: 5) == (2021, 1, 1))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 53, weekday: 6) == (2021, 1, 2))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2020, week: 53, weekday: 7) == (2021, 1, 3))
XCTAssertTrue(ISOCalendar.dateFromISO(year: 2021, week: 1, weekday: 1) == (2021, 1, 4))
}

func testJulianDayNumber() {
Expand Down

0 comments on commit 17820a5

Please sign in to comment.