6

There are lots of ideas about this topic but this one is about Win32/WinEventProc's dwmsEventTime.

MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/dd373885(v=vs.85).aspx states that it's the event time in milliseconds but no further explanations provided. Milliseconds from where?

Here is the sample value returned by that event:

dwmsEventTime: 1209382650 
DateTime.Now:  05/21/2014 16:49:37  (this should be very close to dwmsEventTime)

Does anyone know how to convert this parameter to .NET DateTime()?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
meraydin
  • 89
  • 1
  • 1
  • 11
  • 4
    That number of milliseconds is only 335.94 hours... is it possible that it's "milliseconds since boot"? Can you reboot your machine and see if it starts with a low number? – Jon Skeet May 21 '14 at 13:58
  • see anwser [How to convert Milliseconds to date format in C#?](http://stackoverflow.com/questions/7336932/how-to-convert-milliseconds-to-date-format-in-c) – Mivaweb May 21 '14 at 14:00
  • possibly duplicate http://stackoverflow.com/questions/7336932/how-to-convert-milliseconds-to-date-format-in-c – Andrew May 21 '14 at 14:00
  • It could be an `Int32` which will then wrap-around (overflow) once every 49.7 days. Edit: Yes, your link says `DWORD` which is equivalent to `UInt32`, a 32-bit unsigned integer. It will wrap around once every 49.7 days. – Jeppe Stig Nielsen May 21 '14 at 14:16
  • 2
    It's probably the value from [`GetTickCount`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408%28v=vs.85%29.aspx). – arx May 21 '14 at 14:20

1 Answers1

4

You might try something like:

static DateTime GetDateTimeFromMillisecondNumber(int millisecCount)
{
  return DateTime.Now.AddMilliseconds(millisecCount - Environment.TickCount);
}

But be aware that if the system was rebootet since the millisecCount was obtained, this returns rubbish. The same if this is run on a different machine from the one that you obtained the count from.

Also, this is only correct "modulo 49.7 days", i.e. the correct date and time might be an integral multiple of 49.7 days greater or less than what my method returns.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • 1
    There are no problems. Both TickCount and dwmsEventTime will reset on a reboot. No modulo problem either, 2's complement takes care of it and the callback never takes 49 days to be called. Just don't do this in VB.NET, it will yell OverflowException. – Hans Passant May 21 '14 at 15:35
  • I just changed "int millisecCount" to "uint millisecCount" and worked! - Thanks! :) – meraydin May 21 '14 at 20:45
  • 1
    @meraydin Beware! Your subtraction will be performed as a `long` subtraction, and you will not get the same "wrap-around" behavior. If you need `uint` I suggest you cast to `int` before subtracting, that is `static DateTime GetDateTimeFromMillisecondNumber(uint millisecCount) { return DateTime.Now.AddMilliseconds((int)millisecCount - Environment.TickCount); }`. Please do it that way. – Jeppe Stig Nielsen May 22 '14 at 08:14