casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MVTime.h
Go to the documentation of this file.
1 //# MVTime.h: Class to handle date/time type conversions and I/O
2 //# Copyright (C) 1996,1997,1998,1999,2000,2001
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_MVTIME_H
29 #define CASA_MVTIME_H
30 
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
35 #include <casacore/casa/iosfwd.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# Forward Declarations
40 class String;
41 class MVEpoch;
42 class Time;
43 
44 //# Constants (SUN compiler does not accept non-simple default arguments)
45 
46 // <summary>
47 // Class to handle date/time type conversions and I/O
48 // </summary>
49 
50 // <use visibility=export>
51 
52 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tMeasure" demos="">
53 // </reviewed>
54 
55 // <prerequisite>
56 // <li> <linkto class=Quantum>Quantum</linkto>
57 // <li> <linkto class=MVAngle>MVAngle</linkto>
58 // <li> <a href="http://mcps.k12.md.us/departments/year2000/Technology/ISO_std.html">
59 // ISO8601 standard</a> on dates and time.
60 // </prerequisite>
61 //
62 // <etymology>
63 // From Measure, Value and Time
64 // </etymology>
65 //
66 // <synopsis>
67 // An MVTime is a simple Double for date/time conversions and I/O.
68 // Its internal value is in MJD. For high precision the
69 // <linkto class=MVEpoch>MVEpoch</linkto> class should be used.<br>
70 // It can be constructed from a Double (in which case MJD are assumed),
71 // or from a Quantity (<src>Quantum<Double></src>). Quantities must be in
72 // either angle or time units, or from a
73 // <linkto class=MVEpoch>MVEpoch</linkto><br>
74 // The <linkto class=Time>OS/Time class</linkto> can be used as both input
75 // and output. An <src>MVTime(Time)</src> constructor exists, as well
76 // as a <src>Time getTime()</src>.<br>
77 // Construction from year, month, day is also supported.
78 // <note role=caution> Dates before 16 Oct 1582 are considered to be Julian,
79 // rather than Gregorian</note>
80 // It has an automatic conversion to Double, so all standard mathematical
81 // operations can operate on it.<br>
82 // The class has a number of special functions to obtain data:
83 // <ul>
84 // <li> <src>Double day()</src> will return value in days
85 // <li> <src>Double hour()</src> will return value in hours
86 // <li> <src>Double minute()</src> will return value in minutes
87 // <li> <src>Double second()</src> will return value in seconds
88 // <li> <src>Quantity get()</src> will return days
89 // <li> <src>Quantity get(Unit)</src> will return in specified units
90 // (angle(in which case it will be between -pi and +pi) or time)
91 // <li> <src>uInt weekday()</src> will return day of week (1=Mon, 7=Sun)
92 // <li> <src>uInt month()</src> will return month (1=Jan)
93 // <li> <src>Int year()</src> will return year
94 // <li> <src>uInt monthday()</src> will return day of the month
95 // <li> <src>uInt yearday()</src> will return day of year (Jan01 = 1)
96 // <li> <src>uInt yearweek()</src> will return week of year
97 // (week containing Jan04 = 1, week start on Monday).
98 // The week before the first week will be called 0, contrary
99 // to standard practice (week 53/52 of previous year).
100 // <li> <src>Int ymd()</src> will return yyyymmdd as a single number
101 // <li> <src>const String &dayName()</src> will return name of day
102 // (Sun, Mon, Tue, Wed, Thu, Fri, Sat)
103 // <li> <src>const String &monthName()</src> will retrun name of Month
104 // (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
105 // </ul>
106 // Output formatting is done with the <src><<</src> statement, with the
107 // following rules:
108 // <ul>
109 // <li> standard output is done in the following format:
110 // <src>hh:mm:ss.tt</src>. The number of
111 // digits presented will be based on the precision attached to the
112 // current stream
113 // <li> output can be formatted by using either the <src>setFormat()</src>
114 // method for global angle format setting, or the output of
115 // <src>MVTime::Format()</src> data for a once off change (see later).
116 // Formats have a first argument which
117 // determines the type (default, if not given, MVTime::TIME, other
118 // possibility MVTime::ANGLE (as +ddd.mm.ss.tt..),
119 // the second the number of digits wanted (default stream precision),
120 // with a value:
121 // <ul>
122 // <li> <3 : hh:: only
123 // <li> <5 : hh:mm:
124 // <li> <7 : hh:mm:ss
125 // <li> >6 : with precision-6 t's added
126 // </ul>
127 // comparable for angle. <note role=tip> The added colons are
128 // to enable input
129 // checking of the format. Look at the 'clean' types to bypass them.
130 // </note>
131 // The <src>MVTime::YMD</src> format implies TIME, and will
132 // precede the time with 'yyyy/mm/dd/' (or use
133 // <src>MVTime::YMD_ONLY</src> to include <src>NO_TIME</src>
134 // modifier).<br>
135 // The <src>MVTime::DMY</src> format implies TIME, and will
136 // precede the time with 'dd-Mon-yyyy/'.<br>
137 // The <src>MVTime::FITS</src> format implies TIME, and will
138 // precede the time with 'ccyy-mm-ddT'.<br>
139 // The <src>MVTime::ISO</src> format implies FITS followed by a Z
140 // for the UTC time zone.
141 // The <src>BOOST</src> format implies DMY and USE_SPACE (space instead
142 // of slash between date and time).
143 // <br>
144 // The output format can be modified with modifiers (specify as
145 // MVTime::TIME | MVTime::MOD (or + MVTime::MOD)).
146 // <note role=caution> For overloading/casting
147 // problems with some compilers, the
148 // use of modifiers necessitates either the presence of a precision
149 // (i.e. <src>(A|B, prec)</src>), or an explicit cast:
150 // <src>((MVTime::formatTypes)(A|B))</src>, or make use of
151 // the provided <src>TIME[_CLEAN][_NO_H[M]]</src> and
152 // <src>ANGLE[_CLEAN][_NO_D[M]]</src>.
153 // </note>
154 //
155 // The modifiers can be:
156 // <ul>
157 // <li> <src>MVTime::CLEAN</src> to suppress leading or trailing
158 // periods (or colons for TIME). Note that he result can not be
159 // read automatically.
160 // <li> <src>MVTime::NO_H</src> (or <src>NO_D</src>) to suppress
161 // the output of hours (or degrees): useful for offsets
162 // <li> <src>MVTime::NO_HM</src> (or <src>NO_DM</src>), to
163 // suppress the degrees and minutes.
164 // <li> <src>MVTime::DAY</src> will precede the output with
165 // 'Day-' (e.g. Wed-). Space delimiter is used for USE_SPACE.
166 // <li> <src>MVTime::NO_TIME</src> will suppress printing of time.
167 // </ul>
168 // Output in formats like <src>20'</src> can be done via the standard
169 // Quantum output (e.g. <src> stream << time.get("'") </src>).
170 // <li> Available formats:
171 // <ul>
172 // <li> MVTime::ANGLE in +ddd.mm.ss.ttt format
173 // <li> MVTime::TIME in hh:mm:ss.ttt format
174 // <li> MVTime::[ANGLE|TIME]_CLEAN format without superfluous periods
175 // <li> MVTime::[ANGLE|TIME][_CLEAN]_NO_[D|H][M] in format with
176 // leading zero fields left empty.
177 // <li> MVTime::CLEAN modifier for suppressing superfluous periods
178 // <li> MVTime::USE_SPACE to use a space instead of a slash
179 // as delimiter between date and time.
180 // <li> MVTime::USE_Z to follow the time by a Z for the UTC time zone.
181 // <li> MVTime::NO_[D|H][M] modifier to suppress first field(s)
182 // <li> MVTime::DIG2 modifier to get +dd.mm.ss.ttt in angle or
183 // time format(i.e. in range -90 - +90 or -12 - +12)
184 // <li> MVTime::LOCAL modifier to produce local time (as derived from
185 // aipsrc time.tzoffset). In FITS mode the time zone will
186 // be appended (as <src><sign>hh:mm</src>).
187 // <note role=caution>The adding of the timezone is not part
188 // of the FITS standard, but of the underlying ISO standard. It can
189 // be used to export local times in standard format.</note>
190 // </ul>
191 // </ul>
192 // The default formatting can be overwritten by a
193 // <src> MVTime::setFormat(); </src> statement; which returns an
194 // MVTime::Format
195 // structure, that can be used in a subsequent one to reset to previous.
196 // The format set holds for all MVTime output on all streams.<br>
197 // Temporary formats (i.e. for one MVTime output only), can be set by
198 // outputting a format (i.e. <src> stream << MVTime::Format() << ... </src>).
199 // <note role=caution> A setFormat() will also
200 // reset any lingering temporary format.
201 // A setFormat(getFormat()) will reset without changing. Problems could
202 // arise in parallel processors. </note>
203 // Input can be read if the values are in any of the above (non-clean) output
204 // formats. <br>
205 // For other formatting practice, the output can be written to a String with
206 // the string() member functions.<br>
207 // Note that using a temporary format is inherently thread-unsafe because
208 // the format is kept in a static variable. Another thread may overwrite
209 // the format just set. The only thread-safe way to format an MVTime is using
210 // a <src>print</src> or <src>string</src> that accepts a Format object.
211 //
212 // Strings and input can be converted to an MVTime (or Quantity) by
213 // <src>Bool read(Quantity &out, const String &in)</src> and
214 // <src> istream >> MVTime &</src>. In the latter case the actual
215 // reading is done by the String read, which reads between white-spaces.<br>
216 // The following input formats (note no blanks allowed) are supported
217 // (+stands for an optional + or -; v for an unsigned integer; dv for a
218 // floating number. [] indicate optional values. Separating codes are
219 // case insensitive), numbers(like yyyy) can be of any length.
220 // The separator between date and time part can be a slash (as shown below),
221 // a hyphen, or one or more spaces.
222 // <ul>
223 // <li> today -- (UT) time now
224 // <li> today/[time] -- time on today (0:0:0 if omitted)
225 // <li> yyyy/mm/dd[/time] -- date + time. An omitted date (leading /)
226 // will be today + time; an omitted month will
227 // indicate use of day number in year (1 == 1/1)
228 // <li> dd[-]MMM[-]yyyy[/time] -- date +time If yyyy <100: around 2000.
229 // MMM can be at least first three characters
230 // of month name; or a month number (1 == Jan).
231 // Omitted month indicates day is day number.
232 // <li> ccyy-mm-dd[Ttime[Z|+-hh[:mm]]] -- new FITS format the 'T' as time
233 // separator. Time should be UTC.
234 // The 'Z' separator (for UTC) is part of an
235 // earlier FITS proposal, and will be recognised
236 // for backward compatibility.
237 // A signed hh or hh:mm can be present to
238 // indicate time zone. This value will be
239 // subtracted to give UTC. To recognise this
240 // format, the year should be greater than 1000.
241 // <note role=caution> The time-zone information
242 // is not part of the FITS standard, but of the
243 // underlying ISO standard.</note>
244 // </ul>
245 // The time can be expressed as described in
246 // <linkto class=MVAngle>MVAngle</linkto>
247 // Examples of valid strings:
248 // <srcblock>
249 // ToDay note case independence
250 // 1996/11/20 20 November 1996 0h UT
251 // 1996/11/20/5:20 20 November 1996 at 5h20m
252 // 20Nov96-5h20m same (again no case dependence)
253 // 1996-11-20T5:20 same (FITS format, case dependent)
254 // </srcblock>
255 // </synopsis>
256 //
257 // <example>
258 // See synopsis
259 // </example>
260 //
261 // <motivation>
262 // To be able to format date/time-like values in user-required ways.
263 // </motivation>
264 //
265 // <todo asof="1996/11/15">
266 // <li> Nothing I know of
267 // </todo>
268 
269 class MVTime {
270 
271  public:
272 
273 //# Enumerations
274 // Format types
275  enum formatTypes {
278  CLEAN = 4,
279  NO_D = 8,
280  NO_DM = NO_D+16,
281  YMD = TIME+32,
282  DMY = TIME+64,
283  DAY = 128,
284  NO_TIME = 256,
285  MJD = TIME+512,
286  DIG2 = 1024,
287  FITS = TIME+2048,
288  LOCAL = 4096,
289  USE_SPACE = 8192,
290  ALPHA = 16384,
291  USE_Z = 32768,
304  TIME_CLEAN_NO_H = TIME + CLEAN + NO_H,
307  MOD_MASK = CLEAN + NO_DM + DAY + NO_TIME + DIG2 +
308  LOCAL + USE_SPACE + USE_Z + ALPHA
309  };
310 
311 //# Local structure
312 // Format structure
313  class Format {
314  public:
315  friend class MVTime;
317  uInt inprec = 0) :
318  typ(intyp), prec(inprec) {;};
319  Format(uInt inprec) :
320  typ(MVTime::TIME), prec(inprec) {;};
321 // Construct from type and precision (present due to overlaoding problems)
322  Format(uInt intyp, uInt inprec) :
323  typ((MVTime::formatTypes)intyp), prec(inprec) {;};
324  Format(const Format &other) :
325  typ(other.typ), prec(other.prec) {;};
326  private:
329  };
330 
331 //# Friends
332 // Output a date/time
333  friend ostream &operator<<(ostream &os, const MVTime &meas);
334 // Input a date/time
335  friend istream &operator>>(istream &is, MVTime &meas);
336 // Set a temporary format
337  friend ostream &operator<<(ostream &os, const MVTime::Format &form);
338 
339 //# Constructors
340 // Default constructor: generate a zero value
341  MVTime();
342 // Copy constructor
343  MVTime(const MVTime &other);
344 // Copy assignment
345  MVTime &operator=(const MVTime &other);
346 // Constructor from Double (in MJD)
347  MVTime(Double d);
348 // Constructor from Quantum : value can be an angle or time
349 // <thrown>
350 // <li> AipsError if not a time or angle
351 // </thrown>
352  MVTime(const Quantity &other);
353 // Constructor from Time
354  MVTime(const Time &other);
355 // Constructor from MVEpoch;
356  MVTime(const MVEpoch &other);
357 // Constructor from yy, mm, dd, dd (all dd with fractions allowed)
358  MVTime(Int yy, Int mm, Double dd, Double d=0.0);
359 
360 //# Destructor
361  ~MVTime();
362 
363 //# Operators
364 // Conversion operator
365  operator Double() const;
366 
367 //# General member functions
368  // Make res time Quantity from string. The String version will accept
369  // a time/angle Quantity as well. It returns False in case of an error.
370  // chk=True means that the entire string should be consumed.
371  // throwExcp=True means that an exception is thrown in case of an error.
372  // <group>
373  static Bool read(Quantity &res, const String &in, Bool chk=True);
374  static Bool read(Quantity &res, MUString &in, Bool chk=True);
375  static Bool read(Quantity &res, const String &in, Bool chk, Bool throwExcp);
376  static Bool read(Quantity &res, MUString &in, Bool chk, Bool throwExcp);
377  // </group>
378 // Get value of date/time (MJD) in given units
379 // <group>
380  Double day() const;
381  Double hour() const;
382  Double minute() const;
383  Double second() const;
384  Quantity get() const;
385  Quantity get(const Unit &inunit) const;
386  Time getTime() const;
387 // </group>
388 // Get indicated part of the time/date
389 // <group>
390  const String &dayName() const;
391  static const String &dayName(uInt which);
392  const String &monthName() const;
393  static const String &monthName(uInt which);
394  // Mon = 1; Sun = 7;
395  uInt weekday() const;
396  // Jan =1
397  uInt month() const;
398  uInt monthday() const;
399  Int year() const;
400  Int ymd() const;
401  uInt yearday() const;
402  uInt yearweek() const;
403 // </group>
404 // Output data.
405 // <note role=warning>
406 // The first function below is thread-unsafe because it uses the result of
407 // the setFormat function which changes a static class member.
408 // The other functions are thread-safe because the format is directly given.
409 // </note>
410 // <group>
411  String string() const;
412  String string(MVTime::formatTypes intyp, uInt inprec = 0) const;
413  String string(uInt intyp, uInt inprec) const;
414  String string(uInt inprec) const;
415  String string(const MVTime::Format &form) const;
416  void print(ostream &oss, const MVTime::Format &form) const;
417 // </group>
418 // Set default format
419 // <note role=warning>
420 // It is thread-unsafe to print using the setFormat functions because they
421 // change a static class member. The only thred-safe way to print a time is
422 // to use the print function above.
423 // </note>
424 // <group>
425  static Format setFormat(MVTime::formatTypes intyp,
426  uInt inprec = 0);
427  static Format setFormat(uInt intyp, uInt inprec);
428  static Format setFormat(uInt inprec = 0);
429  static Format setFormat(const Format &form);
430 // </group>
431  // Get default format
432  static Format getFormat();
433  // Get code belonging to string. 0 if not known
434  static MVTime::formatTypes giveMe(const String &in);
435  // Get time zone offset (in days)
436 static Double timeZone();
437 
438  private:
439 //# Data
440 // Value
442 // Default format
444 // Temporary format
445 // <group>
447  static Bool interimSet;
448 // </group>
449 
450 //# Member functions
451  // Get the y,m,d values
452  void ymd(Int &yyyy, Int &mm, Int &dd) const;
453 };
454 
455 // Global functions.
456 // Output
457 // <group>
458 ostream &operator<<(ostream &os, const MVTime &meas);
459 ostream &operator>>(ostream &is, MVTime &meas);
460 // Set a temporary format (thread-unsafe).
461 ostream &operator<<(ostream &os, const MVTime::Format &form);
462 // </group>
463 
464 // is equal operator, uses operator Double which returns days
465 inline Bool operator==(const MVTime &lh, const MVTime &rh)
466 { return (lh.operator Double() == rh.operator Double());}
467 
468 
469 
470 } //# NAMESPACE CASACORE - END
471 
472 #endif
Double val
Value.
Definition: MVTime.h:441
int Int
Definition: aipstype.h:50
Double second() const
friend ostream & operator<<(ostream &os, const MVTime &meas)
Output a date/time.
Time getTime() const
MVTime()
Default constructor: generate a zero value.
Format structure.
Definition: MVTime.h:313
const String & monthName() const
static MVTime::formatTypes giveMe(const String &in)
Get code belonging to string.
static Bool interimSet
Definition: MVTime.h:447
ostream & operator>>(ostream &is, MVTime &meas)
static Format getFormat()
Get default format.
static functions and enumerations
Definition: fits.h:165
Pointed String class to aid analysis of quantity strings.
Definition: MUString.h:229
Int year() const
Format(MVTime::formatTypes intyp=MVTime::TIME, uInt inprec=0)
Definition: MVTime.h:316
uInt month() const
Jan =1.
Double day() const
Get value of date/time (MJD) in given units.
ostream & operator<<(ostream &os, const std::pair< T, U > &p)
Double minute() const
Double hour() const
static Double timeZone()
Get time zone offset (in days)
defines physical units
Definition: Unit.h:189
uInt yearday() const
const String & dayName() const
Get indicated part of the time/date.
static Format setFormat(MVTime::formatTypes intyp, uInt inprec=0)
Set default format Warning: It is thread-unsafe to print using the setFormat functions because they ...
formatTypes
Format types.
Definition: MVTime.h:275
MVTime & operator=(const MVTime &other)
Copy assignment.
double Double
Definition: aipstype.h:55
date and time enquiry functions, with some operations.
Definition: Time.h:88
static Bool read(Quantity &res, const String &in, Bool chk=True)
Make res time Quantity from string.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
MVTime::formatTypes typ
Definition: MVTime.h:325
static MVTime::Format interimFormat
Temporary format.
Definition: MVTime.h:446
Format(const Format &other)
Definition: MVTime.h:324
uInt yearweek() const
void print(ostream &oss, const MVTime::Format &form) const
uInt monthday() const
String string() const
Output data.
A class for high precision time.
Definition: MVEpoch.h:90
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Bool operator==(const MVTime &lh, const MVTime &rh)
is equal operator, uses operator Double which returns days
Definition: MVTime.h:465
static MVTime::Format defaultFormat
Default format.
Definition: MVTime.h:443
friend istream & operator>>(istream &is, MVTime &meas)
Input a date/time.
Class to handle date/time type conversions and I/O.
Definition: MVTime.h:269
Format(uInt inprec)
Definition: MVTime.h:319
uInt weekday() const
Mon = 1; Sun = 7;.
Int ymd() const
const Bool True
Definition: aipstype.h:43
Format(uInt intyp, uInt inprec)
Construct from type and precision (present due to overlaoding problems)
Definition: MVTime.h:322
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42