konsolekalendar

konsolekalendarepoch.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * konsolekalendarepoch.cpp *
3  * *
4  * KonsoleKalendar is a command line interface to KDE calendars *
5  * Copyright (C) 2002-2004 Tuukka Pasanen <illuusio@mailcity.com> *
6  * Copyright (C) 2003-2005 Allen Winter <winter@kde.org> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21  * *
22  * As a special exception, permission is given to link this program *
23  * with any edition of TQt, and distribute the resulting executable, *
24  * without including the source code for TQt in the source distribution. *
25  * *
26  ******************************************************************************/
33 #include <stdlib.h>
34 #include <iostream>
35 
36 #include <tqdatetime.h>
37 #include "konsolekalendarepoch.h"
38 
39 using namespace KCal;
40 using namespace std;
41 
43 {
44 }
45 
47 {
48 }
49 
50 // By "epoch" we mean the number of seconds since 00:00:00 UTC on January 1 1970
51 
52 // Function to convert an epoch value into a TQDateTime
54 {
55  TQDateTime dt;
56  dt.setTime_t( epoch, TQt::UTC );
57  return( dt );
58 }
59 
60 // Function to convert a TQDateTime value into an epoch
62 {
63  // THIS FUNCTION CAN BE OFF BY 1 HOUR DUE TO DAYLIGHT SAVINGS TIME.
64  // SORRY QT DOESN'T HANDLE DAYLIGHT SAVINGS TIME.
65 
66  // Compute #seconds to subtract for local timezone difference from UTC.
67  int offset = TQDateTime::currentDateTime( TQt::UTC ).toTime_t()
68  - TQDateTime::currentDateTime( TQt::LocalTime ).toTime_t();
69  return( dt.toTime_t() - offset );
70 }
71 
72 #if defined (TEST)
73 // Pass -DTEST to the compile command to create the test program, e.g:
74 // cc -DTEST -I/usr/local/KDE/include konsolekalendarepoch.cpp
75 // -L/usr/local/KDE/lib -lqt-mt -pthread
76 main()
77 {
78  uint epoch;
79  TQDateTime dt;
80 
81  cout << endl;
82  cout << "NOTE: Some tests may be off by 1 hour (3600 secs) "
83  << "due to daylight savings time"
84  << endl << endl;
85 
86  // Test1
87  epoch = 0;
89  cout << "TEST 1:" << endl;
90  cout << "epoch="
91  << epoch
92  << " converts to "
93  << dt.toString( TQt::TextDate )
94  << endl;
95 
97  cout << "date="
98  << dt.toString( TQt::TextDate )
99  << " converts to "
100  << "epoch="
101  << epoch
102  << endl;
103 
104  // Test2
105  epoch = 100000;
107  cout << "TEST 2:" << endl;
108  cout << "epoch="
109  << epoch
110  << " converts to "
111  << dt.toString( TQt::TextDate )
112  << endl;
113 
115  cout << "date="
116  << dt.toString( TQt::TextDate )
117  << " converts to "
118  << "epoch="
119  << epoch
120  << endl;
121 
122  // Test3
123  epoch = 10000000;
125  cout << "TEST 3:" << endl;
126  cout << "epoch="
127  << epoch
128  << " converts to "
129  << dt.toString( TQt::TextDate )
130  << endl;
131 
133  cout << "date="
134  << dt.toString( TQt::TextDate )
135  << " converts to "
136  << "epoch="
137  << epoch
138  << endl;
139 
140  // Test4
141  epoch = 1000000000;
143  cout << "TEST 4:" << endl;
144  cout << "epoch="
145  << epoch
146  << " converts to "
147  << dt.toString( TQt::TextDate )
148  << endl;
149 
151  cout << "date="
152  << dt.toString( TQt::TextDate )
153  << " converts to "
154  << "epoch="
155  << epoch
156  << endl;
157 
158  // Test5
159  epoch = 10000000000;
161  cout << "TEST 5:" << endl;
162  cout << "epoch="
163  << epoch
164  << " converts to "
165  << dt.toString( TQt::TextDate )
166  << endl;
167 
169  cout << "date="
170  << dt.toString( TQt::TextDate )
171  << " converts to "
172  << "epoch="
173  << epoch
174  << endl;
175 }
176 #endif
static uint TQDateTime2epoch(TQDateTime dt)
Converts QT DateTime to epoch format.
static TQDateTime epoch2TQDateTime(uint epoch)
Converts epoch time to TQDateTime format.
Provides the KonsoleKalendarEpoch class definition.