calculate epoch time in iPad - iphone

I have an array which contains time in HH:mm:ss format.
I want to convert this into epoch time. To convert a particular time into epoch, date along with time should be passed.
I want to pass today's date along with the time from array.
For example 09:15:30 (date 22/12/2011) which is a string, should be converted into 1324525530000 (corresponding epoch value)
How should I convert this ??
Any help is appreciated.

Maybe you can get an idea looking at this snippet:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] initWithSafeDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSTimeInterval epoch = [date timeIntervalSince1970];
Anyway you can have a look to NSDateFormatter and NSDate in the reference docs.

That looks like an epoch time in milliseconds - as far as I know it's supposed to be in seconds.
You can use NSDateFormatter to parse a string and convert it to a NSDate. This date can then return the seconds since 1970. If you really need it, simply multiply this by 1000.

Related

How to convert any human readable date to unix timestamp [duplicate]

This question already has answers here:
How to convert NSDate into unix timestamp iphone sdk?
(10 answers)
Closed 9 years ago.
I have a date like this 01/20/2013 and trying to get the unix timestamp for this date. I have searched a lot and found out that how to convert present date into unix timestamp but didn't find my solution.
here is my code what i'm doing.
NSDate *date = mydate; //myDate is the date like 01/20/2013
NSDateFormatter *formatter = [[ NSDateFormatter alloc]init];
[formatter setDateFormat:#"mm/dd/yyyy"];
NSString *timestamp = [formatter stringFromDate:date];
NSLog(#"%#",timestamp);
I'm getting null as timestamp value in my console.
iOS provides -(NSTimeInterval)timeIntervalSince1970 for NSDate objects which returns the number of seconds since 00:00:00 GMT January 1, 1970. NSTimeInterval is a double floating point type so you get the seconds and fractions of a second.
time_t unixTime = (time_t) [your_date timeIntervalSince1970];
Here time_t is usually a signed 32-bit integer type (long or int).
you can get it with
- (NSTimeInterval)timeIntervalSince1970
UPDATE:
For Example in int ...
int unixtimestamp = [mydate timeIntervalSince1970];
And other Example in NSTimeInterval
NSTimeInterval ti = [mydate timeIntervalSince1970];

How to convert unix timestamp to human readable time?

I get a unix timestamp from the database and I am trying to create a human readable date from it. I am using this way
long t1=[time longLongValue];
NSDate* date=[NSDate dateWithTimeIntervalSince1970:t1];
where time is the timestamp. When I print date I get
1956-02-18 19:04:01 +0000
instead of
2013-01-02 12:31:03 +0000
The timestamp was 1356765933449
It is a matter of integer overflow, as Boris correctly pointed out in his answer.
I don't know what your time object is, but instead of a signed long int use a NSTimeInterval.
On iOS NSTimeInterval is currently defined as
typedef double NSTimeInterval;
but you shouldn't care too much about that. Sticking with type synonyms will protect you in case Apple decides to change the underlying definition to something else.
That said you should change your code to something like
NSTimeInterval epoch = [time doubleValue];
NSDate * date = [NSDate dateWithTimeIntervalSince1970:epoch];
Concerning the code maintainability issue I described before, here you are explicitly using a doubleValue (you don't have many options), but the good thing is that if Apple changes the NSTimeInterval definition to something not compatible with a double assignment, the compiler will let you know.
Try this
- (NSString *) getDateFromUnixFormat:(NSString *)unixFormat
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[unixFormat intValue]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMM dd, yyyy-h:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
//NSDate *date = [dateFormatter dateFromString:publicationDate];
NSString *dte=[dateFormatter stringFromDate:date];
[dateFormatter release];
return dte;
}
The Unix timestamp has only 32 Bits available.
Because they use a signed int, they count the seconds from 1.1.1970. A 32 Bit signed int can only hold values up to 2147483647, where as you want it to be 1356765933449. That causes an overflow, and that causes your date to be invalid.
This is also known as the Year 2038 Problem, because 2147483647 (max value) will be hit on 03:14:07 UTC on Tuesday, 19 January 2038.
Then format the date using nsdateformatter. Details guide.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

How to parse a UTC epoch 1352716800 to NSDate?

I'm reading data from a XML file which has a UTC date looking like "2011-05-04T00:00:00", and a UTC epoch looking like 1352716800.
Parsing the UTC epoch to NSDate would probably be much safer than messing around with a complex date format. How would I parse the UTC epoch to NSDate? With NSDateFormatter and a special format for "UTC Epoch"?
I think that it is [[NSDate alloc] initWithTimeIntervalSince1970:epoch] and a test seemed to work. But I am not sure if that's just correct by accident or if the "UTC epoch" is "Since 1970". The Apple Docs don't mention UTC Epoch.
YES, you are correct it is UTC Epoch. For Reference if "Epoch time is UTC" checkout this
NSString *epochTime = #"1352716800";
// (Step 1) Convert epoch time to SECONDS since 1970
NSTimeInterval seconds = [epochTime doubleValue];
NSLog (#"Epoch time %# equates to %qi seconds since 1970", epochTime, (long long) seconds);
// (Step 2) Create NSDate object
NSDate *epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:seconds];
NSLog (#"Epoch time %# equates to UTC %#", epochTime, epochNSDate);
You don't really need to parse the UTC epoch date. Instead you can more or less directly create an NSDate instance from it:
long utcEpoch = 1352716800;
NSDate* date = [Date dateWithTimeIntervalSince1970: utcEpoch];
In the case of the timestamp retrieved from Firebase (kFirebaseServerValueTimestamp), the epoch is expressed in milliseconds:
A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) by the Firebase servers.
In that case dividing by 1000 is needed if you use initWithTimeIntervalSince1970 in iOS.

How to see if a date is beween two other dates, and the time used and the time remaining?

If I have set dates like Sunday Jan.29, 2012 2:00:00 PM and Friday Feb.3 2012 5:00:00 PM,
and get the present time, how to I get the spent time from the first date and the present and how do I get the remaining time from the present and the future date?
I have code to show but it is all wrong. There has to be a easy way to do it that I just cant see.
Thank you
Eric
You want to use functions from NSDate
for example:
//get time difference between someDate and now
NSTimeInterval diff = [someDate timeIntervalSinceNow];
//get difference between dates
NSTimeInterval diff2 =[someDate timeIntervalSinceDate: otherDate];
//comparing dates
NSDate * earlierDate = [someDate earlierDate: otherDate];
NSDate * laterDate = [someDate laterDate: otherDate];
If you have your dates available as NSDate objects, you can use timeIntervalSinceDate: to calculate the difference in seconds.
NSTimeInterval sinceThen = [firstDate timeIntervalSinceDate:[NSDate date]];
which will give you the time difference as an NSTimeInterval which is basically a double specifying the time in seconds. If the interval is negative, then firstDate is before now (which is the result of [NSDate date] otherwise it is in the future. If your date is not yet in NSDate form, you might employ an NSDateFormatter to do this (See here, parsing date strings).

Best way to store a 'time' value

My class needs two properties: startTime and endTime. What is the best class to use? I know there is NSDate, but I only need to store a specific time (something in between 00:00-23:59), I don't need a date. What is the most elegant solution here?
NSTimeInterval is probably good enough for this.
It stores a time value in seconds as a double.
Eg. 5 mins = 300.0
I believe the most elegant solution, and what you want, is NSTimeInterval, that is the primitive type that NSDate is built on top.
NSTimeInterval is a typedef for double, and is a measurement of time in seconds. This primitive time type do not have any concept of a reference date. What NSDate do is to add this concept of reference date and anchor the 0.0 time at 1 January 2001 GMT. There is nothing that stops you from inventing your own reference date or anchor, like for example "midnight of whatever day there is".
What you can do is to add two properties of the NSTimeInterval either as startTime and endTime and let them both use midnight as the reference. Or you could skip endTime and go for a startTime and duration combo.
There's NSDateComponents, which "can also be used to specify a duration of time, for example, 5 hours and 16 minutes."
The NSDate class is similar to the DateTime class in C#: both hold a date and time, but they can be independent of each other. In Cocoa, you would compare two NSDate classes:
//Create NSDate objects in the time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *startTimeString = #"00:00:00"; //0 seconds
NSString *endTimeString = #"00:00:52"; //52 seconds
NSDate *startTime = [dateFormatter dateFromString:startTimeString];
NSDate *endTime = [dateFormatter dateFromString:endTimeString];
//Compare the time
BOOL date1before2 = [startTime compare:endTime] == NSOrderedAscending;