QOF  0.8.7
Files | Data Structures | Macros | Typedefs | Functions

Files

file  qofdate.h
 64bit Date handling routines
 

Data Structures

struct  QofDate_s
 Full range replacement for struct tm. More...
 

Macros

#define MAX_DATE_LENGTH   41
 The maximum length of a string used for or created by dates. More...
 
#define MAX_DATE_BUFFER   256
 The maximum length of a QofDate buffer. More...
 
#define SECS_PER_DAY   86400
 
#define SECS_PER_HOUR   3600
 
#define QOF_MOD_DATE   "qof-dates"
 
#define qof_date_isleap(year)   ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
 
#define QOF_UTC_DATE_FORMAT   "%Y-%m-%dT%H:%M:%SZ"
 UTC date format string. More...
 
#define QOF_HOUR_TO_SEC(x)   (x * SECS_PER_HOUR)
 
#define QOF_MIN_TO_SEC(x)   (x * 60)
 
#define QOF_DAYS_TO_SEC(x)   (x * SECS_PER_DAY)
 

Typedefs

typedef struct QofDate_s QofDate
 Full range replacement for struct tm. More...
 
typedef gint QofDateFormat
 

Functions

void qof_date_init (void)
 initialise the QofDate tables
 
void qof_date_close (void)
 close down the QofDate tables
 

Default QofDate formats

#define QOF_DATE_FORMAT_US   1
 Continental US default. "%m/%d/%Y". More...
 
#define QOF_DATE_FORMAT_UK   2
 United Kingdom default. "%d/%m/%Y". More...
 
#define QOF_DATE_FORMAT_CE   3
 Contintental European default. "%d.%m.%Y". More...
 
#define QOF_DATE_FORMAT_ISO   4
 Short ISO form. "%F". More...
 
#define QOF_DATE_FORMAT_UTC   5
 QOF UTC format, xsd:date compatible. QOF_UTC_DATE_FORMAT "%Y-%m-%dT%H:%M:%SZ". More...
 
#define QOF_DATE_FORMAT_ISO8601   6
 
#define QOF_DATE_FORMAT_LOCALE   7
 GNU locale default. "%x". More...
 
#define QOF_DATE_FORMAT_CUSTOM   8
 Date and time for the current locale "%c". More...
 
#define DATE_FORMAT_LAST   QOF_DATE_FORMAT_CUSTOM
 

QofDateFormat - standardised date formats

To simplify usage of strftime and strptime (especially checking error states), QofDate uses a set of standard date formats. You can also register your own format strings as long as they are strftime compatible.

see also QofDate and locales.

gboolean qof_date_format_add (const gchar *str, QofDateFormat *identifier)
 Add a specific strftime compatible string as a new QofDateFormat. More...
 
const gchar * qof_date_format_to_name (QofDateFormat format)
 Retrieve the shorthand name for the selected date format. More...
 
QofDateFormat qof_date_format_from_name (const gchar *name)
 Returns the default date format for a known shorthand name. More...
 
gboolean qof_date_format_set_name (const gchar *name, QofDateFormat format)
 Set a shorthand name for a custom date format. More...
 
QofDateFormat qof_date_format_get_current (void)
 returns the current date format.
 
gboolean qof_date_format_set_current (QofDateFormat df)
 Selects one registered date format as the current default. More...
 
const gchar * qof_date_format_get_format (QofDateFormat df)
 Retrieve the strftime format string for a registered date format. More...
 
gchar qof_date_format_get_date_separator (QofDateFormat df)
 Return the field separator for the current date format. More...
 
gboolean qof_date_format_set_date_separator (const gchar sep, QofDateFormat df)
 Set a locale-specific separator. More...
 

QofDate handlers

QofDateqof_date_new (void)
 
QofDateqof_date_get_current (void)
 
QofDateqof_date_new_dmy (gint day, gint month, gint64 year)
 
void qof_date_free (QofDate *date)
 
QofTimeqof_date_time_difference (const QofDate *date1, const QofDate *date2)
 
gboolean qof_date_is_last_mday (const QofDate *qd)
 
gboolean qof_date_addmonths (QofDate *qd, gint months, gboolean track_last_day)
 
gboolean qof_date_equal (const QofDate *d1, const QofDate *d2)
 
gint qof_date_compare (const QofDate *d1, const QofDate *d2)
 
gboolean qof_date_valid (QofDate *date)
 Validate a QofDate. More...
 
guint16 qof_date_get_yday (gint mday, gint month, gint64 year)
 
guint8 qof_date_get_mday (gint month, gint64 year)
 

Conversion handlers for QofDate

QofDateqof_date_from_qtime (const QofTime *qt)
 
QofTimeqof_date_to_qtime (const QofDate *qd)
 
QofDateqof_date_from_struct_tm (const struct tm *stm)
 Convert a struct tm to a QofDate. More...
 
gboolean qof_date_to_struct_tm (const QofDate *qt, struct tm *stm, glong *nanosecs)
 Convert a QofDate to a struct tm. More...
 
gboolean qof_date_to_gdate (const QofDate *qd, GDate *gd)
 Convert a QofDate to a GDate. More...
 
QofDateqof_date_from_gdate (const GDate *gd)
 Create a QofDate from a GDate. More...
 

Manipulate QofTime as a date

Shorthand routines to modify a QofTime using date-type values, instead of having to always use seconds.

gboolean qof_date_adddays (QofDate *qd, gint days)
 Add a number of days to a QofDate and normalise. More...
 
gboolean qof_date_set_day_end (QofDate *qd)
 
gboolean qof_date_set_day_start (QofDate *qd)
 
gboolean qof_date_set_day_middle (QofDate *qd)
 

Date Printing/Scanning functions

QofDate supports a wider range of dates than either strftime or GDate and supports all non-locale-specific strftime format specifiers over the full range of QofDate.

Note
Locale-specific formats cannot be extended to the full range of QofDate dates because the locale data for these formats is only available to the underlying strftime implementation. The formats affected are those involving the E and O modifiers and other format specifiers that use the current locale. e.g. Japanese Emperor reigns, local numeric specifiers, translated days of the week / month etc. If these are used, only dates within the range of the locale-sensitive strftime on that platform can be supported (either inside or outside QofDate).

The full list of affected format specifiers is:

'a', 'A', 'b', 'h', 'B', 'c', 'C', 'x', 'p', 'P', 'r', 'X', 'E', 'O'.

