konsolekalendar Library API Documentation

konsolekalendarepoch.cpp

00001 /***************************************************************************
00002         konsolekalendarepoch.cpp - epoch calcuations
00003         ----------------------------------
00004     begin                : Thu Jul 10 2003
00005     copyright            : (C) 2003 by Allen Winter
00006     email                : awinterz@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <stdlib.h>
00019 #include <iostream>
00020 
00021 #include <qdatetime.h>
00022 #include "konsolekalendarepoch.h"
00023 
00024 using namespace KCal;
00025 using namespace std;
00026 
00027 KonsoleKalendarEpoch::KonsoleKalendarEpoch( )
00028 {
00029 }
00030 
00031 KonsoleKalendarEpoch::~KonsoleKalendarEpoch()
00032 {
00033 }
00034 
00035 
00036 // By "epoch" we mean the number of seconds since 00:00:00 UTC on January 1 1970
00037 
00038 // Function to convert an epoch value into a QDateTime
00039 QDateTime KonsoleKalendarEpoch::epoch2QDateTime( uint epoch )
00040 {
00041   QDateTime dt;
00042   dt.setTime_t (epoch,Qt::UTC);
00043   return (dt);
00044 }
00045 
00046 // Function to convert a QDateTime value into an epoch
00047 uint KonsoleKalendarEpoch::QDateTime2epoch( QDateTime dt )
00048 {
00049   // THIS FUNCTION CAN BE OFF BY 1 HOUR DUE TO DAYLIGHT SAVINGS TIME.
00050   // SORRY QT DOESN'T HANDLE DAYLIGHT SAVINGS TIME.
00051 
00052   //Compute number of seconds to subtract for local timezone difference from UTC.
00053   int offset =  QDateTime::currentDateTime(Qt::UTC).toTime_t()
00054               - QDateTime::currentDateTime(Qt::LocalTime).toTime_t();
00055   return(dt.toTime_t()-offset);
00056 }
00057 
00058 #if defined (TEST)
00059 // Pass -DTEST to the compile command to create the test program, e.g:
00060 // cc -DTEST -I/usr/local/KDE/include  konsolekalendarepoch.cpp -L/usr/local/KDE/lib -lqt-mt -pthread
00061 main()
00062 {
00063   uint epoch;
00064   QDateTime dt;
00065 
00066   cout << endl;
00067   cout << "NOTE: Some tests may be off by 1 hour (3600 secs) due to daylight savings time" << endl;
00068   cout << endl;
00069 
00070   // Test1
00071   epoch=0;
00072   dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch);
00073   cout << "TEST 1:" << endl;
00074   cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl;
00075 
00076   epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt);
00077   cout << "date=" << dt.toString(Qt::TextDate) << " converts to " << "epoch=" << epoch << endl;
00078   
00079   // Test2
00080   epoch=100000;
00081   dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch);
00082   cout << "TEST 2:" << endl;
00083   cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl;
00084 
00085   epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt);
00086   cout << "date=" << dt.toString(Qt::TextDate) << " converts to " << "epoch=" << epoch << endl;
00087 
00088   // Test3
00089   epoch=10000000;
00090   dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch);
00091   cout << "TEST 3:" << endl;
00092   cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl;
00093 
00094   epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt);
00095   cout << "date=" << dt.toString(Qt::TextDate) << " converts to " << "epoch=" << epoch << endl;
00096 
00097   // Test4
00098   epoch=1000000000;
00099   dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch);
00100   cout << "TEST 4:" << endl;
00101   cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl;
00102 
00103   epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt);
00104   cout << "date=" << dt.toString(Qt::TextDate) << " converts to " << "epoch=" << epoch << endl;
00105 
00106   // Test5
00107   epoch=10000000000;
00108   dt = KonsoleKalendarEpoch::epoch2QDateTime(epoch);
00109   cout << "TEST 5:" << endl;
00110   cout << "epoch=" << epoch << " converts to " << dt.toString(Qt::TextDate) << endl;
00111 
00112   epoch = KonsoleKalendarEpoch::QDateTime2epoch(dt);
00113   cout << "date=" << dt.toString(Qt::TextDate) << " converts to " << "epoch=" << epoch << endl;
00114 }
00115 #endif
KDE Logo
This file is part of the documentation for konsolekalendar Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:15 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003