Parsing date/timestamp from VBO file with NSDateFormatter - objective-c

Im trying to figure out this timestamp format, but it looks weird to me.
There is a timestamp in that format: 184930.60
Explanation is: "This is UTC time since midnight in the form HH:MM:SS.SS."
It looks like number of seconds (based on change in file, for 20Hz GPS module), but it is 51h when you convert these seconds, and this can't be 51h after midnight.
Any idea how to convert that into nsdate?
Details about file format: https://racelogic.support/01VBOX_Automotive/01General_Information/Knowledge_Base/VBO_file_format

This is working example:
-(NSDate *)getDateFromDecimalTime:(double)decimalTime{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HHmmss.SS"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:#"%f", decimalTime]];
//gather current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
//gather date components from date
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
// Get current year/day/month
NSDate *now = [NSDate date];
// Specify which units we would like to use
unsigned units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar2 components:units fromDate:now];
//set date components
[dateComponents setDay:[components day]];
[dateComponents setMonth:[components month]];
[dateComponents setYear:[components year]];
//save date relative from date
NSDate *completeDate = [calendar dateFromComponents:dateComponents];
return completeDate;
}

Related

I have a method that takes timezone name as a parameter and gives the current date's day component. But it gives different incorrect results

Suppose i have two parameters
Australia/Sydney (GMT+10) offset 36000 &
Asia/Kolkata (IST) offset 19800
as timezones, it gives different values for the day component of current day itself. I am not able to figure out what am i doing wrong.. :( A little clue will be appreciated
-(NSInteger)getDayWithTimeZoneName:(NSString *)timeZoneName{
// Instantiate a timezone
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneName];
//Instantiate a date formatter
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:timeZone];
[df setDateFormat:#"yyyy-MM-dd"];
NSString *dateStr = [df stringFromDate:[NSDate date]];
NSDate *date = [df dateFromString:dateStr];
// NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay fromDate:date];
NSDateComponents *components = [[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]components:NSDayCalendarUnit fromDate:date];
NSInteger day = [components day];
NSLog(#"Date component for timeZone %# is %ld", timeZoneName,(long)day);
return day;
}
NSFormatter isn't designed to convert or calculate dates:
NSString *dateStr = [df stringFromDate:[NSDate date]];
dateStr is the date in Sydney (2016-05-16 0:00).
NSDate *date = [df dateFromString:dateStr];
date is GMT, 10 hours behind Sydney (2016-05-15 14:00).
Use NSCalendar to calculate dates:
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = timeZone;
NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:[NSDate date]];
NSInteger day = [components day];

Convert date-based NSString to NSDateComponents

Is it possible to parse out date components from an NSString-based date? For instance, if I have an NSDateFormatter with yyyy-MM-dd HH:mm:ss.SSS ZZZ, how can I get an NSDateComponents object from this string directly?
Specifically, I'd like to preserve timezones, and differentiate this NSDateComponents object with one created from the yyyy-MM-dd format.
Not so directly, but with a few steps:
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS ZZZ"];
NSString *dateString = #"2016-03-15 12:00:00.000 +0800";
NSDate *date = [formatter dateFromString:dateString];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitTimeZone fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSTimeZone *timeZone = [components timeZone];

Going from numerical month to Gregorian name for month

Currently I'm using the following code to acquire a date from a date picker. Right now, when I NSLog the NSDateComponents var, I get the numberical representation for a month (i.e. 7 for July). How can I print out the Month name, as well as the day of the week, like Saturday?
NSDate *selectedDate = _datePicker.date;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM:DD:HH:mm"];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:_datePicker.date];
NSLog(#"components: %#",components);
Use the format strings EEEE for the day of the week, and MMMM for the month of the year in your NSDateFormatter.
You might use monthSymbols from NSDateFormatter:
NSDate *selectedDate = _datePicker.date;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:selectedDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *month = [[dateFormatter monthSymbols] objectAtIndex:components.month - 1];

NSDate conversion setting weekday

I have an NSDate object *myDate.I can get the week day from this date and I want to set the weekday to an integer c and get the new date object in that week only.
I searched for it and I found the answer,here is the code..
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSLog(#"new date==%#",newDate);
This will return date of the next day.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MMM dd, yyy"];
date = [formatter dateFromString:string];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger day = [components day];
NSInteger weekday = [components weekday]; // if necessary

How to find what day of the week for any given date using Cocoa

I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009)
Thank you.
I've been doing:
NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEEE"];
NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]];
This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing the date format string in the setDateFormat: call
Lots more information at:
http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369
You may have a look to the NSDate and NSCalendar classes.
For example, here and here
They provide the following code:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];
Use NSCalendar and NSDateComponents. As shown in the NSDateComponents documentation:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
Here's what I wound up with, which works exactly as intended. It takes a date, alters the date to be the first of the month, and gets the day of that date:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSDateComponents *monthDateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate: date];
// build date as start of month
monthDateComponents.year = components.year;
monthDateComponents.month = components.month;
monthDateComponents.day = 1;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// [gregorian setFirstWeekday:1];
NSDate *builtDate =[gregorian dateFromComponents: monthDateComponents];
// NSDateComponents *weekdayComponents =[gregorian components: NSCalendarUnitWeekday fromDate: builtDate];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:#"E"];
NSString *firstDay = [df stringFromDate:builtDate];
if([firstDay isEqual:#"Sun"]) // do for the other 6 days as appropriate
return 7;