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 *****************************/
47 #include "parsedate.h"
49 static unsigned short mon[] =
57 static const char * abr_month_name[] =
60 "Jan", "Feb", "Mar", "Apr",
61 "May", "Jun", "Jul", "Aug",
62 "Sep", "Oct", "Nov", "Dec"
67 struct parseddate *pd;
92 leap--; // But not every century
94 leap++; // except every 4th century
99 unsigned date::DaysInMonth(void)
103 if (month < 1 || month > 12)
111 if (month == 2 && Leap()) // Februari
120 String date::MonthName(void)
124 return String(abr_month_name[month]);
128 return String("xxx");
132 unsigned date::YearDay(void)
138 for (unsigned m = 1; m < month; m++)
140 days += date(1, m, year).DaysInMonth();
148 date date::add(date D)
158 while (day > DaysInMonth())
160 day -= DaysInMonth();
172 date date::add(unsigned long days)
174 // Calculate the date <days> days in the future
177 while (days > DaysInMonth())
179 days -= DaysInMonth();
192 date date::subtract(unsigned long days)
194 // Calculate the date <days> days in the past
221 return year != -1 && year != 0 &&
222 month > 0 && month <= 12 &&
223 day > 0 && day <= DaysInMonth();
226 date operator+(date d1, date d2)
232 date operator+(unsigned long l, date d)
238 date operator+(date d, unsigned long l)
244 date operator-(unsigned long l, date d)
250 date operator-(date d, unsigned long l)
256 date today(bool local)
264 tp = localtime(&clock);
271 return date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900);
274 /*=========================================================================
275 ** NAME : date::julian
280 ** DESCRIPTION : Return the number of days since the base date of the
290 **=========================================================================
293 long date::julian(void)
313 return (146097 * cent >> 2) + (1461 * yr >> 2) +
314 (153 * mn + 2) / 5 + dy + 1721119;
318 long operator-(date &d1, date &d2)
320 return d1.julian() - d2.julian();
323 std::ostream &operator<<(std::ostream &s, const date &d)
328 s << int(d.day) << "-";
331 s << int(d.month) << "-";
337 std::istream &operator>>(std::istream &s, date &d)
342 s >> D >> c >> M >> c >> Y;
358 String date::format(const char *fmt)
364 t.tm_year = year - 1900;
365 t.tm_mon = month - 1;
368 strftime(buf, 40, fmt, &t);