Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

Get Current Date Time in FileTime structure in Objective-C

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

Upvotes: 2

Views: 1464

Answers (2)

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

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()];

Upvotes: 4

Pranav Bhargava
Pranav Bhargava

Reputation: 390

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];

Upvotes: 1

Related Questions