mardi 30 juin 2015

Storing NSDate in SQLite retrieving and displaying shows in GMT, need to display time in original timezone it was created in

When user creates a new record, I store the NSDate in SQLite with local timezone (say PST). When the same NSDate is retrieved from DB, the time is treated as GMT (which is understandable, since NSDate has no concept of timezone and is absolute).

I want to display the NSDate with the time as per the timezone it was created in - For example, if NSDate is stored in a PST timezone and later, user moves to Northeast US, I still want to be able to retrieve the NSDate from DB and display the time in PST (and not EDT). Should I store the local timezone when the record is created, so that that timezone is used in displaying the time?

This is my code, any help is appreciated.

NSDate *date = xxxxxxxxxxx; // This is retrieved from DB
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"MM/dd/yyyy hh:mm:ss";
NSString *timeStr = [format stringFromDate:date];
NSLog(@"Time :%@",timeStr);

Aucun commentaire:

Enregistrer un commentaire