views:

65

answers:

1

I have been going up and down through all kinds of reference docs and examples over the web but I just can't get how to get what day, month, year, hours, minutes will it be for a given timestamp in Objective C. Any ideas? The code I'm using for the purpose is like this:

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:1286181000];
    unsigned int compFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:compFlags fromDate:date];
    NSLog(@"%d %d, %d:%d", [weekdayComponents day], [weekdayComponents month], [weekdayComponents hour], [weekdayComponents minute]);
    [date release];
    [weekdayComponents release];

Although the timestamp is for a day in October, the output gives me 19th of December.

A: 

The docs for dateWithTimeIntervalSince1970: say:

This method is useful for creating NSDate objects from time_t values returned by BSD system functions.

Can you show the code that gave you that magic number 1286181000? i.e. are you using a time_t value?

Shaggy Frog
The number comes from a value stored in an integer column of a database and instead of that number really is a call to that function in the code which returns a NSNumber type and the value is something like the magic number I have pasted. Is there anyway I can typecast it into NSTimeInterval or something. I have been trying a lot this weekend but getting nowhere, things like: NSTimeInterval t = (long)[db getColumn:@"timestamp"]. So any ideas?
kumar
So I got it working, like: NSTimeInterval t = [[db getColumn:@"timestamp"] doubleValue]; and then using the NSTimeInterval later on in crafting the NSDate. The db class object returned an NSNumber reference and hence the situation. Thanks.
kumar
@kumar glad you got it working. If you found this answer helpful, feel free to mark it as "answered" and give it an upvote.
Shaggy Frog