I tried an experiment because I need to be able to generate a unix timestamp (since 1970) in the app I am working on.
NSLog(#"Getting timeIntervalSince1970");
double theLoggedInTokenTimestampDateEpochSeconds = [[NSDate date] timeIntervalSince1970];
This should've returned epoch seconds (since 1970) in GMT (Seconds since Jan 1, 1970). However, when conducting the experiment at at Mon Aug 15 09:54:30 2011, it returned 1313427270.504315
Testing this with a simple perl one-liner on my Mac OS Terminal, I get:
perl -e 'print scalar(localtime(1313427270))' which returns Mon Aug 15 09:54:30 2011 ...
This is obviously not GMT time when I am in the SF Bay Area and my local timezone is set to "Cupertino". What is going on and how do I fix it please? I need to have my app send UTC time to the server when it communicates so wherever the user is time timestamp would be sent in one equal, valid time zone.
In another part of my app, when the user requests data from the server, it gets it sent in UTC -- converting it to be displayed as follows:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:nil];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *conversationDate = [NSDate dateWithTimeIntervalSince1970:[theConversationTimeStampString intValue]];
NSString *conversationDateString = [dateFormatter stringFromDate:conversationDate];
[dateFormatter release];
and this works beautifully -- displaying the corrected time in the user's timezone and locale... so I know it is being done for incoming timestamps. So, what am I doing wrong with the first function (timeIntervalSince190) that stops it from being in GMT?
Thx
I don't think the first function is actually wrong, although it may look like it on the surface. The time interval you are receiving from timeIntervalSince1970 is NOT returning the time interval in GMT. Rather, it is returning the time interval between now and January 1, 1970 00:00:00 GMT. That might seem like a nitpick, but it is important: The interval values are nothing more than a scalar number of seconds since a fixed point in time. The interval itself is not in GMT, only its fixed reference point is.
I don't know perl, but I did a quick search for documentation on local time and it appears to take any time and print convert a standard date type into local time. That means that your time interval describing a fixed point in time is converted back into your local time at that point. When you display it from your command line, you are getting the local time again. So seeing that absolute time translated to your local time is what I would expect to see.
Depending on how exactly your service expects to receive UTC time, your time interval value is likely to be working just fine. Do you have evidence that it is not based on something other than your terminal check?
Related
Currently GMT-0700(US/pacific) is already in day-light-saving
But I am getting "NO" from NSTimeZone
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:secondsFromGMT]; //Getting timezone as GMT-0700
BOOL isDaylightSavingTime = [timeZone isDaylightSavingTime]; //getting boolean value as NO
How to fix this issue?
REQUIREMENT :I want to know ,my receiver is using dayLightSavingTime or not.i will get only receiver offset value.I have to support different timezones()..What is the best approach to do this
timeZoneForSecondsFromGMT is not specific enough.
The most accurate way is to create the time zone with the (full) region name:
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:#"America/Los_Angeles"];
This is not a wrong value. You get timezone GMT-0700 but this is not a Pacific timezone. To create pacific timezone you need:
timeZone = [NSTimeZone timeZoneWithName:#"PST"];
This is short description from apple documentation:
+ (instancetype)timeZoneForSecondsFromGMT:(NSInteger)seconds;
Description Returns a time zone object offset from Greenwich Mean
Time by a given number of seconds. The name of the new time zone is
GMT +/– the offset, in hours and minutes. Time zones created with this
method never have daylight savings, and the offset is constant no
matter the date.
Other answers mentioning timeZoneWithName are correct but there's one more detail I don't think has been mentioned. The reason that timeZoneForSecondsFromGMT doesn't work is that GMT does not have daylight savings time (or summer time, as it's more sensibly called in some other countries). GMT doesn't jump forward or back; it always moves ahead by one second per second. Since you ask for a fixed number of seconds from GMT, the result also does not have GMT. If it gave you a time zone that observed daylight saving time, the number of seconds from GMT would have to change twice a year. But since you asked for a fixed number of seconds, you get a result that doesn't do that, and never reports daylight saving time in effect.
I have two different values of timestamp on same day in different time. So I want to provide format 'yymmdd' to the NSDate to covert date into timestamp since the value changes acc. to time. If anyone could help me in this or suggest me since i am new to Swift.
I was a bit confused about what you were asking for but after reading your own comments/answers to the original post I understand that you are looking for "the epoch unix timestamp". You can easily get this from any NSDate object as follows:
NSDate *yourDate = [NSDate date];//now in this example
NSTimeInterval epochTimestamp = [yourDate timeIntervalSince1970];
NSString *epochTimestampString = [#(epochTimestamp) stringValue];
NSLog(#"%#",epochTimestampString);
This way you will get the epoch timestamp defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.
My server is build with Java and so is my android app (obviously). For certain actions, I require a timestamp from the app. And I store that timestamp on the server. For server and android I use System.currentTimeMillis(). So what iOS or Objective-C function should I use so the time is the same? Should I use CFAbsoluteTimeGetCurrent() or NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSince1970]. Again, I need the time in milliseconds.
Cheaper and faster is always better, and so I was thinking CFAbsoluteTimeGetCurrent(). But that does not seem to be the correct one to use. Why is that?
In any case, what is the best approach?
I would recommend to use [[NSDate date] timeIntervalSince1970], which always specified in seconds. Also, it yields sub-millisecond precision over a range of 10,000 years.
Just multiply the result with 1000 to get the milliseconds.
currentTimeMillis() is equivalent to [[NSDate date] timeIntervalSince1970] * 1000
Please note that the return value is negative if the date object is earlier than January 1, 1970 at 12:00 a.m. GMT.
CFAbsoluteTimeGetCurrent() returns the time is measured in seconds relative to the absolute reference date of Jan 1 2001 00:00:00 GMT.
For example, the absolute time -32940326 is equivalent to December 16th, 1999 at 17:54:34.
I have to analyze a database coming from an iOS app, containing timestamps of the form
413033364.146713
or
413030924.054397
Unfortunately, it doesn't look like any format I know. Does anyone recognize it ?
If you are more curious about the reason for that: NSDate's reference is a "time value relative to an absolute reference date—the first instant of 1 January 2001, GMT."
Source: Apple's NSDate Class Reference
we can only guess because we don't know how the timestamp was created.
But
NSDate *d = [NSDate dateWithTimeIntervalSinceReferenceDate:413033364.146713];
NSLog(#"%#", d);
produces 2014-02-02 11:29:24 +0000, which is today.
If that is what you expect, then your timestamp is the number of
seconds since 1 January 2001, GMT.
I know that NSDate doesn't have timezone information.
However, I'm trying to understand how to manipulate them properly.
At the moment I'm passing a date into an object. That date is the user selected date at time 00:00:00.
i.e. if the user hits October 21st then the NSDate passed in should be. 21/10/2013 00:00:00.
However, it isn't it's 20/10/2013 23:00:00. (One hour before).
Now, this is nothing about formatting them or displaying them. I'm just using the NSDates.
I'm creating the date using NSDateComponents and NSCalendar.
I guess my question is...
How can I tell what date an NSDate is actually referring to in my local time zone?
I need to send a UNIX time stamp for 00:00:00 and 23:59:59 for a given date. However, at the moment when I set the hour, minute and second to 0, 0 and 0 then I'm not getting midnight in the current time zone I'm getting midnight in GMT.
This isn't what I want.
Fixed?
OK, I've fixed it... I think. At least, it's doing what I want it to do.
The trick is...
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateComponents setSecond:timeZone.secondsFromGMT];
I've been confused by this many times. When you NSLog an NSDate, you'll always get the output in GMT. So the 20/10/2013 23:00:00 (GMT) you're seeing is the same as your expected 21/10/2013 00:00:00 (BST). The UNIX timestamp for both of these dates would be the same because it doesn't take into account timezone - it's always UTC.
If you want to output in a user-readable format, an NSDateFormatter will format the date using your current timezone and locale.