Date/time string to DWORD conversion

Posted on 2006-07-04
Last Modified: 2008-02-01
To all 'Surfing' Wizards:

 I am looking for the working ("C") code example(s) that will do the
  following twist... "By puttin a date/time string into a DWORD?" -- Given
  valid Date and Time strings.  If you feel that more points are required to get this problem answered correctly and make it beneifical, let me know.

     As always, TIA!   Midnightexpress  
Question by:midnightexpress
  • 3

Expert Comment

ID: 17039881
  What should that time in dword store?
Standard, seconds after epoch or
some other binary format like dike dividig bits into day, hour etc.

does strptime work for you?
strptime("4 July 2001 12:00:45", "%d %b %Y %H:%M:%S", &tm);
see man strptime;

Manish Regmi


Expert Comment

ID: 17039883
or strftime();
see man strftime.

Manish Regmi

Author Comment

ID: 17047500
Comment:  I would prefer, as requested, some working "C" source code examples that works, the bugs worked out.  Does this mean more points are required??


Assisted Solution

manish_regmi earned 100 total points
ID: 17047667
so you mean the format of time_t (seconds after epoch works for you.)

//example from posix spec:

int main()
struct tm tm;
time_t t;

if (strptime("6 Dec 2001 12:33:45", "%d %b %Y %H:%M:%S", &tm) == NULL)
    /* Handle error */;

printf("year: %d; month: %d; day: %d;\n",
        tm.tm_year, tm.tm_mon, tm.tm_mday);
printf("hour: %d; minute: %d; second: %d\n",
        tm.tm_hour, tm.tm_min, tm.tm_sec);
printf("week day: %d; year day: %d\n", tm.tm_wday, tm.tm_yday);

t = mktime(&tm);
if (t == -1)
    /* Handle error */;
printf("seconds since the Epoch: %ld\n", (long) t);"

Manish Regmi
LVL 11

Accepted Solution

cup earned 100 total points
ID: 17048301
It really depends on whether you are going to use this DWORD for storage only or for arithmetic.  If it is just for storage only, it is simple.  Say the date/time is stored in ddd, dmm, dyy, thh, tmm, tss

DWORD when =
   ((DWORD) dyy << 24) |
   ((DWORD) dmm << 22) |
   ((DWORD) ddd << 17 ) |
   ((DWORD) thh << 12) |
   ((DWORD) tmm << 6) | tss;

There is enough space to accomodate each item individually:

6 bits for seconds - 0:59 nearest multiple of 2 is 64
6 bits for minutes - 0:59
5 bits for hours 0:23
5 bits for days 0:31
4 bits for months 1:12
remainder for year 0:99

If you wish to use it for arithmetic, use a variant of Zeller's congruence to convert it to a number, multiply that by 86400 and add that to the time converted to seconds.  The reverse of Zeller's congruence is slightly iterative but you can get there in 2 iterations.

Note that there are rounding errors in Zeller's congruence computations so make sure the 30.6 is coded as 30.6001

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question