Giter Site home page Giter Site logo

Comments (5)

naoty avatar naoty commented on June 10, 2024

@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.

norio-nomura avatar norio-nomura commented on June 10, 2024

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.

naoty avatar naoty commented on June 10, 2024

@norio-nomura What problem does your code demonstrate?

from timepiece.

norio-nomura avatar norio-nomura commented on June 10, 2024

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.

naoty avatar naoty commented on June 10, 2024

@norio-nomura Thanks! I will try to write tests and fix the problem.

from timepiece.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.