import TroposCore
import Quick
import Nimble

private func dateFromString(string: String) -> NSDate {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
    return formatter.dateFromString(string)!
}

final class TemperatureComparisonFormatterSpec: QuickSpec {
    override func spec() {
        describe("TRTemperatureComparisonFormatter") {
            describe("localizedStringFromComparison:adjective:precipitation") {
                it("bases the time of day off of the date") {
                    let previousTimeZone = NSTimeZone.defaultTimeZone()
                    NSTimeZone.setDefaultTimeZone(NSTimeZone(abbreviation: "UTC")!)
                    defer { NSTimeZone.setDefaultTimeZone(previousTimeZone) }

                    let date = dateFromString("2015-05-15 22:00:00 UTC")
                    let (description, _) = TemperatureComparisonFormatter().localizedStrings(fromComparison: .Same, precipitation: "", date: date)
                    expect(description) == "It's the same tonight as last night."
                }
            }
        }
    }
}

import TroposCore
import Quick
import Nimble

private func dateFromString(string: String) -> NSDate {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
    return formatter.dateFromString(string)!
}

final class TemperatureComparisonFormatterSpec: QuickSpec {
    override func spec() {
        describe("TRTemperatureComparisonFormatter") {
            describe("localizedStringFromComparison:adjective:precipitation") {
                it("bases the time of day off of the date") {
                    let previousTimeZone = NSCalendar.currentCalendar().timeZone
                    NSCalendar.currentCalendar().timeZone = NSTimeZone(abbreviation: "UTC")!
                    defer { NSCalendar.currentCalendar().timeZone = previousTimeZone }

                    let date = dateFromString("2015-05-15 22:00:00 UTC")
                    let (description, _) = TemperatureComparisonFormatter().localizedStrings(fromComparison: .Same, precipitation: "", date: date)
                    expect(description) == "It's the same tonight as last night."
                }
            }
        }
    }
}

--- 
+++ 
@@ -13,9 +13,9 @@
         describe("TRTemperatureComparisonFormatter") {
             describe("localizedStringFromComparison:adjective:precipitation") {
                 it("bases the time of day off of the date") {
-                    let previousTimeZone = NSCalendar.currentCalendar().timeZone
-                    NSCalendar.currentCalendar().timeZone = NSTimeZone(abbreviation: "UTC")!
-                    defer { NSCalendar.currentCalendar().timeZone = previousTimeZone }
+                    let previousTimeZone = NSTimeZone.defaultTimeZone()
+                    NSTimeZone.setDefaultTimeZone(NSTimeZone(abbreviation: "UTC")!)
+                    defer { NSTimeZone.setDefaultTimeZone(previousTimeZone) }
 
                     let date = dateFromString("2015-05-15 22:00:00 UTC")
                     let (description, _) = TemperatureComparisonFormatter().localizedStrings(fromComparison: .Same, precipitation: "", date: date)
Fix test failure when running in PST

Previously the current calendar's time zone was configured to UTC, but
when a date was constructed using `NSDateFormatter` the default time
zone would be used. When the default time zone is PST, the created time
fell into the "afternoon" range instead of the expected "night" range,
and thus the test would fail.

Instead of updating the current calendar's timezone, we instead update
the default timezone, so that the date formatter is in the correct time
zone when the test runs.
