Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

选择器有时差问题 #5

Closed
carry1024 opened this issue Nov 12, 2019 · 10 comments
Closed

选择器有时差问题 #5

carry1024 opened this issue Nov 12, 2019 · 10 comments

Comments

@carry1024
Copy link

我选择一个时间,确认后会自动往前+8个时区。

@CXTretar
Copy link
Owner

我在模拟器和真机都测试了一下,没有发现时区有变化的问题,你能详细描述一下或者确认你的测试环境吗?

@carry1024
Copy link
Author

我用的是真机测试的,地区选的是北京。我测试发现是NSDate+CXCategory这里面的+ (NSDate *)date:(NSString *)datestr WithFormat:(NSString *)format 这个方法,当返回出的date和datestr就出现了8个小时的偏移,然后我不知道是不是其他的地方有调整回来,我去- (instancetype)initWithDateStyle:(CXDateStyle)datePickerStyle CompleteBlock:(void(^)(NSDate *date))completeBloc调用这个方法回调的时候,获取到的就是偏移过后的时间了。

@CXTretar
Copy link
Owner

  • (NSDate *)date:(NSString *)datestr WithFormat:(NSString *)format {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    NSLog(@"%@", [NSTimeZone localTimeZone]);
    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:datestr];
    #if ! __has_feature(objc_arc)
    [dateFormatter release];
    #endif
    return date;
    }

这个方法里,我已经设定了时区是本地时区。打印结果如下:
2019-11-13 09:47:21.820334+0800 CXDatePickerView[95677:4135150] Local Time Zone (Asia/Shanghai (GMT+8) offset 28800)
2019-11-13 09:47:30.738968+0800 CXDatePickerView[95677:4135150] 选择的日期:2019-11-13 09:47
2019-11-13 09:47:30.739342+0800 CXDatePickerView[95677:4135150] Wed Nov 13 09:47:00 2019

@CXTretar
Copy link
Owner

你能帮我打印一下你那边的信息吗?

@carry1024
Copy link
Author

format __NSCFConstantString * @"yyyy-MM-dd HH:mm" 0x0000000102710120
datestr __NSCFString * @"2019-11-12 10:14" 0x0000000281101b60
NSMutableString NSMutableString
dateFormatter NSDateFormatter * 0x281101b30 0x0000000281101b30
date __NSTaggedDate * 2019-11-12 02:14:00 UTC 0xaea60e7155656a94

@carry1024
Copy link
Author

这个是那个方法,我断点打印的数据

@carry1024
Copy link
Author

po selectDate
2019-11-10 16:00:00 +0000

2019-11-13 10:17:58.603515+0800 Airport[1483:694060] Mon Nov 11 00:00:00 2019
这里是你的打印和返回出来我po出来的回调那个时间,我选择的是2019-11-11

@CXTretar
Copy link
Owner

哦哦哦,我懂你的意思了。
2019-11-13 10:42:25.903905+0800 CXDatePickerView[97194:4185430] Local Time Zone (Asia/Shanghai (GMT+8) offset 28800)
2019-11-13 10:42:25.905723+0800 CXDatePickerView[97194:4185430] Local Time Zone (Asia/Shanghai (GMT+8) offset 28800)
(lldb) po selectDate
2019-11-13 02:42:00 +0000

(lldb) po dateString
2019-11-13 10:42

(lldb)
NSDate对象描述的是时间线上的一个绝对的值,和时区和国家无关的,直接打印都是0时区的,使用时一般是转成时间戳或者字符串。

@carry1024
Copy link
Author

哦,这样的,我是直接使用的date。所以是这个问题吧,我转换一下吧,谢谢细心给我解答

@CXTretar
Copy link
Owner

嗯嗯,很高兴能帮到你。

@CXTretar CXTretar closed this as completed May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants