質問

I'm working on an application that uses a lot of dates, but that doesn't rely on times at all. I'd like to store them internally as NSDates so that I can use NSDatePicker controls, built-in date functions, etc.

However, I keep tripping over the fact that NSDates insist on storing a time as well as a date. Many of the date initialization functions append either the current time or an arbitrary time. I've written my code to wipe out the times on all dates by setting the time components to 0 - but even so, time zone issues screw up some of the comparisons (e.g., comparing the current date to a specific date produces the wrong answer if the current date in the local time zone is different than the date in GMT).

Surely there's a better way to tackle this problem...?

役に立ちましたか?

解決

The answer, as Thilo mentioned, was to set a fixed time zone.

Note that that wasn't quite enough. Initially, I chose GMT, and just fixed the time at 00:00:00. As it happens, that's kind of a stupid move, because any actual adjustment of the time can cause problems. Like, say... loading that value into a DatePicker control, and then using the application around 1am in a time zone west of GMT. Whoops.

To work around most of those problems, I instead chose GMT and set the time at a fixed 12:00:00. That should keep dates concurrent for eleven time zones east or west of GMT.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top