1 /****************************************************************************** 2 * konsolekalendarepoch.cpp * 3 * * 4 * KonsoleKalendar is a command line interface to KDE calendars * 5 * SPDX-FileCopyrightText: 2002-2004 Tuukka Pasanen <illuusio@mailcity.com> * 6 * SPDX-FileCopyrightText: 2003-2005 Allen Winter <winter@kde.org> * 7 * * 8 * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 * 9 * * 10 ******************************************************************************/ 11 /** 12 * @file konsolekalendarepoch.cpp 13 * Provides the KonsoleKalendarEpoch class definition. 14 * @author Tuukka Pasanen 15 * @author Allen Winter 16 */ 17 #include "konsolekalendarepoch.h" 18 19 #include <iostream> 20 #include <stdlib.h> 21 22 using namespace std; 23 KonsoleKalendarEpoch()24KonsoleKalendarEpoch::KonsoleKalendarEpoch() 25 { 26 } 27 ~KonsoleKalendarEpoch()28KonsoleKalendarEpoch::~KonsoleKalendarEpoch() 29 { 30 } 31 32 // By "epoch" we mean the number of seconds since 00:00:00 UTC on January 1 1970 33 34 // Function to convert an epoch value into a QDateTime epoch2QDateTime(uint epoch)35QDateTime KonsoleKalendarEpoch::epoch2QDateTime(uint epoch) 36 { 37 QDateTime dt; 38 dt.setSecsSinceEpoch(epoch); 39 return dt; 40 } 41 42 // Function to convert a QDateTime value into an epoch QDateTime2epoch(const QDateTime & dt)43uint KonsoleKalendarEpoch::QDateTime2epoch(const QDateTime &dt) 44 { 45 // THIS FUNCTION CAN BE OFF BY 1 HOUR DUE TO DAYLIGHT SAVINGS TIME. 46 // SORRY QT DOESN'T HANDLE DAYLIGHT SAVINGS TIME. 47 48 // Compute #seconds to subtract for local timezone difference from UTC. 49 int offset = QDateTime::currentDateTimeUtc().toSecsSinceEpoch() - QDateTime::currentDateTime().toSecsSinceEpoch(); 50 return dt.toSecsSinceEpoch() - offset; 51 } 52 53 #if defined(TEST) 54 // Pass -DTEST to the compile command to create the test program, e.g: 55 // cc -DTEST -I/usr/local/KDE/include konsolekalendarepoch.cpp 56 // -L/usr/local/KDE/lib -lqt-mt -pthread main()57main() 58 { 59 uint epoch; 60 QDateTime dt; 61 62 cout << endl; 63 cout << "NOTE: Some tests may be off by 1 hour (3600 secs) " 64 << "due to daylight savings time" << endl 65 << endl; 66 67 // Test1 68 epoch = 0; 69 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 70 cout << "TEST 1:" << endl; 71 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 72 73 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 74 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 75 << "epoch=" << epoch << endl; 76 77 // Test2 78 epoch = 100000; 79 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 80 cout << "TEST 2:" << endl; 81 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 82 83 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 84 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 85 << "epoch=" << epoch << endl; 86 87 // Test3 88 epoch = 10000000; 89 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 90 cout << "TEST 3:" << endl; 91 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 92 93 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 94 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 95 << "epoch=" << epoch << endl; 96 97 // Test4 98 epoch = 1000000000; 99 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 100 cout << "TEST 4:" << endl; 101 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 102 103 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 104 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 105 << "epoch=" << epoch << endl; 106 107 // Test5 108 epoch = 10000000000; 109 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 110 cout << "TEST 5:" << endl; 111 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 112 113 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 114 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 115 << "epoch=" << epoch << endl; 116 } 117 #endif 118