[evolution] Add more weekday arithmetic functions.



commit 9fa7358f7400c07eec584291ce67274fc64e2fa2
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Mar 7 11:12:23 2013 -0500

    Add more weekday arithmetic functions.
    
    These aren't as efficient as possible, but are as clear as possible.
    
    New functions:
    
        e_weekday_add_days()
        e_weekday_subtract_days()
        e_weekday_get_days_between()

 doc/reference/libeutil/libeutil-sections.txt |    3 +
 e-util/e-misc-utils.c                        |   65 ++++++++++++++++++++++++++
 e-util/e-misc-utils.h                        |    6 ++
 3 files changed, 74 insertions(+), 0 deletions(-)
---
diff --git a/doc/reference/libeutil/libeutil-sections.txt b/doc/reference/libeutil/libeutil-sections.txt
index 3d6758a..4934a95 100644
--- a/doc/reference/libeutil/libeutil-sections.txt
+++ b/doc/reference/libeutil/libeutil-sections.txt
@@ -2249,6 +2249,9 @@ e_get_month_name
 e_get_weekday_name
 e_weekday_get_next
 e_weekday_get_prev
+e_weekday_add_days
+e_weekday_subtract_days
+e_weekday_get_days_between
 e_flexible_strtod
 E_ASCII_DTOSTR_BUF_SIZE
 e_ascii_dtostr
diff --git a/e-util/e-misc-utils.c b/e-util/e-misc-utils.c
index 261612c..60ebc6b 100644
--- a/e-util/e-misc-utils.c
+++ b/e-util/e-misc-utils.c
@@ -1583,6 +1583,71 @@ e_weekday_get_prev (GDateWeekday weekday)
        return prev;
 }
 
+/**
+ * e_weekday_add_days:
+ * @weekday: a #GDateWeekday
+ * @n_days: number of days to add
+ *
+ * Increments @weekday by @n_days.
+ *
+ * Returns: a #GDateWeekday
+ **/
+GDateWeekday
+e_weekday_add_days (GDateWeekday weekday,
+                    guint n_days)
+{
+       n_days %= 7;  /* Weekdays repeat every 7 days. */
+
+       while (n_days-- > 0)
+               weekday = e_weekday_get_next (weekday);
+
+       return weekday;
+}
+
+/**
+ * e_weekday_subtract_days:
+ * @weekday: a #GDateWeekday
+ * @n_days: number of days to subtract
+ *
+ * Decrements @weekday by @n_days.
+ *
+ * Returns: a #GDateWeekday
+ **/
+GDateWeekday
+e_weekday_subtract_days (GDateWeekday weekday,
+                         guint n_days)
+{
+       n_days %= 7;  /* Weekdays repeat every 7 days. */
+
+       while (n_days-- > 0)
+               weekday = e_weekday_get_prev (weekday);
+
+       return weekday;
+}
+
+/**
+ * e_weekday_get_days_between:
+ * @weekday1: a #GDateWeekday
+ * @weekday2: a #GDateWeekday
+ *
+ * Counts the number of days starting at @weekday1 and ending at @weekday2.
+ *
+ * Returns: the number of days
+ **/
+guint
+e_weekday_get_days_between (GDateWeekday weekday1,
+                            GDateWeekday weekday2)
+{
+       guint n_days = 0;
+
+       while (weekday1 != weekday2) {
+               n_days++;
+               weekday1 = e_weekday_get_next (weekday1);
+       }
+
+       return n_days;
+}
+
 /* Evolution Locks for crash recovery */
 static const gchar *
 get_lock_filename (void)
diff --git a/e-util/e-misc-utils.h b/e-util/e-misc-utils.h
index 282b1fd..20a17b8 100644
--- a/e-util/e-misc-utils.h
+++ b/e-util/e-misc-utils.h
@@ -141,6 +141,12 @@ const gchar *      e_get_weekday_name              (GDateWeekday weekday,
                                                 gboolean abbreviated);
 GDateWeekday   e_weekday_get_next              (GDateWeekday weekday);
 GDateWeekday   e_weekday_get_prev              (GDateWeekday weekday);
+GDateWeekday   e_weekday_add_days              (GDateWeekday weekday,
+                                                guint n_days);
+GDateWeekday   e_weekday_subtract_days         (GDateWeekday weekday,
+                                                guint n_days);
+guint          e_weekday_get_days_between      (GDateWeekday weekday1,
+                                                GDateWeekday weekday2);
 
 gboolean       e_file_lock_create              (void);
 void           e_file_lock_destroy             (void);


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