williamr@2: /* GLIB - Library of useful routines for C programming williamr@2: * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald williamr@2: * Portions copyright (c) 2006 Nokia Corporation. All rights reserved. williamr@2: * williamr@2: * This library is free software; you can redistribute it and/or williamr@2: * modify it under the terms of the GNU Lesser General Public williamr@2: * License as published by the Free Software Foundation; either williamr@2: * version 2 of the License, or (at your option) any later version. williamr@2: * williamr@2: * This library is distributed in the hope that it will be useful, williamr@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of williamr@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU williamr@2: * Lesser General Public License for more details. williamr@2: * williamr@2: * You should have received a copy of the GNU Lesser General Public williamr@2: * License along with this library; if not, write to the williamr@2: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, williamr@2: * Boston, MA 02111-1307, USA. williamr@2: */ williamr@2: williamr@2: /* williamr@2: * Modified by the GLib Team and others 1997-2000. See the AUTHORS williamr@2: * file for a list of people on the GLib Team. See the ChangeLog williamr@2: * files for a list of changes. These files are distributed with williamr@2: * GLib at ftp://ftp.gtk.org/pub/gtk/. williamr@2: */ williamr@2: williamr@2: #ifndef __G_DATE_H__ williamr@2: #define __G_DATE_H__ williamr@2: williamr@2: #include <_ansi.h> williamr@2: #include williamr@2: williamr@2: #include williamr@2: #include williamr@2: williamr@2: G_BEGIN_DECLS williamr@2: williamr@2: /* GDate williamr@2: * williamr@2: * Date calculations (not time for now, to be resolved). These are a williamr@2: * mutant combination of Steffen Beyer's DateCalc routines williamr@2: * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's williamr@2: * date routines (written for in-house software). Written by Havoc williamr@2: * Pennington williamr@2: */ williamr@2: williamr@2: typedef gint32 GTime; williamr@2: typedef guint16 GDateYear; williamr@2: typedef guint8 GDateDay; /* day of the month */ williamr@2: typedef struct _GDate GDate; williamr@2: /* make struct tm known without having to include time.h */ williamr@2: struct tm; williamr@2: williamr@2: /* enum used to specify order of appearance in parsed date strings */ williamr@2: typedef enum williamr@2: { williamr@2: G_DATE_DAY = 0, williamr@2: G_DATE_MONTH = 1, williamr@2: G_DATE_YEAR = 2 williamr@2: } GDateDMY; williamr@2: williamr@2: /* actual week and month values */ williamr@2: typedef enum williamr@2: { williamr@2: G_DATE_BAD_WEEKDAY = 0, williamr@2: G_DATE_MONDAY = 1, williamr@2: G_DATE_TUESDAY = 2, williamr@2: G_DATE_WEDNESDAY = 3, williamr@2: G_DATE_THURSDAY = 4, williamr@2: G_DATE_FRIDAY = 5, williamr@2: G_DATE_SATURDAY = 6, williamr@2: G_DATE_SUNDAY = 7 williamr@2: } GDateWeekday; williamr@2: typedef enum williamr@2: { williamr@2: G_DATE_BAD_MONTH = 0, williamr@2: G_DATE_JANUARY = 1, williamr@2: G_DATE_FEBRUARY = 2, williamr@2: G_DATE_MARCH = 3, williamr@2: G_DATE_APRIL = 4, williamr@2: G_DATE_MAY = 5, williamr@2: G_DATE_JUNE = 6, williamr@2: G_DATE_JULY = 7, williamr@2: G_DATE_AUGUST = 8, williamr@2: G_DATE_SEPTEMBER = 9, williamr@2: G_DATE_OCTOBER = 10, williamr@2: G_DATE_NOVEMBER = 11, williamr@2: G_DATE_DECEMBER = 12 williamr@2: } GDateMonth; williamr@2: williamr@2: #define G_DATE_BAD_JULIAN 0U williamr@2: #define G_DATE_BAD_DAY 0U williamr@2: #define G_DATE_BAD_YEAR 0U williamr@2: williamr@2: /* Note: directly manipulating structs is generally a bad idea, but williamr@2: * in this case it's an *incredibly* bad idea, because all or part williamr@2: * of this struct can be invalid at any given time. Use the functions, williamr@2: * or you will get hosed, I promise. williamr@2: */ williamr@2: struct _GDate williamr@2: { williamr@2: guint julian_days : 32; /* julian days representation - we use a williamr@2: * bitfield hoping that 64 bit platforms williamr@2: * will pack this whole struct in one big williamr@2: * int williamr@2: */ williamr@2: williamr@2: guint julian : 1; /* julian is valid */ williamr@2: guint dmy : 1; /* dmy is valid */ williamr@2: williamr@2: /* DMY representation */ williamr@2: guint day : 6; williamr@2: guint month : 4; williamr@2: guint year : 16; williamr@2: }; williamr@2: williamr@2: /* g_date_new() returns an invalid date, you then have to _set() stuff williamr@2: * to get a usable object. You can also allocate a GDate statically, williamr@2: * then call g_date_clear() to initialize. williamr@2: */ williamr@2: IMPORT_C GDate* g_date_new (void); williamr@2: IMPORT_C GDate* g_date_new_dmy (GDateDay day, williamr@2: GDateMonth month, williamr@2: GDateYear year); williamr@2: IMPORT_C GDate* g_date_new_julian (guint32 julian_day); williamr@2: IMPORT_C void g_date_free (GDate *date); williamr@2: williamr@2: /* check g_date_valid() after doing an operation that might fail, like williamr@2: * _parse. Almost all g_date operations are undefined on invalid williamr@2: * dates (the exceptions are the mutators, since you need those to williamr@2: * return to validity). williamr@2: */ williamr@2: IMPORT_C gboolean g_date_valid (const GDate *date); williamr@2: IMPORT_C gboolean g_date_valid_day (GDateDay day) G_GNUC_CONST; williamr@2: IMPORT_C gboolean g_date_valid_month (GDateMonth month) G_GNUC_CONST; williamr@2: IMPORT_C gboolean g_date_valid_year (GDateYear year) G_GNUC_CONST; williamr@2: IMPORT_C gboolean g_date_valid_weekday (GDateWeekday weekday) G_GNUC_CONST; williamr@2: IMPORT_C gboolean g_date_valid_julian (guint32 julian_date) G_GNUC_CONST; williamr@2: IMPORT_C gboolean g_date_valid_dmy (GDateDay day, williamr@2: GDateMonth month, williamr@2: GDateYear year) G_GNUC_CONST; williamr@2: williamr@2: IMPORT_C GDateWeekday g_date_get_weekday (const GDate *date); williamr@2: IMPORT_C GDateMonth g_date_get_month (const GDate *date); williamr@2: IMPORT_C GDateYear g_date_get_year (const GDate *date); williamr@2: IMPORT_C GDateDay g_date_get_day (const GDate *date); williamr@2: IMPORT_C guint32 g_date_get_julian (const GDate *date); williamr@2: IMPORT_C guint g_date_get_day_of_year (const GDate *date); williamr@2: /* First monday/sunday is the start of week 1; if we haven't reached williamr@2: * that day, return 0. These are not ISO weeks of the year; that williamr@2: * routine needs to be added. williamr@2: * these functions return the number of weeks, starting on the williamr@2: * corrsponding day williamr@2: */ williamr@2: IMPORT_C guint g_date_get_monday_week_of_year (const GDate *date); williamr@2: IMPORT_C guint g_date_get_sunday_week_of_year (const GDate *date); williamr@2: IMPORT_C guint g_date_get_iso8601_week_of_year (const GDate *date); williamr@2: williamr@2: /* If you create a static date struct you need to clear it to get it williamr@2: * in a sane state before use. You can clear a whole array at williamr@2: * once with the ndates argument. williamr@2: */ williamr@2: IMPORT_C void g_date_clear (GDate *date, williamr@2: guint n_dates); williamr@2: williamr@2: /* The parse routine is meant for dates typed in by a user, so it williamr@2: * permits many formats but tries to catch common typos. If your data williamr@2: * needs to be strictly validated, it is not an appropriate function. williamr@2: */ williamr@2: IMPORT_C void g_date_set_parse (GDate *date, williamr@2: const gchar *str); williamr@2: IMPORT_C void g_date_set_time (GDate *date, williamr@2: time_t timet); williamr@2: IMPORT_C void g_date_set_time_val (GDate *date, williamr@2: GTimeVal *timeval); williamr@2: #ifndef G_DISABLE_DEPRECATED williamr@2: IMPORT_C void g_date_set_time (GDate *date, williamr@2: GTime time_); williamr@2: #endif williamr@2: IMPORT_C void g_date_set_month (GDate *date, williamr@2: GDateMonth month); williamr@2: IMPORT_C void g_date_set_day (GDate *date, williamr@2: GDateDay day); williamr@2: IMPORT_C void g_date_set_year (GDate *date, williamr@2: GDateYear year); williamr@2: IMPORT_C void g_date_set_dmy (GDate *date, williamr@2: GDateDay day, williamr@2: GDateMonth month, williamr@2: GDateYear y); williamr@2: IMPORT_C void g_date_set_julian (GDate *date, williamr@2: guint32 julian_date); williamr@2: IMPORT_C gboolean g_date_is_first_of_month (const GDate *date); williamr@2: IMPORT_C gboolean g_date_is_last_of_month (const GDate *date); williamr@2: williamr@2: /* To go forward by some number of weeks just go forward weeks*7 days */ williamr@2: IMPORT_C void g_date_add_days (GDate *date, williamr@2: guint n_days); williamr@2: IMPORT_C void g_date_subtract_days (GDate *date, williamr@2: guint n_days); williamr@2: williamr@2: /* If you add/sub months while day > 28, the day might change */ williamr@2: IMPORT_C void g_date_add_months (GDate *date, williamr@2: guint n_months); williamr@2: IMPORT_C void g_date_subtract_months (GDate *date, williamr@2: guint n_months); williamr@2: williamr@2: /* If it's feb 29, changing years can move you to the 28th */ williamr@2: IMPORT_C void g_date_add_years (GDate *date, williamr@2: guint n_years); williamr@2: IMPORT_C void g_date_subtract_years (GDate *date, williamr@2: guint n_years); williamr@2: IMPORT_C gboolean g_date_is_leap_year (GDateYear year) G_GNUC_CONST; williamr@2: IMPORT_C guint8 g_date_get_days_in_month (GDateMonth month, williamr@2: GDateYear year) G_GNUC_CONST; williamr@2: IMPORT_C guint8 g_date_get_monday_weeks_in_year (GDateYear year) G_GNUC_CONST; williamr@2: IMPORT_C guint8 g_date_get_sunday_weeks_in_year (GDateYear year) G_GNUC_CONST; williamr@2: williamr@2: /* Returns the number of days between the two dates. If date2 comes williamr@2: before date1, a negative value is return. */ williamr@2: IMPORT_C gint g_date_days_between (const GDate *date1, williamr@2: const GDate *date2); williamr@2: williamr@2: /* qsort-friendly (with a cast...) */ williamr@2: IMPORT_C gint g_date_compare (const GDate *lhs, williamr@2: const GDate *rhs); williamr@2: IMPORT_C void g_date_to_struct_tm (const GDate *date, williamr@2: struct tm *tm); williamr@2: williamr@2: IMPORT_C void g_date_clamp (GDate *date, williamr@2: const GDate *min_date, williamr@2: const GDate *max_date); williamr@2: williamr@2: /* Swap date1 and date2's values if date1 > date2. */ williamr@2: IMPORT_C void g_date_order (GDate *date1, GDate *date2); williamr@2: williamr@2: /* Just like strftime() except you can only use date-related formats. williamr@2: * Using a time format is undefined. williamr@2: */ williamr@2: IMPORT_C gsize g_date_strftime (gchar *s, williamr@2: gsize slen, williamr@2: const gchar *format, williamr@2: const GDate *date); williamr@2: williamr@2: #ifndef G_DISABLE_DEPRECATED williamr@2: williamr@2: #define g_date_weekday g_date_get_weekday williamr@2: #define g_date_month g_date_get_month williamr@2: #define g_date_year g_date_get_year williamr@2: #define g_date_day g_date_get_day williamr@2: #define g_date_julian g_date_get_julian williamr@2: #define g_date_day_of_year g_date_get_day_of_year williamr@2: #define g_date_monday_week_of_year g_date_get_monday_week_of_year williamr@2: #define g_date_sunday_week_of_year g_date_get_sunday_week_of_year williamr@2: #define g_date_days_in_month g_date_get_days_in_month williamr@2: #define g_date_monday_weeks_in_year g_date_get_monday_weeks_in_year williamr@2: #define g_date_sunday_weeks_in_year g_date_get_sunday_weeks_in_year williamr@2: williamr@2: #endif /* G_DISABLE_DEPRECATED */ williamr@2: williamr@2: G_END_DECLS williamr@2: williamr@2: #endif /* __G_DATE_H__ */ williamr@2: