Question

So, i'm trying to create a NSDate object for sunrise and sunset. I get the date based on NSDatePicker, i get coordinates from a Map, and i get the timezone from the GPS from the map.

I use this code to get the NSDate object: https://github.com/MosheBerman/KosherCocoa-legacy This one to get the coordinates: https://github.com/digdog/MapKitDragAndDrop And this one to get the the timezone based on coordinates: https://github.com/Alterplay/APTimeZones.

Right now my physical location is in Los Angeles, and the sunrise and sunset i'm using to testing is back home in Denmark.

-(NSString *)sunriseDate{
    //Create the GeoLocation based on 'latitude' and 'longitude' (getting the from MapKitDragAndDrop) and 'location.timeZone' (getting that from APTimeZones).
    GeoLocation *position = [[GeoLocation alloc] initWithName:@"position" andLatitude:latitude andLongitude:longitude andTimeZone:location.timeZone];

    AstronomicalCalendar *astronomicalCalender = [[AstronomicalCalendar alloc] initWithLocation:position];

    //daysBetween is the value from NSDatePicker
    astronomicalCalender.workingDate = [NSDate dateWithTimeIntervalSinceNow:kSecondsInADay*[self daysBetween]];

    NSDate *sunriseDate = [astronomicalCalender sunrise];
    NSLog(@"Sunrise time: %@", sunriseDate);
    //This spits out: Sunrise time: 2014-03-05 06:09:53 AM +0000 which is the right time.
    NSDateFormatter *sunriseTime = [[NSDateFormatter alloc] init];
    [sunriseTime setDateFormat:@"HH:mm:ss"];

    NSString *sunriseString = [sunriseTime stringFromDate:sunriseDate];
    NSLog(@"Sunrisestring: %@", sunriseString);
    //This spits out: 10:09:53 PM. 
    return sunriseString;

}

Why does this happen and can anyone maybe give me a solution to this?

Was it helpful?

Solution

To anyone who might stumble into the same thing.

I found a library on github https://github.com/Alterplay/APTimeZones that helped me determine the timezone based on the coordinates.

Then i used

[sunriseTime setTimeZone:location.timeZone];

This put out the right time for the timezone.

Hope this helps anyone!

OTHER TIPS

You need to match the input format correctly.

You may only be interested in the time but the NSDateFormatter doesn't care. NSDate is never JUST a time. It is a point in time and so includes the date too. It doesn't work without the date and time sections.

Also, this is probably one of THE MOST ASKED questions on Stack Overflow. Any other NSDate to NSString (or vice versa) question will answer this.

Your date format should be...

@"YYYY-MM-dd hh:mm:ss a EEEE"

I believe. Something like that anyway.

This spits out: 10:09:53 PM. This is correct local time for your time zone, which differs by 8 hours from Greenwich time Sunrise time: 2014-03-05 06:09:53 AM +0000. That's all. You have evening when a german man wakes up.

As Fogmeister said, you should include the timezone when creating a NSDateFormatter. Take a loot at Apple's Docs, Data Formatting Guide:

Fixed Formats To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns from the Unicode Technical Standard #35.

The Unicode official site:

http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

You may try using this: [sunriseTime setDateFormat:@"yyyy-MM-dd hh:mm:ss a EEEE"];

instead of [sunriseTime setDateFormat:@"HH:mm:ss"];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top