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.
Related
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.
Before flagging this question as a duplicate, please read on.
I need to compare two NSDates. A date I get from a server with the current date.
From NSDate.date() I get this date 2014-09-25 12:48:23 +0000 which is wrong (the time part). I needed to add 5 hours to get the current time so I did the following.
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss ZZZZZ"
let dateString = formatter.stringFromDate(NSDate.date())
The result is the correct date - 2014-09-25 06:21:56 +05:30
But there's a little hitch. This date is a String, not a NSDate. I need it to be a NSDate object to compare it with another date.
I tried converting it back like this,
let date = formatter.dateFromString(dateString)
And I get a wrong result - 2014-09-25 00:55:53 +0000. I tried passing the date string to a new NSDateFormatter to see if that works but again I still I get the wrong date.
My question is, how can I convert this date string to a NSDate object which also retains the correct time.
Thank you.
You are thoroughly confused about NSDate.
An NSDate is a point in time. It has no time zone information. If we both call [NSDate date] right now, we will get the same NSDate, even when you are in India and I'm in the UK. That's intentional. It's the same time. The time displayed on my watch and on your watch is different, but NSDate is the same. You can't convert NSDate to an "Indian" date.
You use calendars and timezones to convert NSDates to strings that you display to a user, in the way your users expect it. That's what you have done. You got a string that makes sense to Indian users. If an Indian user types a time, you take that string and convert it to an NSDate. The NSDate will be in Universal time. If you and I both typed in the time on our watch right now and converted it, you would type a time that looks like 5 1/2 hours earlier than mine. But it's the same time. If you convert it to NSDate, we will both get the exact same NSDate.
So how do you change your NSDate? Quite simple: You don't. NSDate is absolute time, independent of your location on earth.
Upon further Googling, I came across this post. The method described in it does exactly what I want. The original code is in Objective-C and since my question is in Swift, I'm going to post its Swift translation. \
func toLocalTime() -> NSDate {
let timeZone = NSTimeZone.localTimeZone()
let seconds = timeZone.secondsFromGMTForDate(self)
return NSDate(timeInterval: Double(seconds), sinceDate: self)
}
I added these as extension methods of NSDate so you can simply call them like this.
NSDate.date().toLocalTime()
You can compare two dates using any of the following NSDate functions: compare, earlierDate, laterDate, isEqualToDate. You should not compare date strings (oh, goodness, no, think of the nightmare); convert 'date strings' into 'NSDate' as soon as inputed.
In order to compare two dates that arose from strings correctly, you'll need the date strings to be unambiguous. In practice, that requires the date to have a time zone attached. If your server isn't providing a time zone and can't be modified to provide one, then you'll be forced to assume one (which would typically be the time zone where the server is located, assuming one server).
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.
I am trying to convert milliseconds into date. Below shown is the code i am using.
double startDateDb=1380275880000;
NSDate *date=[NSDate dateWithTimeIntervalSince1970:(startDateDb/1000.0)];
NSLog(#"date---%#",date);
My log gives date as 2013-09-27 09:58:00 +0000
When i use online tool to convert i am getting "9/27/13 5:58 AM" which is correct.
Please help me to fix the issue.
NSLog used your timezone when displaying dates. TO get the date in UTC use the NSDate methods and specify the tie zone. All NSDates are UTC timezone based.
Use NSDateFormatter to display the date/time in another timezone.
Your NSLog put the timezone in GMT, the timezone you're looking for is GMT-4.
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?