+++ /dev/null
-/**************************************************************************
-** (c) Copyright 1999, Andromeda Technology & Automation
-***************************************************************************
-** MODULE INFORMATION *
-***********************
-** FILE NAME : date.cpp
-** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation
-** VERSION NUMBER : $Revision: 1.3 $
-**
-** DESCRIPTION :
-**
-** EXPORTED OBJECTS :
-** LOCAL OBJECTS :
-** MODULES USED :
-***************************************************************************
-** ADMINISTRATIVE INFORMATION *
-********************************
-** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
-** CREATION DATE : Feb 06, 1998
-** LAST UPDATE : Oct 16, 1999
-**************************************************************************/
-
-/*****************************
- $Log: date.cpp,v $
- Revision 1.3 2002-09-28 06:58:45 arjen
- Bugfix: conversion of an empty string to a date or hour object
- now makes the values of such an object 0 (null) instead of giving
- a segmentation fault.
- The class UTC combines the date and hour classes. The most basic
- functions of the UTC class are now implemented.
- These include constructors and conversion to and from String objects.
- New functions: date::proper(), hour::proper() and UTC::proper().
- Return true if the object holds a proper clock time and/or calendar
- date; false if at least one value is out of range.
-
- Revision 1.2 2002/09/02 06:18:20 arjen
- Fixed some date and time conversion functions
-
- Revision 1.1 2002/07/25 08:01:26 arjen
- First checkin, AXE release 0.2
-
-*****************************/
-
-static const char *RCSID = "$Id: date.cpp,v 1.3 2002-09-28 06:58:45 arjen Exp $";
-
-#include <iostream>
-#include <time.h>
-#include "date.h"
-#include "parsedate.h"
-
-static unsigned short mon[] =
-{
- 0,
- 31, 28, 31, 30,
- 31, 30, 31, 31,
- 30, 31, 30, 31
-};
-
-static char * abr_month_name[] =
-{
- "xxx",
- "Jan", "Feb", "Mar", "Apr",
- "May", "Jun", "Jul", "Aug",
- "Sep", "Oct", "Nov", "Dec"
-};
-
-date::date(String s)
-{
- struct parseddate *pd;
-
- if (~s == 0)
- {
- year = 0;
- month = 0;
- day = 0;
- }
- else
- {
- pd = parsedate(s);
- year = pd->year;
- month = pd->month;
- day = pd->day;
- }
-}
-
-int date::Leap(void)
-{
- int leap;
-
- leap = 0;
- if (year % 4 == 0)
- leap++; // leapyear
- if (year % 100 == 0)
- leap--; // But not every century
- if (year % 400 == 0)
- leap++; // except every 4th century
-
- return leap;
-}
-
-unsigned date::DaysInMonth(void)
-{
- unsigned days;
-
- if (month < 1 || month > 12)
- {
- days = 0;
- }
- else
- {
- days = mon[month];
-
- if (month == 2 && Leap()) // Februari
- {
- days++;
- }
- }
-
- return days;
-}
-
-date date::add(date D)
-{
- year += D.year;
- month += D.month;
- while (month > 12)
- {
- year++;
- month -= 12;
- }
- day += D.day;
- while (day > DaysInMonth())
- {
- day -= DaysInMonth();
- month++;
- if (month > 12)
- {
- year++;
- month -= 12;
- }
- }
-
- return *this;
-}
-
-date date::add(unsigned long days)
-{
- // Calculate the date <days> days in the future
-
- days += day;
- while (days > DaysInMonth())
- {
- days -= DaysInMonth();
- month++;
- if (month > 12)
- {
- year++;
- month -= 12;
- }
- }
- day = days;
-
- return *this;
-}
-
-date date::subtract(unsigned long days)
-{
- // Calculate the date <days> days in the past
-
- while (days != 0)
- {
- if (days < day)
- {
- day -= days;
- days = 0;
- }
- else
- {
- month--;
- if (month == 0)
- {
- year--;
- month = 12;
- }
- days -= day;
- day = DaysInMonth();
- }
- }
-
- return *this;
-}
-
-bool date::proper()
-{
- return year != -1 && year != 0 &&
- month > 0 && month <= 12 &&
- day > 0 && day <= DaysInMonth();
-}
-
-date operator+(date d1, date d2)
-{
- d1.add(d2);
- return d1;
-}
-
-date operator+(unsigned long l, date d)
-{
- d.add(l);
- return d;
-}
-
-date operator+(date d, unsigned long l)
-{
- d.add(l);
- return d;
-}
-
-date operator-(unsigned long l, date d)
-{
- d.subtract(l);
- return d;
-}
-
-date operator-(date d, unsigned long l)
-{
- d.subtract(l);
- return d;
-}
-
-date today()
-{
- long clock;
- struct tm *tp;
-
- time(&clock);
- tp = localtime(&clock);
-
- return date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900);
-}
-
-/*=========================================================================
-** NAME : date::julian
-** SYNOPSIS :
-** PARAMETERS :
-** RETURN VALUE :
-**
-** DESCRIPTION : Return the number of days since the base date of the
-** Julian Calendar.
-**
-** VARS USED :
-** VARS CHANGED :
-** FUNCTIONS USED :
-** SEE ALSO :
-** LAST MODIFIED :
-** SEE ALSO :
-** LAST MODIFIED :
-**=========================================================================
-*/
-
-long date::julian(void)
-{
- register long cent;
- register int yr, mn, dy;
-
- yr = year;
- mn = month;
- dy = day;
-
- if (mn > 2)
- mn -= 3;
- else
- {
- mn += 9;
- yr -= 1;
- }
-
- cent = yr / 100;
- yr %= 100;
-
- return (146097 * cent >> 2) + (1461 * yr >> 2) +
- (153 * mn + 2) / 5 + dy + 1721119;
-
-}
-
-long operator-(date &d1, date &d2)
-{
- return d1.julian() - d2.julian();
-}
-
-std::ostream &operator<<(std::ostream &s, const date &d)
-{
- s.width(2);
- s.fill('0');
-
- s << int(d.day) << "-";
- s.width(2);
- s.fill('0');
- s << int(d.month) << "-";
- s << d.year;
-
- return s;
-}
-
-std::istream &operator>>(std::istream &s, date &d)
-{
- char c;
- int D, M, Y;
-
- s >> D >> c >> M >> c >> Y;
-
- d.day = D;
- d.month = M;
- if (Y < 100)
- {
- if (Y < 70)
- Y += 2000;
- else
- Y += 1900;
- }
- d.year = Y;
-
- return s;
-}
-
-String date::MonthName(void)
-{
- if (month <= 12)
- {
- return String(abr_month_name[month]);
- }
- else
- {
- return String("xxx");
- }
-}
-
-String date::format(const char *fmt)
-{
- String s;
- char buf[40];
- struct tm t;
-
- t.tm_year = year - 1900;
- t.tm_mon = month - 1;
- t.tm_mday = day;
-
- strftime(buf, 40, fmt, &t);
- s = buf;
-
- return s;
-}
-