Turn lift date and time strings into struct Time
[beifahrer] / src / adac-mitfahrclub.vala
index 10b2221..516c9af 100644 (file)
@@ -49,8 +49,7 @@ public enum LiftFlags {
 public class Lift : Object {
        public string city_from;
        public string city_to;
-       public string date;
-       public string time;
+       public Time time;
        public int places;
        public string price;
        public LiftFlags flags;
@@ -65,6 +64,10 @@ public class Lift : Object {
        public string email;
        public string description;
        public string modified;
+
+       public Lift () {
+               time.hour = -1;
+       }
 }
 
 public class CallbackMessage : Soup.Message {
@@ -329,10 +332,10 @@ public class AdacMitfahrclub {
                                                                        lift.city_to = n3->content;
                                                                        break;
                                                                case 2:
-                                                                       lift.date = n3->content;
+                                                                       parse_date (n3->content, out lift.time);
                                                                        break;
                                                                case 3:
-                                                                       lift.time = n3->content;
+                                                                       parse_time (n3->content.strip (), out lift.time);
                                                                        break;
                                                                case 4:
                                                                        lift.places = n3->content.to_int ();
@@ -439,9 +442,7 @@ public class AdacMitfahrclub {
 
                                var text1 = n2->children->content.strip ();
 
-                               if (text == "Datum")
-                                       lift.date = text1;
-                               else if (text == "Freie Pl\xc3\xa4tze")
+                               if (text == "Freie Pl\xc3\xa4tze")
                                        lift.places = text1.to_int ();
                                else if (text == "Name")
                                        lift.name = text1;
@@ -490,9 +491,9 @@ public class AdacMitfahrclub {
                                else if (text1 == "nach")
                                        lift.city_to = text2;
                                else if (text1 == "Datum")
-                                       lift.date = text2;
+                                       parse_date (text2, out lift.time);
                                else if (text1 == "Uhrzeit")
-                                       lift.time = text2;
+                                       parse_time (text2, out lift.time);
                                else if (text1 == "Raucher")
                                        print ("Raucher: %s\n", text2);
                                else if (text1 == "Fahrpreis")
@@ -540,4 +541,18 @@ public class AdacMitfahrclub {
        Xml.Node* search_tag_by_class (Xml.Node* node, string tag, string @class) requires (node != null) {
                return search_tag_by_property (node, tag, "class", @class);
        }
+
+       void parse_date (string date, out Time time) {
+               int year;
+               if (date.length == 11)
+                       date = date.offset (3);
+               if (date.length != 8)
+                       return;
+               var res = date.scanf ("%02d.%02d.%02d", out time.day, out time.month, out year);
+               time.year = year + 2000;
+       }
+
+       void parse_time (string time, out Time result) {
+               var res = time.scanf ("%d.%02d Uhr", out result.hour, out result.minute);
+       }
 }