为什么 date.dateByAddingTimeInterval(seconds) 会延迟 1 小时?
Why is date.dateByAddingTimeInterval(seconds) off by 1 hour?
为什么 .dateByAddingTimeInterval() 返回 1 小时的时差?夏令时目前没有生效,所以这应该不会影响它。我做错了什么?
import Foundation
struct Gigasecond{
static func newDate(input:String, format:String = "yyyy-MM-dd" ) -> NSDate{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.dateFromString(input) ?? NSDate.distantFuture() as NSDate
}
static func from(dateInString:String) -> NSDate{
var dateIn = newDate(dateInString)
return dateIn.dateByAddingTimeInterval(1_000_000_000)
}
}
let newDate23 = Gigasecond.newDate("2043-01-01T01:46:40", format:"yyyy-MM-dd'T'HH:mm:ss")
println(Gigasecond.from("2011-4-25").timeIntervalSinceDate(newDate23)) //-3600.0 or 1 hour ( 60*60 )
如果您显式设置 timeZone 格式化程序会怎样?
struct Gigasecond{
static func newDate(input:String, format:String = "yyyy-MM-dd" ) -> NSDate{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
// Explicitly set the time Zone for the formatter
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
return dateFormatter.dateFromString(input) ?? NSDate.distantFuture() as NSDate
}
static func from(dateInString:String) -> NSDate{
var dateIn = newDate(dateInString)
return dateIn.dateByAddingTimeInterval(1_000_000_000)
}
}
let newDate23 = Gigasecond.newDate("2011-4-25T01:46:40", format:"yyyy-MM-dd'T'HH:mm:ss")
println(Gigasecond.from("2011-4-25").timeIntervalSinceDate(newDate23))
为什么 .dateByAddingTimeInterval() 返回 1 小时的时差?夏令时目前没有生效,所以这应该不会影响它。我做错了什么?
import Foundation
struct Gigasecond{
static func newDate(input:String, format:String = "yyyy-MM-dd" ) -> NSDate{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.dateFromString(input) ?? NSDate.distantFuture() as NSDate
}
static func from(dateInString:String) -> NSDate{
var dateIn = newDate(dateInString)
return dateIn.dateByAddingTimeInterval(1_000_000_000)
}
}
let newDate23 = Gigasecond.newDate("2043-01-01T01:46:40", format:"yyyy-MM-dd'T'HH:mm:ss")
println(Gigasecond.from("2011-4-25").timeIntervalSinceDate(newDate23)) //-3600.0 or 1 hour ( 60*60 )
如果您显式设置 timeZone 格式化程序会怎样?
struct Gigasecond{
static func newDate(input:String, format:String = "yyyy-MM-dd" ) -> NSDate{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
// Explicitly set the time Zone for the formatter
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
return dateFormatter.dateFromString(input) ?? NSDate.distantFuture() as NSDate
}
static func from(dateInString:String) -> NSDate{
var dateIn = newDate(dateInString)
return dateIn.dateByAddingTimeInterval(1_000_000_000)
}
}
let newDate23 = Gigasecond.newDate("2011-4-25T01:46:40", format:"yyyy-MM-dd'T'HH:mm:ss")
println(Gigasecond.from("2011-4-25").timeIntervalSinceDate(newDate23))