konsolekalendar
konsolekalendarepoch.cpp
Go to the documentation of this file.00001 /******************************************************************************* 00002 * konsolekalendarepoch.cpp * 00003 * * 00004 * KonsoleKalendar is a command line interface to KDE calendars * 00005 * Copyright (C) 2002-2004 Tuukka Pasanen <illuusio@mailcity.com> * 00006 * Copyright (C) 2003-2005 Allen Winter <winter@kde.org> * 00007 * * 00008 * This program is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation; either version 2 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program; if not, write to the Free Software * 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 00021 * * 00022 * As a special exception, permission is given to link this program * 00023 * with any edition of Qt, and distribute the resulting executable, * 00024 * without including the source code for Qt in the source distribution. * 00025 * * 00026 ******************************************************************************/ 00033 #include <stdlib.h> 00034 #include <iostream> 00035 00036 #include <qdatetime.h> 00037 #include "konsolekalendarepoch.h" 00038 00039 using namespace KCal; 00040 using namespace std; 00041 00042 KonsoleKalendarEpoch::KonsoleKalendarEpoch() 00043 { 00044 } 00045 00046 KonsoleKalendarEpoch::~KonsoleKalendarEpoch() 00047 { 00048 } 00049 00050 // By "epoch" we mean the number of seconds since 00:00:00 UTC on January 1 1970 00051 00052 // Function to convert an epoch value into a QDateTime 00053 QDateTime KonsoleKalendarEpoch::epoch2QDateTime( uint epoch ) 00054 { 00055 QDateTime dt; 00056 dt.setTime_t( epoch, Qt::UTC ); 00057 return( dt ); 00058 } 00059 00060 // Function to convert a QDateTime value into an epoch 00061 uint KonsoleKalendarEpoch::QDateTime2epoch( QDateTime dt ) 00062 { 00063 // THIS FUNCTION CAN BE OFF BY 1 HOUR DUE TO DAYLIGHT SAVINGS TIME. 00064 // SORRY QT DOESN'T HANDLE DAYLIGHT SAVINGS TIME. 00065 00066 // Compute #seconds to subtract for local timezone difference from UTC. 00067 int offset = QDateTime::currentDateTime( Qt::UTC ).toTime_t() 00068 - QDateTime::currentDateTime( Qt::LocalTime ).toTime_t(); 00069 return( dt.toTime_t() - offset ); 00070 } 00071 00072 #if defined (TEST) 00073 // Pass -DTEST to the compile command to create the test program, e.g: 00074 // cc -DTEST -I/usr/local/KDE/include konsolekalendarepoch.cpp 00075 // -L/usr/local/KDE/lib -lqt-mt -pthread 00076 main() 00077 { 00078 uint epoch; 00079 QDateTime dt; 00080 00081 cout << endl; 00082 cout << "NOTE: Some tests may be off by 1 hour (3600 secs) " 00083 << "due to daylight savings time" 00084 << endl << endl; 00085 00086 // Test1 00087 epoch = 0; 00088 dt = KonsoleKalendarEpoch::epoch2QDateTime( epoch ); 00089 cout << "TEST 1:" << endl; 00090 cout << "epoch=" 00091 << epoch 00092 << " converts to " 00093 << dt.toString( Qt::TextDate ) 00094 << endl; 00095 00096 epoch = KonsoleKalendarEpoch::QDateTime2epoch( dt ); 00097 cout << "date=" 00098 << dt.toString( Qt::TextDate ) 00099 << " converts to " 00100 << "epoch=" 00101 << epoch 00102 << endl; 00103 00104 // Test2 00105 epoch = 100000; 00106 dt = KonsoleKalendarEpoch::epoch2QDateTime( epoch ); 00107 cout << "TEST 2:" << endl; 00108 cout << "epoch=" 00109 << epoch 00110 << " converts to " 00111 << dt.toString( Qt::TextDate ) 00112 << endl; 00113 00114 epoch = KonsoleKalendarEpoch::QDateTime2epoch( dt ); 00115 cout << "date=" 00116 << dt.toString( Qt::TextDate ) 00117 << " converts to " 00118 << "epoch=" 00119 << epoch 00120 << endl; 00121 00122 // Test3 00123 epoch = 10000000; 00124 dt = KonsoleKalendarEpoch::epoch2QDateTime( epoch ); 00125 cout << "TEST 3:" << endl; 00126 cout << "epoch=" 00127 << epoch 00128 << " converts to " 00129 << dt.toString( Qt::TextDate ) 00130 << endl; 00131 00132 epoch = KonsoleKalendarEpoch::QDateTime2epoch( dt ); 00133 cout << "date=" 00134 << dt.toString( Qt::TextDate ) 00135 << " converts to " 00136 << "epoch=" 00137 << epoch 00138 << endl; 00139 00140 // Test4 00141 epoch = 1000000000; 00142 dt = KonsoleKalendarEpoch::epoch2QDateTime( epoch ); 00143 cout << "TEST 4:" << endl; 00144 cout << "epoch=" 00145 << epoch 00146 << " converts to " 00147 << dt.toString( Qt::TextDate ) 00148 << endl; 00149 00150 epoch = KonsoleKalendarEpoch::QDateTime2epoch( dt ); 00151 cout << "date=" 00152 << dt.toString( Qt::TextDate ) 00153 << " converts to " 00154 << "epoch=" 00155 << epoch 00156 << endl; 00157 00158 // Test5 00159 epoch = 10000000000; 00160 dt = KonsoleKalendarEpoch::epoch2QDateTime( epoch ); 00161 cout << "TEST 5:" << endl; 00162 cout << "epoch=" 00163 << epoch 00164 << " converts to " 00165 << dt.toString( Qt::TextDate ) 00166 << endl; 00167 00168 epoch = KonsoleKalendarEpoch::QDateTime2epoch( dt ); 00169 cout << "date=" 00170 << dt.toString( Qt::TextDate ) 00171 << " converts to " 00172 << "epoch=" 00173 << epoch 00174 << endl; 00175 } 00176 #endif