Convert UTC to local time with NSDateFormatter - cocoa

I am getting the following string from a server in my iOS app:
20140621-061250
How can I convert it to the local time?
How can I define my date formatter? Is this correct?
dateFormatter.dateFormat = #"YYYYMMd-HHmmss";

The question doesn't specify the nature of what you mean by converting, exactly, but the first thing you should do, regardless of the final goal, is to correctly parse the server response using a properly configured NSDateFormatter. This requires specification of the correct format string, and the time zone must be explicitly set on the formatter or it will infer it from the local time, which would be incorrect in most cases.
Specify The Format String
Let's look at the input string provided:
20140621-061250
This uses four digits for the year, two digits (with a zero-padding) for the month, and two digits (presumably, these will be zero-padded as well) for the day. This is followed by a -, then two digits to represent the hour, 2 digits for the minute, and 2 digits for the second.
Referring to the Unicode date format standards, we can derive the format string in the following way. The four digits representing the calendar year will be replaced with yyyy in the format string. Use MM for the month, and dd for the day. Next would come the literal -. For the hours, I assume that it will be in 24 hour format as otherwise this response is ambiguous, so we use HH. Minutes are then mm and seconds ss. Concatenating the format specifiers yields the following format string, which we will use in the next step:
yyyyMMdd-HHmmss
In our program, this would look like:
NSString *dateFormat = #"yyyyMMdd-HHmmss";
Configure the input date formatter
The time format above does not specify a time zone, but because you have been provided the specification for the server response that it represents the UTC time, we can code this into our application. So, we instantiate an NSDateFormatter, set the correct time zone, and set the date format:
NSTimeZone *inputTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setTimeZone:inputTimeZone];
[inputDateFormatter setDateFormat:dateFormat];
Convert the input string to an NSDate
For demonstration purposes, we hard-code the string you received from the server response; you would replace this definition of inputString with the one you get from the server:
NSString *inputString = #"20140621-061250";
NSDate *date = [inputDateFormatter dateFromString:inputString];
At this point, we have the necessary object to do any further conversions or calculations - an NSDate which represents the time communicated by the server. Remember, an NSDate is just a time stamp - it has no relation to a time zone whatsoever, which only plays a role when converting to and from string representations of the date, or representations of a calendrical date via NSDateComponents.
Next steps
The question doesn't clearly specify what type of conversion is needed, so we'll see an example of formatting the date to display in the same format as the server response (although, I can't think of a likely use case for this particular bit of code, to be honest). The steps are quite similar - we specify a format string, a time zone, configure a date formatter, and then generate a string (in the specified format) from the date:
NSTimeZone *outputTimeZone = [NSTimeZone localTimeZone];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setTimeZone:outputTimeZone];
[outputDateFormatter setDateFormat:dateFormat];
NSString *outputString = [outputDateFormatter stringFromDate:date];
Since I'm in UTC-06:00, printing outputString gives the following:
20140621-001250
It's likely you'll instead want to use setDateStyle: and setTimeStyle: instead of a format string if you're displaying this date to the user, or use an NSCalendar to get an NSDateComponents instance to do arithmetic or calculations on the date. An example for displaying a verbose date string to the user:
NSTimeZone *outputTimeZone = [NSTimeZone localTimeZone];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setTimeZone:outputTimeZone];
[outputDateFormatter setDateStyle:NSDateFormatterFullStyle];
[outputDateFormatter setTimeStyle:NSDateFormatterFullStyle];
NSString *outputString = [outputDateFormatter stringFromDate:date];
Printing outputString here gives us the following:
Saturday, June 21, 2014 at 12:12:50 AM Mountain Daylight Time
Note that setting the time zone appropriately will handle transitions over daylight savings time. Changing the input string to "20141121-061250" with the formatter style code above gives us the following date to display (Note that Mountain Standard Time is UTC-7):
Thursday, November 20, 2014 at 11:12:50 PM Mountain Standard Time
Summary
Any time you get date input in a string form representing a calendar date and time, your first step is to convert it using an NSDateFormatter configured for the input's format, time zone, and possibly locale and calendar, depending on the source of the input and your requirements. This will yield an NSDate which is an unambiguous representation of a moment in time. Following the creation of that NSDate, one can format it, style it, or convert it to date components as needed for your application requirements.

