mirror of
https://github.com/ElvishArtisan/rivendell.git
synced 2026-01-14 16:45:54 +01:00
2019-01-29 Fred Gleason <fredg@paravelsystems.com>
* Refactored routines for parsing/writing standard date/time strings (RFC822 and XML xs:dateTime formats) into 'lib/rddatetime.[cpp|h]. * Removed the 'RDWebDateTime()', 'RDGetWebDateTime()', 'RDGetWebDate()', 'RDGetWebTime()', 'RDGetWebMonth()', 'RDXmlDate()', 'RDXmlTime()', 'RDXmlDateTime()' and 'RDXmlTimeZoneSuffix()' functions from 'lib/rdweb.[cpp|h]. * Added a 'dateparse_test' test harness.
This commit is contained in:
@@ -32,6 +32,7 @@ noinst_PROGRAMS = audio_convert_test\
|
||||
audio_metadata_test\
|
||||
audio_peaks_test\
|
||||
datedecode_test\
|
||||
dateparse_test\
|
||||
db_charset_test\
|
||||
getpids_test\
|
||||
log_unlink_test\
|
||||
@@ -63,6 +64,9 @@ audio_peaks_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ -lQt3Support
|
||||
dist_datedecode_test_SOURCES = datedecode_test.cpp datedecode_test.h
|
||||
datedecode_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ -lQt3Support
|
||||
|
||||
dist_dateparse_test_SOURCES = dateparse_test.cpp dateparse_test.h
|
||||
dateparse_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ -lQt3Support
|
||||
|
||||
dist_db_charset_test_SOURCES = db_charset_test.cpp db_charset_test.h
|
||||
db_charset_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT4_LIBS@ -lQt3Support
|
||||
|
||||
|
||||
231
tests/dateparse_test.cpp
Normal file
231
tests/dateparse_test.cpp
Normal file
@@ -0,0 +1,231 @@
|
||||
// dateparse_test.cpp
|
||||
//
|
||||
// Test the Rivendell date/time parser routines.
|
||||
//
|
||||
// (C) Copyright 2019 Fred Gleason <fredg@paravelsystems.com>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License version 2 as
|
||||
// published by the Free Software Foundation.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public
|
||||
// License along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <qapplication.h>
|
||||
#include <qvariant.h>
|
||||
|
||||
#include <rdcmd_switch.h>
|
||||
#include <rddatetime.h>
|
||||
#include <rdweb.h>
|
||||
|
||||
#include "dateparse_test.h"
|
||||
|
||||
MainObject::MainObject(QObject *parent)
|
||||
:QObject(parent)
|
||||
{
|
||||
QString time="";
|
||||
QString datetime="";
|
||||
MainObject::Format format=MainObject::Unknown;
|
||||
MainObject::PrintType type=MainObject::None;
|
||||
|
||||
//
|
||||
// Read Command Options
|
||||
//
|
||||
RDCmdSwitch *cmd=
|
||||
new RDCmdSwitch(qApp->argc(),qApp->argv(),"dateparse_test",
|
||||
DATEPARSE_TEST_USAGE);
|
||||
for(unsigned i=0;i<cmd->keys();i++) {
|
||||
if(cmd->key(i)=="--datetime") {
|
||||
datetime=cmd->value(i);
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(cmd->key(i)=="--print") {
|
||||
if(cmd->value(i).toLower()=="date") {
|
||||
type=MainObject::Date;
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(cmd->value(i).toLower()=="time") {
|
||||
type=MainObject::Time;
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(cmd->value(i).toLower()=="datetime") {
|
||||
type=MainObject::DateTime;
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(!cmd->processed(i)) {
|
||||
fprintf(stderr,"dataparse_test: unknown --print value\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if(cmd->key(i)=="--format") {
|
||||
if(cmd->value(i).toLower()=="rfc822") {
|
||||
format=MainObject::Rfc822;
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(cmd->value(i).toLower()=="xml") {
|
||||
format=MainObject::Xml;
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(cmd->value(i).toLower()=="auto") {
|
||||
format=MainObject::Auto;
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(!cmd->processed(i)) {
|
||||
fprintf(stderr,"dataparse_test: unknown --format value\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if(cmd->key(i)=="--time") {
|
||||
time=cmd->value(i);
|
||||
cmd->setProcessed(i,true);
|
||||
}
|
||||
if(!cmd->processed(i)) {
|
||||
fprintf(stderr,"dateparse_test: unknown option \"%s\"\n",
|
||||
(const char *)cmd->value(i));
|
||||
exit(256);
|
||||
}
|
||||
}
|
||||
if(format==MainObject::Unknown) {
|
||||
fprintf(stderr,"dateparse_test: no --format specified\n");
|
||||
exit(1);
|
||||
}
|
||||
if(type!=MainObject::None) {
|
||||
if(format==MainObject::Auto) {
|
||||
fprintf(stderr,"dateparse_test: auto not supported for printing\n");
|
||||
exit(1);
|
||||
}
|
||||
switch(type) {
|
||||
case MainObject::Date:
|
||||
if(format!=MainObject::Xml) {
|
||||
fprintf(stderr,"dateparse_test: date unsupported for RFC822 format\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("XML xs:date: %s\n",
|
||||
(const char *)RDWriteXmlDate(QDate::currentDate()));
|
||||
break;
|
||||
|
||||
case MainObject::Time:
|
||||
if(format!=MainObject::Xml) {
|
||||
fprintf(stderr,"dateparse_test: time unsupported for RFC822 format\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("XML xs:time: %s\n",
|
||||
(const char *)RDWriteXmlTime(QTime::currentTime()));
|
||||
break;
|
||||
|
||||
case MainObject::DateTime:
|
||||
switch(format) {
|
||||
case MainObject::Xml:
|
||||
printf("XML xs:dateTime: %s\n",
|
||||
(const char *)RDWriteXmlDateTime(QDateTime::currentDateTime()));
|
||||
break;
|
||||
|
||||
case MainObject::Rfc822:
|
||||
printf("RFC822: %s\n",
|
||||
(const char *)RDWriteRfc822DateTime(QDateTime::currentDateTime()));
|
||||
break;
|
||||
|
||||
case MainObject::Auto:
|
||||
case MainObject::Unknown:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case MainObject::None:
|
||||
break;
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
if((!datetime.isEmpty())&&(!time.isEmpty())) {
|
||||
fprintf(stderr,
|
||||
"dateparse_test: --datetime and --time are mutually exclusive\n");
|
||||
exit(256);
|
||||
}
|
||||
if(datetime.isEmpty()&&time.isEmpty()) {
|
||||
fprintf(stderr,
|
||||
"dateparse_test: you must specify --datetime or --time\n");
|
||||
exit(256);
|
||||
}
|
||||
if((!time.isEmpty())&&(format==MainObject::Rfc822)) {
|
||||
fprintf(stderr,"dateparse_test: RFC822 format has no --time parser\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(!datetime.isEmpty()) {
|
||||
QDateTime dt;
|
||||
bool ok=false;
|
||||
|
||||
switch(format) {
|
||||
case MainObject::Rfc822:
|
||||
dt=RDParseRfc822DateTime(datetime,&ok);
|
||||
break;
|
||||
|
||||
case MainObject::Xml:
|
||||
dt=RDParseXmlDateTime(datetime,&ok);
|
||||
break;
|
||||
|
||||
case MainObject::Auto:
|
||||
dt=RDParseDateTime(datetime,&ok);
|
||||
break;
|
||||
|
||||
case MainObject::Unknown:
|
||||
break;
|
||||
}
|
||||
if(ok) {
|
||||
printf("DateTime: %s\n",(const char *)dt.toString("yyyy-MM-dd hh:mm:ss").toUtf8());
|
||||
}
|
||||
else {
|
||||
printf("invalid date/time string\n");
|
||||
}
|
||||
}
|
||||
|
||||
if(!time.isEmpty()) {
|
||||
QTime t;
|
||||
int day_offset=0;
|
||||
bool ok=false;
|
||||
switch(format) {
|
||||
case MainObject::Xml:
|
||||
case MainObject::Auto:
|
||||
t=RDParseXmlTime(time,&ok,&day_offset);
|
||||
break;
|
||||
|
||||
case MainObject::Rfc822:
|
||||
case MainObject::Unknown:
|
||||
break;
|
||||
}
|
||||
if(ok) {
|
||||
QString offset_str="";
|
||||
if(day_offset<0) {
|
||||
offset_str=QString().sprintf(" (lost %d day)",-day_offset);
|
||||
}
|
||||
if(day_offset>0) {
|
||||
offset_str=QString().sprintf(" (gained %d day)",day_offset);
|
||||
}
|
||||
printf("Time: %s\n",(const char *)(t.toString("hh:mm:ss")+offset_str).toUtf8());
|
||||
}
|
||||
else {
|
||||
printf("invalid time string\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
QApplication a(argc,argv,false);
|
||||
new MainObject();
|
||||
return a.exec();
|
||||
}
|
||||
37
tests/dateparse_test.h
Normal file
37
tests/dateparse_test.h
Normal file
@@ -0,0 +1,37 @@
|
||||
// dateparse_test.h
|
||||
//
|
||||
// Test the Rivendell date/time parser routines.
|
||||
//
|
||||
// (C) Copyright 2019 Fred Gleason <fredg@paravelsystems.com>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License version 2 as
|
||||
// published by the Free Software Foundation.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public
|
||||
// License along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
|
||||
#ifndef DATEPARSE_TEST_H
|
||||
#define DATEPARSE_TEST_H
|
||||
|
||||
#include <qobject.h>
|
||||
|
||||
#define DATEPARSE_TEST_USAGE "[options]\n\nTest the Rivendell date parsing routines\n\nOptions are:\n--fomat=rfc822|xml|auto\n Use RFC822 or XML string formats\n\n--print=date|time|datetime\n If specified, print the system's current date/time, using the\n specified format.\n\n--datetime=<datetime-str>\n Parse the <datetime-str> string using RDGetWebDateTime() and print\n the result on stdout.\n\n--time=<time-str>\n Parse the <time-str> string using RDGetWebTime() and\n print the results on stdout.\n\n"
|
||||
|
||||
class MainObject : public QObject
|
||||
{
|
||||
public:
|
||||
enum Format {Unknown=0,Rfc822=1,Xml=2,Auto=3};
|
||||
enum PrintType {None=0,Date=1,Time=2,DateTime=3};
|
||||
MainObject(QObject *parent=0);
|
||||
};
|
||||
|
||||
|
||||
#endif // DATEPARSE_TEST_H
|
||||
Reference in New Issue
Block a user