Add a function to calculate the ISO 8601 week number of a date. (#92579,
authorMatthias Clasen <maclas@gmx.de>
Thu, 2 Sep 2004 00:27:27 +0000 (00:27 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Thu, 2 Sep 2004 00:27:27 +0000 (00:27 +0000)
Wed Sep  1 20:22:39 2004  Matthias Clasen  <maclas@gmx.de>

* glib/gdate.h:
* glib/gdate.c (g_date_get_iso8601_week_of_year): Add
a function to calculate the ISO 8601 week number of
a date.  (#92579, Niklas Lundell)

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/gdate.c
glib/gdate.h

index 51c46b1..9f97711 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Wed Sep  1 20:22:39 2004  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gdate.h:
+       * glib/gdate.c (g_date_get_iso8601_week_of_year): Add
+       a function to calculate the ISO 8601 week number of 
+       a date.  (#92579, Niklas Lundell)
+
 2004-09-01  Anders Carlsson  <andersca@gnome.org>
 
        * glib/goption.c: (g_option_context_parse):
index 51c46b1..9f97711 100644 (file)
@@ -1,3 +1,10 @@
+Wed Sep  1 20:22:39 2004  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gdate.h:
+       * glib/gdate.c (g_date_get_iso8601_week_of_year): Add
+       a function to calculate the ISO 8601 week number of 
+       a date.  (#92579, Niklas Lundell)
+
 2004-09-01  Anders Carlsson  <andersca@gnome.org>
 
        * glib/goption.c: (g_option_context_parse):
index 51c46b1..9f97711 100644 (file)
@@ -1,3 +1,10 @@
+Wed Sep  1 20:22:39 2004  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gdate.h:
+       * glib/gdate.c (g_date_get_iso8601_week_of_year): Add
+       a function to calculate the ISO 8601 week number of 
+       a date.  (#92579, Niklas Lundell)
+
 2004-09-01  Anders Carlsson  <andersca@gnome.org>
 
        * glib/goption.c: (g_option_context_parse):
index 51c46b1..9f97711 100644 (file)
@@ -1,3 +1,10 @@
+Wed Sep  1 20:22:39 2004  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gdate.h:
+       * glib/gdate.c (g_date_get_iso8601_week_of_year): Add
+       a function to calculate the ISO 8601 week number of 
+       a date.  (#92579, Niklas Lundell)
+
 2004-09-01  Anders Carlsson  <andersca@gnome.org>
 
        * glib/goption.c: (g_option_context_parse):
index 51c46b1..9f97711 100644 (file)
@@ -1,3 +1,10 @@
+Wed Sep  1 20:22:39 2004  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gdate.h:
+       * glib/gdate.c (g_date_get_iso8601_week_of_year): Add
+       a function to calculate the ISO 8601 week number of 
+       a date.  (#92579, Niklas Lundell)
+
 2004-09-01  Anders Carlsson  <andersca@gnome.org>
 
        * glib/goption.c: (g_option_context_parse):
index 62f84d5..8e12a8d 100644 (file)
@@ -394,6 +394,42 @@ g_date_get_sunday_week_of_year (const GDate *d)
   return ((day + wd)/7U + (wd == 0 ? 1 : 0));
 }
 
+/**
+ * g_date_get_iso8601_week_of_year:
+ * @date: a valid #GDate
+ *
+ * Returns the week of the year, where weeks are interpreted according
+ * to ISO 8601. 
+ * 
+ * Returns: ISO 8601 week number of the year.
+ *
+ * Since: 2.6
+ **/
+guint
+g_date_get_iso8601_week_of_year (const GDate *d)
+{
+  guint j, d4, L, d1, w;
+
+  g_return_val_if_fail (d != NULL, 0);
+  g_return_val_if_fail (g_date_valid (d), 0);
+  
+  if (!d->julian)
+    g_date_update_julian (d);
+  g_return_val_if_fail (d->julian, 0);
+
+  /* Formula taken from the Calendar FAQ; the formula was for the
+   * Julian Period which starts on 1 January 4713 BC, so we add
+   * 1,721,425 to the number of days before doing the formula. 
+   */
+  j  = d->julian + 1721425;
+  d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
+  L  = d4 / 1460;
+  d1 = ((d4 - L) % 365) + L;
+  w  = d1 / 7 + 1;
+
+  return w;
+}
+
 gint
 g_date_days_between (const GDate *d1,
                     const GDate *d2)
index 2b5eb32..eafb16a 100644 (file)
@@ -150,6 +150,7 @@ guint        g_date_get_day_of_year       (const GDate *date);
  */
 guint        g_date_get_monday_week_of_year (const GDate *date);
 guint        g_date_get_sunday_week_of_year (const GDate *date);
+guint        g_date_get_iso8601_week_of_year (const GDate *date);
 
 /* If you create a static date struct you need to clear it to get it
  * in a sane state before use. You can clear a whole array at