naoty / Timepiece

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

can't change the begin time of day #51

Closed Huanqiang closed 8 years ago

Huanqiang commented 8 years ago

你好:我在尝试设置今天的开始时间(就是0点0分),但是获取的结果不正确。代码如下 1ea4142f-6161-410b-bf68-9cdee780dcb4 bebb3a14-359b-4bee-89b5-8e0d6040117e

Huanqiang commented 8 years ago

我怀疑是不是时区的问题呢?但是我不知道怎么改,还请麻烦您看下。

naoty commented 8 years ago

I don't understand Chinese. Would you tell me your issue in English?

Huanqiang commented 8 years ago

@naoty Sorry: my English is not good. I am trying to set the start time of today (for example: 2016-01-29 00:00:00), but the result is not right. Code as follows: 1ea4142f-6161-410b-bf68-9cdee780dcb4 bebb3a14-359b-4bee-89b5-8e0d6040117e

Huanqiang commented 8 years ago

I suspect that this problem is related to the time zone? But I do not know how to change, but also please trouble you.

Huanqiang commented 8 years ago

Hi,sir: I'm so sorry to bother you, but I really don't know how to solve this problem. Please, help me.

naoty commented 8 years ago

There is something wrong... I'll try to investigate this problem! Thank you for reporting.

Huanqiang commented 8 years ago

Thank you for your efforts, look forward to your results, thank you

naoty commented 8 years ago

@861645007 Sorry for late reply! I found that the result of print(date) looks strange, but actually todayEnd.hour returns 0. Screenshots are here.

2016-02-13 20 55 34 2016-02-13 20 55 41

This problem is caused by the wrong implementation of CustomStringConvertible for extended NSDate. This protocol defines a customized text format returned by print and so on.

I will try to fix the implementation! Thank you for reporting!

naoty commented 8 years ago

It seems that the override of description, which is defined at CustomStringConvertible, is not allowed. I don't know how to solve this problem...

Huanqiang commented 8 years ago

I think that the problem is UTC and GMT。we can get UTC of time from NSDate(), but current time which we print is GMT, It is a difference of 8 hours(when i use beijing time)

aa2e84e1-6cae-42ae-93a1-f65490a52e21

Huanqiang commented 8 years ago

I don't know if my guess is right, please look at it.

naoty commented 8 years ago

Yes, your guess is right. The result of print ignores the timezone because the implementation of CustomStringConvertible for extended NSDate doesn't consider the timezone. If possible, I want to override describe property to consider the timezone. But, it seems that the override is not allowed...

Huanqiang commented 8 years ago

hi: I think you can use this method to change the time. Although this is not a good way, it can be a temporary solution to this problem. i hope you can find a better method.

func tolocalTime() -> NSDate { let tz = NSTimeZone.localTimeZone() let seconds: NSInteger = tz.secondsFromGMTForDate(self) return NSDate(timeInterval: Double(seconds), sinceDate: self) }

Huanqiang commented 8 years ago
func tolocalTime() -> NSDate {
        let tz = NSTimeZone.localTimeZone()
        let seconds: NSInteger = tz.secondsFromGMTForDate(self)
        return NSDate(timeInterval: Double(seconds), sinceDate: self)
    }
naoty commented 8 years ago

@861645007 If you want to change the timezone, you should use -change(timeZone:) at here.

BTW, would you change the title of this issue in English?

Huanqiang commented 8 years ago

-change(timeZone:) this method can't play a part in this problem

naoty commented 8 years ago

@861645007 Could you send a pull request includes tests regenerating your problem and codes solving them?