QofDate will attempt to fallback to a usable format if the date is out of range of the underlying strftime. e.g. QOF_DATE_FORMAT_UTC, QOF_DATE_FORMAT_UK, QOF_DATE_FORMAT_US, QOF_DATE_FORMAT_CE or QOF_DATE_FORMAT_ISO.

Note
It is not particularly sensible to write locale-sensitive date strings to any kind of permanent storage. Locale-specific format specifiers should only be used for displaying dates to the user.

For more information, see QofDate and locales.

gchar * qof_date_print (const QofDate *date, QofDateFormat df)
 Convert a QofDate to a timestamp according to the specified date format. More...
 
QofDateqof_date_parse (const gchar *str, QofDateFormat df)
 Convert a timestamp to a QofTime. More...
 

Detailed Description

All dates in QOF use Universal Coordinated Time, (UTC), wrapped to allow representation of dates before the epoch.

localtime is available via GDate but there are limitations.

  1. The GDate data structure represents a day between January 1, Year 1, and sometime a few thousand years in the future, not the full range of QofTime.
  2. Right now GDate will go to the year 65535 or so, but g_date_set_parse() only parses up to the year 8000 or so - just count on "a few thousand".
  3. g_date_strftime only deals with date related formats, results of using time formats is undefined.

    A QofDateEntry is one a collection of formats for handling dates, including common defaults. New entries need to be registered using qof_date_add_format before being used to print or scan strings containing dates.

Todo:
Add support for customised handlers for added formats.

A QofDate is theoretically able to go forward to the year 292,471,206,707 AD and back to the year 292,471,206,708 BC. Whether such dates actually exist is outside the scope of this documentation.

Note
Time moves at a constant rate, dates do not. Dates are inherently tied to the rotation of the Earth and that rotation is slowing down, causing the need for leapseconds. The incidence of leapseconds cannot be accurately predicted and dates far into the future may be out by a number of seconds. This is important if future dates are based around the start (or end) of a particular day - when those dates come to be viewed as the past, the actual day calculated from the number of seconds may differ to the day originally calculated. When using future dates, avoid using dates at the start or end of a particular day.
Since
v0.7.0

Macro Definition Documentation

#define DATE_FORMAT_LAST   QOF_DATE_FORMAT_CUSTOM

New identifiers must be larger than this.

Definition at line 322 of file qofdate.h.

#define MAX_DATE_BUFFER   256

The maximum length of a QofDate buffer.

Todo:
rationalise with MAX_DATE_LENGTH

Definition at line 92 of file qofdate.h.

#define MAX_DATE_LENGTH   41

The maximum length of a string used for or created by dates.

When setting a custom QofDateFormat, neither the format string itself nor any date string created from that format is allowed to exceed this length.

Definition at line 87 of file qofdate.h.

#define QOF_DATE_FORMAT_CE   3

Contintental European default. "%d.%m.%Y".

9th May 2006 == 09.05.2006

Definition at line 263 of file qofdate.h.

#define QOF_DATE_FORMAT_CUSTOM   8

Date and time for the current locale "%c".

QOF_DATE_FORMAT_LOCALE and QOF_DATE_FORMAT_CUSTOM are only suitable for date / time display - storing these values in any kind of file is a recipe for disaster as the exact format can be affected by environment variables and other imponderables.

One example: 9th May 2006 gives Tue 09 May 2006 14:50:10 UTC

Note
QOF_DATE_FORMAT_CUSTOM includes locale-specific format specifiers and therefore cannot support the full range of QofDate. see QofDate and locales.

Definition at line 319 of file qofdate.h.

#define QOF_DATE_FORMAT_ISO   4

Short ISO form. "%F".

9th May 2006 == 2006-05-09

Definition at line 268 of file qofdate.h.

#define QOF_DATE_FORMAT_ISO8601   6

Date and time with nanoseconds and timezone.

"%Y-%m-%d %H:%M:%S.%N %z"

12th July 2006 gives 2006-07-12 17:08:16.329768000 +0000 in UTC. Timezones can be specified and will then be converted into UTC at validation.

Definition at line 287 of file qofdate.h.

#define QOF_DATE_FORMAT_LOCALE   7

GNU locale default. "%x".

QOF_DATE_FORMAT_LOCALE and QOF_DATE_FORMAT_CUSTOM are only suitable for date / time display - storing these values in any kind of file is a recipe for disaster as the exact format can be affected by environment variables and other imponderables.

One example: 9th May 2006 gives 09/05/06

Note
QOF_DATE_FORMAT_LOCALE includes locale-specific format specifiers and therefore cannot support the full range of QofDate. see QofDate and locales.

Definition at line 303 of file qofdate.h.

#define QOF_DATE_FORMAT_UK   2

United Kingdom default. "%d/%m/%Y".

9th May 2006 == 09/05/2006

Definition at line 258 of file qofdate.h.

#define QOF_DATE_FORMAT_US   1

Continental US default. "%m/%d/%Y".

9th May 2006 == 05/09/2006

Definition at line 253 of file qofdate.h.

#define QOF_DATE_FORMAT_UTC   5

QOF UTC format, xsd:date compatible. QOF_UTC_DATE_FORMAT "%Y-%m-%dT%H:%M:%SZ".

xsd:date is recommended for any XML data storage of dates and times.

9th May 2006 == 2006-05-09T14:49:04Z

Definition at line 277 of file qofdate.h.

#define qof_date_isleap (   year)    ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))

