Comments (5)
@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?
from timepiece.
beginningOfHour
should return +1030 value when receiver was +1030
Sorry, above is not correct.
Maybe result should be as following:
2015-04-05 02:55:00 +1030(2015-04-04 16:25:00 +0000) is in 2015-04-05 02:00:00 +1030(2015-04-04 15:30:00 +0000)
2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:05:00 +0930(2015-04-04 16:35:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:10:00 +0930(2015-04-04 16:40:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:15:00 +0930(2015-04-04 16:45:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:20:00 +0930(2015-04-04 16:50:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:25:00 +0930(2015-04-04 16:55:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:30:00 +0930(2015-04-04 17:00:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:35:00 +0930(2015-04-04 17:05:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:40:00 +0930(2015-04-04 17:10:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:45:00 +0930(2015-04-04 17:15:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:50:00 +0930(2015-04-04 17:20:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 02:55:00 +0930(2015-04-04 17:25:00 +0000) is in 2015-04-05 02:00:00 +0930(2015-04-04 16:30:00 +0000)
2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000) is in 2015-04-05 03:00:00 +0930(2015-04-04 17:30:00 +0000)
I don't know about Timepiece's NSDate extension for supporting timezone.
But, following code gets above result.
//: 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
}
from timepiece.
@norio-nomura What problem does your code demonstrate?
from timepiece.
Sorry, my code is too complex for describing issue.
I make it simple.
//: 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
}
step by 1 hour | beginningOfHour | NG? |
---|---|---|
2015-04-05 00:00:00 +1030 | 2015-04-05 00:00:00 +1030 | |
2015-04-05 01:00:00 +1030 | 2015-04-05 01:00:00 +1030 | |
2015-04-05 02:00:00 +1030 | 2015-04-05 02:00:00 +1030 | |
2015-04-05 02:00:00 +0930 | 2015-04-05 02:00:00 +1030 | <- NG |
2015-04-05 03:00:00 +0930 | 2015-04-05 03:00:00 +0930 | |
2015-04-05 04:00:00 +0930 | 2015-04-05 04:00:00 +0930 | |
2015-04-05 05:00:00 +0930 | 2015-04-05 05:00:00 +0930 | |
2015-04-05 06:00:00 +0930 | 2015-04-05 06:00:00 +0930 | |
2015-04-05 07:00:00 +0930 | 2015-04-05 07:00:00 +0930 | |
2015-04-05 08:00:00 +0930 | 2015-04-05 08:00:00 +0930 | |
2015-04-05 09:00:00 +0930 | 2015-04-05 09:00:00 +0930 | |
2015-04-05 10:00:00 +0930 | 2015-04-05 10:00:00 +0930 | |
2015-04-05 11:00:00 +0930 | 2015-04-05 11:00:00 +0930 | |
2015-04-05 12:00:00 +0930 | 2015-04-05 12:00:00 +0930 | |
2015-04-05 13:00:00 +0930 | 2015-04-05 13:00:00 +0930 | |
2015-04-05 14:00:00 +0930 | 2015-04-05 14:00:00 +0930 | |
2015-04-05 15:00:00 +0930 | 2015-04-05 15:00:00 +0930 | |
2015-04-05 16:00:00 +0930 | 2015-04-05 16:00:00 +0930 | |
2015-04-05 17:00:00 +0930 | 2015-04-05 17:00:00 +0930 | |
2015-04-05 18:00:00 +0930 | 2015-04-05 18:00:00 +0930 | |
2015-04-05 19:00:00 +0930 | 2015-04-05 19:00:00 +0930 | |
2015-04-05 20:00:00 +0930 | 2015-04-05 20:00:00 +0930 | |
2015-04-05 21:00:00 +0930 | 2015-04-05 21:00:00 +0930 | |
2015-04-05 22:00:00 +0930 | 2015-04-05 22:00:00 +0930 | |
2015-04-05 23:00:00 +0930 | 2015-04-05 23:00:00 +0930 |
from timepiece.
@norio-nomura Thanks! I will try to write tests and fix the problem.
from timepiece.
Related Issues (20)
- problems installing with CocoaPods
- Swift Package Manager support, please make this happen
- A new release version?
- SPM 1.3.1 version doesn't work HOT 1
- syntactic sugar for week days HOT 3
- Missing interval ? HOT 2
- inFormat is returning wrong weekday HOT 5
- Weekend HOT 5
- How to modify a date. HOT 4
- How to modify the time zone HOT 4
- changing timezone HOT 2
- Shortcut for timeIntervalSince(_:) HOT 3
- time ago support HOT 3
- Wrong time retrieved
- How to find number of days in a particular year? HOT 1
- endOfDay handles date differently for iOS 10 as it does for iOS 11
- Change the default calendar HOT 4
- Can you add more date math examples to readme? HOT 2
- Swift 4.2 HOT 1
- swift 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from timepiece.