days until practice night
authorThomas Thurman <tthurman@gnome.org>
Mon, 31 Aug 2009 23:52:46 +0000 (19:52 -0400)
committerThomas Thurman <tthurman@gnome.org>
Mon, 31 Aug 2009 23:52:46 +0000 (19:52 -0400)
belltower.c

index 89e9574..dd1bcb0 100644 (file)
@@ -281,6 +281,43 @@ distance_to_tower_str (tower *details)
     }
 }
 
+/**
+ * Returns the number of days from today until
+ * the tower's practice night.  If the tower's
+ * practice night is unknown, returns 9: this
+ * means that such towers always sort to the end.
+ */
+static gint
+days_until_practice_night (tower *details)
+{
+  /* let's not use the date parsing routines, because
+   * we might get confused by locales, and the day names
+   * used in Dove are constant
+   */
+  time_t now = time (NULL);
+  struct tm *calendar = localtime (&now);
+  const char* dove_days = "SunMonTueWedThuFriSat";
+  char *found;
+  gint practice_night;
+
+  if (strcmp (details->fields[FieldPracticeNight], "")==0)
+    {
+      /* we don't know */
+      return 9;
+    }
+
+  found = strstr (dove_days, details->fields[FieldPracticeNight]);
+
+  if (!found)
+    {
+      return 9;
+    }
+
+  practice_night = (found-dove_days)/3;
+
+  return ((practice_night+7)-calendar->tm_wday)%7;
+}
+
 static void
 call_dbus (DBusBusType type,
           char *name,
@@ -798,7 +835,9 @@ found_tower_new (tower *basis)
                                         basis->fields[FieldPlace]);
       break;
     case TOWERSORT_PRACTICE:
-      result->sortkey = g_strdup ("FIXME");
+      result->sortkey = g_strdup_printf ("%d %s",
+                                        days_until_practice_night (basis),
+                                        basis->fields[FieldPlace]);
       break;
     case TOWERSORT_BELLS:
       result->sortkey = g_strdup_printf ("%10s", basis->fields[FieldBells]);