Skip to content

Instantly share code, notes, and snippets.

@kai-vala
Last active February 26, 2022 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kai-vala/5689c17cb72a00186e5c2fdcd563362d to your computer and use it in GitHub Desktop.
Save kai-vala/5689c17cb72a00186e5c2fdcd563362d to your computer and use it in GitHub Desktop.
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")
@Lunna2528
Copy link

print(primerDíaDeLaSemana.IntervaloDeTiempoDesde1970)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment