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