naoty / Timepiece

Intuitive date handling in Swift
MIT License
2.63k stars 147 forks source link

Timezones #24

Closed Prince2k3 closed 9 years ago

Prince2k3 commented 9 years ago

First off. I like the progress of this library. It's very elegant. So to my issue....I think this is more a feature request then an issue. I was thinking would be very nice to be able to set / get Timezones. What do you think?

naoty commented 9 years ago

@Prince2k3 Thanks! Now, this library uses NSCalendar's timezone, which the user specified at Settings. When do you want to set/get timezone?

Prince2k3 commented 9 years ago

So I have a scenario where I would need to check the date at a certain timezone because a server only acts within it. Like for instance checking to see if a transaction is valid before a certain time in CST.

naoty commented 9 years ago

Oh, I see. For example, is it favorable for you to set/get timezone like below?

let cst = NSTimeZone(abbreviation: "CST")
let lastWeekInCST = 1.week.ago.in(timezone: cst)

lastWeekInCST.timezone
//=> cst
Prince2k3 commented 9 years ago

Yes ...that would be very nice setup!

naoty commented 9 years ago

OK! I will try to implement above feature!

Prince2k3 commented 9 years ago

Sweet! Thanks!

naoty commented 9 years ago

@Prince2k3, I have implemented features to set/get the time zone. Please check the playground at https://github.com/naoty/Timepiece/commit/24a117399b7c636ff6db9dace45952b89b4b02f2! If there is no problem, I will merge this and release as the next version.

naoty commented 9 years ago

I noticed that the calculation and comparison of dates with different time zones are not implemented. After they are implemented, I will release this feature.

Prince2k3 commented 9 years ago

Hey tested the the timezone feature and it works. Yeah if there is any way to sort the length of this code even more. Then I'm all for it!

extension NSDate {
    func canCancel() -> Bool {
        var currentDate = NSDate().change(timeZone: NSTimeZone(abbreviation: "CST")!)

        if currentDate.day == day && (currentDate.hour < 13 || (currentDate.hour > 13 && hour > 13)) {
            return true
        } else if (currentDate.timeIntervalSinceDate(self) < 86400) && (hour >= 13) && currentDate.hour < 13 {
            return true
        }
        return false
    }
}
Prince2k3 commented 9 years ago

What did you have in mind for calculation and comparison? So far I seem to do that just fine if I make sure that the timezones are the same.

naoty commented 9 years ago

For example, I think now == now.change(timeZone: cst) should be true. But, actually the result is false in current implementation. When we calculate or compare dates with different time zones, we should change the time zone of either date into one of another date.

naoty commented 9 years ago

I added tests for above cases, which are all passed without any additional implementations. It looks fine. @Prince2k3, can I merge this branch?

Prince2k3 commented 9 years ago

Yes! ... I think it looks good as well!

naoty commented 9 years ago

@Prince2k3 Thanks!