~diesch/ubuntu/trusty/scid/fix-for-1277520

« back to all changes in this revision

Viewing changes to src/date.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Peter van Rossum
  • Date: 2002-04-06 14:49:15 UTC
  • Revision ID: james.westby@ubuntu.com-20020406144915-63pe79by11n28esy
Tags: upstream-3.2
ImportĀ upstreamĀ versionĀ 3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////
 
2
//
 
3
//  FILE:       date.cc
 
4
//              Date functions.
 
5
//
 
6
//  Part of:    Scid (Shane's Chess Information Database)
 
7
//  Version:    1.9
 
8
//
 
9
//  Notice:     Copyright (c) 1999  Shane Hudson.  All rights reserved.
 
10
//
 
11
//  Author:     Shane Hudson (shane@cosc.canterbury.ac.nz)
 
12
//
 
13
//////////////////////////////////////////////////////////////////////
 
14
 
 
15
 
 
16
#include "common.h"
 
17
#include "date.h"
 
18
#include "misc.h"
 
19
 
 
20
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
21
// date_DecodeToString(): convert date to PGN tag string.
 
22
void
 
23
date_DecodeToString (dateT date, char * str)
 
24
{
 
25
    ASSERT(str != NULL);
 
26
    uint year, month, day;
 
27
 
 
28
    year = date_GetYear (date);
 
29
    month = date_GetMonth (date);
 
30
    day = date_GetDay (date);
 
31
 
 
32
    if (year == 0) {
 
33
        *str++ = '?'; *str++ = '?'; *str++ = '?'; *str++ = '?';
 
34
    } else {
 
35
        *str++ = '0' + (year / 1000);
 
36
        *str++ = '0' + (year % 1000) / 100;
 
37
        *str++ = '0' + (year % 100) / 10;
 
38
        *str++ = '0' + (year % 10);
 
39
    }
 
40
    *str++ = '.';
 
41
 
 
42
    if (month == 0) {
 
43
        *str++ = '?'; *str++ = '?';
 
44
    } else {
 
45
        *str++ = '0' + (month / 10);
 
46
        *str++ = '0' + (month % 10);
 
47
    }
 
48
    *str++ = '.';
 
49
 
 
50
    if (day == 0) {
 
51
        *str++ = '?'; *str++ = '?';
 
52
    } else {
 
53
        *str++ = '0' + (day / 10);
 
54
        *str++ = '0' + (day % 10);
 
55
    }
 
56
    *str = 0;
 
57
}
 
58
 
 
59
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
60
// date_EncodeFromString(): convert PGN tag string to date.
 
61
//      The date string format is: "yyyy.mm.dd".
 
62
dateT
 
63
date_EncodeFromString (const char * str)
 
64
{
 
65
    // Do checks on str's validity as a date string:
 
66
    ASSERT(str != NULL);
 
67
 
 
68
    dateT date;
 
69
    uint year, month, day;
 
70
 
 
71
    // convert year:
 
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++; }
 
77
 
 
78
    // convert month:
 
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++; }
 
84
 
 
85
    // convert day:
 
86
    day = strGetUnsigned (str);
 
87
    if (day > 31) { return date; }
 
88
    date |= (day << DAY_SHIFT);
 
89
 
 
90
    return date;
 
91
}
 
92
 
 
93
bool
 
94
date_ValidString (const char * str)
 
95
{
 
96
    uint maxValues[3] = { YEAR_MAX, 12, 31 };
 
97
 
 
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 != '.') {
 
105
            char ch = *str;
 
106
            if (ch >= '0'  &&  ch <= '9') {
 
107
                seenDigit = true;
 
108
            } else if (ch == '?') {
 
109
                seenQuestion = true;
 
110
            } else {
 
111
                seenOther = true;
 
112
            }
 
113
            str++;
 
114
        }
 
115
        // Here, we should have seen question marks or digits, not both:
 
116
        if (seenOther) { return false; }
 
117
        if (seenQuestion  &&  seenDigit) { return false; }
 
118
        if (seenDigit) {
 
119
            // Check that the value is not too large:
 
120
            uint value = strGetUnsigned (start);
 
121
            if (value > maxValue) { return false; }
 
122
        }
 
123
        if (*str == 0) { return true; } else { str++; }
 
124
    }
 
125
    return false;
 
126
}
 
127
 
 
128
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
129
// date_AddMonths:
 
130
//      Returns the date incremented by the specified number of months.
 
131
//
 
132
dateT
 
133
date_AddMonths (dateT date, int numMonths)
 
134
{
 
135
    uint year = date_GetYear (date);
 
136
    uint month = date_GetMonth (date);
 
137
    uint day = date_GetDay (date);
 
138
 
 
139
    while (numMonths < 0) {
 
140
        if (month == 0  ||  month == 1) {
 
141
            year--;
 
142
            month = 12;
 
143
        } else {
 
144
            month--;
 
145
        }
 
146
        numMonths++;
 
147
    }
 
148
    while (numMonths > 0) {
 
149
        month++;
 
150
        if (month > 12) {
 
151
            year++;
 
152
            month = 1;
 
153
        }
 
154
        numMonths--;
 
155
    }
 
156
    return ((year << YEAR_SHIFT) | (month << MONTH_SHIFT) | day);
 
157
}
 
158
 
 
159
//////////////////////////////////////////////////////////////////////
 
160
//  EOF: date.cc
 
161
//////////////////////////////////////////////////////////////////////
 
162