OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dateio.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// Borland Class Library
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// TDate class IO and conversion implementation
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/date.h>
10#include <stdio.h>
11#include <ctype.h>
12
14#include <owl/profile.h>
15
16using namespace std;
17
18namespace owl {
19
20//
21//
22//
23TDate::HowToPrint TDate::PrintOption = TDate::Normal;
24//TDate::HowToPrint TDate::PrintOption = TDate::WinIntSection;
25
26//
27/// Converts the TDate object to a string object.
28//
30{
32 strtemp << (*this);
33 return strtemp.str();
34}
35
36//
37/// Sets the print option for all TDate objects and returns the old setting.
38//
40{
41 HowToPrint oldoption = PrintOption;
42 PrintOption = h;
43 return oldoption;
44}
45
46//
47// Skip any characters except alphanumeric characters
48//
49_OWLSTATICFUNC(static void) SkipDelim(tistream& strm )
50{
51 tchar c;
52 if( !strm.good() )
53 return;
54
55 do {
56 strm >> c;
57 } while (strm.good() && !(_istdigit(c)||_istalpha(c))) ; //_istalnum is not yet implemented in WineLib
58
59 if (strm.good())
60 strm.putback(c);
61}
62
63//
64// Parse the name of a month from input stream.
65//
66_OWLSTATICFUNC(static tchar*)
68{
69// static tchar month[12];
70 tchar* p = month;
71 tchar c;
72 SkipDelim(s);
73 s.get(c);
74 while (s.good() && _istalpha(c) && (p != &month[10])){
75 *p++ = c;
76 s.get(c);
77 }
78 if( s.good() )
79 s.putback(c);
80 *p = _T('\0');
81 return month;
82}
83
84//
85// Parse a date from the specified input stream.
86// The date must be in one of the following forms:
87// dd-mmm-yy, mm/dd/yy, or mmm dd,yy
88// e.g.: 10-MAR-86, 3/10/86, or March 10, 1986.
89// Any non-alphanumeric character may be used as a delimiter.
90//
91void TDate::ParseFrom(tistream& s )
92{
93 unsigned d,m,y;
94 tchar month[20];
95
96 if (s.good()){
97 SkipDelim(s);
98 s >> m; // try to parse day or month number
99 SkipDelim(s);
100 if (s.eof())
101 return;
102 if( s.fail()){ // parse <monthName><day><year>
103 s.clear();
104 m = IndexOfMonth(ParseMonth(s,month)); // parse month name
105 SkipDelim(s);
106 s >> d; // parse day
107 }
108 else{ // try to parse day number
109 s >> d;
110 if (s.eof())
111 return;
112 if (s.fail()){ // parse <day><monthName><year>
113 d = m;
114 s.clear();
115 m = IndexOfMonth(ParseMonth(s,month)); // parse month name
116 }
117 }
118 SkipDelim(s);
119 s >> y;
120 }
121 JulTy julnum = !s.fail() ? Jday(m, d, y) : 0;
122 if (julnum != 0)
123 Julnum = julnum;
124 else
125 s.clear(tistream::failbit);
126}
127
128namespace
129{
130
131 void FormatDate(tostream& out, LPCTSTR picture, const TDate& d)
132 {
133 out.fill(_T('0'));
134 while (*picture)
135 {
136 switch (*picture)
137 {
138 case _T('d'): case _T('D'):
139 {
140 int length = 0;
141 while (*picture == _T('d') || *picture == _T('D'))
142 {
143 picture++;
144 length++;
145 }
146 out.width(length < 2 ? 2 : length);
147 out << d.DayOfMonth();
148 break;
149 }
150 case _T('m'): case _T('M'):
151 {
152 int length = 0;
153 while (*picture == _T('m') || *picture == _T('M'))
154 {
155 picture++;
156 length++;
157 }
158 out.width(length < 2 ? 2 : length);
159 out << d.Month();
160 break;
161 }
162 case _T('y'): case _T('Y'):
163 {
164 while (*picture == _T('y') || *picture == _T('Y'))
165 picture++;
166 out << d.Year();
167 break;
168 }
169 default:
170 {
171 out << *picture;
172 picture++;
173 break;
174 }
175 }
176 }
177 }
178
179}
180
181//
182/// Inserts date into output stream os.
183//
185{
186 // We use a string stream to format the date so that
187 // we don't affect the ostream's width setting.
188 //
190 switch (TDate::PrintOption)
191 {
192 case TDate::Normal:
193 out.width(2);
194 out.fill(_T('0'));
195 out << d.NameOfMonth() << _T(" ") << d.DayOfMonth() << _T(", ") << d.Year();
196 break;
197
198 case TDate::Terse:
199 {
200 tchar buf[80]; // NB! Arbitrary size!
201 _stprintf(buf,_T("%02u-%.3s-%.4u"), d.DayOfMonth(), d.NameOfMonth(), d.Year());
202 out << buf;
203 break;
204 }
205
206 case TDate::Numbers:
207 out.width(2);
208 out.fill('0');
209 out << d.Month() << _T("/") << d.DayOfMonth() << _T("/") << d.Year();
210 break;
211
213 out.width(2);
214 out.fill('0');
215 out << d.DayOfMonth() << _T("/") << d.Month() << _T("/") << d.Year();
216 break;
217
218 case TDate::European:
219 out.width(2);
220 out.fill('0');
221 out << d.DayOfMonth() << _T(" ") << d.NameOfMonth() << _T(" ") << d.Year();
222 break;
223
225 {
226 TProfile p(_T("intl"));
227 tstring shortFmt = p.GetString(_T("sShortDate"), _T("mm/dd/yyyy"));
228 FormatDate(out, shortFmt.c_str(), d);
229 break;
230 }
231 }
232
233 s << out.str();
234 return s;
235}
236
237//
238/// Extracts date from input stream is.
239//
241{
242 d.ParseFrom(s);
243 return s;
244}
245
246} // OWL namespace
247/* ========================================================================== */
248
The TDate class represents a date.
Definition date.h:48
static MonthTy IndexOfMonth(const tstring &monthName)
Returns the number, 1-12, of the month named nameOfMonth.
Definition date.cpp:203
static HowToPrint SetPrintOption(HowToPrint h)
Sets the print option for all TDate objects and returns the old setting.
Definition dateio.cpp:39
tstring AsString() const
Converts the TDate object to a string object.
Definition dateio.cpp:29
HowToPrint
Lists different print formats.
Definition date.h:52
@ Terse
Definition date.h:54
@ WinIntSection
Definition date.h:58
@ Numbers
Definition date.h:55
@ Normal
Definition date.h:53
@ EuropeanNumbers
Definition date.h:56
@ European
Definition date.h:57
static JulTy Jday(MonthTy, DayTy, YearTy)
Convert Gregorian calendar date to the corresponding Julian day number j.
Definition date.cpp:215
An instance of TProfile encapsulates a setting within a system file, often referred to as a profile o...
Definition profile.h:44
bool GetString(LPCTSTR key, TCHAR *buff, unsigned buffSize, LPCTSTR defaultString=0)
Looks up and returns the string value associated with the given key string.
Definition profile.cpp:100
#define _istdigit
Definition cygwin.h:70
#define _istalpha
Definition cygwin.h:71
#define _stprintf
Definition cygwin.h:88
#define _T(x)
Definition cygwin.h:51
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
std::ostringstream tostringstream
Definition strmdefs.h:36
owl::opstream & operator<<(owl::opstream &os, const TColor &c)
Insert the color value into a persistent output stream.
Definition color.h:498
char tchar
Definition defs.h:77
unsigned long JulTy
Julian calendar type.
Definition date.h:35
std::istream tistream
Definition strmdefs.h:39
std::string tstring
Definition defs.h:79
std::ostream tostream
Definition strmdefs.h:40
owl::ipstream & operator>>(owl::ipstream &is, TColor &c)
Extract the color value from a persistent input stream.
Definition color.h:489
ParseMonth(tistream &s, tchar *month)
Definition dateio.cpp:67
#define _OWLSTATICFUNC(p)
Definition defs.h:308
#define _OWLCFUNC(p)
Definition defs.h:342
Definition of TProfile class.