Scid  4.6.5
date.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////
2 //
3 // FILE: date.h
4 // Date format and inline 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 (sgh@users.sourceforge.net)
12 //
13 //////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef SCID_DATE_H
17 #define SCID_DATE_H
18 
19 #include "common.h"
20 #include <cstdlib>
21 
22 // DATE STORAGE FORMAT:
23 // In memory, dates are stored in a 32-bit (4-byte) uint, of which only
24 // the lowest 3 bytes need be used, with the lowest 5 bits for the
25 // day, the next highest 4 bits for the month and the highest bits for
26 // the year. This makes date comparisons easy: a bigger date value is
27 // a more recent date. If a field is unknown, its value is set to zero.
28 // On disk, the date is stored in 3 bytes.
29 
30 
31 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 // CONSTANTS and MACROS:
33 
34 const dateT ZERO_DATE = 0;
35 
36 const uint YEAR_SHIFT = 9;
37 const uint MONTH_SHIFT = 5;
38 const uint DAY_SHIFT = 0;
39 
40 // DAY (31 days) 5 bits (32) , MONTH (12 months) 4 bits (16)
41 
42 const uint YEAR_MAX = 2047; // 2^11 - 1
43 
44 #define DATE_MAKE(y,m,d) (((y) << YEAR_SHIFT) | ((m) << MONTH_SHIFT) | (d))
45 
46 
47 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 // PUBLIC FUNCTIONS
49 bool date_ValidString (const char * str);
50 
51 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52 // date_GetYear():
53 // Get the year from a date.
54 inline uint
56 {
57  return (uint) (date >> YEAR_SHIFT);
58 }
59 
60 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 // date_GetMonth():
62 // Get the month from a date.
63 inline uint
65 {
66  return (uint) ((date >> MONTH_SHIFT) & 15);
67 }
68 
69 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 //### date_GetDay():
71 // Get the day of the month from a date.
72 inline uint
74 {
75  return (uint) (date & 31);
76 }
77 
78 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 //### date_GetMonthDay():
80 // Get the month and day; used to check for year-only dates. S.A
81 inline uint
83 {
84  return (uint) (date & 511);
85 }
86 
87 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88 // date_DecodeToString(): convert date to PGN tag string.
89 inline void
91 {
92  ASSERT(str != NULL);
93  uint year, month, day;
94 
95  year = date_GetYear (date);
96  month = date_GetMonth (date);
97  day = date_GetDay (date);
98 
99  if (year == 0) {
100  *str++ = '?'; *str++ = '?'; *str++ = '?'; *str++ = '?';
101  } else {
102  *str++ = '0' + (year / 1000);
103  *str++ = '0' + (year % 1000) / 100;
104  *str++ = '0' + (year % 100) / 10;
105  *str++ = '0' + (year % 10);
106  }
107  *str++ = '.';
108 
109  if (month == 0) {
110  *str++ = '?'; *str++ = '?';
111  } else {
112  *str++ = '0' + (month / 10);
113  *str++ = '0' + (month % 10);
114  }
115  *str++ = '.';
116 
117  if (day == 0) {
118  *str++ = '?'; *str++ = '?';
119  } else {
120  *str++ = '0' + (day / 10);
121  *str++ = '0' + (day % 10);
122  }
123  *str = 0;
124 }
125 
126 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127 // date_EncodeFromString(): convert PGN tag string to date.
128 // The date string format is: "yyyy.mm.dd".
129 inline dateT
130 date_EncodeFromString (const char * str)
131 {
132  // Do checks on str's validity as a date string:
133  ASSERT(str != NULL);
134 
135  dateT date;
136  uint year, month, day;
137 
138  // convert year:
139  year = std::strtoul(str, NULL, 10);
140  if (year > YEAR_MAX) { year = 0; }
141  date = year << YEAR_SHIFT;
142  while (*str != 0 && *str != '.') { str++; }
143  if (*str == '.') { str++; }
144 
145  // convert month:
146  month = std::strtoul(str, NULL, 10);
147  if (month > 12) { return date; }
148  date |= (month << MONTH_SHIFT);
149  while (*str != 0 && *str != '.') { str++; }
150  if (*str == '.') { str++; }
151 
152  // convert day:
153  day = std::strtoul(str, NULL, 10);
154  if (day > 31) { return date; }
155  date |= (day << DAY_SHIFT);
156 
157  return date;
158 }
159 
160 #endif // #ifndef SCID_DATE_H
161 
162 //////////////////////////////////////////////////////////////////////
163 // EOF: date.h
164 //////////////////////////////////////////////////////////////////////
165 
uint date_GetYear(dateT date)
Definition: date.h:55
const uint YEAR_MAX
Definition: date.h:42
const uint MONTH_SHIFT
Definition: date.h:37
const uint YEAR_SHIFT
Definition: date.h:36
uint dateT
Definition: common.h:155
#define ASSERT(f)
Definition: common.h:67
void date_DecodeToString(dateT date, char *str)
Definition: date.h:90
const dateT ZERO_DATE
Definition: date.h:34
uint32_t uint
Definition: common.h:99
const uint DAY_SHIFT
Definition: date.h:38
bool date_ValidString(const char *str)
Definition: date.cpp:21
uint date_GetMonthDay(dateT date)
Definition: date.h:82
uint date_GetMonth(dateT date)
Definition: date.h:64
dateT date_EncodeFromString(const char *str)
Definition: date.h:130
uint date_GetDay(dateT date)
Definition: date.h:73