DWORD date value

L

Lee K

This is not strictly a C++ issue but if anyone will know the answer its you
guys!

I am trying to figure out a date format stored in the registry by a piece of
software (I am trying to write something to enable me to script a
configuration). The program stores a createdate for a set of registry keys
held in a DWORD value. I assumed it would be the number of seconds since
epoch (1/1/1970 00:00:00) - but its not. Here is a couple of examples:

19/12/2002 11:41:38 = 764632371
13/12/2002 11:36:58 = 764239005

Any one have any other ideas????

Thanks

Lee
 
V

Victor Bazarov

Lee K said:
This is not strictly a C++ issue but if anyone will know the answer its you
guys!

I am trying to figure out a date format stored in the registry by a piece of
software (I am trying to write something to enable me to script a
configuration). The program stores a createdate for a set of registry keys
held in a DWORD value. I assumed it would be the number of seconds since
epoch (1/1/1970 00:00:00) - but its not. Here is a couple of examples:

19/12/2002 11:41:38 = 764632371
13/12/2002 11:36:58 = 764239005

Any one have any other ideas????

Looks like your DWORD is a (<date> << 16) + <time>. Perhaps even
the regular packed MSDOS date and time values. Remember those?

bits:

Date: YYYYYYYMMMMDDDDD
Time: HHHHHMMMMMMSSSSS, where S is half of real seconds.

So, to calculate the date you need to do

void breakDate(DWORD dvalue, int& year, int& month, int& day,
int& hour, int& minute, int& sec)
{
year = (dvalue >> 25) + 1980;
month = (dvalue >> 21) & 0xf;
day = (dvalue >> 16) & 0x1f;
hour = (dvalue >> 11) & 0x1f;
minute = (dvalue >> 5) & 0x3f;
sec = (dvalue & 0x1f) * 2;
}

It's possible to have invalid values. Month can be more than 12,
day can be more than the nubmer of days in the month, hours could
be > 23, minutes can be > 59, seconds could be > 59.

Victor
 
L

Lee K

You are right - thank you very much.

What is the best way to actually create the DWORD value from the current
date?

Thanks,

Lee
 
V

Victor Bazarov

Lee K said:
You are right - thank you very much.

What is the best way to actually create the DWORD value from the current
date?

Use << and | operators and just do the reverse of what
I did in the breakDate.

Victor
 
V

Victor Bazarov

Lee K said:
Victor,

I tried that but am obviously making a school boy error as it does not work,
any chance you could post an example???

If we want to make 'XXXKKKKKPPPP' out of three values x, k, and p,
then one approach could be

result = ((x & 7) << 9) | ((k & 31) << 4) | (p & 15);

Can you figure out why '7', '9', '31', '4', and '15'? Let it be
your homework.

Victor
 
L

Lee K

Got the answer in the end thanks to Victor!

int yr = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int sec = 0;
CTime n = CTime::GetCurrentTime();
DWORD dValue = 0;

yr = n.GetYear();
month = n.GetMonth();
day = n.GetDay();
....

dValue = ((yr - 1980) << 25) | ((month & 0xF) << 21) | ((day & 0x1F) << 16)
| ((hour & 0x1F) << 11) | ((minute & 0x3F) << 5) | (sec / 2);

Thank you for your help.... This is why ng's are so great

Lee
 
V

Victor Bazarov

Lee K said:
int yr = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int sec = 0;
CTime n = CTime::GetCurrentTime();
DWORD dValue = 0;

yr = n.GetYear();
month = n.GetMonth();
day = n.GetDay();
...

dValue = ((yr - 1980) << 25) | ((month & 0xF) << 21) | ((day & 0x1F) << 16)
| ((hour & 0x1F) << 11) | ((minute & 0x3F) << 5) | (sec / 2);

Add some protection to this. Verify that the year you get is,
in fact >= 1980. Otherwise subtracting 1980 from it may lead
to unexpected or even undefined results.

Good luck with your project!

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top