1

I am new to objective-C and Mac:

How to convert the Windows64BitSeconds in below to its MacOS NSDate localtime and UTC?. By some online reading/searching came up with the following, but not sure if this is correct.

//found some useful code online: Convert Windows Filetime to second in Unix/Linux

#define WINDOWS_TICK 10000000
#define SEC_TO_UNIX_EPOCH 11644473600LL

windows-filetime-to-second-in-unix-linux.
unsigned WindowsTickToUnixSeconds(long long windowsTicks) 
{
    return (unsigned)(windowsTicks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
}

long long Windows64BitSeconds = A 64 Bit Value of type FILETIME;

long long UnixSeconds = WindowsTickToUnixSeconds(Windows64BitSeconds); 
NSDate *srcDate = [NSDate dateWithTimeIntervalSince1970:UnixSeconds];
NSLog(@"srcDate=%@", srcDate);  //What this supposed to display, UTC?.This seems to display local time???

NSTimeZone* localTZ = [NSTimeZone localTimeZone];
NSTimeZone* utcTZ = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger localGMTOffset = [localTZ secondsFromGMTForDate:sourceDate];
NSInteger gmtOffset = [utcTZ secondsFromGMTForDate:sourceDate];
NSTimeInterval gmtInterval = gmtOffset - localGMTOffset;

NSDate* destDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:srcDate];
NSLog(@"destinationDate=%@", destDate);  //What this supposed to display, UTC?.This seems to display Localtime???

Not certain abut the above code versus the correct code?.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
umc
  • 39
  • 3
  • This is a good question. I don't think you are far off the correct code. I would imagine the `srcDate` is in the same timezone as the input Win32 `FILETIME`, so you need to verify that first. – trojanfoe Aug 30 '19 at 06:12
  • on the windows side got the local date+time, converted it to int64bit. On the Mac side used it, assigned the int64 generated at the Windows side to the Windows64BitSeconds as shown in the code. Observed: 1) srcDate in above was a few hours behind the Windows time. 2)destDate in above matached the Windows date+time. I do not have solid explanation for this observation. Can anyone see what is happening?. Thanks. – umc Aug 31 '19 at 04:50
  • 1
    I can only assume time zone. You should always store/transmit times in UTC and display in local time as necessary. Either that or the calculation is wrong, however it looks good from the link you provided. – trojanfoe Aug 31 '19 at 07:09

0 Answers0