diff options
author | Matthew Barnes <mbarnes@redhat.com> | 2013-03-08 00:12:23 +0800 |
---|---|---|
committer | Matthew Barnes <mbarnes@redhat.com> | 2013-03-08 04:50:41 +0800 |
commit | 9fa7358f7400c07eec584291ce67274fc64e2fa2 (patch) | |
tree | c2406c70a0f205857408481daa9808d464181ed0 | |
parent | 2b8003742082d5bbd9b80ed20de8cd0a14d89359 (diff) | |
download | gsoc2013-evolution-9fa7358f7400c07eec584291ce67274fc64e2fa2.tar gsoc2013-evolution-9fa7358f7400c07eec584291ce67274fc64e2fa2.tar.gz gsoc2013-evolution-9fa7358f7400c07eec584291ce67274fc64e2fa2.tar.bz2 gsoc2013-evolution-9fa7358f7400c07eec584291ce67274fc64e2fa2.tar.lz gsoc2013-evolution-9fa7358f7400c07eec584291ce67274fc64e2fa2.tar.xz gsoc2013-evolution-9fa7358f7400c07eec584291ce67274fc64e2fa2.tar.zst gsoc2013-evolution-9fa7358f7400c07eec584291ce67274fc64e2fa2.zip |
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()
-rw-r--r-- | doc/reference/libeutil/libeutil-sections.txt | 3 | ||||
-rw-r--r-- | e-util/e-misc-utils.c | 65 | ||||
-rw-r--r-- | 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 3d6758a6be..4934a95c21 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 261612c87b..60ebc6bf79 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 282b1fda04..20a17b8add 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); |