-1

I am currently working on bid application,bid APP works mainly based on time i used to calculated the time by using

NSLog(@"date %d",(int)[[NSDate date] timeIntervalSince1970]);
output:1409031539

I took the server time and compare the device time and calculated, but while change the time zone this not working time get varies for that i used this code.

 NSLocale* currentLocale = [NSLocale currentLocale];
 NSLog(@"date: %d",(int)[[NSDate date] descriptionWithLocale:currentLocale]);

output:196208512

But this is not helping me to calculate the time when I run the code, I see the different output.

PREMKUMAR
  • 8,283
  • 8
  • 28
  • 51
sadeesh
  • 343
  • 1
  • 8

1 Answers1

0

This line:

 NSLog(@"date: %d",(int)[[NSDate date] descriptionWithLocale:currentLocale]);

is so wrong it hurts my head. The returned data from descriptionWithLocale is an NSString *, not an NSTimeInterval, printing it as an int is just converting a pointer to an int, which is a bad idea at the best of times.

I'm presuming that your server is returning a timestamp that is #seconds after 1970 as well, so I just hard-coded an example of that in this piece of code:

NSLocale* currentLocale = [NSLocale currentLocale];
NSTimeInterval nsti = [[NSDate date] timeIntervalSince1970];
NSLog(@"date (as seconds from 1970): %ld", (long)nsti);
NSLog(@"date (as locale-relative string): %@", [[NSDate dateWithTimeIntervalSince1970:nsti] descriptionWithLocale:currentLocale]);
 // This is Mon 25 Aug 2014, 20:17:51 BST (UTC +0100) obtained by date +%s, subtracting 60,000
NSTimeInterval time_from_server = 1408994271;
NSLog(@"date (from server as string): %@", [[NSDate dateWithTimeIntervalSince1970:time_from_server] descriptionWithLocale:currentLocale]);

Note, however that neither an NSDate or an NSTimeInterval have any concept of a timezone, and thus don't have a concept of an offset from GMT. What you need for that is an NSTimeZone. Bearing in mind that the offset from GMT depends on the current date/time (e.g. summer-time, winter-time), you have to use code like the following:

NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSDate *date111 = [NSDate date];
NSInteger delta = [localZone secondsFromGMTForDate:date111];
NSLog(@"date= %ld", (long)delta);

Get the timezone, get the date, get the offset from GMT for the date, log it.

Displaying time as a specific timezone's local time:

NSTimeZone *phoenix = [NSTimeZone timeZoneWithName:@"America/Phoenix"];
NSTimeZone *dublin = [NSTimeZone timeZoneWithName:@"Europe/Dublin"];
NSTimeZone *tokyo = [NSTimeZone timeZoneWithName:@"Asia/Tokyo"];
NSDate *date111 = [NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateStyle:NSDateFormatterFullStyle];
[fmt setTimeStyle:NSDateFormatterFullStyle];
[fmt setLocale:[NSLocale currentLocale]];
[fmt setTimeZone:phoenix];
NSLog(@"phoenix %@", [fmt stringFromDate:date111]);
[fmt setTimeZone:dublin];
NSLog(@"dublin %@", [fmt stringFromDate:date111]);
[fmt setTimeZone:tokyo];
NSLog(@"tokyo %@", [fmt stringFromDate:date111]);

Gives me (as of now):

phoenix Tuesday, August 26, 2014 at 9:33:12 AM Mountain Standard Time
dublin Tuesday, August 26, 2014 at 5:33:12 PM Irish Summer Time
tokyo Wednesday, August 27, 2014 at 1:33:12 AM Japan Standard Time

The fundamental concepts here are:

  • NSDate doesn't have a concept of a timezone
  • NSTimeInterval doesn't have a concept of a timezone
  • Only the act of formatting a time for display needs a timezone

So with that in mind, when you get device time it's just either an NSDate or an NSTimeInterval which means that it does not encode the timezone into it.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • instead of [NSDate date]i need date111 as seconds.... NSLocale* currentLocale = [NSLocale currentLocale]; NSLog(@"Date: %@",[[NSDate date] descriptionWithLocale:currentLocale]); NSDate *date111 = (NSDate*)[[NSDate date] descriptionWithLocale:currentLocale] ; NSTimeInterval interval=[date111 timeIntervalSince1970]; NSLog(@"date= %f",interval); – sadeesh Aug 26 '14 at 12:42
  • i tried this code but app get crashed in Does NSTimeinterval doesn't take locale time? – sadeesh Aug 26 '14 at 12:43
  • You can't cast an `NSString *` to an `NSDate` - that's simply not going to work (this is the `date111 = ...` line). This sounds like an [XY problem](http://mywiki.wooledge.org/XyProblem). What is this interval supposed to represent? Seconds difference between UTC time and local time? – Anya Shenanigans Aug 26 '14 at 13:02
  • I've updated the answer with getting the seconds offset from GMT for an NSDate using the local timezone. – Anya Shenanigans Aug 26 '14 at 13:54
  • But if the printed nslog i check with American/Phoneix timeZone it shows in 1970.i used this Url to check the timestamp.http://www.timestampconvert.com/?go2=true&offset=-5.5&timestamp=11409106681&Submit=++++++Convert+to+Date++++++ – sadeesh Aug 26 '14 at 15:43
  • That timezone is called `America/Phoenix`. I'm still at a loss as to what you're trying to accomplish though, You need to use an NSDateFormatter to display times from different timezones. I'll update the answer with an example of doing that – Anya Shenanigans Aug 26 '14 at 16:30
  • I need this exact line in to seconds,phoenix Tuesday, August 26, 2014 at 9:33:12 AM Mountain Standard Time.....how to convert this Date and time in to seconds. – sadeesh Aug 27 '14 at 04:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60054/discussion-between-petesh-and-sadeesh). – Anya Shenanigans Aug 27 '14 at 07:55