Convert NSDate to Tick

I wanted to convert the date (nsdate) to tick. Tick ​​values ​​(1 Tick = 0.1 microseconds or 0.0001 milliseconds) from January 1 0001 00:00:00 GMT. NSDate has features like timeIntervalSince1970. So how to convert it?

+4
source share
1 answer

I would like to share my experience:

I tried to find the seconds from 01/01/0001, and then multiply by 10,000,000. However, this gave me the wrong results. So, I found out that 01/01/1970 is 621355968000000000 ticks from 01/01/0001 and used the following formula along with the timeIntervalSince1970 NSDate function.

Ticks = (MilliSeconds * 10000) + 621355968000000000

MilliSeconds = (Ticks - 621355968000000000) / 10000

Here is the result:

+(NSString *) dateToTicks:(NSDate *) date { NSString *conversionDateStr = [self dateToYYYYMMDDString:date]; NSDate *conversionDate = [self stringYYYYMMDDToDate:conversionDateStr]; NSLog(@"%@",[date description]); NSLog(@"%@",[conversionDate description]); double tickFactor = 10000000; double timeSince1970 = [conversionDate timeIntervalSince1970]; double doubleValue = (timeSince1970 * tickFactor ) + 621355968000000000; NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease]; [numberFormatter setNumberStyle:NSNumberFormatterNoStyle]; NSNumber *nsNumber = [NSNumber numberWithDouble:doubleValue]; return [numberFormatter stringFromNumber:nsNumber]; } 

Similarly, to convert from mark to date:

 //MilliSeconds = (Ticks - 621355968000000000) / 10000 +(NSDate *) ticksToDate:(NSString *) ticks { double tickFactor = 10000000; double ticksDoubleValue = [ticks doubleValue]; double seconds = ((ticksDoubleValue - 621355968000000000)/ tickFactor); NSDate *returnDate = [NSDate dateWithTimeIntervalSince1970:seconds]; NSLog(@"%@",[returnDate description]); return returnDate; } 
+3
source

Source: https://habr.com/ru/post/1434037/


All Articles