summaryrefslogtreecommitdiffstats
path: root/libkdeedu/extdate/extdatetime.h
blob: c398b7c56918d8b014e8d56ba278af713f76e647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*************************************************************************
** Definition of extended range date class
** (c) 2004 by Michel Guitel <michel.guitel@sap.ap-hop-paris.fr>
** modifications by Jason Harris <kstars@30doradus.org>
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#ifndef EXTDATETIME_H
#define EXTDATETIME_H

#include <limits.h>
#include "tqstring.h"
#include "tqnamespace.h"
#include "tqdatetime.h"
#include <kdemacros.h>

#define INVALID_DAY LONG_MIN

class ExtDateTime;

/*****************************************************************************
  ExtDate class
 *****************************************************************************/

extern	void test2_unit(int y, int m, int d);

class ExtDateTime;

class KDE_EXPORT ExtDate
{
public:
	ExtDate() : m_jd(INVALID_DAY), m_year(0), m_month(0), m_day(0) {}
	ExtDate( int y, int m, int d );
	ExtDate( const TQDate &q ) { ExtDate( q.year(), q.month(), q.day() ); }
	ExtDate( long int jd );

	bool isNull() const { return m_jd == INVALID_DAY; }
	bool isValid() const;

	TQDate qdate() const;

	int year() const { return m_year; }
	int month() const { return m_month; }
	int day() const { return m_day; }
	int dayOfWeek() const;
	int dayOfYear() const;
	int daysInMonth() const;
	int daysInYear() const;
	int weekNumber( int *yearNum = 0 ) const;
	long int jd() const { return m_jd; }

#ifndef QT_NO_TEXTDATE
#ifndef QT_NO_COMPAT
	static TQString monthName( int month ) { return shortMonthName( month ); }
	static TQString dayName( int weekday ) { return shortDayName( weekday ); }
#endif
	static TQString shortMonthName( int month );
	static TQString shortDayName( int weekday );
	static TQString longMonthName( int month );
	static TQString longDayName( int weekday );
#endif //QT_NO_TEXTDATE
#ifndef QT_NO_TEXTSTRING
#if !defined(QT_NO_SPRINTF)
	TQString toString( Qt::DateFormat f = Qt::TextDate ) const;
#endif
	TQString toString( const TQString& format ) const;
#endif
	bool setYMD( int y, int m, int d );
	bool setJD( long int _jd );

	ExtDate addDays( int days ) const;
	ExtDate addMonths( int months ) const;
	ExtDate addYears( int years ) const;
	int daysTo( const ExtDate & ) const;

	bool operator==( const ExtDate &d ) const { return m_jd == d.jd(); }
	bool operator!=( const ExtDate &d ) const { return m_jd != d.jd(); }
	bool operator<( const ExtDate &d )  const { return m_jd  < d.jd(); }
	bool operator<=( const ExtDate &d ) const { return m_jd <= d.jd(); }
	bool operator>( const ExtDate &d )  const { return m_jd  > d.jd(); }
	bool operator>=( const ExtDate &d ) const { return m_jd >= d.jd(); }

	static ExtDate tqcurrentDate( Qt::TimeSpec ts = Qt::LocalTime );
#ifndef QT_NO_DATESTRING
	static ExtDate fromString( const TQString &s );
	static ExtDate fromString( const TQString &s, Qt::DateFormat f );
#endif
	static bool isValid( int y, int m, int d );
	static bool leapYear( int year );

	static long int GregorianToJD( int y, int m, int d );
	static void JDToGregorian( long int jd, int &y, int &m, int &d );

private:
	static int dayOfYear(int y, int m, int d);

	long int m_jd;
	int m_year, m_month, m_day;
	static uint m_monthLength[12];
	static uint m_monthOrigin[12];
	static TQString m_shortMonthNames[12];
	static TQString m_shortDayNames[7];
	static TQString m_longMonthNames[12];
	static TQString m_longDayNames[7];

	friend class ExtDateTime;

#ifndef QT_NO_DATASTREAM
	friend Q_EXPORT TQDataStream &operator<<( TQDataStream &, const ExtDate & );
	friend Q_EXPORT TQDataStream &operator>>( TQDataStream &, ExtDate & );
	friend Q_EXPORT TQDataStream &operator<<( TQDataStream &, const ExtDateTime & );
	friend Q_EXPORT TQDataStream &operator>>( TQDataStream &, ExtDateTime & );
#endif
};

/*****************************************************************************
  ExtDateTime class
 *****************************************************************************/

class KDE_EXPORT ExtDateTime
{
public:
    ExtDateTime() {}				// set null date and null time
    ExtDateTime( const ExtDate & );
    ExtDateTime( const ExtDate &, const TQTime & );

    bool   isNull()	const		{ return d.isNull() && t.isNull(); }
    bool   isValid()	const		{ return d.isValid() && t.isValid(); }

    ExtDate  date()	const		{ return d; }
    TQTime  time()	const		{ return t; }
    uint   toTime_t()	const;
    void   setDate( const ExtDate &date ) { d = date; }
    void   setTime( const TQTime &time ) { t = time; }
    void   setTime_t( uint secsSince1Jan1970UTC );
    void   setTime_t( uint secsSince1Jan1970UTC, Qt::TimeSpec );
#ifndef QT_NO_DATESTRING
#ifndef QT_NO_SPRINTF
    TQString toString( Qt::DateFormat f = Qt::TextDate )	const;
#endif
    TQString toString( const TQString& format ) const;
#endif
    ExtDateTime addDays( int days )	const;
    ExtDateTime addMonths( int months )   const;
    ExtDateTime addYears( int years )     const;
    ExtDateTime addSecs( int secs )	const;
    int	   daysTo( const ExtDateTime & )	const;
    int	   secsTo( const ExtDateTime & )	const;

    bool   operator==( const ExtDateTime &dt ) const;
    bool   operator!=( const ExtDateTime &dt ) const;
    bool   operator<( const ExtDateTime &dt )  const;
    bool   operator<=( const ExtDateTime &dt ) const;
    bool   operator>( const ExtDateTime &dt )  const;
    bool   operator>=( const ExtDateTime &dt ) const;

    static ExtDateTime tqcurrentDateTime();
    static ExtDateTime tqcurrentDateTime( Qt::TimeSpec );
#ifndef QT_NO_DATESTRING
    static ExtDateTime fromString( const TQString &s );
    static ExtDateTime fromString( const TQString &s, Qt::DateFormat f );
#endif
private:
    ExtDate  d;
    TQTime  t;
#ifndef QT_NO_DATASTREAM
    friend Q_EXPORT TQDataStream &operator<<( TQDataStream &, const ExtDateTime &);
    friend Q_EXPORT TQDataStream &operator>>( TQDataStream &, ExtDateTime & );
#endif
};

/*****************************************************************************
  Date and time stream functions
 *****************************************************************************/

#ifndef QT_NO_DATASTREAM
Q_EXPORT TQDataStream &operator<<( TQDataStream &, const ExtDate & );
Q_EXPORT TQDataStream &operator>>( TQDataStream &, ExtDate & );
#endif // QT_NO_DATASTREAM

#endif // EXTDATE_H