File indexing completed on 2024-12-22 04:17:19

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2005 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "dateparser.h"
0014 #include <qstringlist.h>
0015 
0016 #include <assert.h>
0017 #include <math.h>
0018 #include <stdlib.h>
0019 
0020 namespace Kst {
0021 
0022 QDateTime millisecondsToQDateTime(double ms) {
0023   QDateTime edt;
0024   edt.setTime_t(0);
0025   if (ms > 0.0) {
0026     double milli = fmod(ms, 1000.0);
0027     ms = (ms - milli) / 1000.0;
0028     assert(ms < 60*365*24*60*60); // we can't handle big dates yet
0029     // this will have to change when we do
0030     edt.setTime_t(int(ms));
0031     QTime t = edt.time();
0032     t.setHMS(t.hour(), t.minute(), t.second(), int(milli));
0033     edt.setTime(t);
0034   } if (ms < 0.0) {
0035     abort(); // unhandled at this point
0036   }
0037   return edt;
0038 }
0039 
0040 
0041 double extDateTimeToMilliseconds(const QDateTime& edt) {
0042   double rc = 0.0;
0043   if (edt.isNull()) {
0044     return rc;
0045   }
0046 
0047   int year = edt.date().year();
0048   if (year > 1969 && year < 2030) { // fix later
0049     rc = 1000.0 * edt.toTime_t() + edt.time().msec();
0050   } else {
0051     // Manually construct rc
0052     abort();
0053   }
0054   return rc;
0055 }
0056 
0057 
0058 QDateTime parsePlanckDate(const QString& dateString) {
0059   QStringList secondSplit = dateString.split('.');
0060   if (secondSplit.isEmpty() || secondSplit.count() > 2) {
0061     return QDateTime();
0062   }
0063 
0064   int seconds = 0;
0065   if (secondSplit.count() > 1) {
0066     seconds = secondSplit[1].toUInt();
0067   }
0068 
0069   QStringList mainSplit = secondSplit[0].split(':');
0070   QDateTime edt = QDateTime::currentDateTime();
0071   int offset = QDateTime::currentDateTime().toUTC().toTime_t() - edt.toTime_t();
0072   QDate d = edt.date();
0073   QTime t = edt.time();
0074   int i = 0;
0075   switch (mainSplit.count()) {
0076     default:
0077       return QDateTime();
0078     case 5:
0079       {
0080         int years = mainSplit[i++].toInt();
0081         if (years < 100) {
0082           if (years < 0) {
0083             years = 1970 - years;
0084           } else {
0085             years += 2000;
0086           }
0087         }
0088         d.setDate(years, d.month(), d.day());
0089       }
0090     case 4:
0091       {
0092         unsigned month = mainSplit[i++].toUInt();
0093         d.setDate(d.year(), month, d.day());
0094       }
0095     case 3:
0096       {
0097         unsigned day = mainSplit[i++].toInt();
0098         d.setDate(d.year(), d.month(), day);
0099       }
0100       edt.setDate(d);
0101     case 2:
0102       {
0103         unsigned hour = mainSplit[i++].toInt();
0104         t.setHMS(hour, t.minute(), t.second());
0105       }
0106     case 1:
0107       {
0108         unsigned minute = mainSplit[i].toInt();
0109         t.setHMS(t.hour(), minute, t.second());
0110       }
0111     case 0:
0112       t.setHMS(t.hour(), t.minute(), seconds);
0113       edt.setTime(t);
0114       break;
0115   }
0116   return edt.addSecs(-offset);
0117 }
0118 
0119 }
0120 
0121 // vim: ts=2 sw=2 et