-
-
Save kai-vala/5689c17cb72a00186e5c2fdcd563362d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Foundation | |
import XCTest | |
private let formatter: Foundation.DateFormatter = { | |
let dateFormatter = DateFormatter() | |
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | |
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ" | |
return dateFormatter | |
}() | |
var calendar = Calendar.current | |
calendar.locale = Locale(identifier: "fi_FI") | |
calendar.firstWeekday = 2 | |
print(calendar) | |
let dateInWeek16 = formatter.date(from: "2020-04-17 17:20:00 +0300")! | |
var components = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: dateInWeek16) | |
// weekday: 2 weekOfYear: 16 yearForWeekOfYear: 2020 isLeapMonth: false | |
print(components) | |
let firstDayInWeek = calendar.date(from: components)! | |
let day = calendar.component(.day, from: firstDayInWeek) | |
print("Day: \(day)") | |
XCTAssertEqual(day, 13, "Day did not match expected") | |
print("Date: \(firstDayInWeek)") | |
let expectedDate = formatter.date(from: "2020-04-13 00:00:00 +0300")! | |
// The problem here was that I was ignoring the timezone info | |
// With the timezone info in place, this works as expected | |
print(firstDayInWeek.timeIntervalSince1970) | |
print(expectedDate.timeIntervalSince1970) | |
print("Expt: \(expectedDate)") | |
XCTAssertEqual(firstDayInWeek, expectedDate, "Date did not match expected") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
print(primerDíaDeLaSemana.IntervaloDeTiempoDesde1970)