1
//////////////////////////////////////////////////////////////////////
6
// Part of: Scid (Shane's Chess Information Database)
9
// Notice: Copyright (c) 1999 Shane Hudson. All rights reserved.
11
// Author: Shane Hudson (shane@cosc.canterbury.ac.nz)
13
//////////////////////////////////////////////////////////////////////
20
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
// date_DecodeToString(): convert date to PGN tag string.
23
date_DecodeToString (dateT date, char * str)
26
uint year, month, day;
28
year = date_GetYear (date);
29
month = date_GetMonth (date);
30
day = date_GetDay (date);
33
*str++ = '?'; *str++ = '?'; *str++ = '?'; *str++ = '?';
35
*str++ = '0' + (year / 1000);
36
*str++ = '0' + (year % 1000) / 100;
37
*str++ = '0' + (year % 100) / 10;
38
*str++ = '0' + (year % 10);
43
*str++ = '?'; *str++ = '?';
45
*str++ = '0' + (month / 10);
46
*str++ = '0' + (month % 10);
51
*str++ = '?'; *str++ = '?';
53
*str++ = '0' + (day / 10);
54
*str++ = '0' + (day % 10);
59
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60
// date_EncodeFromString(): convert PGN tag string to date.
61
// The date string format is: "yyyy.mm.dd".
63
date_EncodeFromString (const char * str)
65
// Do checks on str's validity as a date string:
69
uint year, month, day;
72
year = strGetUnsigned (str);
73
if (year > YEAR_MAX) { year = 0; }
74
date = year << YEAR_SHIFT;
75
while (*str != 0 && *str != '.') { str++; }
76
if (*str == '.') { str++; }
79
month = strGetUnsigned (str);
80
if (month > 12) { return date; }
81
date |= (month << MONTH_SHIFT);
82
while (*str != 0 && *str != '.') { str++; }
83
if (*str == '.') { str++; }
86
day = strGetUnsigned (str);
87
if (day > 31) { return date; }
88
date |= (day << DAY_SHIFT);
94
date_ValidString (const char * str)
96
uint maxValues[3] = { YEAR_MAX, 12, 31 };
98
// Check year, then month, then day:
99
for (uint i=0; i < 3; i++) {
100
uint maxValue = maxValues[i];
101
bool seenQuestion, seenDigit, seenOther;
102
seenQuestion = seenDigit = seenOther = false;
103
const char * start = str;
104
while (*str != 0 && *str != '.') {
106
if (ch >= '0' && ch <= '9') {
108
} else if (ch == '?') {
115
// Here, we should have seen question marks or digits, not both:
116
if (seenOther) { return false; }
117
if (seenQuestion && seenDigit) { return false; }
119
// Check that the value is not too large:
120
uint value = strGetUnsigned (start);
121
if (value > maxValue) { return false; }
123
if (*str == 0) { return true; } else { str++; }
128
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130
// Returns the date incremented by the specified number of months.
133
date_AddMonths (dateT date, int numMonths)
135
uint year = date_GetYear (date);
136
uint month = date_GetMonth (date);
137
uint day = date_GetDay (date);
139
while (numMonths < 0) {
140
if (month == 0 || month == 1) {
148
while (numMonths > 0) {
156
return ((year << YEAR_SHIFT) | (month << MONTH_SHIFT) | day);
159
//////////////////////////////////////////////////////////////////////
161
//////////////////////////////////////////////////////////////////////