PCL
TimePoint.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/TimePoint.h - Released 2019-11-07T10:59:34Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2019 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (http://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_TimePoint_h
53 #define __PCL_TimePoint_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Math.h>
60 #include <pcl/String.h>
61 
62 /*
63  * N.B.: Remove potentially conflicting names that may be defined by 3rd-party
64  * packages used by some standard modules.
65  */
66 #ifdef J2000
67 # undef J2000
68 #endif
69 #ifdef J2100
70 # undef J2100
71 #endif
72 #ifdef B1950
73 # undef B1950
74 #endif
75 
76 namespace pcl
77 {
78 
79 // ----------------------------------------------------------------------------
80 
85 // ----------------------------------------------------------------------------
86 
87 struct FileTime;
88 
102 class PCL_CLASS TimePoint
103 {
104 public:
105 
110  TimePoint() = default;
111 
116  TimePoint( double jd ) :
117  m_jdi( TruncInt( jd ) ), m_jdf( Frac( jd ) )
118  {
119  }
120 
132  TimePoint( double jd1, double jd2 ) :
133  m_jdi( TruncInt( jd1 ) + TruncInt( jd2 ) ),
134  m_jdf( Frac( jd1 ) + Frac( jd2 ) )
135  {
136  Normalize();
137  }
138 
142  TimePoint( int year, int month, double days )
143  {
144  ComplexTimeToJD( m_jdi, m_jdf, year, month, TruncInt( days ), Frac( days ) );
145  }
146 
151  TimePoint( int year, int month, int day, double dayFraction )
152  {
153  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, dayFraction );
154  }
155 
159  TimePoint( int year, int month, int day, int hour, int minute, double seconds )
160  {
161  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, (hour + (minute + seconds/60)/60)/24 );
162  }
163 
173  TimePoint( const IsoString& dateTime )
174  {
175  int year, month, day;
176  double dayf, tz;
177  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
178  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, dayf - tz/24 );
179  }
180 
190  TimePoint( const String& dateTime )
191  {
192  int year, month, day;
193  double dayf, tz;
194  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
195  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, dayf - tz/24 );
196  }
197 
202  TimePoint( time_t time );
203 
213  TimePoint( const FileTime& fileTime );
214 
218  TimePoint( const TimePoint& ) = default;
219 
223  TimePoint& operator =( const TimePoint& ) = default;
224 
230  constexpr bool IsValid() const
231  {
232  return ((m_jdi > 0) ? m_jdi : -m_jdi) < 2147438065;
233  }
234 
247  void GetComplexTime( int& year, int& month, int& day ) const
248  {
249  double dum;
250  JDToComplexTime( year, month, day, dum, m_jdi, m_jdf );
251  }
252 
269  void GetComplexTime( int& year, int& month, int& day, double& dayf ) const
270  {
271  JDToComplexTime( year, month, day, dayf, m_jdi, m_jdf );
272  }
273 
296  void GetComplexTime( int& year, int& month, int& day, int& hour, int& minute, double& seconds ) const
297  {
298  double dayf;
299  JDToComplexTime( year, month, day, dayf, m_jdi, m_jdf );
300  double h = Frac( dayf )*24;
301  double m = Frac( h )*60;
302  seconds = Frac( m )*60;
303  minute = TruncInt( m );
304  hour = TruncInt( h );
305  }
306 
310  int Year() const
311  {
312  int year, foo; double bar;
313  JDToComplexTime( year, foo, foo, bar, m_jdi, m_jdf );
314  return year;
315  }
316 
320  int Month() const
321  {
322  int foo, month; double bar;
323  JDToComplexTime( foo, month, foo, bar, m_jdi, m_jdf );
324  return month;
325  }
326 
330  int Day() const
331  {
332  int foo, day; double bar;
333  JDToComplexTime( foo, foo, day, bar, m_jdi, m_jdf );
334  return day;
335  }
336 
340  double DayFraction() const
341  {
342  int foobar; double dayf;
343  JDToComplexTime( foobar, foobar, foobar, dayf, m_jdi, m_jdf );
344  return dayf;
345  }
346 
361  String ToString( const ISO8601ConversionOptions& options = ISO8601ConversionOptions(), double tz = 0 ) const
362  {
363  return ToString( options, tz, (String*)0 );
364  }
365 
392  String ToString( unsigned timeItems, unsigned precision = 3, double tz = 0, bool timeZone = true, bool zuluTime = true ) const
393  {
394  return ToString( ISO8601ConversionOptions( timeItems, precision, timeZone, zuluTime ), tz );
395  }
396 
411  IsoString ToIsoString( const ISO8601ConversionOptions& options = ISO8601ConversionOptions(), double tz = 0 ) const
412  {
413  return ToString( options, tz, (IsoString*)0 );
414  }
415 
442  IsoString ToIsoString( unsigned timeItems, unsigned precision = 3, double tz = 0, bool timeZone = true, bool zuluTime = true ) const
443  {
444  return ToIsoString( ISO8601ConversionOptions( timeItems, precision, timeZone, zuluTime ), tz );
445  }
446 
517  String ToString( const String& format ) const;
518 
529  IsoString ToIsoString( const IsoString& format ) const;
530 
531  IsoString ToIsoString( const char* format ) const
532  {
533  return ToIsoString( IsoString( format ) );
534  }
535 
539  explicit operator String() const
540  {
541  return ToString();
542  }
543 
547  explicit operator IsoString() const
548  {
549  return ToIsoString();
550  }
551 
556  constexpr int JDI() const
557  {
558  return m_jdi;
559  }
560 
565  constexpr double JDF() const
566  {
567  return m_jdf;
568  }
569 
582  constexpr double JD() const
583  {
584  return m_jdi + m_jdf;
585  }
586 
593  constexpr double MJD() const
594  {
595  return (m_jdi-2400000) + (m_jdf-0.5);
596  }
597 
615  double DeltaT() const;
616 
634  double DeltaAT() const;
635 
640  TimePoint& operator +=( double days )
641  {
642  m_jdf += days;
643  Normalize();
644  return *this;
645  }
646 
651  TimePoint& operator -=( double days )
652  {
653  m_jdf -= days;
654  Normalize();
655  return *this;
656  }
657 
662  constexpr double DaysSince( const TimePoint& t ) const
663  {
664  return m_jdi-t.m_jdi + m_jdf-t.m_jdf;
665  }
666 
671  constexpr double YearsSince( const TimePoint& t ) const
672  {
673  return DaysSince( t )/365.25;
674  }
675 
680  constexpr double CenturiesSince( const TimePoint& t ) const
681  {
682  return DaysSince( t )/36525;
683  }
684 
689  constexpr double SecondsSince( const TimePoint& t ) const
690  {
691  return DaysSince( t )*86400;
692  }
693 
698  constexpr double DaysSinceJ2000() const
699  {
700  return m_jdi-2451545 + m_jdf;
701  }
702 
707  constexpr double YearsSinceJ2000() const
708  {
709  return DaysSinceJ2000()/365.25;
710  }
711 
716  constexpr double CenturiesSinceJ2000() const
717  {
718  return DaysSinceJ2000()/36525;
719  }
720 
725  constexpr double SecondsSinceJ2000() const
726  {
727  return DaysSinceJ2000()*86400;
728  }
729 
734  constexpr double DaysSinceUNIXEpoch() const
735  {
736  return m_jdi-2440587 + m_jdf-0.5;
737  }
738 
743  constexpr double YearsSinceUNIXEpoch() const
744  {
745  return DaysSinceUNIXEpoch()/365.25;
746  }
747 
752  constexpr double CenturiesSinceUNIXEpoch() const
753  {
754  return DaysSinceUNIXEpoch()/36525;
755  }
756 
761  constexpr double SecondsSinceUNIXEpoch() const
762  {
763  return DaysSinceUNIXEpoch()*86400;
764  }
765 
770  constexpr double MillisecondsSinceUNIXEpoch() const
771  {
772  return DaysSinceUNIXEpoch()*86400000;
773  }
774 
781  double SystemOffsetFromUTC() const;
782 
788  {
789  return TimePoint( m_jdi, m_jdf + SystemOffsetFromUTC()/24 );
790  }
791 
797  {
798  return TimePoint( m_jdi, m_jdf - SystemOffsetFromUTC()/24 );
799  }
800 
805  static TimePoint Now();
806 
811  static TimePoint J2000()
812  {
813  TimePoint t;
814  t.m_jdi = 2451545;
815  t.m_jdf = 0;
816  return t;
817  }
818 
823  static TimePoint J2100()
824  {
825  TimePoint t;
826  t.m_jdi = 2488070;
827  t.m_jdf = 0;
828  return t;
829  }
830 
835  static TimePoint B1950()
836  {
837  TimePoint t;
838  t.m_jdi = 2433282;
839  t.m_jdf = 0.42345905;
840  return t;
841  }
842 
848  {
849  TimePoint t;
850  t.m_jdi = 2440587;
851  t.m_jdf = 0.5;
852  return t;
853  }
854 
870  static TimePoint FromString( double& tz, const IsoString& dateTime )
871  {
872  int year, month, day;
873  double dayf;
874  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
875  return TimePoint( year, month, day, dayf - tz/24 );
876  }
877 
893  static TimePoint FromString( double& tz, const String& dateTime )
894  {
895  int year, month, day;
896  double dayf;
897  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
898  return TimePoint( year, month, day, dayf - tz/24 );
899  }
900 
913  static TimePoint FromString( const IsoString& dateTime )
914  {
915  return TimePoint( dateTime );
916  }
917 
930  static TimePoint FromString( const String& dateTime )
931  {
932  return TimePoint( dateTime );
933  }
934 
953  static bool TryFromString( TimePoint& t, double& tz, const IsoString& dateTime )
954  {
955  int year, month, day;
956  double dayf;
957  if ( dateTime.TryParseISO8601DateTime( year, month, day, dayf, tz ) )
958  {
959  t = TimePoint( year, month, day, dayf - tz/24 );
960  return true;
961  }
962  return false;
963  }
964 
983  static bool TryFromString( TimePoint& t, double& tz, const String& dateTime )
984  {
985  int year, month, day;
986  double dayf;
987  if ( dateTime.TryParseISO8601DateTime( year, month, day, dayf, tz ) )
988  {
989  t = TimePoint( year, month, day, dayf - tz/24 );
990  return true;
991  }
992  return false;
993  }
994 
1012  static bool TryFromString( TimePoint& t, const IsoString& dateTime )
1013  {
1014  double tz;
1015  return TryFromString( t, tz, dateTime );
1016  }
1017 
1035  static bool TryFromString( TimePoint& t, const String& dateTime )
1036  {
1037  double tz;
1038  return TryFromString( t, tz, dateTime );
1039  }
1040 
1041 private:
1042 
1043  int32 m_jdi = int32_max; // invalid by default
1044  double m_jdf = 0.0;
1045 
1051  void Normalize()
1052  {
1053  if ( Abs( m_jdf ) >= 1 )
1054  {
1055  m_jdi += TruncInt( m_jdf );
1056  m_jdf = Frac( m_jdf );
1057  }
1058  }
1059 
1060  template <class S>
1061  S ToString( const ISO8601ConversionOptions& options, double tz, S* ) const
1062  {
1063  if ( IsValid() )
1064  {
1065  int year, month, day;
1066  double dayf;
1067  JDToComplexTime( year, month, day, dayf, m_jdi, m_jdf );
1068  return S::ToISO8601DateTime( year, month, day, dayf, tz, options );
1069  }
1070  return S();
1071  }
1072 };
1073 
1074 /*
1075  * ### N.B.: TimePoint cannot have virtual member functions.
1076  * This is because sizeof( TimePoint ) _must_ be equal to
1077  * sizeof( uint32 ) + sizeof( uint64 ).
1078  */
1079 struct PCL_AssertTimePointSize
1080 {
1081  struct TestData { uint32 x; uint64 y; };
1082  static_assert( sizeof( TimePoint ) == sizeof( TestData ), "Invalid sizeof( TimePoint )" );
1083 };
1084 
1090 inline constexpr bool operator ==( const TimePoint& t1, const TimePoint& t2 )
1091 {
1092  return t1.JDI() == t2.JDI() && t1.JDF() == t2.JDF();
1093 }
1094 
1100 inline constexpr bool operator ==( const TimePoint& t1, double jd2 )
1101 {
1102  return t1.JD() == jd2;
1103 }
1104 
1111 inline constexpr bool operator ==( double jd1, const TimePoint& t2 )
1112 {
1113  return t2.JD() == jd1;
1114 }
1115 
1126 inline constexpr bool operator <( const TimePoint& t1, const TimePoint& t2 )
1127 {
1128  return !t1.IsValid() || t2.IsValid() && ((t1.JDI() != t2.JDI()) ? t1.JDI() < t2.JDI() : t1.JDF() < t2.JDF());
1129 }
1130 
1140 inline constexpr bool operator <( const TimePoint& t1, double jd2 )
1141 {
1142  return !t1.IsValid() || t1.JD() < jd2;
1143 }
1144 
1154 inline constexpr bool operator <( double jd1, const TimePoint& t2 )
1155 {
1156  return t2.IsValid() && t2.JD() < jd1;
1157 }
1158 
1165 inline constexpr double operator -( const TimePoint& t1, const TimePoint& t2 )
1166 {
1167  return (t1.JDI() - t2.JDI()) + (t1.JDF() - t2.JDF());
1168 }
1169 
1175 inline TimePoint operator +( const TimePoint& t, double d )
1176 {
1177  return TimePoint( t.JDI(), t.JDF()+d );
1178 }
1179 
1186 inline TimePoint operator +( double d, const TimePoint& t )
1187 {
1188  return t + d;
1189 }
1190 
1197 inline TimePoint operator -( const TimePoint& t, double d )
1198 {
1199  return TimePoint( t.JDI(), t.JDF()-d );
1200 }
1201 
1202 // ----------------------------------------------------------------------------
1203 
1204 } // pcl
1205 
1206 #endif // __PCL_TimePoint_h
1207 
1208 // ----------------------------------------------------------------------------
1209 // EOF pcl/TimePoint.h - Released 2019-11-07T10:59:34Z
constexpr double YearsSinceJ2000() const
Definition: TimePoint.h:707
constexpr T Frac(T x)
Definition: Math.h:601
TimePoint(const IsoString &dateTime)
Definition: TimePoint.h:173
An instant in any timescale.
Definition: TimePoint.h:102
constexpr int JDI() const
Definition: TimePoint.h:556
Complex< T1 > operator+(const Complex< T1 > &c1, const Complex< T2 > &c2)
Definition: Complex.h:455
constexpr double JDF() const
Definition: TimePoint.h:565
IsoString ToIsoString(const ISO8601ConversionOptions &options=ISO8601ConversionOptions(), double tz=0) const
Definition: TimePoint.h:411
constexpr double JD() const
Definition: TimePoint.h:582
static TimePoint FromString(const IsoString &dateTime)
Definition: TimePoint.h:913
PCL root namespace.
Definition: AbstractImage.h:76
static bool TryFromString(TimePoint &t, const String &dateTime)
Definition: TimePoint.h:1035
constexpr double SecondsSinceUNIXEpoch() const
Definition: TimePoint.h:761
double DayFraction() const
Definition: TimePoint.h:340
static TimePoint J2100()
Definition: TimePoint.h:823
constexpr double MJD() const
Definition: TimePoint.h:593
void GetComplexTime(int &year, int &month, int &day) const
Definition: TimePoint.h:247
int Year() const
Definition: TimePoint.h:310
constexpr double YearsSinceUNIXEpoch() const
Definition: TimePoint.h:743
static TimePoint UNIXEpoch()
Definition: TimePoint.h:847
TimePoint UTCToLocalTime() const
Definition: TimePoint.h:787
TimePoint(int year, int month, int day, int hour, int minute, double seconds)
Definition: TimePoint.h:159
void GetComplexTime(int &year, int &month, int &day, int &hour, int &minute, double &seconds) const
Definition: TimePoint.h:296
TimePoint(int year, int month, double days)
Definition: TimePoint.h:142
TimePoint(int year, int month, int day, double dayFraction)
Definition: TimePoint.h:151
constexpr double DaysSinceJ2000() const
Definition: TimePoint.h:698
TimePoint(double jd)
Definition: TimePoint.h:116
String ToString(unsigned timeItems, unsigned precision=3, double tz=0, bool timeZone=true, bool zuluTime=true) const
Definition: TimePoint.h:392
static bool TryFromString(TimePoint &t, const IsoString &dateTime)
Definition: TimePoint.h:1012
constexpr double CenturiesSinceJ2000() const
Definition: TimePoint.h:716
T Abs(const Complex< T > &c)
Definition: Complex.h:420
Unicode (UTF-16) string.
Definition: String.h:7911
unsigned long long uint64
Definition: Defs.h:616
int TruncInt(T x)
Definition: Math.h:1035
constexpr double DaysSince(const TimePoint &t) const
Definition: TimePoint.h:662
static TimePoint FromString(double &tz, const String &dateTime)
Definition: TimePoint.h:893
void PCL_FUNC JDToComplexTime(int &year, int &month, int &day, double &dayf, int jdi, double jdf)
bool TryParseISO8601DateTime(int &year, int &month, int &day, double &dayf, double &tz) const
void PCL_FUNC ComplexTimeToJD(int &jdi, double &jdf, int year, int month, int day, double dayf=0)
Complex< T1 > operator-(const Complex< T1 > &c1, const Complex< T2 > &c2)
Definition: Complex.h:495
signed int int32
Definition: Defs.h:594
Formatting options for string representations of dates and times in ISO 8601 format.
Definition: String.h:385
static TimePoint B1950()
Definition: TimePoint.h:835
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2)
Definition: Array.h:2086
constexpr double DaysSinceUNIXEpoch() const
Definition: TimePoint.h:734
static bool TryFromString(TimePoint &t, double &tz, const String &dateTime)
Definition: TimePoint.h:983
String ToString(const ISO8601ConversionOptions &options=ISO8601ConversionOptions(), double tz=0) const
Definition: TimePoint.h:361
static TimePoint J2000()
Definition: TimePoint.h:811
constexpr double SecondsSinceJ2000() const
Definition: TimePoint.h:725
static TimePoint FromString(const String &dateTime)
Definition: TimePoint.h:930
int Month() const
Definition: TimePoint.h:320
static TimePoint FromString(double &tz, const IsoString &dateTime)
Definition: TimePoint.h:870
TimePoint(const String &dateTime)
Definition: TimePoint.h:190
void ParseISO8601DateTime(int &year, int &month, int &day, double &dayf, double &tz) const
int Day() const
Definition: TimePoint.h:330
bool TryParseISO8601DateTime(int &year, int &month, int &day, double &dayf, double &tz) const
TimePoint(double jd1, double jd2)
Definition: TimePoint.h:132
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2)
Definition: Array.h:2075
constexpr bool IsValid() const
Definition: TimePoint.h:230
void ParseISO8601DateTime(int &year, int &month, int &day, double &dayf, double &tz) const
constexpr double CenturiesSinceUNIXEpoch() const
Definition: TimePoint.h:752
File date and time.
Definition: File.h:288
IsoString ToIsoString(unsigned timeItems, unsigned precision=3, double tz=0, bool timeZone=true, bool zuluTime=true) const
Definition: TimePoint.h:442
#define int32_max
Definition: Defs.h:800
constexpr double SecondsSince(const TimePoint &t) const
Definition: TimePoint.h:689
unsigned int uint32
Definition: Defs.h:600
constexpr double YearsSince(const TimePoint &t) const
Definition: TimePoint.h:671
static bool TryFromString(TimePoint &t, double &tz, const IsoString &dateTime)
Definition: TimePoint.h:953
TimePoint LocalTimeToUTC() const
Definition: TimePoint.h:796
constexpr double CenturiesSince(const TimePoint &t) const
Definition: TimePoint.h:680
constexpr double MillisecondsSinceUNIXEpoch() const
Definition: TimePoint.h:770
void GetComplexTime(int &year, int &month, int &day, double &dayf) const
Definition: TimePoint.h:269
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5387