Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie Question: Working with dates

Reply
Thread Tools

Newbie Question: Working with dates

 
 
Erik
Guest
Posts: n/a
 
      06-08-2005
Is there a variable for storing dates and how can I count with dates:
For example, how do I code how much days there are between 01-01-2005
and 03-03-2005?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-08-2005


Erik wrote:
> Is there a variable for storing dates and how can I count with dates:
> For example, how do I code how much days there are between 01-01-2005
> and 03-03-2005?


The library provides some rudimentary support for date
computations, but they're not really very well-supported by
C as such. Here's something you could try:

#include <time.h>
...
struct tm date1 = { 0 }, date2 = { 0 };
time_t time1, time2;
int days;

date1.tm_year = 2005 - 1900;
date1.tm_mon = 1 - 1;
date1.tm_mday = 1;
date1.tm_isdst = -1;
time1 = mktime(&date1);

date2.tm_year = 2005 - 1900;
date2.tm_mon = 3 - 1;
date2.tm_mday = 3;
date2.tm_isdst = -1;
time2 = mktime(&date2);

days = difftime(time2, time1) / (60.0 * 60.0 * 24.0);

Unfortunately, this is not guaranteed to work as you'd hope.
Different implementations of C support different ranges of
dates, and either or both of the mktime() calls could fail.
(All implementations I know of can handle dates in 2005, but
if you start trying to work with dates in 2100 or 1776 your
results may be disappointing.) Also, the "divide by one day's
worth of seconds" step is a little too naive: Most days are
24 hours long, but many places observe one 23-hour and one
25-hour day each year -- some C implementations can handle
this adjustment, others can't. Days of 24:00:01 or 23:59:59
are also possible, and (believe it or not) some systems are
actually *forbidden* to pay attention to this!

The method illustrated above will work reasonably well on
many systems for a limited range of dates most of the time,
but that's about all that can be said for it.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RFC-822 dates into Ruby dates kellygreer1 Ruby 1 06-08-2008 10:59 PM
Need to use dates earlier than 1900 (Time library says out of range for dates < 1900) me@benjaminarai.com Ruby 1 07-17-2007 02:25 PM
FAQ Dates; Opera Dates. Dr J R Stockton Javascript 0 06-18-2007 03:51 PM
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM



Advertisments