[mythtv] [PATCH] implemented time offset for mythfilldatabase

Christian Hoenig mythtv-dev@snowman.net
Mon, 18 Nov 2002 15:14:32 +0100


--Boundary-00=_IXP29OsectuYif1
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi mythies,

since I try to add nxtvepg(.sf.net) as a source for the xmltv-data I needed to 
make mythfilldatabase aware of timeoffsets in the data (given as <programm 
start="20021118150800 +0100" ...>). Therefore i programmed a patch for 
programms/mythfilldatabase/filldata.cpp.

You only need to add a settingskey "str TIMEoffset=+0100" with your local 
offset (in hours). If you don't set this key, everything should work as 
before.

This is, of course, only tested on my system, so could you please verify that 
this is working? 


take care, have fun
/christian

PS.: This is not the nicest code, but I tried to keep it easy readable, like 
the other code of mythtv :-)
--Boundary-00=_IXP29OsectuYif1
Content-Type: text/x-diff;
  charset="us-ascii";
  name="filldata.diff.offset"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="filldata.diff.offset"

--- filldata.cpp	2002-11-09 23:21:58.000000000 +0100
+++ mythtv-0.7/programs/mythfilldatabase/filldata.cpp	2002-11-18 14:37:45.000000000 +0100
@@ -169,22 +175,125 @@
     return chaninfo;
 }
 
+
+void addTimeOffset(QString &timestr, int config_off, QString offset )
+{
+	bool ok;
+	int off = offset.stripWhiteSpace().left(3).toInt(&ok, 10);
+		
+	if (ok && (off != config_off))
+	{
+		int diff = config_off - off;
+		
+		if (timestr.length() == 14)
+		{
+		
+			int year  = timestr.left(4).toInt(&ok, 10);
+			int month = timestr.mid(4,2).toInt(&ok, 10);
+			int day   = timestr.mid(6,2).toInt(&ok, 10);
+			int hour  = timestr.mid(8,2).toInt(&ok, 10) + diff;
+		
+			if (ok)
+			{
+				if (hour >= 24)
+				{
+					hour %= 24;
+					day += 1;
+				}
+				
+				int maxd;
+				switch(month)
+				{
+				case(1):case(3):case(5):case(7):case(8):case(10):case(12):
+					maxd = 31;
+					break;
+				case(2):
+					// here is code missing, that calculates the "leap year" correct
+					maxd = 28;
+					break;
+				case(4):case(6):case(9):case(11):
+					maxd = 30;
+					break;
+				default:
+					maxd=31;
+				}
+				
+				if (day > maxd)
+				{
+					day %= maxd;
+					month += 1;
+				}
+				if (month > 12)
+				{
+					month %= 12;
+					year++;
+				}
+				
+				QString tmp;
+				tmp.setNum(year);
+				timestr = timestr.replace(0, 4, tmp);
+				tmp.setNum(month);
+				if (tmp.length() == 1) tmp.prepend("0");
+				timestr = timestr.replace(4, 2, tmp);
+				tmp.setNum(day);
+				if (tmp.length() == 1) tmp.prepend("0");
+				timestr = timestr.replace(6, 2, tmp);
+				tmp.setNum(hour);
+				if (tmp.length() == 1) tmp.prepend("0");
+				timestr = timestr.replace(8, 2, tmp);
+			}
+		}
+	}
+}
+
 ProgInfo *parseProgram(QDomElement &element)
 {
+	QString config_offset = context->GetSetting("TIMEoffset");
+	
+	bool ok;
+	int config_off = config_offset.left(3).toInt(&ok,10);
+	
+	if (!ok)
+		config_off = 25;
+
     ProgInfo *pginfo = new ProgInfo;
  
     QString text = element.attribute("start", "");
     QStringList split = QStringList::split(" ", text);
+    
+	QString st = split[0];
+    QString offset;
+	if (split.size() > 1)
+		offset = split[1];
+	else 
+		offset = config_offset;
+    
+	
+    
+    if (config_off >= 0 && config_off <= 24 && offset != "")
+		addTimeOffset(st, config_off, offset);
+
+	pginfo->startts = st;
 
-    pginfo->startts = split[0];
 
     text = element.attribute("stop", "");
     split = QStringList::split(" ", text);
 
+	QString et;
     if (split.size() > 0)
-        pginfo->endts = split[0]; 
+	{
+        et = split[0]; 
+		if (split.size() > 1)	offset = split[1];
+		else 					offset = config_offset;
+	}
     else
-        pginfo->endts = "";
+        et = "";
+		
+    if (config_off >= 0 && config_off <= 24 && offset != "")
+		addTimeOffset(et, config_off, offset);
+    
+	pginfo->endts = et; 
+
 
     text = element.attribute("channel", "");
     split = QStringList::split(" ", text);

--Boundary-00=_IXP29OsectuYif1--