-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NSDate.beginningOfHour is not compatible with Daylight Saving Time. #31
Comments
@norio-nomura Thank you for reporting! Do you mean that a NSDate object which has own time zone is not compatible with daylight-saving time? |
Sorry, above is not correct.
I don't know about Timepiece's NSDate extension for supporting timezone. //: Playground - noun: a place where people can play
import Foundation
import Timepiece
extension NSCalendar {
public func startOfHourForDate(date: NSDate) -> NSDate {
let comp = components(.CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitNanosecond, fromDate: date)
comp.minute *= -1
comp.second *= -1
comp.nanosecond *= -1
return dateByAddingComponents(comp, toDate: date, options: .MatchNextTime)!
}
}
let timeZone = NSTimeZone(name: "Australia/Adelaide")!
timeZone.daylightSavingTime // true
NSTimeZone.setDefaultTimeZone(timeZone)
let calendar = NSCalendar.currentCalendar()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
// This Year
let comp = calendar.components(.CalendarUnitEra | .CalendarUnitYear, fromDate: NSDate())
let startOfThisYear = calendar.dateFromComponents(comp)!
var prevDate = startOfThisYear
// transition
while let transition = timeZone.nextDaylightSavingTimeTransitionAfterDate(prevDate)
where calendar.isDate(transition, equalToDate: startOfThisYear, toUnitGranularity: .CalendarUnitYear) {
println("transition: \(formatter.stringFromDate(transition))(\(transition))")
let twoHourBefore = transition - 2.hours
let twoHourAfter = transition + 2.hours
var time = twoHourBefore
while time <= twoHourAfter {
let beginningOfHour = calendar.startOfHourForDate(time)
// check equality by hour
if calendar.isDate(time, equalToDate: beginningOfHour, toUnitGranularity: .CalendarUnitHour) {
println("\(formatter.stringFromDate(time))(\(time)) is in \(formatter.stringFromDate(beginningOfHour))(\(beginningOfHour))")
} else {
println("\(formatter.stringFromDate(time))(\(time)) is not in \(formatter.stringFromDate(beginningOfHour))(\(beginningOfHour))")
}
time = time + 5.minutes
}
prevDate = transition
} |
@norio-nomura What problem does your code demonstrate? |
Sorry, my code is too complex for describing issue. //: Playground - noun: a place where people can play
import Foundation
import Timepiece
let timeZone = NSTimeZone(name: "Australia/Adelaide")!
timeZone.daylightSavingTime // true
NSTimeZone.setDefaultTimeZone(timeZone)
let calendar = NSCalendar.currentCalendar()
let transitionDay = calendar.dateWithEra(1, year: 2015, month: 4, day: 5, hour: 0, minute: 0, second: 0, nanosecond: 0)!
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
var stepBy1Hour = transitionDay
println("step by 1 hour | beginningOfHour | NG?")
println("-------------- | --------------- | ---")
while calendar.isDate(stepBy1Hour, equalToDate: transitionDay, toUnitGranularity: .CalendarUnitDay) {
let beginningOfHour = stepBy1Hour.beginningOfHour
print("\(formatter.stringFromDate(stepBy1Hour))|\(formatter.stringFromDate(beginningOfHour))")
if calendar.isDate(stepBy1Hour, equalToDate: beginningOfHour, toUnitGranularity: .CalendarUnitHour) {
println("|")
} else {
println("|<- NG")
}
stepBy1Hour = stepBy1Hour + 1.hour
}
|
@norio-nomura Thanks! I will try to write tests and fix the problem. |
Sample code is following:
result is following:
On above result, "2015-04-05 02:00:00" appear twice on +1030 and +0930 before transition.
beginningOfHour
should return +1030 value when receiver was +1030The text was updated successfully, but these errors were encountered: