NCBI C++ ToolKit
Date_std.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: Date_std.cpp 66776 2015-03-24 16:16:20Z gotvyans $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Aaron Ucko, NCBI
27  *
28  * File Description:
29  * Useful member functions for standard dates: comparison and formatting
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using specifications from the ASN data definition file
34  * 'general.asn'.
35  *
36  */
37 
38 // standard includes
39 
40 // generated includes
41 #include <ncbi_pch.hpp>
44 
45 #include <vector>
46 
47 // generated classes
48 
50 
51 BEGIN_objects_SCOPE // namespace ncbi::objects::
52 
53 // destructor
55 {
56 }
57 
58 
60 {
61  switch (prec) {
63  SetSecond(time.Second());
64  SetMinute(time.Minute());
65  SetHour (time.Hour());
66  // fall through
68  SetDay (time.Day());
69  SetMonth (time.Month());
70  SetYear (time.Year());
71  break;
72  default:
73  break;
74  }
75 }
76 
77 
79 {
80  return CTime(GetYear(),
81  CanGetMonth() ? GetMonth() : 1,
82  CanGetDay() ? GetDay() : 1,
83  CanGetHour() ? GetHour() : 0,
84  CanGetMinute() ? GetMinute() : 0,
85  CanGetSecond() ? GetSecond() : 0,
86  0, // nanoseconds, not supported by CDate_std.
87  tz);
88 }
89 
90 
92 {
93  if (GetYear() < date.GetYear()) {
95  } else if (GetYear() > date.GetYear()) {
96  return CDate::eCompare_after;
97  }
98 
99  if ((CanGetSeason() || date.CanGetSeason())
100  && ( !CanGetSeason() || !date.CanGetSeason()
101  || GetSeason() != date.GetSeason())) {
103  }
104 
105  if (CanGetMonth() || date.CanGetMonth()) {
106  if ( !CanGetMonth() || !date.CanGetMonth()) {
108  } else if (GetMonth() < date.GetMonth()) {
109  return CDate::eCompare_before;
110  } else if (GetMonth() > date.GetMonth()) {
111  return CDate::eCompare_after;
112  }
113  }
114 
115  if (CanGetDay() || date.CanGetDay()) {
116  if ( !CanGetDay() || !date.CanGetDay()) {
118  } else if (GetDay() < date.GetDay()) {
119  return CDate::eCompare_before;
120  } else if (GetDay() > date.GetDay()) {
121  return CDate::eCompare_after;
122  }
123  }
124 
125  if (CanGetHour() || date.CanGetHour()) {
126  if ( !CanGetHour() || !date.CanGetHour()) {
128  } else if (GetHour() < date.GetHour()) {
129  return CDate::eCompare_before;
130  } else if (GetHour() > date.GetHour()) {
131  return CDate::eCompare_after;
132  }
133  }
134 
135  if (CanGetMinute() || date.CanGetMinute()) {
136  if ( !CanGetMinute() || !date.CanGetMinute()) {
138  } else if (GetMinute() < date.GetMinute()) {
139  return CDate::eCompare_before;
140  } else if (GetMinute() > date.GetMinute()) {
141  return CDate::eCompare_after;
142  }
143  }
144 
145  if (CanGetSecond() || date.CanGetSecond()) {
146  if ( !CanGetSecond() || !date.CanGetSecond()) {
148  } else if (GetSecond() < date.GetSecond()) {
149  return CDate::eCompare_before;
150  } else if (GetSecond() > date.GetSecond()) {
151  return CDate::eCompare_after;
152  }
153  }
154 
155  return CDate::eCompare_same;
156 }
157 
158 
159 void CDate_std::GetDate(string* label, const string& format) const
160 {
161  static const char* const kMonths[] = {
162  "0", "January", "February", "March", "April", "May", "June",
163  "July", "August", "September", "October", "November", "December"
164  };
165  static const int kNumMonths = sizeof (kMonths) / sizeof (char*);
166 
167  if (!label) {
168  return;
169  }
170  unsigned int depth = 0;
171  vector<pair<SIZE_TYPE, SIZE_TYPE> > starts;
172  starts.push_back(make_pair(label->size(), (SIZE_TYPE)0));
173  ITERATE (string, it, format) {
174  if (*it != '%') {
175  *label += *it;
176  continue;
177  }
178  if (++it == format.end()) {
180  "CDate_std::GetDate(): incomplete % expression",
181  it - format.begin());
182  }
183  // Check for things that can only immediately follow %
184  if (*it == '%') {
185  *label += '%';
186  continue;
187  }
188  else if (*it == '{') {
189  depth++;
190  starts.push_back(make_pair(label->size(),
191  SIZE_TYPE(it - format.begin())));
192  continue;
193  } else if (*it == '}') {
194  if (depth == 0) {
196  "CDate_std::GetDate(): unbalanced %}",
197  it - format.begin());
198  }
199  depth--;
200  starts.pop_back();
201  continue;
202  } else if (*it == '|') {
203  // We survived, so just look for the appropriate %}.
204  if (depth == 0) {
205  return; // Can ignore rest of format
206  }
207  unsigned int depth2 = 0;
208  for (;;) {
209  while (++it != format.end() && *it != '%')
210  ;
211  if (it == format.end() || ++it == format.end()) {
213  "CDate_std::GetDate(): unbalanced %{",
214  starts.back().second);
215  }
216  if (*it == '}') {
217  if (depth2 == 0) {
218  depth--;
219  starts.pop_back();
220  break;
221  } else {
222  depth2--;
223  }
224  } else if (*it == '{') {
225  depth2++;
226  }
227  }
228  continue;
229  }
230 
231  unsigned int length = 0;
232  int value = -1;
233  while (isdigit((unsigned char)(*it))) {
234  length = length * 10 + *it - '0';
235  if (++it == format.end()) {
237  "CDate_std::GetDate(): incomplete % expression",
238  it - format.begin());
239  }
240  }
241  switch (*it) {
242  case 'Y': value = GetYear(); break;
243  case 'M':
244  case 'N': value = CanGetMonth() ? GetMonth() : -1; break;
245  case 'D': value = CanGetDay() ? GetDay() : -1; break;
246  case 'S': value = CanGetSeason() ? 1 : -1; break;
247  case 'h': value = CanGetHour() ? GetHour() : -1; break;
248  case 'm': value = CanGetMinute() ? GetMinute() : -1; break;
249  case 's': value = CanGetSecond() ? GetSecond() : -1; break;
250  default:
252  "CDate_std::GetDate(): unrecognized format specifier",
253  it - format.begin());
254  }
255 
256  if (value >= 0) {
257  if (*it == 'N') { // special cases
258  const char* name;
259  if (value >= kNumMonths) {
260  name = "inv";
261  } else {
262  name = kMonths[value];
263  }
264  if (length > 0) {
265  label->append(name, length);
266  } else {
267  *label += name;
268  }
269  } else if (*it == 'S') {
270  if (length > 0) {
271  label->append(GetSeason(), 0, length);
272  } else {
273  *label += GetSeason();
274  }
275  } else { // just a number
276  // We want exactly <length> digits.
277  string s;
279  size_t zeropad = length < s.length() ? 0 : length-s.length();
280  size_t start = (length > 0 && length < s.length() ) ? s.size() - length : 0;
281  if (zeropad>0)
282  label->append(zeropad, '0');
283  label->append(s, start, s.size() - start);
284  }
285  } else {
286  // missing...roll back label and look for alternatives, or
287  // throw if at top level and none found
288  label->erase(starts.back().first);
289  char request = *it;
290  unsigned int depth2 = 0;
291  for (;;) {
292  while (++it != format.end() && *it != '%')
293  ;
294  if (it == format.end() || ++it == format.end()) {
295  if (depth > 0 || depth2 > 0) {
297  "CDate_std::GetDate(): unbalanced %{",
298  starts.back().second);
299  } else {
301  string("CDate_std::GetDate():"
302  " missing required field %")
303  + request, it - format.begin() - 1);
304  }
305  }
306  if (*it == '|' && depth2 == 0) {
307  break;
308  } else if (*it == '}') {
309  if (depth2 == 0) {
310  if (depth == 0) {
312  "CDate_std::GetDate(): unbalanced %}",
313  it - format.begin());
314  }
315  depth--;
316  starts.pop_back();
317  break;
318  } else {
319  depth2--;
320  }
321  } else if (*it == '{') {
322  depth2++;
323  }
324  }
325  }
326  }
327 }
328 
329 
330 END_objects_SCOPE // namespace ncbi::objects::
331 
333 
334 /* Original file checksum: lines: 61, chars: 1885, CRC32: 4ef42d28 */
CDate::ECompare Compare(const CDate_std &date) const
Indicate how *this relates to another date.
Definition: Date_std.cpp:91
void SetToTime(const CTime &time, CDate::EPrecision prec=CDate::ePrecision_second)
Definition: Date_std.cpp:59
CTime AsCTime(CTime::ETimeZone tz=CTime::eLocal) const
Definition: Date_std.cpp:78
void GetDate(string *label, const string &format) const
Append a custom string representation of the date to the label.
Definition: Date_std.cpp:159
~CDate_std(void)
Definition: Date_std.cpp:54
EPrecision
for conversion from CTime
Definition: Date.hpp:57
@ ePrecision_second
Definition: Date.hpp:59
@ ePrecision_day
Definition: Date.hpp:58
ECompare
How *this relates to another date.
Definition: Date.hpp:73
@ eCompare_before
*this comes first.
Definition: Date.hpp:74
@ eCompare_same
They're equivalent.
Definition: Date.hpp:75
@ eCompare_after
*this comes second.
Definition: Date.hpp:76
@ eCompare_unknown
Comparison is impossible.
Definition: Date.hpp:77
CTime –.
Definition: ncbitime.hpp:296
static unsigned char depth[2 *(256+1+29)+1]
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NCBI_THROW2(exception_class, err_code, message, extra)
Throw exception with extra parameter.
Definition: ncbiexpt.hpp:1754
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
int Minute(void) const
Get minute.
Definition: ncbitime.hpp:2277
ETimeZone
Which initial value to use for timezone.
Definition: ncbitime.hpp:305
int Year(void) const
Get year.
Definition: ncbitime.hpp:2265
int Day(void) const
Get day.
Definition: ncbitime.hpp:2271
int Hour(void) const
Get hour.
Definition: ncbitime.hpp:2274
int Second(void) const
Get second.
Definition: ncbitime.hpp:2280
int Month(void) const
Get month.
Definition: ncbitime.hpp:2268
static const char label[]
TMinute GetMinute(void) const
Get the Minute member data.
Definition: Date_std_.hpp:661
TMinute & SetMinute(void)
Assign a value to Minute data member.
Definition: Date_std_.hpp:677
TSecond GetSecond(void) const
Get the Second member data.
Definition: Date_std_.hpp:708
TMonth & SetMonth(void)
Assign a value to Month data member.
Definition: Date_std_.hpp:489
const TSeason & GetSeason(void) const
Get the Season member data.
Definition: Date_std_.hpp:560
THour GetHour(void) const
Get the Hour member data.
Definition: Date_std_.hpp:614
bool CanGetHour(void) const
Check if it is safe to call GetHour method.
Definition: Date_std_.hpp:601
bool CanGetDay(void) const
Check if it is safe to call GetDay method.
Definition: Date_std_.hpp:507
bool CanGetSecond(void) const
Check if it is safe to call GetSecond method.
Definition: Date_std_.hpp:695
bool CanGetSeason(void) const
Check if it is safe to call GetSeason method.
Definition: Date_std_.hpp:554
bool CanGetMinute(void) const
Check if it is safe to call GetMinute method.
Definition: Date_std_.hpp:648
TYear & SetYear(void)
Assign a value to Year data member.
Definition: Date_std_.hpp:442
TYear GetYear(void) const
Get the Year member data.
Definition: Date_std_.hpp:426
TMonth GetMonth(void) const
Get the Month member data.
Definition: Date_std_.hpp:473
TDay & SetDay(void)
Assign a value to Day data member.
Definition: Date_std_.hpp:536
TDay GetDay(void) const
Get the Day member data.
Definition: Date_std_.hpp:520
TSecond & SetSecond(void)
Assign a value to Second data member.
Definition: Date_std_.hpp:724
THour & SetHour(void)
Assign a value to Hour data member.
Definition: Date_std_.hpp:630
bool CanGetMonth(void) const
Check if it is safe to call GetMonth method.
Definition: Date_std_.hpp:460
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int isdigit(Uchar c)
Definition: ncbictype.hpp:64
static Format format
Definition: njn_ioutil.cpp:53
Modified on Sun May 19 04:48:16 2024 by modify_doxy.py rev. 669887