To get your string into a NSDate, you would use a NSDateFormatter like this:
NSString *myDateAsAStringValue = #"20140621-061250"
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyyMMdd-HHmmss"];
NSDate *myDate = [df dateFromString: myDateAsAStringValue];
You may want to read this post about working with Date and Time
EDIT:
To parse it as UTC you have to add the line:
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
Also, when you print it with NSLog, if you are using the same NSDateFormatter, you will get the input string as output (since you apply the inverse of the parsing function).
Here is the full code, for parsing and for getting the output with a standard format:
//The input
NSString *myDateAsAStringValue = #"20140621-061250";
//create the formatter for parsing
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[df setDateFormat:#"yyyyMMdd-HHmmss"];
//parsing the string and converting it to NSDate
NSDate *myDate = [df dateFromString: myDateAsAStringValue];
//create the formatter for the output
NSDateFormatter *out_df = [[NSDateFormatter alloc] init];
[out_df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
//output the date
NSLog(#"the date is %#",[out_df stringFromDate:myDate]);

One possible solution in Swift using NSDate extension (maybe it could help future viewers of this question):
import UIKit
// For your personal information: NSDate() initializer
// always returns a date in UTC, no matter the time zone specified.
extension NSDate {
// Convert UTC (or GMT) to local time
func toLocalTime() -> NSDate {
let timezone: NSTimeZone = NSTimeZone.localTimeZone()
let seconds: NSTimeInterval = NSTimeInterval(timezone.secondsFromGMTForDate(self))
return NSDate(timeInterval: seconds, sinceDate: self)
}
// Convert local time to UTC (or GMT)
func toGlobalTime() -> NSDate {
let timezone: NSTimeZone = NSTimeZone.localTimeZone()
let seconds: NSTimeInterval = -NSTimeInterval(timezone.secondsFromGMTForDate(self))
return NSDate(timeInterval: seconds, sinceDate: self)
}
}

Related

Set up custom time to allow NSLocalNotif xcode

See sample code as follows - unable to use custom time here. Why is this not possible giving a result:
NSDate *dt1 = [NSDate date]; //2018-07-26 08:31:22 +0000
NSString *datestr = [NSString stringWithFormat:#"%#", [NSDate date]];//2018-07-26 10:42:09 +0001
NSString *cutstring = 09:00 PM;
I would just create date with NSDateComponents where you can set year, month, hour, minute etc. Refer https://nshipster.com/nsdatecomponents/
Extracting Components From Dates
NSDateComponents can be initialized and manipulated manually, but most often, they’re extracted from a specified date, using NSCalendar -components:fromDate::
Swift
Objective-C
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let components = calendar.components([.Month, .Day], fromDate: date)
The components parameter is a bitmask of the date component values to retrieve, with many to choose from:
Swift
Objective-C
NSCalendarUnit.Era
NSCalendarUnit.Year
NSCalendarUnit.Month
NSCalendarUnit.Day
NSCalendarUnit.Hour
NSCalendarUnit.Minute
NSCalendarUnit.Second
NSCalendarUnit.Weekday
NSCalendarUnit.WeekdayOrdinal
NSCalendarUnit.Quarter
NSCalendarUnit.WeekOfMonth
NSCalendarUnit.WeekOfYear
NSCalendarUnit.YearForWeekOfYear
NSCalendarUnit.Calendar
NSCalendarUnit.TimeZone
Since it would be expensive to compute all of the possible values, specify only the components that will be used in subsequent calculations (joining with |, the bitwise OR operator).

How to get parse objects newer then a week

I need to get objects from parse that not older then a week.
i was trying it with
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE, MMMM d yyyy"];
NSDate *date = [object createdAt];
[dateFormatter stringFromDate:date]
Your code is a long way from working and shows some basic misunderstandings. I think you really need to go and learn about Objective-C and Cocoa/UIKit before you jump into Parse. However:
Parse has an automatic column called createdAt on all objects. You should use that.
I'm not sure about your definition of "a week ago" but to get a date exactly one week ago to the second use:
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:-60*60*24*7];
There are more complex ways of doing this if you mean "midnight one week ago" or something like that but I'll leave this as is for the sake of clarity.
Pass this date to a PFQuery using the built in createdAt column:
PFQuery* q = [PFQuery queryWithClassName:[MyClass parseClassName]];
[q whereKey:#"createdAt" greaterThan:date];
Then issue the find:
[q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// here objects will contain your PFObject subclasses from the server
}];

Parsing rfc3339 dates with NSDateFormatter in iOS 4.x and MacOS X 10.6: impossible?

Parsing a rfc3339 date with NSDateFormatter appears to be impossible, in the general case. Am I wrong? [Edit 2 years later: there is now a way! See below and footnote.]
A not-especially-malleable web service is feeding me dates like:
2009-12-31T00:00:00-06:00
Rfc3339 compliant, default output of the jaxb library they're using. Note the colon, which rfc3339 requires when the offset isn't a literal "z":
time-numoffset = ("+" / "-") time-hour ":" time-minute
time-offset = "Z" / time-numoffset
I want to parse these into NSDates.
NSDateFormatter wants patterns in the syntax specified by Unicode, which offers date field symbols for timezones like "PDT", "-0800", "GMT-08:00" but not "-08:00".
Googling, and other similar SO questions, produces only date formats like
[myDateParser setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"];
/* or: */ [myDateParser setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
The latter of which requires a literal "Z", and the former insists either the absence of a colon or presence of a "GMT". However, they appeared to work before ios 4.x (possibly by discarding the tz offset completely; my data aren't clear.)
My options at this point are a sorry lot:
discover some undocumented format specifier, or some strange mode to put NSDateFormatter into, that will accept the stray colon: longshot, likely nonexistent. [footnote]
persuade my service publisher to turn all dates into zulu time and specify 'Z': politically challenging.
write my own NSFormatter subclass or research good old strptime_l: work. :)
string-manipulate my input and strip the last colon: brittle and ugly, but the likely path of least resistance.
Have I understood the situation accurately, that current NSDateFormatter follows unicode strictly without extensions; and the unicode formats are insufficient to fully describe an rfc3339 date?
[FOOTNOTE] I come back to this three years later to tack on a small addendum: Unicode and Apple have added this feature to the format strings, as of iOS6/OSX10.8. Compare The latest revision as of this writing with its immediate predecessor, and note the addition of 5 "Z"s, which yields a zone format like "-08:00". So if you can get away with ditching support for 5.x/10.7, there's a new right way to do it. I'll leave the previous answer stand, as it's still the best approach when backward compatibility is required.
– getObjectValue:forString:range:error: can actually parse RFC3339 dates correctly. I have no idea why - dateWithString: cannot:
// RFC3339 date formatting
NSString *dateString = #"2012-04-11T18:34:19+00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:dateString range:nil error:&error];
Date string parsing in Cocoa can be a pain, especially if you have to deal with dates generated by .NET based web services.
I'd suggest looking at the NSDate+InternetDateTime category that Michael Waterfall has on NSDate as part of his MWFeedParser project on github. It's worked well for me parsing exactly the format of date you describe.
https://github.com/mwaterfall/MWFeedParser/
The string format characters are documented nowhere in the Apple documentation. Instead there is a link hidden deep in some document pointing to the Unicode standard at
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
Using the information at that link it's quite simple:
- (NSDate*)dateFromRFC3339String:(NSString*)aString
{
static NSDateFormatter* sRFC3339DateFormatter = nil;
static NSDateFormatter* sRFC3339DateFormatterSubSeconds = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
sRFC3339DateFormatter = [[NSDateFormatter alloc] init];
[sRFC3339DateFormatter setLocale:enUSPOSIXLocale];
[sRFC3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ssXXXXX"];
[sRFC3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
sRFC3339DateFormatterSubSeconds = [[NSDateFormatter alloc] init];
[sRFC3339DateFormatterSubSeconds setLocale:enUSPOSIXLocale];
[sRFC3339DateFormatterSubSeconds setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSSSSXXXXX"];
[sRFC3339DateFormatterSubSeconds setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
});
NSDate* date = [sRFC3339DateFormatter dateFromString:aString];
if (date == nil)
date = [sRFC3339DateFormatterSubSeconds dateFromString:aString];
return date;
}

Time Zone conversion form stringdate

I have a stringdate 16-MAY-2010 23:04:44 which i need to convert to gmt time zone that is the out put required is 17-May-2010 12:03:03.
I used date formatters to convert but the result i am getting is not in the format i required.I am sending the code please let me know if i am doing correct or not
Here is the code:
NSString *timeStamp = [format stringFromDate:[NSDate date]];
NSString *output = [timeConv dateStringFromString:timeStamp];
- (NSString *)dateStringFromString:(NSString *)sourceString
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"dd-MMMM-yyyy HH:MM:ss"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:sourceString];
return [dateFormatter stringFromDate:date];
}
The out put i am getting is the same old time without conversion. so please let me know the correct solution.
I know no cocoa whatsoever, but if I'm understanding your code correctly, you're never telling the parser that your original string is not GMT, but rather GMT-1.
Based on your own code (and again, with no knowledge of cocoa), this is what I believe you need to do:
NSTimeZone *gmtminusone = [NSTimeZone timeZoneForSecondsFromGMT:-3600.0];
[dateFormatter setTimeZone:gmtminusone];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:date];
If it doesn't work right away, maybe it gives you the idea of what to do:
Parse the string with a date formatter set to GMT-1, to output a date.
Parse the date with a date formatter set to GMT, to output a string.
The date string didn't include a time zone specification, so the date formatter relied on you to tell it what time zone it's in.
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:sourceString];
You told it that the input string was in GMT.
You need to tell the date formatter what time zone the date is in. Once you've done that, you can tell the formatter to convert the string to a date (dateFromString:), and it will interpret the string as representing a date in that time zone.
Then, once you have the date so interpreted, switch the formatter's time zone to GMT and have it output the string (stringFromDate:). That string will represent the date converted to GMT.

Calculate time difference in Cocoa

I have got two timevalues in the format: %H%M%S (E.G.161500)
These values are text-based integers.
Is there a simple function in Cocoa that calculates the difference between these two integers using a 60 seconds and minutes scale?
So if
time 1 = 161500
time 2 = 171500
timedifference = 003000
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HHmmss"];
NSDate *date1 = [dateFormatter dateFromString:#"161500"];
NSDate *date2 = [dateFormatter dateFromString:#"171500"];
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // diff = 3600.0
The class for manipulating dates is NSDate. The method for getting time intervals is -timeIntervalSinceDate:. The result is a NSTimeInterval value, which is a double representing the interval in seconds.
You can create a NSDate object from a NSString with +dateWithString:, provided that your date is formatted as 2001-03-24 10:45:32 +0600.
try this code.
- (NSTimeInterval)intervalBetweenDate:(NSDate *)dt1 andDate:(NSDate *)dt2 {
NSTimeInterval interval = [dt2 timeIntervalSinceDate:dt1];
NSLog(#"%f",interval);
return interval;
}
I would create an NSFormatter subclass to parse time values in that format from input data (you can put one on a text field to automatically convert user input, or use it to parse from a data source in code). Have it return the combined number of seconds as an NSTimeInterval (double representing seconds) wrapped in an NSNumber. From there it's easy to subtract the difference, and display it using the same NSFormatter class you created. In both parsing and displaying values, you're the one responsible to write code converting from seconds to hours:minutes:seconds or whatever format you like. You could also convert these values to an NSDate like mouviciel mentioned, if it makes sense for your application. Just keep in mind you're always going to be storing the time difference from a reference date, usually Jan 1st 1970 (NSDate has methods to do this for you).

Resources