2

I am trying to get current Date Time in FileTime structure in objective-c, is that possible? Thanks.

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
  • A file time represents the specific date and time at which a given file was created, last accessed, or last written to. A file time is stored in a FILETIME structure. This structure is used with various Win32 API calls. – Ahmad Kayyali Oct 15 '11 at 13:05

2 Answers2

4

I found an elegant solution for this:

/**
 * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
 */
#include <sys/time.h>
#define EPOCH_DIFF 11644473600LL

unsigned long long getfiletime()
{
    struct timeval tv;
    unsigned long long result = EPOCH_DIFF;
    gettimeofday(&tv,NULL);
    result += tv.tv_sec;
    result *= 10000000LL;
    result += tv.tv_usec * 10;
    return result;
}

store the current time in FileTime using:

NSString* timeStamp  = [NSString stringWithFormat:@"%llu",getfiletime()];
Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
  • Your way works, but you could do the same as what Pranav suggested using a different format string. – sosborn Sep 15 '11 at 08:42
  • I'm trying to use this to get a similar output in Objective-C to the output I get from `DateTime.Now.ToFileTimeUtc()` in Unity. It seems like this should be exactly what I need, but when I run them each simultaneously, this function returns a number with a difference of around 252,276,759,470 from the Unity result. Any idea why that would be? – Nerrolken Apr 19 '18 at 02:27
1

Do you mean "YYYY:MM:DD:HH:MM:SS"

You can use the following code.

    NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
    [formatDate setDateFormat:@"yyyy:MM:dd:HH:mm:ss"];

    NSDate *now = [[NSDate alloc] init];

    NSString *formattedDate = [formatDate stringFromDate:now];

    NSLog(@"%@", formattedDate);

    [now release];
    [formatDate release];
Pranav Bhargava
  • 390
  • 3
  • 9