HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

DateTime internal representation to FILETIME

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
internalrepresentationdatetimefiletime

Problem

Here is a routine I put together to convert SQL Server's internal DateTime representation (BINARY(8)) to a FILETIME. I know that the SQL DateTime (as in GetDate()) is not precise and will vary from the SystemTimeAsFileTime. But it should be different by milliseconds, but by seconds, right?

In any case, here is my routine and I was wondering if anyone saw any flaws in it:

int SQLDateTimeToFileTime(PULARGE_INTEGER t_sql, PULARGE_INTEGER t_ft)
{
    ULONGLONG days = t_sql->LowPart;
    ULONGLONG ticks = t_sql->HighPart;
    days += (ULONGLONG) 109207;         // 01/01/1900 - 01/01/1601
    days *= (ULONGLONG) 864000000000;   // (FT_SECONDS_PER_DAY * FT_SECOND);
    ticks *= (ULONGLONG) 33333333333;   // MS per tick (3.33...) * 10000);
    ticks /= (ULONGLONG) 1000000;       // to avoid floating point operation
    days += ticks;
    t_ft->QuadPart = days;
    return(0);
}

Solution

-
This function always returns 0, but you haven't stated why that's important for this use (if it's intended). Since it doesn't also return 1 or another non-0 value for failure, I assume that it's not returning error codes. Otherwise, it should return void, and the return 0 should then be removed.

-
Since this is also tagged with c++, I'll also provide the C++ equivalent of the constants given in @chux's answer:

constexpr unsigned long long FT_SECONDS_PER_DAY = 24ULL * 60 * 60;
constexpr unsigned long long FT_SECOND = 10000000ULL;


(constexpr has them evaluated at compile time.)

Or for brevity, they can use auto instead:

constexpr auto FT_SECONDS_PER_DAY = 24ULL * 60 * 60;
constexpr auto FT_SECOND = 10000000ULL;

Code Snippets

constexpr unsigned long long FT_SECONDS_PER_DAY = 24ULL * 60 * 60;
constexpr unsigned long long FT_SECOND = 10000000ULL;
constexpr auto FT_SECONDS_PER_DAY = 24ULL * 60 * 60;
constexpr auto FT_SECOND = 10000000ULL;

Context

StackExchange Code Review Q#61560, answer score: 2

Revisions (0)

No revisions yet.