Solved

Date/time string to DWORD conversion

Posted on 2006-07-04
5
484 Views
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  
0
Comment
Question by:midnightexpress
  • 3
5 Comments
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17039881
hi,
  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;

regards
Manish Regmi

0
 
LVL 8

Expert Comment

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

regards
Manish Regmi
0
 

Author Comment

by:midnightexpress
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??

  Midnightexpress
0
 
LVL 8

Assisted Solution

by:manish_regmi
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 */;
   perror("error");
   exit(1);
}

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 */;
   perror("error:");
exit(1);
}
printf("seconds since the Epoch: %ld\n", (long) t);"
}

regards
Manish Regmi
0
 
LVL 11

Accepted Solution

by:
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
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

770 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