[california] Easier conversion of Calendar.ExactTime to Calendar.WallTime



commit d35e06cf9b54f3ea678d4405e7d0a4f61c4d9798
Author: Jim Nelson <jim yorba org>
Date:   Thu May 22 17:42:36 2014 -0700

    Easier conversion of Calendar.ExactTime to Calendar.WallTime
    
    Old way used a name constructor in WallTime.  Since these conversions
    are often chained with adjustments, a ExactTime.to_wall_time() is more
    natural.

 src/calendar/calendar-exact-time.vala       |    9 ++++++++-
 src/calendar/calendar-wall-time.vala        |   10 ----------
 src/component/component-details-parser.vala |   10 +++++-----
 src/host/host-create-update-event.vala      |   18 ++++++++----------
 src/tests/tests-quick-add.vala              |   20 +++++++++-----------
 src/view/week/week-day-pane.vala            |   10 +++++-----
 src/view/week/week-grid.vala                |    2 +-
 7 files changed, 36 insertions(+), 43 deletions(-)
---
diff --git a/src/calendar/calendar-exact-time.vala b/src/calendar/calendar-exact-time.vala
index 129a835..49ff335 100644
--- a/src/calendar/calendar-exact-time.vala
+++ b/src/calendar/calendar-exact-time.vala
@@ -176,6 +176,13 @@ public class ExactTime : BaseObject, Gee.Comparable<ExactTime>, Gee.Hashable<Exa
     }
     
     /**
+     * Returns the { link WallTime} for the { link ExactTime}.
+     */
+    public WallTime to_wall_time() {
+        return new WallTime(hour, minute, second);
+    }
+    
+    /**
      * Returns prettified, localized user-visible date string of an { link ExactTime}.
      */
     public string to_pretty_date_string(Date.PrettyFlag date_flags) {
@@ -186,7 +193,7 @@ public class ExactTime : BaseObject, Gee.Comparable<ExactTime>, Gee.Hashable<Exa
      * Returns prettified, localized user-visible time string of an { link ExactTime}.
      */
     public string to_pretty_time_string(WallTime.PrettyFlag time_flags) {
-        return new WallTime.from_exact_time(this).to_pretty_string(time_flags);
+        return to_wall_time().to_pretty_string(time_flags);
     }
      
     
diff --git a/src/calendar/calendar-wall-time.vala b/src/calendar/calendar-wall-time.vala
index efa8d34..7145474 100644
--- a/src/calendar/calendar-wall-time.vala
+++ b/src/calendar/calendar-wall-time.vala
@@ -110,16 +110,6 @@ public class WallTime : BaseObject, Gee.Comparable<WallTime>, Gee.Hashable<WallT
     }
     
     /**
-     * Generate a new { link WallTime} with the { link ExactTIme}'s values.
-     *
-     * Because date and timezone information is lost in this conversion, the caller should convert
-     * the ExactTime to the desired timezone before constructing the WallTime.
-     */
-    public WallTime.from_exact_time(ExactTime exact_time) {
-        this (exact_time.hour, exact_time.minute, exact_time.second);
-    }
-    
-    /**
      * Called from Calendar.init().
      */
     internal static void init() {
diff --git a/src/component/component-details-parser.vala b/src/component/component-details-parser.vala
index ff27110..5bd1f20 100644
--- a/src/component/component-details-parser.vala
+++ b/src/component/component-details-parser.vala
@@ -162,9 +162,9 @@ public class DetailsParser : BaseObject {
         // if no start time or date but a duration was specified, assume start is now and use
         // duration for end time
         if (start_time == null && start_date == null && duration != null) {
-            start_time = new Calendar.WallTime.from_exact_time(Calendar.System.now);
-            end_time = new Calendar.WallTime.from_exact_time(
-                Calendar.System.now.adjust_time((int) duration.minutes, Calendar.TimeUnit.MINUTE));
+            start_time = Calendar.System.now.to_wall_time();
+            end_time =
+                Calendar.System.now.adjust_time((int) duration.minutes, 
Calendar.TimeUnit.MINUTE).to_wall_time();
             duration = null;
         }
         
@@ -292,8 +292,8 @@ public class DetailsParser : BaseObject {
         if (delay == null)
             return false;
         
-        start_time = new Calendar.WallTime.from_exact_time(
-            Calendar.System.now.adjust_time((int) delay.minutes, Calendar.TimeUnit.MINUTE));
+        start_time =
+            Calendar.System.now.adjust_time((int) delay.minutes, Calendar.TimeUnit.MINUTE).to_wall_time();
         
         return true;
     }
diff --git a/src/host/host-create-update-event.vala b/src/host/host-create-update-event.vala
index 4c9c494..88b3631 100644
--- a/src/host/host-create-update-event.vala
+++ b/src/host/host-create-update-event.vala
@@ -119,22 +119,20 @@ public class CreateUpdateEvent : Gtk.Grid, Toolkit.Card {
         if (event.exact_time_span != null) {
             all_day_toggle.active = false;
             selected_date_span = event.exact_time_span.get_date_span();
-            initial_start_time = new Calendar.WallTime.from_exact_time(
-                event.exact_time_span.start_exact_time.to_timezone(Calendar.Timezone.local));
-            initial_end_time = new Calendar.WallTime.from_exact_time(
-                event.exact_time_span.end_exact_time.to_timezone(Calendar.Timezone.local));
+            initial_start_time =
+                event.exact_time_span.start_exact_time.to_timezone(Calendar.Timezone.local).to_wall_time();
+            initial_end_time =
+                event.exact_time_span.end_exact_time.to_timezone(Calendar.Timezone.local).to_wall_time();
         } else if (event.date_span != null) {
             all_day_toggle.active = true;
             selected_date_span = event.date_span;
-            initial_start_time = new Calendar.WallTime.from_exact_time(Calendar.System.now);
-            initial_end_time = new Calendar.WallTime.from_exact_time(
-                Calendar.System.now.adjust_time(1, Calendar.TimeUnit.HOUR));
+            initial_start_time = Calendar.System.now.to_wall_time();
+            initial_end_time = Calendar.System.now.adjust_time(1, Calendar.TimeUnit.HOUR).to_wall_time();
         } else {
             all_day_toggle.active = false;
             selected_date_span = new Calendar.DateSpan(Calendar.System.today, Calendar.System.today);
-            initial_start_time = new Calendar.WallTime.from_exact_time(Calendar.System.now);
-            initial_end_time = new Calendar.WallTime.from_exact_time(
-                Calendar.System.now.adjust_time(1, Calendar.TimeUnit.HOUR));
+            initial_start_time = Calendar.System.now.to_wall_time();
+            initial_end_time = Calendar.System.now.adjust_time(1, Calendar.TimeUnit.HOUR).to_wall_time();
         }
         
         // initialize start and end time controls (as in, wall clock time)
diff --git a/src/tests/tests-quick-add.vala b/src/tests/tests-quick-add.vala
index d4b3314..3d6bf8c 100644
--- a/src/tests/tests-quick-add.vala
+++ b/src/tests/tests-quick-add.vala
@@ -82,14 +82,12 @@ private class QuickAdd : UnitTest.Harness {
     private bool with_delay() throws Error {
         Component.DetailsParser parser = new Component.DetailsParser("meet Alice in 3 hours", null);
         
-        Calendar.WallTime start = new Calendar.WallTime.from_exact_time(Calendar.System.now).adjust(
-            3, Calendar.TimeUnit.HOUR, null);
+        Calendar.WallTime start = Calendar.System.now.to_wall_time().adjust(3, Calendar.TimeUnit.HOUR, null);
         Calendar.WallTime end = start.adjust(1, Calendar.TimeUnit.HOUR, null);
         
         assert(parser.event.summary == "meet Alice");
-        assert(new 
Calendar.WallTime.from_exact_time(parser.event.exact_time_span.start_exact_time).equal_to(start));
-        assert(new Calendar.WallTime.from_exact_time(
-                parser.event.exact_time_span.start_exact_time).adjust(1, Calendar.TimeUnit.HOUR, 
null).equal_to(end));
+        assert(parser.event.exact_time_span.start_exact_time.to_wall_time().equal_to(start));
+        assert(parser.event.exact_time_span.start_exact_time.to_wall_time().adjust(1, 
Calendar.TimeUnit.HOUR, null).equal_to(end));
         
         return true;
     }
@@ -97,23 +95,23 @@ private class QuickAdd : UnitTest.Harness {
     private bool with_duration() throws Error {
         Component.DetailsParser parser = new Component.DetailsParser("meet Alice for 2 hrs", null);
         
-        Calendar.WallTime start = new Calendar.WallTime.from_exact_time(Calendar.System.now);
+        Calendar.WallTime start = Calendar.System.now.to_wall_time();
         Calendar.WallTime end = start.adjust(2, Calendar.TimeUnit.HOUR, null);
         
         return parser.event.summary == "meet Alice"
-            && new 
Calendar.WallTime.from_exact_time(parser.event.exact_time_span.start_exact_time).equal_to(start)
-            && new 
Calendar.WallTime.from_exact_time(parser.event.exact_time_span.end_exact_time).equal_to(end);
+            && parser.event.exact_time_span.start_exact_time.to_wall_time().equal_to(start)
+            && parser.event.exact_time_span.end_exact_time.to_wall_time().equal_to(end);
     }
     
     private bool with_delay_and_duration() throws Error {
         Component.DetailsParser parser = new Component.DetailsParser("meet Alice in 3 hours for 30 min", 
null);
         
-        Calendar.WallTime start = new Calendar.WallTime.from_exact_time(Calendar.System.now.adjust_time(3, 
Calendar.TimeUnit.HOUR));
+        Calendar.WallTime start = Calendar.System.now.adjust_time(3, Calendar.TimeUnit.HOUR).to_wall_time();
         Calendar.WallTime end = start.adjust(30, Calendar.TimeUnit.MINUTE, null);
         
         return parser.event.summary == "meet Alice"
-            && new 
Calendar.WallTime.from_exact_time(parser.event.exact_time_span.start_exact_time).equal_to(start)
-            && new 
Calendar.WallTime.from_exact_time(parser.event.exact_time_span.end_exact_time).equal_to(end);
+            && parser.event.exact_time_span.start_exact_time.to_wall_time().equal_to(start)
+            && parser.event.exact_time_span.end_exact_time.to_wall_time().equal_to(end);
     }
     
     private bool indeterminate_time() throws Error {
diff --git a/src/view/week/week-day-pane.vala b/src/view/week/week-day-pane.vala
index 2365e7f..9c232b5 100644
--- a/src/view/week/week-day-pane.vala
+++ b/src/view/week/week-day-pane.vala
@@ -148,10 +148,10 @@ internal class DayPane : Pane {
             if (!date_span.is_same_day || !(date in date_span))
                 continue;
             
-            Calendar.WallTime start_time = new Calendar.WallTime.from_exact_time(
-                event.exact_time_span.start_exact_time.to_timezone(Calendar.Timezone.local));
-            Calendar.WallTime end_time = new Calendar.WallTime.from_exact_time(
-                event.exact_time_span.end_exact_time.to_timezone(Calendar.Timezone.local));
+            Calendar.WallTime start_time =
+                event.exact_time_span.start_exact_time.to_timezone(Calendar.Timezone.local).to_wall_time();
+            Calendar.WallTime end_time =
+                event.exact_time_span.end_exact_time.to_timezone(Calendar.Timezone.local).to_wall_time();
             
             int start_y = get_line_y(start_time);
             int end_y = get_line_y(end_time);
@@ -187,7 +187,7 @@ internal class DayPane : Pane {
         
         // draw horizontal line indicating current time
         if (date.equal_to(Calendar.System.today)) {
-            int time_of_day_y = get_line_y(new Calendar.WallTime.from_exact_time(Calendar.System.now));
+            int time_of_day_y = get_line_y(Calendar.System.now.to_wall_time());
             
             Palette.prepare_hairline(ctx, Palette.instance.current_time);
             ctx.move_to(0, time_of_day_y);
diff --git a/src/view/week/week-grid.vala b/src/view/week/week-grid.vala
index 8936e98..dff191a 100644
--- a/src/view/week/week-grid.vala
+++ b/src/view/week/week-grid.vala
@@ -174,7 +174,7 @@ internal class Grid : Gtk.Box {
         // scroll to 6am when first created, unless in the current date, in which case scroll to
         // current time
         Calendar.WallTime start_time = Calendar.System.today in week
-            ? new Calendar.WallTime.from_exact_time(Calendar.System.now)
+            ? Calendar.System.now.to_wall_time()
             : new Calendar.WallTime(6, 0, 0);
         
         // scroll there


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]