Date/time string to DWORD conversion

Posted on 2006-07-04
Medium Priority
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 300 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 300 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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

594 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