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.3 $
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.3 2002-09-28 06:58:45 arjen
26 Bugfix: conversion of an empty string to a date or hour object
27 now makes the values of such an object 0 (null) instead of giving
29 The class UTC combines the date and hour classes. The most basic
30 functions of the UTC class are now implemented.
31 These include constructors and conversion to and from String objects.
32 New functions: date::proper(), hour::proper() and UTC::proper().
33 Return true if the object holds a proper clock time and/or calendar
34 date; false if at least one value is out of range.
36 Revision 1.2 2002/09/02 06:18:20 arjen
37 Fixed some date and time conversion functions
39 Revision 1.1 2002/07/25 08:01:26 arjen
40 First checkin, AXE release 0.2
42 *****************************/
44 static const char *RCSID = "$Id: date.cpp,v 1.3 2002-09-28 06:58:45 arjen Exp $";
49 #include "parsedate.h"
51 static unsigned short mon[] =
59 static char * abr_month_name[] =
62 "Jan", "Feb", "Mar", "Apr",
63 "May", "Jun", "Jul", "Aug",
64 "Sep", "Oct", "Nov", "Dec"
69 struct parseddate *pd;
94 leap--; // But not every century
96 leap++; // except every 4th century
101 unsigned date::DaysInMonth(void)
105 if (month < 1 || month > 12)
113 if (month == 2 && Leap()) // Februari
122 date date::add(date D)
132 while (day > DaysInMonth())
134 day -= DaysInMonth();
146 date date::add(unsigned long days)
148 // Calculate the date <days> days in the future
151 while (days > DaysInMonth())
153 days -= DaysInMonth();
166 date date::subtract(unsigned long days)
168 // Calculate the date <days> days in the past
195 return year != -1 && year != 0 &&
196 month > 0 && month <= 12 &&
197 day > 0 && day <= DaysInMonth();
200 date operator+(date d1, date d2)
206 date operator+(unsigned long l, date d)
212 date operator+(date d, unsigned long l)
218 date operator-(unsigned long l, date d)
224 date operator-(date d, unsigned long l)
236 tp = localtime(&clock);
238 return date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900);
241 /*=========================================================================
242 ** NAME : date::julian
247 ** DESCRIPTION : Return the number of days since the base date of the
257 **=========================================================================
260 long date::julian(void)
263 register int yr, mn, dy;
280 return (146097 * cent >> 2) + (1461 * yr >> 2) +
281 (153 * mn + 2) / 5 + dy + 1721119;
285 long operator-(date &d1, date &d2)
287 return d1.julian() - d2.julian();
290 std::ostream &operator<<(std::ostream &s, const date &d)
295 s << int(d.day) << "-";
298 s << int(d.month) << "-";
304 std::istream &operator>>(std::istream &s, date &d)
309 s >> D >> c >> M >> c >> Y;
325 String date::MonthName(void)
329 return String(abr_month_name[month]);
333 return String("xxx");
337 String date::format(const char *fmt)
343 t.tm_year = year - 1900;
344 t.tm_mon = month - 1;
347 strftime(buf, 40, fmt, &t);