gdate.h

Go to the documentation of this file.
00001 /* GLIB - Library of useful routines for C programming
00002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 /*
00021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00022  * file for a list of people on the GLib Team.  See the ChangeLog
00023  * files for a list of changes.  These files are distributed with
00024  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
00025  */
00026 
00027 #ifndef __G_DATE_H__
00028 #define __G_DATE_H__
00029 
00030 #include <gquark.h>
00031 
00032 G_BEGIN_DECLS
00033 
00034 /* GDate
00035  *
00036  * Date calculations (not time for now, to be resolved). These are a
00037  * mutant combination of Steffen Beyer's DateCalc routines
00038  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
00039  * date routines (written for in-house software).  Written by Havoc
00040  * Pennington <hp@pobox.com>
00041  */
00042 
00043 typedef gint32  GTime;
00044 typedef guint16 GDateYear;
00045 typedef guint8  GDateDay;   /* day of the month */
00046 typedef struct _GDate GDate;
00047 /* make struct tm known without having to include time.h */
00048 struct tm;
00049 
00050 /* enum used to specify order of appearance in parsed date strings */
00051 typedef enum
00052 {
00053   G_DATE_DAY   = 0,
00054   G_DATE_MONTH = 1,
00055   G_DATE_YEAR  = 2
00056 } GDateDMY;
00057 
00058 /* actual week and month values */
00059 typedef enum
00060 {
00061   G_DATE_BAD_WEEKDAY  = 0,
00062   G_DATE_MONDAY       = 1,
00063   G_DATE_TUESDAY      = 2,
00064   G_DATE_WEDNESDAY    = 3,
00065   G_DATE_THURSDAY     = 4,
00066   G_DATE_FRIDAY       = 5,
00067   G_DATE_SATURDAY     = 6,
00068   G_DATE_SUNDAY       = 7
00069 } GDateWeekday;
00070 typedef enum
00071 {
00072   G_DATE_BAD_MONTH = 0,
00073   G_DATE_JANUARY   = 1,
00074   G_DATE_FEBRUARY  = 2,
00075   G_DATE_MARCH     = 3,
00076   G_DATE_APRIL     = 4,
00077   G_DATE_MAY       = 5,
00078   G_DATE_JUNE      = 6,
00079   G_DATE_JULY      = 7,
00080   G_DATE_AUGUST    = 8,
00081   G_DATE_SEPTEMBER = 9,
00082   G_DATE_OCTOBER   = 10,
00083   G_DATE_NOVEMBER  = 11,
00084   G_DATE_DECEMBER  = 12
00085 } GDateMonth;
00086 
00087 #define G_DATE_BAD_JULIAN 0U
00088 #define G_DATE_BAD_DAY    0U
00089 #define G_DATE_BAD_YEAR   0U
00090 
00091 /* Note: directly manipulating structs is generally a bad idea, but
00092  * in this case it's an *incredibly* bad idea, because all or part
00093  * of this struct can be invalid at any given time. Use the functions,
00094  * or you will get hosed, I promise.
00095  */
00096 struct _GDate
00097 {
00098   guint julian_days : 32; /* julian days representation - we use a
00099                            *  bitfield hoping that 64 bit platforms
00100                            *  will pack this whole struct in one big
00101                            *  int
00102                            */
00103 
00104   guint julian : 1;    /* julian is valid */
00105   guint dmy    : 1;    /* dmy is valid */
00106 
00107   /* DMY representation */
00108   guint day    : 6;
00109   guint month  : 4;
00110   guint year   : 16;
00111 };
00112 
00113 /* g_date_new() returns an invalid date, you then have to _set() stuff
00114  * to get a usable object. You can also allocate a GDate statically,
00115  * then call g_date_clear() to initialize.
00116  */
00117 GDate*       g_date_new                   (void);
00118 GDate*       g_date_new_dmy               (GDateDay     day,
00119                                            GDateMonth   month,
00120                                            GDateYear    year);
00121 GDate*       g_date_new_julian            (guint32      julian_day);
00122 void         g_date_free                  (GDate       *date);
00123 
00124 /* check g_date_valid() after doing an operation that might fail, like
00125  * _parse.  Almost all g_date operations are undefined on invalid
00126  * dates (the exceptions are the mutators, since you need those to
00127  * return to validity).
00128  */
00129 gboolean     g_date_valid                 (GDate       *date);
00130 gboolean     g_date_valid_day             (GDateDay     day) G_GNUC_CONST;
00131 gboolean     g_date_valid_month           (GDateMonth month) G_GNUC_CONST;
00132 gboolean     g_date_valid_year            (GDateYear  year) G_GNUC_CONST;
00133 gboolean     g_date_valid_weekday         (GDateWeekday weekday) G_GNUC_CONST;
00134 gboolean     g_date_valid_julian          (guint32 julian_date) G_GNUC_CONST;
00135 gboolean     g_date_valid_dmy             (GDateDay     day,
00136                                            GDateMonth   month,
00137                                            GDateYear    year) G_GNUC_CONST;
00138 
00139 GDateWeekday g_date_get_weekday           (GDate       *date);
00140 GDateMonth   g_date_get_month             (GDate       *date);
00141 GDateYear    g_date_get_year              (GDate       *date);
00142 GDateDay     g_date_get_day               (GDate       *date);
00143 guint32      g_date_get_julian            (GDate       *date);
00144 guint        g_date_get_day_of_year       (GDate       *date);
00145 /* First monday/sunday is the start of week 1; if we haven't reached
00146  * that day, return 0. These are not ISO weeks of the year; that
00147  * routine needs to be added.
00148  * these functions return the number of weeks, starting on the
00149  * corrsponding day
00150  */
00151 guint        g_date_get_monday_week_of_year (GDate      *date);
00152 guint        g_date_get_sunday_week_of_year (GDate      *date);
00153 
00154 /* If you create a static date struct you need to clear it to get it
00155  * in a sane state before use. You can clear a whole array at
00156  * once with the ndates argument.
00157  */
00158 void         g_date_clear                 (GDate       *date,
00159                                            guint        n_dates);
00160 
00161 /* The parse routine is meant for dates typed in by a user, so it
00162  * permits many formats but tries to catch common typos. If your data
00163  * needs to be strictly validated, it is not an appropriate function.
00164  */
00165 void         g_date_set_parse             (GDate       *date,
00166                                            const gchar *str);
00167 void         g_date_set_time              (GDate       *date,
00168                                            GTime        time);
00169 void         g_date_set_month             (GDate       *date,
00170                                            GDateMonth   month);
00171 void         g_date_set_day               (GDate       *date,
00172                                            GDateDay     day);
00173 void         g_date_set_year              (GDate       *date,
00174                                            GDateYear    year);
00175 void         g_date_set_dmy               (GDate       *date,
00176                                            GDateDay     day,
00177                                            GDateMonth   month,
00178                                            GDateYear    y);
00179 void         g_date_set_julian            (GDate       *date,
00180                                            guint32      julian_date);
00181 gboolean     g_date_is_first_of_month     (GDate       *date);
00182 gboolean     g_date_is_last_of_month      (GDate       *date);
00183 
00184 /* To go forward by some number of weeks just go forward weeks*7 days */
00185 void         g_date_add_days              (GDate       *date,
00186                                            guint        n_days);
00187 void         g_date_subtract_days         (GDate       *date,
00188                                            guint        n_days);
00189 
00190 /* If you add/sub months while day > 28, the day might change */
00191 void         g_date_add_months            (GDate       *date,
00192                                            guint        n_months);
00193 void         g_date_subtract_months       (GDate       *date,
00194                                            guint        n_months);
00195 
00196 /* If it's feb 29, changing years can move you to the 28th */
00197 void         g_date_add_years             (GDate       *date,
00198                                            guint        n_years);
00199 void         g_date_subtract_years        (GDate       *date,
00200                                            guint        n_years);
00201 gboolean     g_date_is_leap_year          (GDateYear    year) G_GNUC_CONST;
00202 guint8       g_date_get_days_in_month     (GDateMonth   month,
00203                                            GDateYear    year) G_GNUC_CONST;
00204 guint8       g_date_get_monday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
00205 guint8       g_date_get_sunday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
00206 
00207 /* qsort-friendly (with a cast...) */
00208 gint         g_date_compare               (GDate       *lhs,
00209                                            GDate       *rhs);
00210 void         g_date_to_struct_tm          (GDate       *date,
00211                                            struct tm   *tm);
00212 
00213 /* Just like strftime() except you can only use date-related formats.
00214  *   Using a time format is undefined.
00215  */
00216 gsize        g_date_strftime              (gchar       *s,
00217                                            gsize        slen,
00218                                            const gchar *format,
00219                                            GDate       *date);
00220 
00221 G_END_DECLS
00222 
00223 #endif /* __G_DATE_H__ */
00224 

Generated on Tue Jul 5 14:14:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1