1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
|
/****************************************************************************
*
* MODULE: datetime
* AUTHOR(S): Bill Brown <brown gis.uiuc.edu> and Michael Shapiro (CERL)
* (original contributors, 1995)
* Brad Douglas <rez touchofmadness.com>,
* Markus Neteler <neteler itc.it>,
* Bernhard Reiter <bernhard intevation.de>,
* Radim Blazek <radim.blazek gmail.com>
* Glynn Clements <glynn gclements.plus.com>,
* PURPOSE: library for date time structure
* COPYRIGHT: (C) 2002-2007 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
Note: The timestamp routines
G_read_grid3_timestamp +
G_remove_grid3_timestamp +
G_write_grid3_timestamp
are stored in src/libes/gis/timestamp.c
(added 3/2001)
---------------------------------------------------------------------------
This is the DateTime structure
typedef struct {
int mode; /* absolute or relative */
int from, to; /* range of values */
int positive; /* positive/negative datetime */
int year, month, day;
int hour, minute;
double second;
int fracsec; /* #decimal place in printed seconds */
int tz; /* timezone - minutes from UTC */
} DateTime;
DateTimes have a 3-part type consisting of
'mode' and
range qualifiers 'from' and 'to'
mode: one of
#define DATETIME_TYPE_ABSOLUTE 1
#define DATETIME_TYPE_RELATIVE 2
from, to: one of
#define DATETIME_YEAR 1
#define DATETIME_MONTH 2
#define DATETIME_DAY 3
#define DATETIME_HOUR 4
#define DATETIME_MINUTE 5
#define DATETIME_SECOND 6
The values for the from/to #defines must increase from YEAR to SECOND
In other words YEAR < MONTH < DAY < HOUR < MINUTE < SECOND. The idea
is that the higher elements represent higher precision for a date/time.
For example, having seconds in the time is more precise than if seconds
are not present.
There are some restrictions on legal values for from/to.
1. from <= to
2. if the 'mode' is ABSOLUTE, then 'from' must be YEAR
3. if the 'mode' is RELATIVE, then
'from' and 'to' in {YEAR,MONTH}
or in
'from' and 'to' in {DAY,HOUR,MINUTE,SECOND}
year,month,day
hour,minute,second:
These are non-negative values.
For ABSOLUTE types, these must be valid date/time values
year a complete year, all the digits (not just the last 2 digits)
must be positive (since 0 isn't a legal year).
month [1,12]
day [1,n] where n depends on the year/month.
hour [0,23]
minute [0,59]
second [0.0,<60.0]
For RELATIVE types, the value corresponding to 'from' is
unrestricted (except that it can't be negative). The other values
are restricted as follows:
if from==YEAR, month is [0,11]
if from==DAY, hour is [0,23], min is [0,59], sec is [0.0,<60.0]
if from==HOUR, min is [0,59], sec is [0.0,<60.0]
if from==MINUTE, sec is [0.0,<60.0]
fracsec:
This controls the number of decimal places to print after the seconds.
It is only used if the 'to' element is SECOND. It must be non-neagtive.
tz:
The time (hour/minute) in ABSOLUTE types is in local time.
The specification of a timezone (tz) is an (subtractive)
offset to convert from local time to UTC.
To get UTC from localtime: LT - TZ
tz is expressed in minutes from -720 to 780
(720 == 12 hours, 780 minutes == 13 hours).
[See ANSI X3.51-1975, section 2.2.3]
For a timezone to be allowed, the 'to' field must be one of
{MINUTE, SECOND}
positive:
this indicates if the datetime value is to considered
"positive" (!=0) or "negative" (==0)
For mode ABSOLUTE, positive==0 means BC
----------------------------------------------------
API
(*) Error messages
Guidelines:
All functions that return int status codes should return
0 (or positive) if OK;
a negative integer if not;
and register the error with a call to datetime_error()
applications can test for error by
if (datetime_function() < 0) {process the error}
int
:+ datetime_error (int code, char *msg)
record 'code' and 'msg' as error code/msg (in static variables)
code==0 will clear the error (ie set msg=NULL)
returns 'code' so that it can be used like:
return datetime_error (-1, "bad date");
char *
:+ datetime_get_error_msg()
returns pointer to static error msg (which is NULL if no error)
int
:+ datetime_get_error_code()
returns error code
void
:+ datetime_clear_error()
clears error code and message
(*) Type:
int
:+ datetime_set_type (DateTime *dt; int mode, from, to, fracsec)
This routine must be called before any use of dt can be made
with other datetime functions
initialize all the elements in dt.
Set all values to zero except
tz (set to illegal value - 99*24)
positive (set to 1 for positive)
Set the type info in dt: mode, from, to, fracsec
validate the mode/from/to/fracsec (according to the rules for the mode)
ie. return the return value from datetime_check_type(dt)
void
:+ datetime_get_type (DateTime *dt; int *mode, *from, *to, *fracsec)
extract the mode, from, to, and fracsec out of dt.
int
:+ datetime_check_type (DateTime *dt)
checks the mode/from/to/fracsec in dt.
returns:
0: OK
-1: mode is invalid - not one of {ABSOLUTE,RELATIVE}
-2: from is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
-3: to is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
-4: from/to are reversed (from>to is illegal)
-5: invalid from/to combination for RELATIVE mode
from in {YEAR,MONTH} but to is not, or
from in {DAY,HOUR,MINUTE,SECOND} but to is not
-6: from is invalid for ABSOLUTE mode (from != YEAR is illegal)
-7: fracsec is negative (only if to==SECOND)
int
:+ datetime_is_valid_type (DateTime *dt)
returns 1 if datetime_check_type() returns 0
0 if not.
int
:+ datetime_change_from_to (DateTime *dt; int from, to; int round)
change the from/to of the type for dt.
'dt' must be legal
The 'from/to' must be legal values for the mode of dt;
(if they are not legal, then the original values are preserved,
dt is not changed).
returns 0 OK, -1 invalid 'dt', -2 invalid 'from/to'
round = negative implies floor() [decrease magnitude]
0 implies normal rounding, [incr/decr magnitude]
positive implies ceil() [increase magnitude]
Rounding up should be implemented using datetime_increment().
If dt.mode is ABSOLUTE, then 'from' must be YEAR.
If dt.from < 'from' (losing "lower" elements), convert the
"lost" values to the equivalent value for the new 'from'
Lost elements are then set to zero.
(This case can only occur for dt.mode relative):
months += lost years * 12 ; years = 0
hours += lost days * 24 ; days = 0
minutes += lost hours * 60 ; hours = 0
seconds += lost minutes * 60.0 ; minutes = 0
If dt.from > 'from' (adding "lower" elements), the new elements
are set to zero.
If dt.to < 'to' (adding "higher" elements), the new elements
are set to zero.
If dt.to > 'to' (losing "higher" elements), the
the new 'to' is adjusted according to the value for 'round'
After rounding the "lost" elements are set to zero.
if 'round' < 0, then no change is made to the lower elements
if 'round' > 0 and if the higher elements are not all zero then
the new 'to' element is incremented by by creating a
relative DateTime 'incr' with from='to' and to='to'
and value 1 then calling datetime_increment(dt,incr)
if round == 0, then if the doubling all higher elements
would cause a carry to occur, then the new 'to'
element is incremented, as follows
create a DateTime will all higher elements
equal to their corresponding elements in 'dt'
and then calling datetime_increment(dt,incr)
int
:+ datetime_is_absolute (DateTime *dt)
Returns
1 if dt.mode is absolute
0 if not (even if dt.mode is not defined)
int
:+ datetime_is_relative (DateTime *dt)
Returns
1 if dt.mode is relative
0 if not (even if dt.mode is not defined)
(*) Copy
void
:+ datetime_copy (DateTime *dst, *src)
This function copies the 'src' to the 'dst' by
dst "=" src :
dst.from = src.from
dst.to = src.to
...
(*) Same
int
:+ datetime_is_same (DateTime *dt1, *dt2)
Returns
1 if dt1 is exactly the same as dt2
0 if they differ
(*) Ascii
The ascii representation of DateTime is
ABSOLUTE: 15 Jan 1994 [bc] 10:35:23.456 -0500
RELATIVE: [-] 2 years 5 months
[-] 100 days 15 hours 25 minutes 35.34 seconds
The parts can be missing.
ABSOLUTE: 1994 [bc]
Jan 1994 [bc]
15 jan 1000 [bc]
15 jan 1994 [bc] 10 [+0000]
15 jan 1994 [bc] 10:00 [+0100]
15 jan 1994 [bc] 10:00:23.34 [-0500]
RELATIVE: [-] 2 years
[-] 5 months
[-] 2 years 5 months
[-] 100 days
[-] 15 hours 25 minutes 35.34 seconds
[-] 100 days 25 minutes
[-] 1000 hours 35.34 seconds
etc.
NOTE: values missing between the from/to are assumed to be zero;
when scanning, they can be missing; when formatting they will
appear as 0 (to preserve the from/to):
1000 hours 0 minutes 35.34 seconds
0 days 10 hours 0 minutes
NOTE: when scanning the from/to are determined by the fields
present. Compare:
10 hours 0 minutes 35.34 seconds [from=HOUR,to=SECOND]
and
0 days 10 hours 0 minutes 35.34 seconds [from=DAY,to=SECOND]
int
:+ datetime_scan (DateTime *dt, char *string)
Convert the ascii string into a DateTime
This determines the mode/from/to based on the string,
inits 'dt' and then sets values in 'dt' based on the
'string'
Returns 0 if 'string' is legal, -1 if not.
void
:+ datetime_format (DateTime *dt, char *string)
Convert 'dt' to a printable string. 'string' should
be large enough to hold the result, perhaps 80 bytes?
(*) Values
These routines get/set elements of 'dt'. They return
0 if OK
-1 if the value being gotten or set is not a legal value
-2 if the from/to for 'dt' doesn't include this value
Values don't get set if they are invalid.
int
:+ datetime_check_year (DateTime *dt, int year)
Returns
0 is legal year for dt
-1 illegal year for this dt
-2 dt has no year component
int
:+ datetime_set_year (DateTime *dt, int year)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
int
:+ datetime_get_year (DateTime *dt, int *year)
int
:+ datetime_check_month (DateTime *dt, int month)
Returns
0 is legal month for dt
-1 illegal month for this dt
-2 dt has no month component
int
:+ datetime_set_month (DateTime *dt, int month)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
int
:+ datetime_get_month (DateTime *dt, int *month)
int
:+ datetime_check_day (DateTime *dt, int day)
Returns
0 is legal day for dt
-1 illegal day for this dt
-2 dt has no day component
Note: if dt.mode is ABSOLUTE, then dt.year and dt.month
must also be legal, since the 'day' must be a legal
value for the dt.year/dt.month
int
:+ datetime_set_day (DateTime *dt, int day)
if dt.mode = ABSOLUTE, then
'day' must be less than or equal to the number of days in
the dt.year,dt.month:
if (day > datetime_days_in_month (dt.year, dt.month))
{error}
This implies that year/month must be set before days for
ABSOLUTE datetimes.
int
:+ datetime_get_day (DateTime *dt, int *day)
int
:+ datetime_check_hour (DateTime *dt, int hour)
int
:+ datetime_set_hour (DateTime *dt, int hour)
int
:+ datetime_get_hour (DateTime *dt, int *hour)
int
:+ datetime_check_minute (DateTime *dt, int minute)
int
:+ datetime_set_minute (DateTime *dt, int minute)
int
:+ datetime_get_minute (DateTime *dt, int *minute)
int
:+ datetime_check_second (DateTime *dt, double second)
int
:+ datetime_set_second (DateTime *dt, double second)
int
:+ datetime_get_second (DateTime *dt, double *second)
int
:+ datetime_check_fracsec (DateTime *dt, int fracsec)
int
:+ datetime_set_fracsec (DateTime *dt, int fracsec)
int
:+ datetime_get_fracsec (DateTime *dt, int *fracsec)
(*) Arithmetic
These functions perform addition/subtraction on datetimes.
int
: datetime_increment (DateTime *src, *incr)
this function changes the 'src' date/time data based on the 'incr'
The type (mode/from/to) of the 'src' can be anything.
The mode of the 'incr' must be RELATIVE,
and the type (mode/from/to) for 'incr' must be a valid increment
for 'src'. See datetime_is_valid_increment(), datetime_check_increment()
returns
0: OK
-1: 'incr' is invalid increment for 'src'
-2: 'src' is ABSOLUTE, 'incr' is a {DAY-SECOND} interval
and 'src' is a date for which this function
has not been implemented. NOTE: this should only happen
if the total number of days in the 'src' would exceed 28
since this would then cause date arithmetic to kick in
to figure out if these are too many days for the month.
## BROWN - identify which dates you know how to handle, and which you do not.
For src.mode ABSOLUTE,
positive 'incr' moves into the future,
negative 'incr' moves into the past.
BC implies the year is negative, but all else is positive.
Suppose the date is 10jan100bc.
To add 1 year would decrease the year to 99 (10jan99bc).
To increase the day by 1 would set the day to 11 (11jan100bc).
To increase the month by 1 would change the month from 1 (jan)
to 2 (feb) (10feb100bc)
Also, year==0 is illegal: adding 1 year to 1[bc] gives 1[ad]
For src.mode RELATIVE
Incrementing or decrementing must consider the 'src' and 'incr'
as single values (+ or -) and work with this value.
For example, suppose
A = -4 days, 5 hours, 15 minutes
B = 25 hours
then
A = -(4*24*60 +5*60+15) = -6075
B = (25*60) = 1500
A + B = -4575 = -(3*24*60 + 4*60 + 15)
A = -3 days, 4 hours, 15 minutes
The 'fracsec' in 'src' is preserved.
The 'from/to' of the 'src' is preserved.
A timezone in 'src' is allowed - it's presence is ignored.
NOTE: There is no datetime_decrement()
To decrement, set the 'incr' negative.
void
:+ datetime_set_positive (DateTime *dt)
void
:+ datetime_set_negative (DateTime *dt)
void
:+ datetime_invert_sign (DateTime *dt)
int
:+ datetime_is_positive (DateTime *dt)
these control the "sign" of the datetime value.
int
: datetime_difference (DateTime *a, *b, *result)
This performs the formula: result = a - b;
both a and b must be absolute.
result will be relative
If a is "earlier" than b, then result should be set negative.
b must be no more "precise" than a.
(a copy of b is "extended" to the precision of a)
datetime_copy (tb, b)
datetime_reset_from_to (tb, b.from, a.to, a.fracsec))
If result.to == SECOND, then result.fracsec is a.fracsec
result will have the following from/to based on a.to:
result
a.to from to
YEAR YEAR YEAR
MONTH YEAR MONTH
DAY DAY DAY
HOUR DAY HOUR
MINUTE DAY MINUTE
SECOND DAY SECOND
If either 'a' or 'b' has a timezone, both must have a timezone.
The difference will account for the differences in the time zones.
int
:+ datetime_is_valid_increment (DateTime *src, *incr)
Returns
datetime_check_increment(src, incr) == 0
int
:+ datetime_check_increment (DateTime *src, *incr)
This checks if the type of 'incr' is valid for
incrementing/decrementing 'src'.
Returns:
1 src is not a legal DateTime, error code/msg
are those set by datetime_is_valid_type()
2 incr is not a legal DateTime, error code/msg
are those set by datetime_is_valid_type()
-1 incr.mode not relative
-2 incr more precise that src
-3 illegal incr, must be YEAR-MONTH
-4 illegal incr, must be DAY-SECOND
The type (mode/from/to) of the 'src' can be anything.
The incr.mode must be RELATIVE
(return -1 if not)
incr.to is restricted based on the src.to:
incr.to <= src.to (incr not more precise than src)
(return -2 if not)
if src.to is in {YEAR,MONTH} then
incr.to must be in {YEAR,MONTH}
(return -3 if not)
if src.to is in {DAY,HOUR,MINUTE,SECOND} then
incr.to must be in {DAY,HOUR,MINUTE,SECOND}
(return -4 if not)
note: it is ok for incr.from > src.from
(which can only happen for src.mode RELATIVE).
A timezone in 'src' is allowed - it's presence is ignored.
To aid in setting the 'incr' type, the following routine can be used
int
:+ datetime_get_increment_type (DateTime *src; int *mode, *from, *to, *fracsec)
this returns the components of a type (mode/from/to/fracsec) that
can be used to construct a DateTime object that can be used
to increment the 'src'. See datetime_set_increment_type().
returns 0 dt is legal
!=0 why dt is illegal
Implemented as follows:
*mode = RELATIVE
*to = src.to
*fracsec = src.fracsec
if src.mode is ABSOLUTE
if src.to is in {YEAR,MONTH} then
*from = YEAR
if src.to is in {DAY,HOUR,MINUTE,SECOND} then
*from = DAY
if src.mode is RELATIVE, then
*from = src.from
int
:+ datetime_set_increment_type (DateTime *src, *incr)
src must be legal
This is a convenience routine which is implemented as follows:
int mode, from ,to;
int fracsec;
if(datetime_get_increment_type (src, &mode, &from, &to, &fracsec))
return datetime_error_cde();
return datetime_set_type (incr, mode, from, to, fracsec);
(*) Misc
int
:+ datetime_days_in_month (int month, year)
int
:+ datetime_is_leap_year (int year)
int
:+ datetime_days_in_year (int year)
(*) Timezone
int
:+ datetime_is_valid_timezone (int minutes)
returns:
1 OK: -720 <= minutes <= 780 (720 = 12 hours; 780 = 13 hours)
0 NOT OK
int
:+ datetime_check_timezone (DateTime *dt, int minutes)
'dt' must be mode ABSOLUTE and dt.to must be one of {MINUTE,SECOND}
minutes must be a valid timezone offset: ie in the range [-720,+780]
return
0: OK
-1: mode not ABSOLUTE
-2: dt.to not in {MINUTE,SECOND}
-3: minutes not valid - not in the range [-720,+780]
int
:+ datetime_set_timezone (DateTime *dt, int minutes)
if (datetime_check_timezone(dt,minutes)==0)
set dt.tz=minutes
'dt' must be mode ABSOLUTE and dt.to must be one of {MINUTE,SECOND}
minutes must be a valid timezone offset: ie in the range [-720,+780]
return
0: OK
-1: mode not ABSOLUTE
-2: dt.to not in {MINUTE,SECOND}
-3: minutes not valid - not in the range [-720,+780]
int
:+ datetime_get_timezone (DateTime *dt, int *minutes)
return
0: OK - if 'dt' has a timezone and sets 'minutes'
-1: mode not ABSOLUTE
-2: dt.to not in {MINUTE,SECOND}
-3: minutes not valid - not in the range [-720,+780]
void
:+ datetime_unset_timezone (DateTime *dt)
Remove timezone from 'dt'
dt.tz = 99*60 (some illegal value)
int
:+ datetime_change_timezone (DateTime *dt; int minutes)
if dt has a timezone,
increment dt by minutes-dt.tz MINUTES
set dt.tz = minutes
returns 0 OK, datetime_check_timezone(dt) if not,
or -4 if minues invalid
int
:+ datetime_change_to_utc (DateTime *dt)
return datetime_change_timezone (dt, 0);
void
:+ datetime_decompose_timezone (int tz, int *hour, int *minute)
tz = abs(tz)
*hour = tz/60
*minute = tz%60
note: hour,minute are non-negative. Must look at sign of tz
itself to see if the tz is negative offset or not. This routine
would be used to format tz for output. For example if tz=-350
this would be hour=5 minute=50, but negative. Output might econde
this as -0550: printf ("%s%02d%02d", tz<0?"-":"", hour, minute)
(*) A function that returns the timezone for the local system??
int
:+ datetime_get_local_timezone (int *minutes)
returns 0 OK, -1 local timezone info not available
(*) Local time
void
:+ datetime_get_local_time (DateTime *dt)
set mode/from/to ABSOLUTE/YEAR/SECOND
set the local time into 'dt'
does not set timezone.
(*) GRASS will probably have to have a function that returns the
timezone of the database:
### BROWN
## so a database only has one timezone? Or can each map have its own?
### SHAPIRO
## good question. A location may span more than one time zone. Also timezones
## are not nice shapes. We probably need a function that returns a timezone
## for a specified latlon/longitude and/or for a region. How would this
## function deal with daylight savings time?
## The timezone(s) might have to be represented as a vector map
## I suggest we hold off on this and add it later.
G_get_database_timezone (int *tz)
tz in minutes as defined for datetime library
returns
0 database has no timezone
1 database has a timezone - returned in tz
-1 error reading timezone file.
|