1 /**************************************************************************
2 ** (c) Copyright 1999, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 ** FILE NAME : date.cpp
7 ** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation
8 ** VERSION NUMBER : $Revision: 1.2 $
15 ***************************************************************************
16 ** ADMINISTRATIVE INFORMATION *
17 ********************************
18 ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 ** CREATION DATE : Feb 06, 1998
20 ** LAST UPDATE : Oct 16, 1999
21 **************************************************************************/
23 /*****************************
25 Revision 1.2 2002-09-02 06:18:20 arjen
26 Fixed some date and time conversion functions
28 Revision 1.1 2002/07/25 08:01:26 arjen
29 First checkin, AXE release 0.2
31 *****************************/
33 static const char *RCSID = "$Id: date.cpp,v 1.2 2002-09-02 06:18:20 arjen Exp $";
38 #include "parsedate.h"
40 static unsigned short mon[] =
48 static char * abr_month_name[] =
51 "Jan", "Feb", "Mar", "Apr",
52 "May", "Jun", "Jul", "Aug",
53 "Sep", "Oct", "Nov", "Dec"
58 struct parseddate *pd;
74 leap--; // But not every century
76 leap++; // except every 4th century
81 unsigned date::DaysInMonth(void)
85 if (month < 1 || month > 12)
93 if (month == 2 && Leap()) // Februari
102 date date::add(date D)
112 while (day > DaysInMonth())
114 day -= DaysInMonth();
126 date date::add(unsigned long days)
128 // Calculate the date <days> days in the future
131 while (days > DaysInMonth())
133 days -= DaysInMonth();
146 date date::subtract(unsigned long days)
148 // Calculate the date <days> days in the past
173 date operator+(date d1, date d2)
179 date operator+(unsigned long l, date d)
185 date operator+(date d, unsigned long l)
191 date operator-(unsigned long l, date d)
197 date operator-(date d, unsigned long l)
209 tp = localtime(&clock);
211 return date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900);
214 /*=========================================================================
215 ** NAME : date::julian
220 ** DESCRIPTION : Return the number of days since the base date of the
230 **=========================================================================
233 long date::julian(void)
236 register int yr, mn, dy;
253 return (146097 * cent >> 2) + (1461 * yr >> 2) +
254 (153 * mn + 2) / 5 + dy + 1721119;
258 long operator-(date &d1, date &d2)
260 return d1.julian() - d2.julian();
263 ostream &operator<<(ostream &s, const date &d)
268 s << int(d.day) << "-";
271 s << int(d.month) << "-";
277 istream &operator>>(istream &s, date &d)
282 s >> D >> c >> M >> c >> Y;
298 String date::MonthName(void)
302 return String(abr_month_name[month]);
306 return String("xxx");
310 String date::format(const char *fmt)
316 t.tm_year = year - 1900;
317 t.tm_mon = month - 1;
320 strftime(buf, 40, fmt, &t);