Nonzero if YEAR is a leap year (every 4 years, except every 100th isn't, and every 400th is).

Definition at line 222 of file qofdate.h.

#define QOF_DAYS_TO_SEC (   x)    (x * SECS_PER_DAY)

convenience macro to turn days into seconds.

Definition at line 331 of file qofdate.h.

#define QOF_HOUR_TO_SEC (   x)    (x * SECS_PER_HOUR)

convenience macro to turn hours into seconds.

Definition at line 327 of file qofdate.h.

#define QOF_MIN_TO_SEC (   x)    (x * 60)

convenience macro to turn minutes into seconds.

Definition at line 329 of file qofdate.h.

#define QOF_MOD_DATE   "qof-dates"

QofLogModule name for QofDate

Definition at line 98 of file qofdate.h.

#define QOF_UTC_DATE_FORMAT   "%Y-%m-%dT%H:%M:%SZ"

UTC date format string.

Timezone independent, date and time inclusive, as used in the QSF backend. The T and Z characters are from xsd:dateTime format in coordinated universal time, UTC. You can reproduce the string from the GNU/Linux command line using the date utility:

$ date -u +%Y-%m-%dT%H:M:SZ 
2004-12-12T23:39:11Z

The datestring must be timezone independent and include all specified fields. Remember to use gmtime() NOT localtime()!

Definition at line 244 of file qofdate.h.

#define SECS_PER_DAY   86400

number of seconds in one whole day.

Definition at line 94 of file qofdate.h.

#define SECS_PER_HOUR   3600

number of seconds in one whole hour.

Definition at line 96 of file qofdate.h.

Typedef Documentation

typedef struct QofDate_s QofDate

Full range replacement for struct tm.

Based on struct tm but using signed integers. The year value uses a signed 64bit value to prevent overflows. (A glong is insufficient by two orders of magnitude.) To retain precision, a QofDate includes a nanoseconds value that can be used with a QofTime and a 64bit value for seconds.

Note
All QofDate values can be negative. The normalising cascade handles rollovers. e.g. If a QofDate qd_min value is 5 initially, setting qd_sec to 68 causes qd_sec to actually hold the value 8 and qd_min to hold the value 6. Alternatively, setting qd_sec to -64 with qd_min set to 5 causes qd_sec to hold the value 56 and qd_min to hold the value 3.
Todo:
check - years work like this, days don't!! Only qd_year retains a negative value once set. Adding one year to a negative QofDate causes the QofDate to be set to one year further into the past. This follows the same pattern as typical BC dates: the 1st of May 501BC is further into the past than the 5th of May 500BC.

Why is this a date? Because it represents a date, broken down into the component variables. A QofTime always (and only) relates to seconds, a QofDate always relates to how that number of seconds can be represented as a sequence of days, months, years etc.

Note
Although values can be set directly, qof_date_valid should be called before attempting to manipulate a QofDate.
Todo:
Reorganise the qof_time_* functions to reflect this statement. qof_time_set_day_end should be qof_date_set_day_end and the various qof_date_time functions need to be reviewed.
typedef gint QofDateFormat

Index value of the selected QofDateFormat in the DateFormatTable

Definition at line 335 of file qofdate.h.

Function Documentation

gboolean qof_date_adddays ( QofDate qd,
gint  days 
)

Add a number of days to a QofDate and normalise.

Parameters
qdA valid QofDate.
daysNumber of days to add - use negative value to subtract.
Returns
FALSE on error, otherwise TRUE.

Definition at line 1000 of file qofdate.c.

1001 {
1002  g_return_val_if_fail (qd, FALSE);
1003  g_return_val_if_fail (qof_date_valid (qd), FALSE);
1004  qd->qd_mday += days;
1005  return qof_date_valid (qd);
1006 }
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
gboolean qof_date_valid(QofDate *date)
Validate a QofDate.
Definition: qofdate.c:650
gboolean qof_date_addmonths ( QofDate qd,
gint  months,
gboolean  track_last_day 
)

Add (or subtract) months from a QofDate

Optionally tracks the last day of the month so that if the original QofDate is the last day of the month in the specified year, the updated QofDate will also be the last day of the updated month in the updated year.

Parameters
qdA QofDate which will be normalised before calculations begin.
monthsNumber of months to add (or subtract if months is negative).
track_last_dayWhether to track the last day.
Returns
FALSE on error, otherwise TRUE.

Definition at line 1009 of file qofdate.c.

1011 {
1012  g_return_val_if_fail (qd, FALSE);
1013  g_return_val_if_fail (qof_date_valid (qd), FALSE);
1014  qd->qd_mon += months % 12;
1015  qd->qd_year += months / 12;
1016  g_return_val_if_fail (qof_date_valid (qd), FALSE);
1017  if (track_last_day && qof_date_is_last_mday (qd))
1018  {
1019  qd->qd_mday = qof_date_get_mday (qd->qd_mon,
1020  qd->qd_year);
1021  }
1022  return TRUE;
1023 }
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
guint8 qof_date_get_mday(gint month, gint64 year)
Definition: qofdate.c:183
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
gboolean qof_date_is_last_mday(const QofDate *qd)
Definition: qofdate.c:193
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
gboolean qof_date_valid(QofDate *date)
Validate a QofDate.
Definition: qofdate.c:650
gint qof_date_compare ( const QofDate d1,
const QofDate d2 
)

Compare two QofDates

Definition at line 671 of file qofdate.c.

672 {
673  if ((!d1) && (!d2))
674  return 0;
675  if (d1 == d2)
676  return 0;
677  if (!d1)
678  return -1;
679  if (!d2)
680  return 1;
681  if (d1->qd_year < d2->qd_year)
682  return -1;
683  if (d1->qd_year > d2->qd_year)
684  return 1;
685  if (d1->qd_mon < d2->qd_mon)
686  return -1;
687  if (d1->qd_mon > d2->qd_mon)
688  return 1;
689  if (d1->qd_mday < d2->qd_mday)
690  return -1;
691  if (d1->qd_mday > d2->qd_mday)
692  return 1;
693  if (d1->qd_hour < d2->qd_hour)
694  return -1;
695  if (d1->qd_hour > d2->qd_hour)
696  return 1;
697  if (d1->qd_min < d2->qd_min)
698  return -1;
699  if (d1->qd_min > d2->qd_min)
700  return 1;
701  if (d1->qd_sec < d2->qd_sec)
702  return -1;
703  if (d1->qd_sec > d2->qd_sec)
704  return 1;
705  if (d1->qd_nanosecs < d2->qd_nanosecs)
706  return -1;
707  if (d1->qd_nanosecs > d2->qd_nanosecs)
708  return 1;
709  return 0;
710 }
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
glong qd_hour
Signed replacement of struct tm.tm_hour.
Definition: qofdate.h:157
glong qd_min
Signed replacement of struct tm.tm_min.
Definition: qofdate.h:150
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
gint64 qd_sec
Definition: qofdate.h:143
glong qd_nanosecs
Definition: qofdate.h:141
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
gboolean qof_date_equal ( const QofDate d1,
const QofDate d2 
)

Check two QofDates for equality

Definition at line 663 of file qofdate.c.

664 {
665  if (0 == qof_date_compare (d1, d2))
666  return TRUE;
667  return FALSE;
668 }
gint qof_date_compare(const QofDate *d1, const QofDate *d2)
Definition: qofdate.c:671
gboolean qof_date_format_add ( const gchar *  str,
QofDateFormat identifier 
)

Add a specific strftime compatible string as a new QofDateFormat.

Unlike GDate, QofDate allows time-related formats.

Parameters
strA pre-formatted string, suitable to be passed directly to strftime.
identifierinteger pointer. Will be set to the positive value to be used to identify this date format later.
Returns
TRUE on success, otherwise FALSE
Todo:
Move to QofDate and qofgmtime_r

Definition at line 202 of file qofdate.c.

203 {
204  struct tm check;
205  gint len;
206  time_t now;
207  gchar test[MAX_DATE_BUFFER];
208 
210  g_return_val_if_fail (QofDateInit, FALSE);
211  g_return_val_if_fail (str, FALSE);
212  g_return_val_if_fail (strlen (str) != 0, FALSE);
213  /* prevent really long strings being passed */
214  ENTER (" str=%s", str);
215  if (strlen (str) > MAX_DATE_LENGTH)
216  {
217  LEAVE (" '%s' is too long! Max=%d str_len=%d",
218  str, MAX_DATE_LENGTH, (gint) strlen (str));
219  return FALSE;
220  }
221  /* test the incoming string using the current time. */
222  now = time (NULL);
223  test[0] = '\1';
224  check = *gmtime_r (&now, &check);
225  /* need to allow time related formats -
226  don't use g_date_strftime here. */
227  len = strftime (test, (MAX_DATE_BUFFER - 1), str, &check);
228  if (len == 0 && test[0] != '\0')
229  {
230  LEAVE (" strftime could not understand '%s'", str);
231  return FALSE;
232  }
233  len = strlen (test);
234  if (len > MAX_DATE_LENGTH)
235  {
236  LEAVE (" %s creates a string '%s' that is too long!"
237  " Max=%d str_len=%d", str, test, MAX_DATE_LENGTH, len);
238  return FALSE;
239  }
240  *identifier = g_hash_table_size (DateFormatTable) + 1;
241  {
242  QofDateEntry *d = g_new0 (QofDateEntry, 1);
243  d->format = str;
244  d->name = str;
245  d->separator = locale_separator;
246  d->df = *identifier;
247  g_hash_table_insert (DateFormatTable, GINT_TO_POINTER (d->df), d);
248  }
249  LEAVE (" successful");
250  return TRUE;
251 }
#define MAX_DATE_BUFFER
The maximum length of a QofDate buffer.
Definition: qofdate.h:92
#define LEAVE(format, args...)
Definition: qoflog.h:227
#define MAX_DATE_LENGTH
The maximum length of a string used for or created by dates.
Definition: qofdate.h:87
#define ENTER(format, args...)
Definition: qoflog.h:217
QofDateFormat qof_date_format_from_name ( const gchar *  name)

Returns the default date format for a known shorthand name.

If the selected QofDateFormat is one of the defaults, the shorthand "name" is returned. If format is not a default, returns negative one.

Parameters
nameShorthand "name" of this format.
Returns
the QofDateFormat on success, negative one on failure.

Definition at line 385 of file qofdate.c.

386 {
387  struct iter i;
388 
389  if (!name)
390  return -1;
391  if (0 == safe_strcmp (name, "us"))
392  return QOF_DATE_FORMAT_US;
393  if (0 == safe_strcmp (name, "uk"))
394  return QOF_DATE_FORMAT_UK;
395  if (0 == safe_strcmp (name, "ce"))
396  return QOF_DATE_FORMAT_CE;
397  if (0 == safe_strcmp (name, "utc"))
398  return QOF_DATE_FORMAT_UTC;
399  if (0 == safe_strcmp (name, "iso"))
400  return QOF_DATE_FORMAT_ISO;
401  if (0 == safe_strcmp (name, "locale"))
402  return QOF_DATE_FORMAT_LOCALE;
403  if (0 == safe_strcmp (name, "custom"))
404  return QOF_DATE_FORMAT_CUSTOM;
405  i.name = name;
406  i.df = -1;
407  g_hash_table_foreach (DateFormatTable, lookup_name, &i);
408  return i.df;
409 }
#define QOF_DATE_FORMAT_UTC
QOF UTC format, xsd:date compatible. QOF_UTC_DATE_FORMAT "%Y-%m-%dT%H:%M:%SZ".
Definition: qofdate.h:277
#define QOF_DATE_FORMAT_ISO
Short ISO form. "%F".
Definition: qofdate.h:268
#define QOF_DATE_FORMAT_CE
Contintental European default. "%d.%m.%Y".
Definition: qofdate.h:263
#define QOF_DATE_FORMAT_UK
United Kingdom default. "%d/%m/%Y".
Definition: qofdate.h:258
#define QOF_DATE_FORMAT_LOCALE
GNU locale default. "%x".
Definition: qofdate.h:303
#define QOF_DATE_FORMAT_CUSTOM
Date and time for the current locale "%c".
Definition: qofdate.h:319
gint safe_strcmp(const gchar *da, const gchar *db)
Definition: qofutil.c:75
#define QOF_DATE_FORMAT_US
Continental US default. "%m/%d/%Y".
Definition: qofdate.h:253
gchar qof_date_format_get_date_separator ( QofDateFormat  df)

Return the field separator for the current date format.

Note
The separator only relates to the date portion of any date format string, i.e. the separator used between day, month and year. Separators used between time fields like hour, minute, second in any date format are not available.
Returns
date single non-digit character to separate fields within the date section of a date format or a null on error.

Definition at line 325 of file qofdate.c.

326 {
327  QofDateEntry *d;
328 
329  g_return_val_if_fail (QofDateInit, locale_separator);
330  d = g_hash_table_lookup (DateFormatTable, GINT_TO_POINTER (df));
331  if (!d)
332  {
333  PERR (" unknown format: '%d'", df);
334  return locale_separator;
335  }
336  return d->separator;
337 }
#define PERR(format, args...)
Definition: qoflog.h:183
const gchar* qof_date_format_get_format ( QofDateFormat  df)

Retrieve the strftime format string for a registered date format.

Parameters
dfThe QofDateFormat identifier for the registered date format.
Returns
The format string for this date format or NULL on error.

Definition at line 310 of file qofdate.c.

311 {
312  QofDateEntry *d;
313 
314  g_return_val_if_fail (QofDateInit, NULL);
315  d = g_hash_table_lookup (DateFormatTable, GINT_TO_POINTER (df));
316  if (!d)
317  {
318  PERR (" unknown format: '%d'", df);
319  return NULL;
320  }
321  return d->format;
322 }
#define PERR(format, args...)
Definition: qoflog.h:183
gboolean qof_date_format_set_current ( QofDateFormat  df)

Selects one registered date format as the current default.

Parameters
dfQofDateFormat identifier indicating preferred format.
Returns
TRUE on success, else FALSE.

Definition at line 294 of file qofdate.c.

295 {
296  QofDateEntry *d;
297 
298  g_return_val_if_fail (QofDateInit, FALSE);
299  d = g_hash_table_lookup (DateFormatTable, GINT_TO_POINTER (df));
300  if (!d)
301  {
302  PERR (" unknown format: '%d'", df);
303  return FALSE;
304  }
305  dateFormat = d->df;
306  return TRUE;
307 }
#define PERR(format, args...)
Definition: qoflog.h:183
gboolean qof_date_format_set_date_separator ( const gchar  sep,
QofDateFormat  df 
)

Set a locale-specific separator.

Sets the date separator for a date format added using qof_date_format_add.

Returns
FALSE if date format is not one of the QOF defaults or if the character is a digit, TRUE on success.

Definition at line 340 of file qofdate.c.

341 {
342  QofDateEntry *d;
343 
344  g_return_val_if_fail (QofDateInit, FALSE);
345  if (df < DATE_FORMAT_LAST)
346  {
347  DEBUG (" Prevented attempt to override a default format");
348  return FALSE;
349  }
350  if (g_ascii_isdigit (sep))
351  return FALSE;
352  d = g_hash_table_lookup (DateFormatTable, GINT_TO_POINTER (df));
353  if (!d)
354  {
355  PERR (" unknown format: '%d'", df);
356  return FALSE;
357  }
358  d->separator = sep;
359  g_hash_table_insert (DateFormatTable, GINT_TO_POINTER (df), d);
360  return TRUE;
361 }
#define PERR(format, args...)
Definition: qoflog.h:183
#define DATE_FORMAT_LAST
Definition: qofdate.h:322
#define DEBUG(format, args...)
Definition: qoflog.h:208
gboolean qof_date_format_set_name ( const gchar *  name,
QofDateFormat  format 
)

Set a shorthand name for a custom date format.

Used alongside qof_date_format_add to allow any date format to have a shorthand name.

Parameters
nameShorthand name for a date format added with qof_date_format_add. The string becomes the property of QofDate and should not be freed.
formatidentifier used previously with qof_date_format_add
Returns
TRUE if the shorthand name can be set, FALSE on error or if the chosen QofDateFormat is one of the defaults.

Definition at line 269 of file qofdate.c.

270 {
271  QofDateEntry *d;
272 
273  g_return_val_if_fail (QofDateInit, FALSE);
274  if (format <= DATE_FORMAT_LAST)
275  return FALSE;
276  d = g_hash_table_lookup (DateFormatTable, GINT_TO_POINTER (format));
277  if (!d)
278  {
279  PERR (" unknown format: '%d'", format);
280  return FALSE;
281  }
282  d->name = name;
283  g_hash_table_insert (DateFormatTable, GINT_TO_POINTER (format), d);
284  return TRUE;
285 }
#define PERR(format, args...)
Definition: qoflog.h:183
#define DATE_FORMAT_LAST
Definition: qofdate.h:322
const gchar* qof_date_format_to_name ( QofDateFormat  format)

Retrieve the shorthand name for the selected date format.

If the selected QofDateFormat is one of the defaults, a shorthand "name" is used. If it is a string added using qof_date_add_format, the string itself is returned.

Parameters
formatThe QofDateFormat to lookup.
Returns
FALSE on success and TRUE on failure.

Definition at line 254 of file qofdate.c.

255 {
256  QofDateEntry *d;
257 
258  g_return_val_if_fail (QofDateInit, NULL);
259  d = g_hash_table_lookup (DateFormatTable, GINT_TO_POINTER (format));
260  if (!d)
261  {
262  PERR (" unknown format: '%d'", format);
263  return NULL;
264  }
265  return d->name;
266 }
#define PERR(format, args...)
Definition: qoflog.h:183
void qof_date_free ( QofDate date)

free a QofDate

Definition at line 642 of file qofdate.c.

643 {
644  g_return_if_fail (date);
645  g_free (date);
646  date = NULL;
647 }
QofDate* qof_date_from_gdate ( const GDate *  gd)

Create a QofDate from a GDate.

A GDate is always within the range of a QofDate.

Parameters
gdA valid GDate.
Returns
NULL on error, otherwise a newly allocated, valid, QofDate.

Definition at line 784 of file qofdate.c.

785 {
786  QofDate * qd;
787 
788  g_return_val_if_fail (g_date_valid (date), NULL);
789  qd = qof_date_new ();
790  qd->qd_year = g_date_get_year (date);
791  qd->qd_mon = g_date_get_month (date);
792  qd->qd_mday = g_date_get_day (date);
793  qd = date_normalise (qd);
794  return qd;
795 }
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
Full range replacement for struct tm.
Definition: qofdate.h:138
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
QofDate * qof_date_new(void)
Definition: qofdate.c:607
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
QofDate* qof_date_from_qtime ( const QofTime qt)

Return a QofDate in UTC from a QofTime.

Definition at line 884 of file qofdate.c.

885 {
886  QofDate *qd;
887  gint leap_extra_secs;
888 
889  /* may not want to create a new time or date - it
890  complicates memory management. */
891  g_return_val_if_fail (qt, NULL);
892  g_return_val_if_fail (qof_time_is_valid (qt), NULL);
893  qd = qof_date_new ();
894  leap_extra_secs = 0;
895  setenv ("TZ", "GMT", 1);
896  tzset();
897  leap_extra_secs = extract_interval (qt);
898  qof_date_offset (qt, leap_extra_secs, qd);
900  qd->qd_is_dst = 0;
901  qd->qd_zone = "GMT";
902  qd->qd_gmt_off = 0L;
903  if (!qof_date_valid(qd))
904  return NULL;
905  return qd;
906 }
const gchar * qd_zone
Calculated value based on struct tm.tm_zone.
Definition: qofdate.h:208
gshort qd_is_dst
Definition: qofdate.h:193
glong qof_time_get_nanosecs(const QofTime *qt)
Get the number of seconds.
Definition: qoftime.c:141
Full range replacement for struct tm.
Definition: qofdate.h:138
glong qd_gmt_off
Calculated value based on struct tm.tm_gmtoff.
Definition: qofdate.h:201
glong qd_nanosecs
Definition: qofdate.h:141
QofDate * qof_date_new(void)
Definition: qofdate.c:607
gboolean qof_date_valid(QofDate *date)
Validate a QofDate.
Definition: qofdate.c:650
QofDate* qof_date_from_struct_tm ( const struct tm *  stm)

Convert a struct tm to a QofDate.

Parameters
stmA pointer to a valid struct tm.
Returns
Newly allocated QofDate or NULL if tm is NULL.

Definition at line 713 of file qofdate.c.

714 {
715  QofDate *d;
716 
717  g_return_val_if_fail (stm, NULL);
718  d = g_new0 (QofDate, 1);
719  d->qd_sec = stm->tm_sec;
720  d->qd_min = stm->tm_min;
721  d->qd_hour = stm->tm_hour;
722  d->qd_mday = stm->tm_mday;
723  d->qd_mon = stm->tm_mon + 1;
724  d->qd_year = stm->tm_year + 1900;
725  d->qd_wday = stm->tm_wday;
726  d->qd_yday = stm->tm_yday;
727  d->qd_is_dst = stm->tm_isdst;
728  d->qd_gmt_off = stm->tm_gmtoff;
729  d->qd_zone = stm->tm_zone;
730  d->qd_valid = TRUE;
731  d = date_normalise(d);
732  return d;
733 }
gshort qd_yday
Definition: qofdate.h:189
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
gboolean qd_valid
If the QofDate is valid or merely initialised.
Definition: qofdate.h:217
const gchar * qd_zone
Calculated value based on struct tm.tm_zone.
Definition: qofdate.h:208
gshort qd_is_dst
Definition: qofdate.h:193
Full range replacement for struct tm.
Definition: qofdate.h:138
glong qd_hour
Signed replacement of struct tm.tm_hour.
Definition: qofdate.h:157
glong qd_min
Signed replacement of struct tm.tm_min.
Definition: qofdate.h:150
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
gint64 qd_sec
Definition: qofdate.h:143
glong qd_gmt_off
Calculated value based on struct tm.tm_gmtoff.
Definition: qofdate.h:201
gshort qd_wday
Definition: qofdate.h:185
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
QofDate* qof_date_get_current ( void  )

create a new QofDate for the current date and time.

Definition at line 616 of file qofdate.c.

617 {
618  QofTime *qt;
619  QofDate *qd;
620 
621  qt = qof_time_get_current ();
622  qd = qof_date_from_qtime (qt);
623  qof_time_free (qt);
624  return qd;
625 }
QofDate * qof_date_from_qtime(const QofTime *qt)
Definition: qofdate.c:884
Full range replacement for struct tm.
Definition: qofdate.h:138
QofTime * qof_time_get_current(void)
Get the current QofTime.
Definition: qoftime.c:362
void qof_time_free(QofTime *qt)
Free a QofTime when no longer required.
Definition: qoftime.c:56
struct QofTime64 QofTime
Use a 64-bit signed int QofTime.
Definition: qoftime.h:112
guint8 qof_date_get_mday ( gint  month,
gint64  year 
)

full range version of g_date_get_days_in_month

Parameters
monthAny valid QofDate qd_mon, 1 to 12.
yearAny valid QofDate qd_year.
Returns
0 on error, otherwise the number of days in the specified month, taking leap years into account.

Definition at line 183 of file qofdate.c.

184 {
185  g_return_val_if_fail (month != 0, 0);
186  g_return_val_if_fail (month <= 12, 0);
187  g_return_val_if_fail (month >= 1, 0);
188  g_return_val_if_fail (year != 0, 0);
189  return days_in_months[qof_date_isleap (year)][month];
190 }
#define qof_date_isleap(year)
Definition: qofdate.h:222
guint16 qof_date_get_yday ( gint  mday,
gint  month,
gint64  year 
)

full range version of g_date_get_day_of_year

Parameters
mdayAny valid QofDate qd_mday, 1 to 31.
monthAny valid QofDate qd_mon, 1 to 12.
yearAny valid QofDate qd_year.
Returns
0 if error, otherwise the day of the year, where Jan 1 is 1, the first day of the year.

Definition at line 167 of file qofdate.c.

168 {
169  guint8 leap;
170 
171  g_return_val_if_fail (mday != 0, 0);
172  g_return_val_if_fail (month != 0, 0);
173  g_return_val_if_fail (month <= 12, 0);
174  g_return_val_if_fail (month >= 1, 0);
175  g_return_val_if_fail (year != 0, 0);
176  leap = qof_date_isleap (year);
177  g_return_val_if_fail (mday <=
178  qof_date_get_mday (month, year), 0);
179  return days_in_year[leap][month] + mday;
180 }
guint8 qof_date_get_mday(gint month, gint64 year)
Definition: qofdate.c:183
#define qof_date_isleap(year)
Definition: qofdate.h:222
gboolean qof_date_is_last_mday ( const QofDate qd)

Checks if QofDate the last day of the month.

Parameters
qdA valid QofDate.
Returns
TRUE if qd_mday is the last day of qd_mon in qd_year, otherwise (or on error), FALSE.

Definition at line 193 of file qofdate.c.

194 {
195  g_return_val_if_fail (qd, FALSE);
196  g_return_val_if_fail (qd->qd_valid, FALSE);
197  return (qd->qd_mday ==
198  qof_date_get_mday (qd->qd_mon, qd->qd_year));
199 }
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
gboolean qd_valid
If the QofDate is valid or merely initialised.
Definition: qofdate.h:217
guint8 qof_date_get_mday(gint month, gint64 year)
Definition: qofdate.c:183
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
QofDate* qof_date_new ( void  )

create a new, empty, QofDate

Definition at line 607 of file qofdate.c.

608 {
609  QofDate *d;
610 
611  d = g_new0 (QofDate, 1);
612  return d;
613 }
Full range replacement for struct tm.
Definition: qofdate.h:138
QofDate* qof_date_new_dmy ( gint  day,
gint  month,
gint64  year 
)

create a new QofDate from basic calendar data.

Definition at line 628 of file qofdate.c.

629 {
630  QofDate *qd;
631 
632  qd = g_new0 (QofDate, 1);
633  qd->qd_mday = day;
634  qd->qd_mon = month;
635  qd->qd_year = year;
636  if(!qof_date_valid (qd))
637  return NULL;
638  return qd;
639 }
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
Full range replacement for struct tm.
Definition: qofdate.h:138
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
gboolean qof_date_valid(QofDate *date)
Validate a QofDate.
Definition: qofdate.c:650
QofDate* qof_date_parse ( const gchar *  str,
QofDateFormat  df 
)

Convert a timestamp to a QofTime.

Safe for all dates within the range of QofDate.

Parameters
stra timestamp created with one of the registered QofDateFormat formats.
dfThe registered QofDateFormat that produced the string.
Returns
a newly allocated, valid, QofDate or NULL on error.

Definition at line 557 of file qofdate.c.

558 {
559  const gchar *format;
560  QofDateError error;
561  QofDate *date;
562  gchar * G_GNUC_UNUSED check;
563 
564  check = NULL;
565  error = ERR_NO_ERROR;
566  date = qof_date_new ();
567  format = qof_date_format_get_format (df);
568  check = strptime_internal (str, format, date, &error);
569  check = NULL;
570  if (error != ERR_NO_ERROR)
571  {
572  qof_date_free (date);
573  return NULL;
574  }
575 
576  date = date_normalise (date);
577  return date;
578 }
void qof_date_free(QofDate *date)
Definition: qofdate.c:642
const gchar * qof_date_format_get_format(QofDateFormat df)
Retrieve the strftime format string for a registered date format.
Definition: qofdate.c:310
Full range replacement for struct tm.
Definition: qofdate.h:138
QofDate * qof_date_new(void)
Definition: qofdate.c:607
gchar* qof_date_print ( const QofDate date,
QofDateFormat  df 
)

Convert a QofDate to a timestamp according to the specified date format.

Unlike qof_time_stamp_now, any supported QofDate can be converted in any registered QofDateFormat.

Parameters
dateA valid QofDate.
dfa registered QofDateFormat to use to create the string.
Returns
NULL on error, otherwise a string which should be freed when no longer needed.

Definition at line 581 of file qofdate.c.

582 {
583  size_t result;
584  gchar temp[MAX_DATE_BUFFER];
585  QofDateEntry *d;
586 
587  g_return_val_if_fail (QofDateInit, NULL);
588  g_return_val_if_fail (date, NULL);
589  g_return_val_if_fail (date->qd_valid, NULL);
590  d = g_hash_table_lookup (DateFormatTable,
591  GINT_TO_POINTER (df));
592  g_return_val_if_fail (d, NULL);
593  temp[0] = '\1';
594  result = strftime_case (FALSE, temp, MAX_DATE_BUFFER,
595  d->format, date, 1, date->qd_nanosecs);
596  if (result == 0 && temp[0] != '\0')
597  {
598  PERR (" qof extended strftime failed");
599  return NULL;
600  }
601  return g_strndup(temp, result);
602 }
#define MAX_DATE_BUFFER
The maximum length of a QofDate buffer.
Definition: qofdate.h:92
#define PERR(format, args...)
Definition: qoflog.h:183
gboolean qd_valid
If the QofDate is valid or merely initialised.
Definition: qofdate.h:217
glong qd_nanosecs
Definition: qofdate.h:141
QofTime* qof_date_time_difference ( const QofDate date1,
const QofDate date2 
)

Calculate the QofTime between two QofDates

Definition at line 959 of file qofdate.c.

961 {
962  gint64 days;
963  QofTime *secs;
964 
965  secs = qof_time_new ();
966  days = days_between (date1->qd_year, date2->qd_year);
967  qof_time_add_secs(secs, QOF_DAYS_TO_SEC(days));
968  if (days >= 0)
969  {
970  /* positive value, add date2 secs, subtract date1 */
971  qof_time_add_secs(secs, -1 *
972  (QOF_HOUR_TO_SEC(date1->qd_hour) -
973  QOF_MIN_TO_SEC(date1->qd_min) -
974  (date1->qd_sec)));
975  qof_time_add_secs(secs,
976  QOF_HOUR_TO_SEC(date2->qd_hour) +
977  QOF_MIN_TO_SEC(date2->qd_min) +
978  (date2->qd_sec));
979  qof_time_set_nanosecs(secs,
980  (date1->qd_nanosecs - date2->qd_nanosecs));
981  }
982  if (days < 0)
983  {
984  /* negative value*/
985  qof_time_add_secs (secs,
986  QOF_HOUR_TO_SEC(date1->qd_hour) -
987  QOF_MIN_TO_SEC(date1->qd_min) -
988  (date1->qd_sec));
989  qof_time_add_secs (secs, -1 *
990  (QOF_HOUR_TO_SEC(date2->qd_hour) +
991  QOF_MIN_TO_SEC(date2->qd_min) +
992  (date2->qd_sec)));
993  qof_time_set_nanosecs(secs,
994  (date2->qd_nanosecs - date1->qd_nanosecs));
995  }
996  return secs;
997 }
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
QofTime * qof_time_new(void)
create an empty QofTime
Definition: qoftime.c:46
glong qd_hour
Signed replacement of struct tm.tm_hour.
Definition: qofdate.h:157
glong qd_min
Signed replacement of struct tm.tm_min.
Definition: qofdate.h:150
void qof_time_add_secs(QofTime *qt, QofTimeSecs secs)
Add (or subtract) seconds from a QofTime.
Definition: qoftime.c:65
#define QOF_HOUR_TO_SEC(x)
Definition: qofdate.h:327
void qof_time_set_nanosecs(QofTime *qt, glong nano)
Set the number of seconds.
Definition: qoftime.c:125
gint64 qd_sec
Definition: qofdate.h:143
glong qd_nanosecs
Definition: qofdate.h:141
struct QofTime64 QofTime
Use a 64-bit signed int QofTime.
Definition: qoftime.h:112
#define QOF_MIN_TO_SEC(x)
Definition: qofdate.h:329
#define QOF_DAYS_TO_SEC(x)
Definition: qofdate.h:331
gboolean qof_date_to_gdate ( const QofDate qd,
GDate *  gd 
)

Convert a QofDate to a GDate.

Parameters
qda valid QofDate
gda new GDate to store the converted value.
Returns
FALSE on error, if the QofDate is out of range of a GDate or if QofDate is not valid, otherwise TRUE.

Definition at line 764 of file qofdate.c.

765 {
766  g_return_val_if_fail (qd, FALSE);
767  g_return_val_if_fail (gd, FALSE);
768  g_return_val_if_fail (qd->qd_valid, FALSE);
769  if (qd->qd_year >= G_MAXUINT16)
770  {
771  PERR (" QofDate out of range of GDate");
772  return FALSE;
773  }
774  if (!g_date_valid_dmy (qd->qd_mday, qd->qd_mon, qd->qd_year))
775  {
776  PERR (" GDate failed to allow day, month and/or year");
777  return FALSE;
778  }
779  g_date_set_dmy (gd, qd->qd_mday, qd->qd_mon, qd->qd_year);
780  return TRUE;
781 }
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
#define PERR(format, args...)
Definition: qoflog.h:183
gboolean qd_valid
If the QofDate is valid or merely initialised.
Definition: qofdate.h:217
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
QofTime* qof_date_to_qtime ( const QofDate qd)

Return a valid QofTime from a valid QofDate.

Definition at line 926 of file qofdate.c.

927 {
928  QofTime *qt;
929  QofTimeSecs c;
930 
931  g_return_val_if_fail (qd, NULL);
932  g_return_val_if_fail (qd->qd_valid, NULL);
933  c = 0;
934  qt = NULL;
935  if (qd->qd_year < 1970)
936  {
937  c = qd->qd_sec;
938  c += QOF_MIN_TO_SEC(qd->qd_min);
939  c += QOF_HOUR_TO_SEC(qd->qd_hour);
940  c += QOF_DAYS_TO_SEC(qd->qd_yday);
941  c -= QOF_DAYS_TO_SEC(days_between (1970, qd->qd_year));
942  c -= qd->qd_gmt_off;
943  qt = qof_time_set (c, qd->qd_nanosecs);
944  }
945  if (qd->qd_year >= 1970)
946  {
947  c = qd->qd_sec;
948  c += QOF_MIN_TO_SEC(qd->qd_min);
949  c += QOF_HOUR_TO_SEC(qd->qd_hour);
950  c += QOF_DAYS_TO_SEC(qd->qd_yday);
951  c += QOF_DAYS_TO_SEC(days_between (1970, qd->qd_year));
952  c -= qd->qd_gmt_off;
953  qt = qof_time_set (c, qd->qd_nanosecs);
954  }
955  return qt;
956 }
gshort qd_yday
Definition: qofdate.h:189
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
gboolean qd_valid
If the QofDate is valid or merely initialised.
Definition: qofdate.h:217
glong qd_hour
Signed replacement of struct tm.tm_hour.
Definition: qofdate.h:157
glong qd_min
Signed replacement of struct tm.tm_min.
Definition: qofdate.h:150
#define QOF_HOUR_TO_SEC(x)
Definition: qofdate.h:327
gint64 qd_sec
Definition: qofdate.h:143
glong qd_gmt_off
Calculated value based on struct tm.tm_gmtoff.
Definition: qofdate.h:201
QofTime * qof_time_set(QofTimeSecs t, glong nanosecs)
Definition: qoftime.c:210
glong qd_nanosecs
Definition: qofdate.h:141
gint64 QofTimeSecs
Replacement for time_t.
Definition: qoftime.h:121
struct QofTime64 QofTime
Use a 64-bit signed int QofTime.
Definition: qoftime.h:112
#define QOF_MIN_TO_SEC(x)
Definition: qofdate.h:329
#define QOF_DAYS_TO_SEC(x)
Definition: qofdate.h:331
gboolean qof_date_to_struct_tm ( const QofDate qt,
struct tm *  stm,
glong *  nanosecs 
)

Convert a QofDate to a struct tm.

Warning
Check the return value - a QofDate has a larger range than a struct tm. The struct tm will be unchanged if a conversion would have been out of range.
Parameters
qtA valid QofDate.
stmPointer to a struct tm to store the result.
nanosecsPointer to a glong to store the nanoseconds.
Returns
FALSE on error or if the QofDate is invalid or out of the range of a struct tm, otherwise TRUE.

Definition at line 736 of file qofdate.c.

738 {
739  g_return_val_if_fail (qd, FALSE);
740  g_return_val_if_fail (stm, FALSE);
741  g_return_val_if_fail (qd->qd_valid, FALSE);
742  if ((qd->qd_year > G_MAXINT) || (qd->qd_year < 1900))
743  {
744  PERR (" date too large for struct tm");
745  return FALSE;
746  }
747  stm->tm_sec = qd->qd_sec;
748  stm->tm_min = qd->qd_min;
749  stm->tm_hour = qd->qd_hour;
750  stm->tm_mday = qd->qd_mday;
751  stm->tm_mon = qd->qd_mon - 1;
752  stm->tm_year = qd->qd_year - 1900;
753  stm->tm_wday = qd->qd_wday;
754  stm->tm_yday = qd->qd_yday;
755  stm->tm_isdst = qd->qd_is_dst;
756  stm->tm_gmtoff = qd->qd_gmt_off;
757  stm->tm_zone = qd->qd_zone;
758  if (nanosecs != NULL)
759  *nanosecs = qd->qd_nanosecs;
760  return TRUE;
761 }
#define PERR(format, args...)
Definition: qoflog.h:183
gboolean qof_date_valid ( QofDate date)

Validate a QofDate.

If the QofDate is already valid, just returns TRUE. If the QofDate is not valid but can be normalised, the QofDate is normalised and the function returns TRUE. If the QofDate cannot be normalised, returns FALSE.

Year Zero does not exist in the Christian Era, the Gregorian calendar or the Julian calendar. A year zero does exist in ISO 8601:2004 and in the astronomical year numbering with a defined year zero equal to 1 BC, as well as in some Buddhist and Hindu lunar calendars.

In QofDate, 1BC is immediately followed by 1AD and months are numbered from 1 to 12, not from zero.

Normalising a QofDate tries to use sensible defaults:

  • if qd_mon == 0, validating sets qd_mon to 1 (January)
  • if qd_year == 0, validating sets qd_year to -1 (1BC).
  • if qd_mday == 0, validating sets qd_mday to 1.

Definition at line 650 of file qofdate.c.

651 {
652  g_return_val_if_fail (date, FALSE);
653  date = date_normalise (date);
654  if (date->qd_valid == FALSE)
655  {
656  PERR (" unknown QofDate error");
657  return FALSE;
658  }
659  return TRUE;
660 }
#define PERR(format, args...)
Definition: qoflog.h:183
gboolean qd_valid
If the QofDate is valid or merely initialised.
Definition: qofdate.h:217