File indexing completed on 2024-05-12 05:10:36
0001 /****************************************************************************** 0002 * konsolekalendarepoch.cpp * 0003 * * 0004 * KonsoleKalendar is a command line interface to KDE calendars * 0005 * SPDX-FileCopyrightText: 2002-2004 Tuukka Pasanen <illuusio@mailcity.com> * 0006 * SPDX-FileCopyrightText: 2003-2005 Allen Winter <winter@kde.org> * 0007 * * 0008 * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 * 0009 * * 0010 ******************************************************************************/ 0011 /** 0012 * @file konsolekalendarepoch.cpp 0013 * Provides the KonsoleKalendarEpoch class definition. 0014 * @author Tuukka Pasanen 0015 * @author Allen Winter 0016 */ 0017 #include "konsolekalendarepoch.h" 0018 0019 #include <cstdlib> 0020 #include <iostream> 0021 0022 using namespace std; 0023 0024 KonsoleKalendarEpoch::KonsoleKalendarEpoch() = default; 0025 0026 KonsoleKalendarEpoch::~KonsoleKalendarEpoch() = default; 0027 0028 // By "epoch" we mean the number of seconds since 00:00:00 UTC on January 1 1970 0029 0030 // Function to convert an epoch value into a QDateTime 0031 QDateTime KonsoleKalendarEpoch::epoch2QDateTime(uint epoch) 0032 { 0033 QDateTime dt; 0034 dt.setSecsSinceEpoch(epoch); 0035 return dt; 0036 } 0037 0038 // Function to convert a QDateTime value into an epoch 0039 uint KonsoleKalendarEpoch::QDateTime2epoch(const QDateTime &dt) 0040 { 0041 // THIS FUNCTION CAN BE OFF BY 1 HOUR DUE TO DAYLIGHT SAVINGS TIME. 0042 // SORRY QT DOESN'T HANDLE DAYLIGHT SAVINGS TIME. 0043 0044 // Compute #seconds to subtract for local timezone difference from UTC. 0045 int offset = QDateTime::currentDateTimeUtc().toSecsSinceEpoch() - QDateTime::currentDateTime().toSecsSinceEpoch(); 0046 return dt.toSecsSinceEpoch() - offset; 0047 } 0048 0049 #if defined(TEST) 0050 // Pass -DTEST to the compile command to create the test program, e.g: 0051 // cc -DTEST -I/usr/local/KDE/include konsolekalendarepoch.cpp 0052 // -L/usr/local/KDE/lib -lqt-mt -pthread 0053 main() 0054 { 0055 uint epoch; 0056 QDateTime dt; 0057 0058 cout << endl; 0059 cout << "NOTE: Some tests may be off by 1 hour (3600 secs) " 0060 << "due to daylight savings time" << endl 0061 << endl; 0062 0063 // Test1 0064 epoch = 0; 0065 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 0066 cout << "TEST 1:" << endl; 0067 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 0068 0069 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 0070 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 0071 << "epoch=" << epoch << endl; 0072 0073 // Test2 0074 epoch = 100000; 0075 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 0076 cout << "TEST 2:" << endl; 0077 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 0078 0079 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 0080 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 0081 << "epoch=" << epoch << endl; 0082 0083 // Test3 0084 epoch = 10000000; 0085 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 0086 cout << "TEST 3:" << endl; 0087 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 0088 0089 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 0090 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 0091 << "epoch=" << epoch << endl; 0092 0093 // Test4 0094 epoch = 1000000000; 0095 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 0096 cout << "TEST 4:" << endl; 0097 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 0098 0099 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 0100 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 0101 << "epoch=" << epoch << endl; 0102 0103 // Test5 0104 epoch = 10000000000; 0105 dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch); 0106 cout << "TEST 5:" << endl; 0107 cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl; 0108 0109 epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt); 0110 cout << "date=" << dt.toString(Qt::TextDate) << " converts to " 0111 << "epoch=" << epoch << endl; 0112 } 0113 #endif