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