Solved

Date/time string to DWORD conversion

Posted on 2006-07-04
5
461 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
Comment Utility
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
Comment Utility
or strftime();
see man strftime.

regards
Manish Regmi
0
 

Author Comment

by:midnightexpress
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now