Hi Guys,
Here's the best way I could think of to get the local time, and UTC time
into C char arrays timechar, and utimechar:
Swapping the month and day manually was pretty silly when I could
probably just change the formatter, but there are some really wild
solutions on the net that are supposed to achieve the same thing.
My question is if there is any overhead to do this in a function that is called often.
By "this", I mean allocating the formatters inside the function,
also with regard to other objects, but this one specifically uses the word "allocate".
or is it better to:
Cheers, Art.
Here's the best way I could think of to get the local time, and UTC time
into C char arrays timechar, and utimechar:
Code:
// get local time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
datestring = [dateFormat stringFromDate:today];
const char *c = [datestring UTF8String];
strncpy(timechar, c, 20);
char done;
char dtwo;
done = timechar[0]; // swap date to Australian format
dtwo = timechar[1];
timechar[0] = timechar[3];
timechar[1] = timechar[4];
timechar[3] = done;
timechar[4] = dtwo;
// get utc time
NSDateFormatter* dfutc = [[NSDateFormatter alloc] init];
[dfutc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dfutc setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
datestring = [dfutc stringFromDate:today];
const char *d = [datestring UTF8String];
strncpy(utimechar, d, 20);
done = utimechar[0]; // swap date to Australian format
dtwo = utimechar[1];
utimechar[0] = utimechar[3];
utimechar[1] = utimechar[4];
utimechar[3] = done;
utimechar[4] = dtwo;
probably just change the formatter, but there are some really wild
solutions on the net that are supposed to achieve the same thing.
My question is if there is any overhead to do this in a function that is called often.
By "this", I mean allocating the formatters inside the function,
also with regard to other objects, but this one specifically uses the word "allocate".
or is it better to:
Code:
// declare object
NSDateFormatter* dfutc;
// do this somewhere once in the program
dfutc = [[NSDateFormatter alloc] init];
// then continue to use the object inside the function
Cheers, Art.
Last edited: