SAGA API v9.10
Loading...
Searching...
No Matches
datetime.cpp
Go to the documentation of this file.
1
3// //
4// SAGA //
5// //
6// System for Automated Geoscientific Analyses //
7// //
8// Application Programming Interface //
9// //
10// Library: SAGA_API //
11// //
12//-------------------------------------------------------//
13// //
14// datetime.cpp //
15// //
16// Copyright (C) 2015 by Olaf Conrad //
17// //
18//-------------------------------------------------------//
19// //
20// This file is part of 'SAGA - System for Automated //
21// Geoscientific Analyses'. //
22// //
23// This library is free software; you can redistribute //
24// it and/or modify it under the terms of the GNU Lesser //
25// General Public License as published by the Free //
26// Software Foundation, either version 2.1 of the //
27// License, or (at your option) any later version. //
28// //
29// This library is distributed in the hope that it will //
30// be useful, but WITHOUT ANY WARRANTY; without even the //
31// implied warranty of MERCHANTABILITY or FITNESS FOR A //
32// PARTICULAR PURPOSE. See the GNU Lesser General Public //
33// License for more details. //
34// //
35// You should have received a copy of the GNU Lesser //
36// General Public License along with this program; if //
37// not, see <http://www.gnu.org/licenses/>. //
38// //
39//-------------------------------------------------------//
40// //
41// contact: Olaf Conrad //
42// Institute of Geography //
43// University of Hamburg //
44// Germany //
45// //
46// e-mail: oconrad@saga-gis.org //
47// //
49
50//---------------------------------------------------------
51#include <time.h>
52
53#include <wx/datetime.h>
54
55#include "datetime.h"
56
57
59// //
60// //
61// //
63
64//---------------------------------------------------------
66{
67 wxTimeSpan ts((wxLongLong(m_span)));
68
69 wxString s = !Format.is_Empty() ? ts.Format(Format.c_str()) : ts.Format(wxDefaultTimeSpanFormat);
70
71 return( CSG_String(&s) );
72}
73
74
76// //
77// //
78// //
80
81//---------------------------------------------------------
83{
84 m_pDateTime = new wxDateTime(wxDateTime::Now());
85}
86
87//---------------------------------------------------------
89{
90 m_pDateTime = new wxDateTime(*DateTime.m_pDateTime);
91}
92
93//---------------------------------------------------------
95{
96 m_pDateTime = new wxDateTime(JDN);
97}
98
99//---------------------------------------------------------
101{
102 m_pDateTime = new wxDateTime(wxDateTime::Now());
103
104 Set(ISODate);
105}
106
107//---------------------------------------------------------
109{
110 m_pDateTime = new wxDateTime(Hour, Minute, Second, Millisec);
111}
112
113//---------------------------------------------------------
115{
116 m_pDateTime = new wxDateTime(Day, (wxDateTime::Month)Month, Year, Hour, Minute, Second, Millisec);
117}
118
119//---------------------------------------------------------
121{
122 delete(m_pDateTime);
123}
124
125
127// //
129
130//---------------------------------------------------------
132{
133 m_pDateTime->Set(DateTime.m_pDateTime->GetTm());
134
135 return( *this );
136}
137
138//---------------------------------------------------------
140{
141 m_pDateTime->Set(JDN);
142
143 return( *this );
144}
145
146//---------------------------------------------------------
148{
149 // 0123456789
150 // YYYY-MM-DD
151
152 if( ISODate.Length() >= 10 && ISODate[4] == '-' && ISODate[7] == '-' )
153 {
154 int y = ISODate.Left ( 4).asInt();
155 int m = ISODate.Mid (5, 2).asInt();
156 int d = ISODate.Right( 2).asInt();
157
158 Set((TSG_DateTime)d, (Month)(m - 1), y);
159 }
160
161 return( *this );
162}
163
164//---------------------------------------------------------
166{
167 m_pDateTime->Set(Hour, Minute, Second, Millisec);
168
169 return( *this );
170}
171
172//---------------------------------------------------------
174{
175 m_pDateTime->Set(Day, (wxDateTime::Month)Month, Year, Hour, Minute, Second, Millisec);
176
177 return( *this );
178}
179
180//---------------------------------------------------------
182{
183 m_pDateTime->SetMillisecond(Value);
184
185 return( *this );
186}
187
188//---------------------------------------------------------
190{
191 m_pDateTime->SetSecond(Value);
192
193 return( *this );
194}
195
196//---------------------------------------------------------
198{
199 m_pDateTime->SetMinute(Value);
200
201 return( *this );
202}
203
204//---------------------------------------------------------
206{
207 m_pDateTime->SetHour(Value);
208
209 return( *this );
210}
211
213{
214 m_pDateTime->ResetTime();
215
216 Value = fmod(Value, 24.0); if( Value < 0.0 ) Value += 24.0;
217
218 m_pDateTime->SetHour ((wxDateTime::wxDateTime_t)Value); Value = (Value - (wxDateTime::wxDateTime_t)Value) * 60.0;
219 m_pDateTime->SetMinute ((wxDateTime::wxDateTime_t)Value); Value = (Value - (wxDateTime::wxDateTime_t)Value) * 60.0;
220 m_pDateTime->SetSecond ((wxDateTime::wxDateTime_t)Value); Value = (Value - (wxDateTime::wxDateTime_t)Value) * 1000.0;
221 m_pDateTime->SetMillisecond((wxDateTime::wxDateTime_t)Value);
222
223 return( *this );
224}
225
226//---------------------------------------------------------
227CSG_DateTime & CSG_DateTime::Set_Day(unsigned short Value)
228{
229 m_pDateTime->SetDay(Value);
230
231 return( *this );
232}
233
234//---------------------------------------------------------
236{
237 m_pDateTime->SetMonth((wxDateTime::Month)Value);
238
239 return( *this );
240}
241
242//---------------------------------------------------------
244{
245 m_pDateTime->SetYear(Value);
246
247 return( *this );
248}
249
250//---------------------------------------------------------
252{
253 m_pDateTime->SetToYearDay(Value);
254
255 return( *this );
256}
257
258//---------------------------------------------------------
260{
261 m_pDateTime->SetToCurrent();
262
263 return( *this );
264}
265
266//---------------------------------------------------------
268{
269 m_pDateTime->ResetTime();
270
271 return( *this );
272}
273
274
276// //
278
279//---------------------------------------------------------
281{
282 return( CSG_DateTime(m_pDateTime->FromUTC(noDST).GetJDN()) );
283
284// CSG_DateTime dt; dt.m_pDateTime->Set(m_pDateTime->FromUTC(noDST).GetTm()); return( dt );
285}
286
287//---------------------------------------------------------
289{
290 return( CSG_DateTime(m_pDateTime->ToUTC(noDST).GetJDN()) );
291}
292
293//---------------------------------------------------------
295{
296 m_pDateTime->MakeUTC(noDST);
297
298 return( *this );
299}
300
301//---------------------------------------------------------
302bool CSG_DateTime::is_DST(void) const
303{
304 return( m_pDateTime->IsDST() != 0 );
305}
306
307
309// //
311
312//---------------------------------------------------------
314{
315 return( m_pDateTime->GetValue().GetValue() );
316}
317
318
320// //
322
323//---------------------------------------------------------
325{
326 CSG_DateTime dt(*this);
327
328 return( dt.Add(TimeSpan) );
329}
330
332{
333 m_pDateTime->Add(wxTimeSpan((wxLongLong)TimeSpan.m_span));
334
335 return( *this);
336}
337
338//---------------------------------------------------------
340{
341 CSG_DateTime dt(*this);
342
343 return( dt.Subtract(TimeSpan) );
344}
345
347{
348 m_pDateTime->Subtract(wxTimeSpan((wxLongLong)TimeSpan.m_span));
349
350 return( *this);
351}
352
353//---------------------------------------------------------
355{
356 CSG_TimeSpan Span((sLong)m_pDateTime->Subtract(*DateTime.m_pDateTime).GetValue().GetValue());
357
358 return( Span );
359}
360
361
363// //
365
366//---------------------------------------------------------
368{
369 return( m_pDateTime->IsValid() );
370}
371
372//---------------------------------------------------------
373unsigned short CSG_DateTime::Get_Millisecond(void) const { return( m_pDateTime->GetMillisecond() ); }
374unsigned short CSG_DateTime::Get_Second (void) const { return( m_pDateTime->GetSecond () ); }
375unsigned short CSG_DateTime::Get_Minute (void) const { return( m_pDateTime->GetMinute () ); }
376unsigned short CSG_DateTime::Get_Hour (void) const { return( m_pDateTime->GetHour () ); }
377unsigned short CSG_DateTime::Get_Day (void) const { return( m_pDateTime->GetDay () ); }
378
380{
381 return( (Month)m_pDateTime->GetMonth() );
382}
383
385{
386 return( m_pDateTime->GetYear() );
387}
388
389//---------------------------------------------------------
390unsigned short CSG_DateTime::Get_DayOfYear(void) const
391{
392 return( m_pDateTime->GetDayOfYear() );
393}
394
396{
397 return( (WeekDay)m_pDateTime->GetWeekDay() );
398}
399
401{
402 return( (TSG_DateTime)m_pDateTime->GetWeekOfMonth() );
403}
404
406{
407 return( (TSG_DateTime)m_pDateTime->GetWeekOfYear() );
408}
409
410//---------------------------------------------------------
411double CSG_DateTime::Get_JDN(void) const
412{
413 return( m_pDateTime->GetJDN() );
414}
415
416double CSG_DateTime::Get_MJD(void) const
417{
418 return( m_pDateTime->GetMJD() );
419}
420
422{
423 return( m_pDateTime->GetTicks() );
424}
425
427// //
429
430//---------------------------------------------------------
431bool CSG_DateTime::is_EarlierThan (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsEarlierThan(*DateTime.m_pDateTime) ); }
432bool CSG_DateTime::is_EqualTo (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsEqualTo (*DateTime.m_pDateTime) ); }
433bool CSG_DateTime::is_LaterThan (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsLaterThan (*DateTime.m_pDateTime) ); }
434
435//---------------------------------------------------------
436bool CSG_DateTime::is_SameDate (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsSameDate (*DateTime.m_pDateTime) ); }
437bool CSG_DateTime::is_SameTime (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsSameTime (*DateTime.m_pDateTime) ); }
438
439//---------------------------------------------------------
440bool CSG_DateTime::is_Between (const CSG_DateTime &t1, const CSG_DateTime &t2) const { return( m_pDateTime->IsBetween (*t1.m_pDateTime, *t2.m_pDateTime) ); }
441bool CSG_DateTime::is_StrictlyBetween(const CSG_DateTime &t1, const CSG_DateTime &t2) const { return( m_pDateTime->IsStrictlyBetween(*t1.m_pDateTime, *t2.m_pDateTime) ); }
442
443
445// //
447
448//---------------------------------------------------------
450{
451 wxString s(m_pDateTime->Format(Format.c_str())); CSG_String _s(&s); return( _s );
452}
453
455{
456 wxString s(m_pDateTime->FormatDate()); CSG_String _s(&s); return( _s );
457}
458
460{
461 wxString s(m_pDateTime->FormatTime()); CSG_String _s(&s); return( _s );
462}
463
465{
466 wxString s(m_pDateTime->FormatISODate()); CSG_String _s(&s); return( _s );
467}
468
470{
471 wxString s(m_pDateTime->FormatISOTime()); CSG_String _s(&s); return( _s );
472}
473
475{
476 wxString s(m_pDateTime->FormatISOCombined(sep)); CSG_String _s(&s); return( _s );
477}
478
479//---------------------------------------------------------
481{
482 wxString::const_iterator end; return( m_pDateTime->ParseDate(date.c_str(), &end) );
483}
484
486{
487 wxString::const_iterator end; return( m_pDateTime->ParseDateTime(datetime.c_str(), &end) );
488}
489
490bool CSG_DateTime::Parse_Format(const CSG_String &date, const CSG_String &format, const CSG_DateTime &dateDef)
491{
492 wxString::const_iterator end; return( m_pDateTime->ParseFormat(date.c_str(), format.c_str(), *dateDef.m_pDateTime, &end) );
493}
494
495bool CSG_DateTime::Parse_Format(const CSG_String &date, const CSG_String &format)
496{
497 wxString::const_iterator end; return( m_pDateTime->ParseFormat(date.c_str(), format.c_str(), &end) );
498}
499
501{
502 wxString::const_iterator end; return( m_pDateTime->ParseFormat(date.c_str(), &end) );
503}
504
505//---------------------------------------------------------
507{
508 return( m_pDateTime->ParseISOCombined(date.c_str(), sep) );
509}
510
512{
513 return( m_pDateTime->ParseISODate(date.c_str()) );
514}
515
517{
518 return( m_pDateTime->ParseISOTime(date.c_str()) );
519}
520
521
523// //
525
526//---------------------------------------------------------
528{
530
531 Now.Set_To_Current();
532
533 return( Now.Get_Day() );
534}
535
537{
538 return( (Month)wxDateTime::GetCurrentMonth() );
539}
540
542{
543 return( wxDateTime::GetCurrentYear() );
544}
545
546//---------------------------------------------------------
548{
549 wxString s(wxDateTime::GetMonthName ((wxDateTime::Month)month , (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
550}
551
553{
554 wxString s(wxDateTime::GetEnglishMonthName ((wxDateTime::Month)month , (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
555}
556
558{
559 wxString s(wxDateTime::GetWeekDayName ((wxDateTime::WeekDay)weekday, (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
560}
561
563{
564 wxString s(wxDateTime::GetEnglishWeekDayName((wxDateTime::WeekDay)weekday, (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
565}
566
567//---------------------------------------------------------
569{
570 return( (TSG_DateTime)wxDateTime::GetNumberOfDays(year) );
571}
572
574{
575 return( (TSG_DateTime)wxDateTime::GetNumberOfDays((wxDateTime::Month)month, year) );
576}
577
578//---------------------------------------------------------
580{
581 return( wxDateTime::IsLeapYear(year) );
582}
583
584//---------------------------------------------------------
586{
587 CSG_DateTime DateTime; *DateTime.m_pDateTime = wxDateTime::Now();
588
589 return( DateTime );
590}
591
592
594// //
596
597//---------------------------------------------------------
599{
600 time_t tUnix = Seconds;
601 struct tm t;
602
603 #ifdef _SAGA_LINUX
604 t = *gmtime(&tUnix);
605 #else
606 gmtime_s(&t, &tUnix);
607 #endif
608
609 Set(t.tm_mday, (CSG_DateTime::Month)(t.tm_mon), t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec);
610
611 return( *this );
612}
613
614//---------------------------------------------------------
616{
617 long d, n;
618 double h;
619
620 h = 1721424.0 + (Hours - 12.0) / 24.0;
621 d = (long)h; // Truncate to integral day
622 h = h - d + 0.5; // Fractional part of calendar day
623 if( h >= 1.0 ) // Is it really the next calendar day?
624 {
625 h--;
626 d++;
627 }
628
629 int day, mon, year, hour, min, sec;
630
631 h = 24.0 * (h);
632 hour = (int)h;
633 h = 60.0 * (h - hour);
634 min = (int)h;
635 h = 60.0 * (h - min);
636 sec = (int)h;
637
638 d = d + 68569;
639 n = 4 * d / 146097;
640 d = d - (146097 * n + 3) / 4;
641 year = 4000 * (d + 1) / 1461001;
642 d = d - 1461 * year / 4 + 31; // 1461 = 365.25 * 4
643 mon = 80 * d / 2447;
644 day = d - 2447 * mon / 80;
645 d = mon / 11;
646 mon = mon + 2 - 12 * d;
647 year = 100 * (n - 49) + year + d;
648
649 Set(day, (CSG_DateTime::Month)(mon - 1), year, hour, min, sec);
650
651 return( *this );
652}
653
654
656// //
658
659//---------------------------------------------------------
661{
662 CSG_String Choices;
663
664 Choices.Printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|",
665 _TL("January"), _TL("February"), _TL("March" ), _TL("April" ), _TL("May" ), _TL("June" ),
666 _TL("July" ), _TL("August" ), _TL("September"), _TL("October"), _TL("November"), _TL("December")
667 );
668
669 return( Choices );
670}
671
672
674// //
676
677//---------------------------------------------------------
681//---------------------------------------------------------
682bool SG_JulianDayNumber_To_Date(double JDN, int &y, int &m, int &d)
683{
684 double Z = floor(JDN + 0.5);
685 double F = JDN + 0.5 - Z;
686 double a = floor((Z - 1867216.25) / 36524.25);
687 double A = Z + 1 + a - floor(a / 4);
688 double B = A + 1524;
689 double C = floor((B - 122.1) / 365.25);
690 double D = floor(365.25 * C);
691 double E = floor((B - D) / 30.6001);
692
693 d = int(B - D - floor(30.6001 * E) + F);
694
695 if( E <= 13 )
696 {
697 m = E - 1; y = C - 4716;
698 }
699 else
700 {
701 m = E - 13; y = C - 4715;
702 }
703
704 return( true );
705}
706
707//---------------------------------------------------------
711//---------------------------------------------------------
713{
714 CSG_String Date; int y, m, d;
715
716 if( SG_JulianDayNumber_To_Date(JDN, y, m, d) )
717 {
718 Date.Printf("%04d-%02d-%02d", y, m, d); // yyyy-mm-dd (ISO 8601)
719 }
720
721 return( Date );
722}
723
724//---------------------------------------------------------
728//---------------------------------------------------------
730{
731 return( SG_JulianDayNumber_To_Date(JDN + 0.5) );
732}
733
734//---------------------------------------------------------
741//---------------------------------------------------------
742double SG_Date_To_JulianDayNumber(int Year, int Month, int Day)
743{
744 if( Month <= 2 ) { Year--; Month += 12; }
745
746 return( floor(365.25 * (Year + 4716)) + floor(30.6001 * (Month + 1)) + Day + 2. - floor(Year / 100) + floor(Year / 400) - 1524.5 );
747}
748
749//---------------------------------------------------------
755//---------------------------------------------------------
757{
758 if( Date.Length() >= 10 )
759 {
760 bool BC = Date[0] == '-';
761
762 CSG_Strings ymd(SG_String_Tokenize(BC ? Date.AfterFirst('-') : Date, "-./"));
763
764 if( ymd.Get_Count() >= 3 )
765 {
766 bool inv = ymd[2].Length() == 4;
767
768 int y = ymd[inv ? 2 : 0].asInt(); if( BC ) { y = -y; }
769 int m = ymd[ 1].asInt(); if( m < 1 ) { m = 1; } else if( m > 12 ) { m = 12; }
770 int d = ymd[inv ? 0 : 2].asInt(); if( d < 1 ) { d = 1; } else if( d > 31 ) { d = 31; }
771
772 return( SG_Date_To_JulianDayNumber(y, m, d) );
773 }
774 }
775
776 return( 0. );
777}
778
779
781// //
783
784//---------------------------------------------------------
789//---------------------------------------------------------
790int SG_Get_Day_MidOfMonth(int Month, bool bLeapYear)
791{
792 static const int MidOfMonth[12] =
793 // JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
794 // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
795 // 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
796 { 15, 45, 74, 105, 135, 166, 196, 227, 258, 288, 319, 349 };
797
798 int Day;
799
800 if( Month < 0 )
801 {
802 Month = (Month % 12) + 12;
803 Day = MidOfMonth[Month] - 365;
804 }
805 else if( Month >= 12 )
806 {
807 Month = (Month % 12);
808 Day = MidOfMonth[Month] + 365;
809 }
810 else
811 {
812 Day = MidOfMonth[Month];
813 }
814
815 if( bLeapYear && Month > 1 )
816 {
817 Day++;
818 }
819
820 return( Day );
821}
822
823
825// //
826// //
827// //
829
830//---------------------------------------------------------
831#include "mat_tools.h"
832
833//---------------------------------------------------------
840//---------------------------------------------------------
841bool SG_Get_Sun_Position(double JDN, double &RA, double &Dec)
842{
843 double T = (JDN - 2451545.) / 36525.; // Number of Julian centuries since 2000-01-01 at 12 UT (JDN = 2451545.0)
844
845 double M = 357.52910 + 35999.05030 * T - 0.0001559 * T*T - 0.00000048 * T*T*T; // mean anomaly
846
847 M *= M_DEG_TO_RAD;
848
849 double L = (280.46645 + 36000.76983 * T + 0.0003032 * T*T) // mean longitude
850 + ((1.914600 - 0.004817 * T - 0.000014 * T*T) * sin(M)
851 + (0.019993 - 0.000101 * T) * sin(2. * M) + 0.000290 * sin(3. * M)); // true longitude
852
853 L *= M_DEG_TO_RAD;
854
855 //-----------------------------------------------------
856 // convert ecliptic longitude to right ascension RA and declination delta
857
858 static const double Ecliptic_Obliquity = M_DEG_TO_RAD * 23.43929111;
859
860 double X = cos(L);
861 double Y = cos(Ecliptic_Obliquity) * sin(L);
862 double Z = sin(Ecliptic_Obliquity) * sin(L);
863 double R = sqrt(1. - Z*Z);
864
865 Dec = atan2(Z, R);
866 RA = 2. * atan2(Y, (X + R));
867
868 return( true );
869}
870
871//---------------------------------------------------------
872bool SG_Get_Sun_Position(const CSG_DateTime &Time, double &RA, double &Dec)
873{
874 return( SG_Get_Sun_Position(Time.Get_JDN(), RA, Dec) );
875}
876
877//---------------------------------------------------------
884//---------------------------------------------------------
885bool SG_Get_Sun_Position(double JDN, double Longitude, double Latitude, double &Height, double &Azimuth, bool bRefraction)
886{
887 //-----------------------------------------------------
888 // 1. Get right ascension RA and declination delta
889
890 double RA, Dec; SG_Get_Sun_Position(JDN, RA, Dec);
891
892 //-----------------------------------------------------
893 // 2. compute sidereal time (radians) at Greenwich local sidereal time at longitude (radians)
894
895 double T = (JDN - 2451545.) / 36525.;
896
897 double Theta = Longitude + M_DEG_TO_RAD * (280.46061837 + 360.98564736629 * (JDN - 2451545.) + T*T * (0.000387933 - T / 38710000.));
898
899 double HA = Theta - RA; // compute local hour angle (radians)
900
901 //-----------------------------------------------------
902 // 3. convert (HA, Dec) to horizon coordinates (height, azimuth) of the observer
903
904 Height = asin ( sin(Latitude) * sin(Dec) + cos(Latitude) * cos(Dec) * cos(HA));
905 Azimuth = atan2(-sin(HA) * cos(Dec), cos(Latitude) * sin(Dec) - sin(Latitude) * cos(Dec) * cos(HA));
906// Azimuth = atan2(-sin(HA), cos(Latitude) * tan(Dec) - sin(Latitude) * cos(HA)); // previous formula gives same result but is better because of division by zero effects...
907
908 if( bRefraction )
909 {
910 double Refraction = SG_Get_Sun_Refraction(Height, true);
911
912 if( Refraction >= 0. )
913 {
914 Height += Refraction;
915 }
916 }
917
918 return( Height > 0. );
919}
920
921//---------------------------------------------------------
922bool SG_Get_Sun_Position(const CSG_DateTime &Time, double Longitude, double Latitude, double &Height, double &Azimuth, bool bRefraction)
923{
924 return( SG_Get_Sun_Position(Time.Get_JDN(), Longitude, Latitude, Height, Azimuth, bRefraction) );
925}
926
927//---------------------------------------------------------
928bool SG_Get_Sun_Position(double JDN, double Longitude, double Latitude, CSG_Vector &Position, bool bRefraction)
929{
930 Position.Create(2); return( SG_Get_Sun_Position(JDN, Longitude, Latitude, Position[0], Position[1], bRefraction) );
931}
932
933//---------------------------------------------------------
934bool SG_Get_Sun_Position(const CSG_DateTime &Time, double Longitude, double Latitude, CSG_Vector &Position, bool bRefraction)
935{
936 Position.Create(2); return( SG_Get_Sun_Position(Time.Get_JDN(), Longitude, Latitude, Position[0], Position[1], bRefraction) );
937}
938
939//---------------------------------------------------------
946//---------------------------------------------------------
947double SG_Get_Sun_Refraction(double Height, bool bRadians)
948{
949 // 3.51823 = 1013.25mb / 288K
950
951 double z = bRadians ? M_RAD_TO_DEG * Height : Height;
952
953 if( z > -0.766 )
954 {
955 if( z >= 19.225 )
956 {
957 z = 3.51823 * 0.00452 / tan(z * M_DEG_TO_RAD);
958 }
959 else
960 {
961 z = 3.51823 * (0.1594 + z * (0.0196 + 0.00002 * z) ) / (1. + z * (0.505 + 0.0845 * z));
962 }
963
964 return( bRadians ? M_DEG_TO_RAD * z : z );
965 }
966
967 return( -1. );
968}
969
970
972// //
974
975//---------------------------------------------------------
976double SG_Get_Day_Length(int DayOfYear, double Latitude)
977{
978 double tanLat = tan(Latitude * M_DEG_TO_RAD);
979
980 double JD = DayOfYear * M_PI_360 / 365.;
981
982 double SunDec = 0.4093 * sin(JD - 1.405); // solar declination
983
984 double d = -tanLat * tan(SunDec); // sunset hour angle
985
986 return( acos(d < -1 ? -1 : d < 1 ? d : 1) * 24. / M_PI );
987}
988
989//---------------------------------------------------------
990double SG_Get_Day_Length(const CSG_DateTime &Date, double Latitude)
991{
992 return( SG_Get_Day_Length(Date.Get_DayOfYear(), Latitude) );
993}
994
995
997// //
998// //
999// //
1001
1002//---------------------------------------------------------
signed long long sLong
Definition api_core.h:158
SAGA_API_DLL_EXPORT CSG_Strings SG_String_Tokenize(const CSG_String &String, const CSG_String &Delimiters=SG_DEFAULT_DELIMITERS, TSG_String_Tokenizer_Mode Mode=SG_TOKEN_DEFAULT)
#define _TL(s)
Definition api_core.h:1568
TSG_DateTime Get_WeekOfMonth(void) const
Definition datetime.cpp:400
CSG_DateTime Add(const CSG_TimeSpan &TimeSpan) const
Definition datetime.cpp:324
bool is_Between(const CSG_DateTime &t1, const CSG_DateTime &t2) const
Definition datetime.cpp:440
unsigned short Get_Millisecond(void) const
Definition datetime.cpp:373
sLong Get_Unix_Time(void) const
Definition datetime.cpp:421
unsigned short Get_DayOfYear(void) const
Definition datetime.cpp:390
unsigned short Get_Minute(void) const
Definition datetime.cpp:375
static CSG_String Get_EnglishWeekDayName(WeekDay weekday, NameFlags flags=Name_Full)
Definition datetime.cpp:562
CSG_DateTime & Reset_Time(void)
Definition datetime.cpp:267
static TSG_DateTime Get_NumberOfDays(int year)
Definition datetime.cpp:568
CSG_DateTime & Set_Year(int Value)
Definition datetime.cpp:243
CSG_String Format_Time(void) const
Definition datetime.cpp:459
CSG_String Format(const CSG_String &Format="") const
Definition datetime.cpp:449
static CSG_String Get_Month_Choices(void)
Definition datetime.cpp:660
CSG_DateTime & Set_Hours_AD(int Hours)
Definition datetime.cpp:615
bool Parse_ISOTime(const CSG_String &date)
Definition datetime.cpp:516
CSG_DateTime & Set_Day(unsigned short Value)
Definition datetime.cpp:227
CSG_DateTime & Set_DayOfYear(unsigned short Value)
Definition datetime.cpp:251
bool Parse_Date(const CSG_String &date)
Definition datetime.cpp:480
sLong Get_Value(void) const
Definition datetime.cpp:313
static CSG_String Get_MonthName(Month month, NameFlags flags=Name_Full)
Definition datetime.cpp:547
static int Get_Current_Year(void)
Definition datetime.cpp:541
Month Get_Month(void) const
Definition datetime.cpp:379
double Get_MJD(void) const
Definition datetime.cpp:416
static CSG_String Get_EnglishMonthName(Month month, NameFlags flags=Name_Full)
Definition datetime.cpp:552
bool is_DST(void) const
Definition datetime.cpp:302
CSG_DateTime & Set_To_Current(void)
Definition datetime.cpp:259
CSG_DateTime From_UTC(bool noDST=false) const
Definition datetime.cpp:280
bool is_EarlierThan(const CSG_DateTime &DateTime) const
Definition datetime.cpp:431
unsigned short Get_Second(void) const
Definition datetime.cpp:374
bool is_SameDate(const CSG_DateTime &DateTime) const
Definition datetime.cpp:436
static bool is_LeapYear(int year=Inv_Year)
Definition datetime.cpp:579
CSG_String Format_ISOCombined(char sep='T') const
Definition datetime.cpp:474
bool is_EqualTo(const CSG_DateTime &DateTime) const
Definition datetime.cpp:432
CSG_String Format_ISOTime(void) const
Definition datetime.cpp:469
CSG_DateTime & Make_UTC(bool noDST=false)
Definition datetime.cpp:294
CSG_DateTime To_UTC(bool noDST=false) const
Definition datetime.cpp:288
bool Parse_DateTime(const CSG_String &datetime)
Definition datetime.cpp:485
CSG_DateTime & Set_Minute(unsigned short Value)
Definition datetime.cpp:197
CSG_DateTime & Set_Millisecond(unsigned short Value)
Definition datetime.cpp:181
static Month Get_Current_Month(void)
Definition datetime.cpp:536
static TSG_DateTime Get_Current_Day(void)
Definition datetime.cpp:527
unsigned short TSG_DateTime
Definition datetime.h:189
WeekDay Get_WeekDay(void) const
Definition datetime.cpp:395
CSG_DateTime & Set_Unix_Time(sLong Seconds)
Definition datetime.cpp:598
CSG_DateTime(void)
Definition datetime.cpp:82
CSG_String Format_ISODate(void) const
Definition datetime.cpp:464
int Get_Year(void) const
Definition datetime.cpp:384
double Get_JDN(void) const
Definition datetime.cpp:411
CSG_DateTime & Set_Month(unsigned short Value)
Definition datetime.cpp:235
CSG_String Format_Date(void) const
Definition datetime.cpp:454
unsigned short Get_Day(void) const
Definition datetime.cpp:377
bool is_StrictlyBetween(const CSG_DateTime &t1, const CSG_DateTime &t2) const
Definition datetime.cpp:441
bool is_Valid(void) const
Definition datetime.cpp:367
bool is_SameTime(const CSG_DateTime &DateTime) const
Definition datetime.cpp:437
static CSG_DateTime Now(void)
Definition datetime.cpp:585
virtual ~CSG_DateTime(void)
Definition datetime.cpp:120
unsigned short Get_Hour(void) const
Definition datetime.cpp:376
bool Parse_ISODate(const CSG_String &date)
Definition datetime.cpp:511
bool Parse_Format(const CSG_String &date, const CSG_String &format, const CSG_DateTime &dateDef)
Definition datetime.cpp:490
bool Parse_ISOCombined(const CSG_String &date, char sep='T')
Definition datetime.cpp:506
CSG_DateTime Subtract(const CSG_TimeSpan &TimeSpan) const
Definition datetime.cpp:339
CSG_DateTime & Set(const CSG_DateTime &DateTime)
Definition datetime.cpp:131
bool is_LaterThan(const CSG_DateTime &DateTime) const
Definition datetime.cpp:433
static CSG_String Get_WeekDayName(WeekDay weekday, NameFlags flags=Name_Full)
Definition datetime.cpp:557
CSG_DateTime & Set_Hour(unsigned short Value)
Definition datetime.cpp:205
CSG_DateTime & Set_Second(unsigned short Value)
Definition datetime.cpp:189
TSG_DateTime Get_WeekOfYear(void) const
Definition datetime.cpp:405
size_t Length(void) const
CSG_String AfterFirst(char Character) const
CSG_String Right(size_t count) const
int asInt(void) const
const SG_Char * c_str(void) const
int Printf(const char *Format,...)
CSG_String Mid(size_t first, size_t count=0) const
CSG_String Left(size_t count) const
int Get_Count(void) const
Definition api_core.h:714
CSG_String Format(const CSG_String &format="") const
Definition datetime.cpp:65
bool Create(const CSG_Vector &Vector)
double SG_Get_Sun_Refraction(double Height, bool bRadians)
Definition datetime.cpp:947
bool SG_JulianDayNumber_To_Date(double JDN, int &y, int &m, int &d)
Definition datetime.cpp:682
double SG_Get_Day_Length(int DayOfYear, double Latitude)
Definition datetime.cpp:976
int SG_Get_Day_MidOfMonth(int Month, bool bLeapYear)
Definition datetime.cpp:790
bool SG_Get_Sun_Position(double JDN, double &RA, double &Dec)
Definition datetime.cpp:841
double SG_Date_To_JulianDayNumber(int Year, int Month, int Day)
Definition datetime.cpp:742
#define B
#define A
#define C
#define M_PI_360
Definition mat_tools.h:106
#define M_RAD_TO_DEG
Definition mat_tools.h:108
#define M_DEG_TO_RAD
Definition mat_tools.h:109
#define M_PI
Definition mat_tools.h:96