PCL
TimePoint.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.4.0
6 // ----------------------------------------------------------------------------
7 // pcl/TimePoint.h - Released 2020-08-25T19:17:02Z
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-2020 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 (https://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 ) )
118  , m_jdf( Frac( jd ) )
119  {
120  }
121 
133  TimePoint( double jd1, double jd2 )
134  : m_jdi( TruncInt( jd1 ) + TruncInt( jd2 ) )
135  , m_jdf( Frac( jd1 ) + Frac( jd2 ) )
136  {
137  Normalize();
138  }
139 
143  TimePoint( int year, int month, double days )
144  {
145  ComplexTimeToJD( m_jdi, m_jdf, year, month, TruncInt( days ), Frac( days ) );
146  }
147 
152  TimePoint( int year, int month, int day, double dayFraction )
153  {
154  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, dayFraction );
155  }
156 
160  TimePoint( int year, int month, int day, int hour, int minute, double seconds )
161  {
162  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, (hour + (minute + seconds/60)/60)/24 );
163  }
164 
174  TimePoint( const IsoString& dateTime )
175  {
176  int year, month, day;
177  double dayf, tz;
178  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
179  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, dayf - tz/24 );
180  }
181 
191  TimePoint( const String& dateTime )
192  {
193  int year, month, day;
194  double dayf, tz;
195  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
196  ComplexTimeToJD( m_jdi, m_jdf, year, month, day, dayf - tz/24 );
197  }
198 
203  TimePoint( time_t time );
204 
214  TimePoint( const FileTime& fileTime );
215 
219  TimePoint( const TimePoint& ) = default;
220 
224  TimePoint& operator =( const TimePoint& ) = default;
225 
231  constexpr bool IsValid() const
232  {
233  return ((m_jdi > 0) ? m_jdi : -m_jdi) < 2147438065;
234  }
235 
248  void GetComplexTime( int& year, int& month, int& day ) const
249  {
250  double dum;
251  JDToComplexTime( year, month, day, dum, m_jdi, m_jdf );
252  }
253 
270  void GetComplexTime( int& year, int& month, int& day, double& dayf ) const
271  {
272  JDToComplexTime( year, month, day, dayf, m_jdi, m_jdf );
273  }
274 
297  void GetComplexTime( int& year, int& month, int& day, int& hour, int& minute, double& seconds ) const
298  {
299  double dayf;
300  JDToComplexTime( year, month, day, dayf, m_jdi, m_jdf );
301  double h = Frac( dayf )*24;
302  double m = Frac( h )*60;
303  seconds = Frac( m )*60;
304  minute = TruncInt( m );
305  hour = TruncInt( h );
306  }
307 
311  int Year() const
312  {
313  int year, foo; double bar;
314  JDToComplexTime( year, foo, foo, bar, m_jdi, m_jdf );
315  return year;
316  }
317 
321  int Month() const
322  {
323  int foo, month; double bar;
324  JDToComplexTime( foo, month, foo, bar, m_jdi, m_jdf );
325  return month;
326  }
327 
331  int Day() const
332  {
333  int foo, day; double bar;
334  JDToComplexTime( foo, foo, day, bar, m_jdi, m_jdf );
335  return day;
336  }
337 
341  double DayFraction() const
342  {
343  int foobar; double dayf;
344  JDToComplexTime( foobar, foobar, foobar, dayf, m_jdi, m_jdf );
345  return dayf;
346  }
347 
362  String ToString( const ISO8601ConversionOptions& options = ISO8601ConversionOptions(), double tz = 0 ) const
363  {
364  return ToString( options, tz, (String*)0 );
365  }
366 
393  String ToString( unsigned timeItems, unsigned precision = 3, double tz = 0, bool timeZone = true, bool zuluTime = true ) const
394  {
395  return ToString( ISO8601ConversionOptions( timeItems, precision, timeZone, zuluTime ), tz );
396  }
397 
412  IsoString ToIsoString( const ISO8601ConversionOptions& options = ISO8601ConversionOptions(), double tz = 0 ) const
413  {
414  return ToString( options, tz, (IsoString*)0 );
415  }
416 
443  IsoString ToIsoString( unsigned timeItems, unsigned precision = 3, double tz = 0, bool timeZone = true, bool zuluTime = true ) const
444  {
445  return ToIsoString( ISO8601ConversionOptions( timeItems, precision, timeZone, zuluTime ), tz );
446  }
447 
518  String ToString( const String& format ) const;
519 
530  IsoString ToIsoString( const IsoString& format ) const;
531 
532  IsoString ToIsoString( const char* format ) const
533  {
534  return ToIsoString( IsoString( format ) );
535  }
536 
540  explicit operator String() const
541  {
542  return ToString();
543  }
544 
548  explicit operator IsoString() const
549  {
550  return ToIsoString();
551  }
552 
557  constexpr int JDI() const
558  {
559  return m_jdi;
560  }
561 
566  constexpr double JDF() const
567  {
568  return m_jdf;
569  }
570 
583  constexpr double JD() const
584  {
585  return m_jdi + m_jdf;
586  }
587 
594  constexpr double MJD() const
595  {
596  return (m_jdi-2400000) + (m_jdf-0.5);
597  }
598 
616  double DeltaT() const;
617 
635  double DeltaAT() const;
636 
641  TimePoint& operator +=( double days )
642  {
643  m_jdf += days;
644  Normalize();
645  return *this;
646  }
647 
652  TimePoint& operator -=( double days )
653  {
654  m_jdf -= days;
655  Normalize();
656  return *this;
657  }
658 
663  constexpr double DaysSince( const TimePoint& t ) const
664  {
665  return m_jdi-t.m_jdi + m_jdf-t.m_jdf;
666  }
667 
672  constexpr double YearsSince( const TimePoint& t ) const
673  {
674  return DaysSince( t )/365.25;
675  }
676 
681  constexpr double CenturiesSince( const TimePoint& t ) const
682  {
683  return DaysSince( t )/36525;
684  }
685 
690  constexpr double SecondsSince( const TimePoint& t ) const
691  {
692  return DaysSince( t )*86400;
693  }
694 
699  constexpr double DaysSinceJ2000() const
700  {
701  return m_jdi-2451545 + m_jdf;
702  }
703 
708  constexpr double YearsSinceJ2000() const
709  {
710  return DaysSinceJ2000()/365.25;
711  }
712 
717  constexpr double CenturiesSinceJ2000() const
718  {
719  return DaysSinceJ2000()/36525;
720  }
721 
726  constexpr double SecondsSinceJ2000() const
727  {
728  return DaysSinceJ2000()*86400;
729  }
730 
735  constexpr double DaysSinceUNIXEpoch() const
736  {
737  return m_jdi-2440587 + m_jdf-0.5;
738  }
739 
744  constexpr double YearsSinceUNIXEpoch() const
745  {
746  return DaysSinceUNIXEpoch()/365.25;
747  }
748 
753  constexpr double CenturiesSinceUNIXEpoch() const
754  {
755  return DaysSinceUNIXEpoch()/36525;
756  }
757 
762  constexpr double SecondsSinceUNIXEpoch() const
763  {
764  return DaysSinceUNIXEpoch()*86400;
765  }
766 
771  constexpr double MillisecondsSinceUNIXEpoch() const
772  {
773  return DaysSinceUNIXEpoch()*86400000;
774  }
775 
782  double SystemOffsetFromUTC() const;
783 
789  {
790  return TimePoint( m_jdi, m_jdf + SystemOffsetFromUTC()/24 );
791  }
792 
798  {
799  return TimePoint( m_jdi, m_jdf - SystemOffsetFromUTC()/24 );
800  }
801 
806  static TimePoint Now();
807 
812  static TimePoint J2000()
813  {
814  TimePoint t;
815  t.m_jdi = 2451545;
816  t.m_jdf = 0;
817  return t;
818  }
819 
824  static TimePoint J2100()
825  {
826  TimePoint t;
827  t.m_jdi = 2488070;
828  t.m_jdf = 0;
829  return t;
830  }
831 
836  static TimePoint B1950()
837  {
838  TimePoint t;
839  t.m_jdi = 2433282;
840  t.m_jdf = 0.42345905;
841  return t;
842  }
843 
849  {
850  TimePoint t;
851  t.m_jdi = 2440587;
852  t.m_jdf = 0.5;
853  return t;
854  }
855 
871  static TimePoint FromString( double& tz, const IsoString& dateTime )
872  {
873  int year, month, day;
874  double dayf;
875  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
876  return TimePoint( year, month, day, dayf - tz/24 );
877  }
878 
894  static TimePoint FromString( double& tz, const String& dateTime )
895  {
896  int year, month, day;
897  double dayf;
898  dateTime.ParseISO8601DateTime( year, month, day, dayf, tz );
899  return TimePoint( year, month, day, dayf - tz/24 );
900  }
901 
914  static TimePoint FromString( const IsoString& dateTime )
915  {
916  return TimePoint( dateTime );
917  }
918 
931  static TimePoint FromString( const String& dateTime )
932  {
933  return TimePoint( dateTime );
934  }
935 
954  static bool TryFromString( TimePoint& t, double& tz, const IsoString& dateTime )
955  {
956  int year, month, day;
957  double dayf;
958  if ( dateTime.TryParseISO8601DateTime( year, month, day, dayf, tz ) )
959  {
960  t = TimePoint( year, month, day, dayf - tz/24 );
961  return true;
962  }
963  return false;
964  }
965 
984  static bool TryFromString( TimePoint& t, double& tz, const String& dateTime )
985  {
986  int year, month, day;
987  double dayf;
988  if ( dateTime.TryParseISO8601DateTime( year, month, day, dayf, tz ) )
989  {
990  t = TimePoint( year, month, day, dayf - tz/24 );
991  return true;
992  }
993  return false;
994  }
995 
1013  static bool TryFromString( TimePoint& t, const IsoString& dateTime )
1014  {
1015  double tz;
1016  return TryFromString( t, tz, dateTime );
1017  }
1018 
1036  static bool TryFromString( TimePoint& t, const String& dateTime )
1037  {
1038  double tz;
1039  return TryFromString( t, tz, dateTime );
1040  }
1041 
1042 private:
1043 
1044  int32 m_jdi = int32_max; // invalid by default
1045  double m_jdf = 0.0;
1046 
1052  void Normalize()
1053  {
1054  if ( Abs( m_jdf ) >= 1 )
1055  {
1056  m_jdi += TruncInt( m_jdf );
1057  m_jdf = Frac( m_jdf );
1058  }
1059  }
1060 
1061  template <class S>
1062  S ToString( const ISO8601ConversionOptions& options, double tz, S* ) const
1063  {
1064  if ( IsValid() )
1065  {
1066  int year, month, day;
1067  double dayf;
1068  JDToComplexTime( year, month, day, dayf, m_jdi, m_jdf );
1069  return S::ToISO8601DateTime( year, month, day, dayf, tz, options );
1070  }
1071  return S();
1072  }
1073 };
1074 
1075 /*
1076  * ### N.B.: TimePoint cannot have virtual member functions.
1077  * This is because sizeof( TimePoint ) _must_ be equal to
1078  * sizeof( uint32 ) + sizeof( uint64 ).
1079  */
1080 struct PCL_AssertTimePointSize
1081 {
1082  struct TestData { uint32 x; uint64 y; };
1083  static_assert( sizeof( TimePoint ) == sizeof( TestData ), "Invalid sizeof( TimePoint )" );
1084 };
1085 
1091 inline constexpr bool operator ==( const TimePoint& t1, const TimePoint& t2 )
1092 {
1093  return t1.JDI() == t2.JDI() && t1.JDF() == t2.JDF();
1094 }
1095 
1101 inline constexpr bool operator ==( const TimePoint& t1, double jd2 )
1102 {
1103  return t1.JD() == jd2;
1104 }
1105 
1112 inline constexpr bool operator ==( double jd1, const TimePoint& t2 )
1113 {
1114  return t2.JD() == jd1;
1115 }
1116 
1127 inline constexpr bool operator <( const TimePoint& t1, const TimePoint& t2 )
1128 {
1129  return !t1.IsValid() || t2.IsValid() && ((t1.JDI() != t2.JDI()) ? t1.JDI() < t2.JDI() : t1.JDF() < t2.JDF());
1130 }
1131 
1141 inline constexpr bool operator <( const TimePoint& t1, double jd2 )
1142 {
1143  return !t1.IsValid() || t1.JD() < jd2;
1144 }
1145 
1155 inline constexpr bool operator <( double jd1, const TimePoint& t2 )
1156 {
1157  return t2.IsValid() && t2.JD() < jd1;
1158 }
1159 
1166 inline constexpr double operator -( const TimePoint& t1, const TimePoint& t2 )
1167 {
1168  return (t1.JDI() - t2.JDI()) + (t1.JDF() - t2.JDF());
1169 }
1170 
1176 inline TimePoint operator +( const TimePoint& t, double d )
1177 {
1178  return TimePoint( t.JDI(), t.JDF()+d );
1179 }
1180 
1187 inline TimePoint operator +( double d, const TimePoint& t )
1188 {
1189  return t + d;
1190 }
1191 
1198 inline TimePoint operator -( const TimePoint& t, double d )
1199 {
1200  return TimePoint( t.JDI(), t.JDF()-d );
1201 }
1202 
1203 // ----------------------------------------------------------------------------
1204 
1205 } // pcl
1206 
1207 #endif // __PCL_TimePoint_h
1208 
1209 // ----------------------------------------------------------------------------
1210 // EOF pcl/TimePoint.h - Released 2020-08-25T19:17:02Z
constexpr double YearsSinceJ2000() const
Definition: TimePoint.h:708
constexpr T Frac(T x)
Definition: Math.h:640
TimePoint(const IsoString &dateTime)
Definition: TimePoint.h:174
An instant in any timescale.
Definition: TimePoint.h:102
constexpr int JDI() const
Definition: TimePoint.h:557
Complex< T1 > operator+(const Complex< T1 > &c1, const Complex< T2 > &c2)
Definition: Complex.h:466
constexpr double JDF() const
Definition: TimePoint.h:566
IsoString ToIsoString(const ISO8601ConversionOptions &options=ISO8601ConversionOptions(), double tz=0) const
Definition: TimePoint.h:412
constexpr double JD() const
Definition: TimePoint.h:583
static TimePoint FromString(const IsoString &dateTime)
Definition: TimePoint.h:914
PCL root namespace.
Definition: AbstractImage.h:76
static bool TryFromString(TimePoint &t, const String &dateTime)
Definition: TimePoint.h:1036
constexpr double SecondsSinceUNIXEpoch() const
Definition: TimePoint.h:762
double DayFraction() const
Definition: TimePoint.h:341
static TimePoint J2100()
Definition: TimePoint.h:824
constexpr double MJD() const
Definition: TimePoint.h:594
void GetComplexTime(int &year, int &month, int &day) const
Definition: TimePoint.h:248
int Year() const
Definition: TimePoint.h:311
constexpr double YearsSinceUNIXEpoch() const
Definition: TimePoint.h:744
static TimePoint UNIXEpoch()
Definition: TimePoint.h:848
TimePoint UTCToLocalTime() const
Definition: TimePoint.h:788
TimePoint(int year, int month, int day, int hour, int minute, double seconds)
Definition: TimePoint.h:160
void GetComplexTime(int &year, int &month, int &day, int &hour, int &minute, double &seconds) const
Definition: TimePoint.h:297
TimePoint(int year, int month, double days)
Definition: TimePoint.h:143
TimePoint(int year, int month, int day, double dayFraction)
Definition: TimePoint.h:152
constexpr double DaysSinceJ2000() const
Definition: TimePoint.h:699
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:393
static bool TryFromString(TimePoint &t, const IsoString &dateTime)
Definition: TimePoint.h:1013
constexpr double CenturiesSinceJ2000() const
Definition: TimePoint.h:717
T Abs(const Complex< T > &c)
Definition: Complex.h:431
Unicode (UTF-16) string.
Definition: String.h:7916
unsigned long long uint64
Definition: Defs.h:616
int TruncInt(T x)
Definition: Math.h:1132
constexpr double DaysSince(const TimePoint &t) const
Definition: TimePoint.h:663
static TimePoint FromString(double &tz, const String &dateTime)
Definition: TimePoint.h:894
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:506
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:836
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2)
Definition: Array.h:2103
constexpr double DaysSinceUNIXEpoch() const
Definition: TimePoint.h:735
static bool TryFromString(TimePoint &t, double &tz, const String &dateTime)
Definition: TimePoint.h:984
String ToString(const ISO8601ConversionOptions &options=ISO8601ConversionOptions(), double tz=0) const
Definition: TimePoint.h:362
static TimePoint J2000()
Definition: TimePoint.h:812
constexpr double SecondsSinceJ2000() const
Definition: TimePoint.h:726
static TimePoint FromString(const String &dateTime)
Definition: TimePoint.h:931
int Month() const
Definition: TimePoint.h:321
static TimePoint FromString(double &tz, const IsoString &dateTime)
Definition: TimePoint.h:871
TimePoint(const String &dateTime)
Definition: TimePoint.h:191
void ParseISO8601DateTime(int &year, int &month, int &day, double &dayf, double &tz) const
int Day() const
Definition: TimePoint.h:331
bool TryParseISO8601DateTime(int &year, int &month, int &day, double &dayf, double &tz) const
TimePoint(double jd1, double jd2)
Definition: TimePoint.h:133
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2)
Definition: Array.h:2092
constexpr bool IsValid() const
Definition: TimePoint.h:231
void ParseISO8601DateTime(int &year, int &month, int &day, double &dayf, double &tz) const
constexpr double CenturiesSinceUNIXEpoch() const
Definition: TimePoint.h:753
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:443
#define int32_max
Definition: Defs.h:800
constexpr double SecondsSince(const TimePoint &t) const
Definition: TimePoint.h:690
unsigned int uint32
Definition: Defs.h:600
constexpr double YearsSince(const TimePoint &t) const
Definition: TimePoint.h:672
static bool TryFromString(TimePoint &t, double &tz, const IsoString &dateTime)
Definition: TimePoint.h:954
TimePoint LocalTimeToUTC() const
Definition: TimePoint.h:797
constexpr double CenturiesSince(const TimePoint &t) const
Definition: TimePoint.h:681
constexpr double MillisecondsSinceUNIXEpoch() const
Definition: TimePoint.h:771
void GetComplexTime(int &year, int &month, int &day, double &dayf) const
Definition: TimePoint.h:270
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5390