In one of my unit tests in Xcode 12, I have following code snippet:
NSDate *timestamp = [NSDate date];
NSDateFormatter *timestampFormatter = [[NSDateFormatter alloc] init];
[timestampFormatter setDateFormat:@"YYYY-MM-dd 'at' HH:MM:ss.SSSXXXXX"];
NSLog(@">>>>> Timestamp: %@", [timestampFormatter stringFromDate:timestamp]);
I get unit test output as:
2021-03-26 16:42:32.109836-0700 xctest[23961:1430979] >>>>> Timestamp: 2021-03-26 at 16:03:32.110-07:00
Notice how different captured timestamp is from the output line's timestamp! The output line's timestamp does correlate with the current system time, while the captured timestamp is about 39 minutes behind. The whole snippet takes sub-seconds to run :)
the captured timestamp is about 39 minutes behind
No, it isn't. It's the way you are displaying the timestamp. The trouble is that the second part of your format, HH:MM:ss.SSSXXXXX
, means "show me hours, months, and seconds." That is probably not what you want.
So when it is 16:42:32, you see "16:03:32"
coming from your formatter, because the month is 03
.