malcommac / SwiftDate

🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.
MIT License
7.64k stars 768 forks source link

Getting the day component of a DateInRegion always returned the day in UTC #785

Open Bawenang opened 3 years ago

Bawenang commented 3 years ago

I was under the impression the DateInRegion.day will return the day of the date in a certain region. However, when I tried it, it always returned the day in UTC, similar to Date.day. I have a need to get the current local day in my unit test. In my test, I need to compare the current date to the date of the end of the day (approx. 23:59). Here's the code:

    func testCreate_FromAppointment_WithStartDateIsTomorrow_ShouldMapStartTimeCounterAccordingly() throws {
        let dateTomorrow = DateInRegion() + 1.days
        let startDateTomorrow = DateInRegion(
            year: dateTomorrow.year, month: dateTomorrow.month, day: dateTomorrow.day,
            hour: 0, minute: 1, second: 0,
            region: Region.current)
        let entity = Appointment.sample(startDate: startDateTomorrow)

        XCTAssertEqual("Tomorrow", AppointmentStartTimerContent.create(from: entity))
    }

This test works only 17 hours of the day. Because I am at UTC+7 TimeZone, It always fails at 17.00 UTC - 24.00 UTC because the dateTomorrow.day always returns today's day component.

Is this by design? If it is, it seems useless and can deceive the developers (like what I experienced myself). Is there another way for me to get the day component in a certain region?

Thanks.