Solved

Date/time string to DWORD conversion

Posted on 2006-07-04
5
496 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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 how to use strings and some functions related to them in the C programming language.

831 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