views:

650

answers:

2

Is there a good+easy way to convert an Oracle Timestamp to an NSDate object on the iPhone? I have asked my customer to give me a DB with timestamps as Unix Timestamp (doubles with 0 = start of 1970) but it seems to be a problem for them. Thanks.

Note: You can easily convert a Unix Timestamp to an NSDate with

[NSDate dateWithTimeIntervalSince1970: timestamp]  // timestamp is a "double"

What I need to do is convert the Oracle timestamp to a double, aka Unix Timestamp. The rest is easy.

More info: I need Objective-C code to run on the iPhone using NSStrings that represent dates in Oracle Timestamp format. Another StackOverflow thread suggests using NSDateFormatter, but I don't know what format to use and the initialization method for the formatter suggested in that thread generates a warning for me.

+2  A: 

I'm not sure about the converting to an NSDate, but you can convert an Oracle date to a unix timestamp easily. The following SQL snippet will do it:

(my_date - to_date('01/01/1970','DD/MM/YYYY')) * 86400
ar
Sorry, but I'm not clear on the context for that. to_date is not an iPhone SDK function. (And see original post for NSDate part.)
Amagrammer
@Amagrammer: to_date( ) is a function in Oracle within SQL. Ar would intend you to run this snippet against the Oracle database via SQL when retrieving out your date value, or have your Customer run this and give you that output instead of the Oracle date.
Dougman
My apologies, if I wasn't clear on that point.
ar
No apologies needed. However, I am not actually working with an Oracle DB. Rather, I am working with an SQLite DB, which I will construct on the iPhone from data pulled down from a PHP page. I need Objective-C code to run on the iPhone.
Amagrammer
I'm going to accept this answer as the best solution, even though it requires me to ask the customer to do the work for me, using the method "ar" suggests. Thanks
Amagrammer
A: 

It looks like my best bet is to use the following:

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy HH:mm a"];

I'm not sure how much variance there is in Oracle timestamps, so if there is a problem with the above approach, I'd love to know.

Thanks for the other answers. I will leave the question open to invite other approaches.

Amagrammer