GRASS Programmer's Manual  6.4.2(2012)
datetime/type.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1995.  Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro
00003  *
00004  * This program is free software under the GPL (>=v2)
00005  * Read the file GPL.TXT coming with GRASS for details.
00006  */
00007 #include <grass/datetime.h>
00008 
00009 
00037 int datetime_set_type(DateTime * dt, int mode, int from, int to, int fracsec)
00038 {
00039     dt->mode = mode;
00040     dt->from = from;
00041     dt->to = to;
00042     dt->fracsec = fracsec;
00043 
00044     dt->year = 0;
00045     dt->month = 0;
00046     dt->day = 0;
00047     dt->hour = 0;
00048     dt->minute = 0;
00049     dt->second = 0.0;
00050     datetime_unset_timezone(dt);
00051 
00052     dt->positive = 1;
00053 
00054     return datetime_check_type(dt);
00055 }
00056 
00057 int
00058 datetime_get_type(const DateTime * dt, int *mode, int *from, int *to,
00059                   int *fracsec)
00060 {
00061     *mode = dt->mode;
00062     *to = dt->to;
00063     *from = dt->from;
00064     *fracsec = dt->fracsec;
00065     return datetime_check_type(dt);
00066 }
00067 
00068 
00080 int datetime_is_valid_type(const DateTime * dt)
00081 {
00082     /* Returns 0 if DateTime structure is not valid. */
00083     return datetime_check_type(dt) == 0;
00084 }
00085 
00086 
00116 int datetime_check_type(const DateTime * dt)
00117 {
00118     /* Returns 0 for a valid DateTime structure.
00119        Sets the error code and error message if the structure is not
00120        valid.  Returns error code. */
00121     switch (dt->mode) {
00122     case DATETIME_ABSOLUTE:
00123     case DATETIME_RELATIVE:
00124         break;
00125     default:
00126         return datetime_error(-1, "invalid datetime 'mode'");
00127     }
00128 
00129     if (!datetime_is_between(dt->from, DATETIME_YEAR, DATETIME_SECOND))
00130         return datetime_error(-2, "invalid datetime 'from'");
00131     if (!datetime_is_between(dt->to, DATETIME_YEAR, DATETIME_SECOND))
00132         return datetime_error(-3, "invalid datetime 'to'");
00133     if (dt->from > dt->to)
00134         return datetime_error(-4, "invalid datetime 'from-to'");
00135     if (dt->mode == DATETIME_RELATIVE) {
00136         if (datetime_in_interval_year_month(dt->from)
00137             && !datetime_in_interval_year_month(dt->to))
00138             return datetime_error(-5, "invalid relative datetime 'from-to'");
00139         if (datetime_in_interval_day_second(dt->from)
00140             && !datetime_in_interval_day_second(dt->to))
00141             return datetime_error(-5, "invalid relative datetime 'from-to'");
00142     }
00143     if (dt->mode == DATETIME_ABSOLUTE && dt->from != DATETIME_YEAR)
00144         return datetime_error(-6, "invalid absolute datetime 'from'");
00145     if (dt->to == DATETIME_SECOND && dt->fracsec < 0)
00146         return datetime_error(-7, "invalid datetime 'fracsec'");
00147 
00148     return 0;
00149 }
00150 
00151 int datetime_in_interval_year_month(int x)
00152 {
00153     return datetime_is_between(x, DATETIME_YEAR, DATETIME_MONTH);
00154 }
00155 
00156 int datetime_in_interval_day_second(int x)
00157 {
00158     return datetime_is_between(x, DATETIME_DAY, DATETIME_SECOND);
00159 }
00160 
00161 
00173 int datetime_is_absolute(const DateTime * dt)
00174 {
00175     return (dt->mode == DATETIME_ABSOLUTE);
00176 }
00177 
00178 
00190 int datetime_is_relative(const DateTime * dt)
00191 {
00192     return (dt->mode == DATETIME_RELATIVE);
00193 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines