SAGA API Version 9.13
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
385{
386 return( (Month)m_pDateTime->GetMonth() );
387}
388
394int CSG_DateTime::Get_Month(bool bZeroBased) const
395{
396 return( bZeroBased ? (int)m_pDateTime->GetMonth() : 1 + (int)m_pDateTime->GetMonth() );
397}
398
400{
401 return( m_pDateTime->GetYear() );
402}
403
404//---------------------------------------------------------
405unsigned short CSG_DateTime::Get_DayOfYear(void) const
406{
407 return( m_pDateTime->GetDayOfYear() );
408}
409
411{
412 return( (WeekDay)m_pDateTime->GetWeekDay() );
413}
414
416{
417 return( (TSG_DateTime)m_pDateTime->GetWeekOfMonth() );
418}
419
421{
422 return( (TSG_DateTime)m_pDateTime->GetWeekOfYear() );
423}
424
425//---------------------------------------------------------
426double CSG_DateTime::Get_JDN(void) const
427{
428 return( m_pDateTime->GetJDN() );
429}
430
431double CSG_DateTime::Get_MJD(void) const
432{
433 return( m_pDateTime->GetMJD() );
434}
435
437{
438 return( m_pDateTime->GetTicks() );
439}
440
442// //
444
445//---------------------------------------------------------
446bool CSG_DateTime::is_EarlierThan (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsEarlierThan(*DateTime.m_pDateTime) ); }
447bool CSG_DateTime::is_EqualTo (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsEqualTo (*DateTime.m_pDateTime) ); }
448bool CSG_DateTime::is_LaterThan (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsLaterThan (*DateTime.m_pDateTime) ); }
449
450//---------------------------------------------------------
451bool CSG_DateTime::is_SameDate (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsSameDate (*DateTime.m_pDateTime) ); }
452bool CSG_DateTime::is_SameTime (const CSG_DateTime &DateTime) const { return( m_pDateTime->IsSameTime (*DateTime.m_pDateTime) ); }
453
454//---------------------------------------------------------
455bool CSG_DateTime::is_Between (const CSG_DateTime &t1, const CSG_DateTime &t2) const { return( m_pDateTime->IsBetween (*t1.m_pDateTime, *t2.m_pDateTime) ); }
456bool CSG_DateTime::is_StrictlyBetween(const CSG_DateTime &t1, const CSG_DateTime &t2) const { return( m_pDateTime->IsStrictlyBetween(*t1.m_pDateTime, *t2.m_pDateTime) ); }
457
458
460// //
462
463//---------------------------------------------------------
465{
466 wxString s(m_pDateTime->Format(Format.c_str())); CSG_String _s(&s); return( _s );
467}
468
470{
471 wxString s(m_pDateTime->FormatDate()); CSG_String _s(&s); return( _s );
472}
473
475{
476 wxString s(m_pDateTime->FormatTime()); CSG_String _s(&s); return( _s );
477}
478
480{
481 wxString s(m_pDateTime->FormatISODate()); CSG_String _s(&s); return( _s );
482}
483
485{
486 wxString s(m_pDateTime->FormatISOTime()); CSG_String _s(&s); return( _s );
487}
488
490{
491 wxString s(m_pDateTime->FormatISOCombined(sep)); CSG_String _s(&s); return( _s );
492}
493
494//---------------------------------------------------------
496{
497 wxString::const_iterator end; return( m_pDateTime->ParseDate(date.c_str(), &end) );
498}
499
501{
502 wxString::const_iterator end; return( m_pDateTime->ParseDateTime(datetime.c_str(), &end) );
503}
504
505bool CSG_DateTime::Parse_Format(const CSG_String &date, const CSG_String &format, const CSG_DateTime &dateDef)
506{
507 wxString::const_iterator end; return( m_pDateTime->ParseFormat(date.c_str(), format.c_str(), *dateDef.m_pDateTime, &end) );
508}
509
510bool CSG_DateTime::Parse_Format(const CSG_String &date, const CSG_String &format)
511{
512 wxString::const_iterator end; return( m_pDateTime->ParseFormat(date.c_str(), format.c_str(), &end) );
513}
514
516{
517 wxString::const_iterator end; return( m_pDateTime->ParseFormat(date.c_str(), &end) );
518}
519
520//---------------------------------------------------------
522{
523 return( m_pDateTime->ParseISOCombined(date.c_str(), sep) );
524}
525
527{
528 return( m_pDateTime->ParseISODate(date.c_str()) );
529}
530
532{
533 return( m_pDateTime->ParseISOTime(date.c_str()) );
534}
535
536
538// //
540
541//---------------------------------------------------------
543{
545
546 Now.Set_To_Current();
547
548 return( Now.Get_Day() );
549}
550
552{
553 return( (Month)wxDateTime::GetCurrentMonth() );
554}
555
557{
558 return( wxDateTime::GetCurrentYear() );
559}
560
561//---------------------------------------------------------
563{
564 wxString s(wxDateTime::GetMonthName ((wxDateTime::Month)month , (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
565}
566
568{
569 wxString s(wxDateTime::GetEnglishMonthName ((wxDateTime::Month)month , (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
570}
571
573{
574 wxString s(wxDateTime::GetWeekDayName ((wxDateTime::WeekDay)weekday, (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
575}
576
578{
579 wxString s(wxDateTime::GetEnglishWeekDayName((wxDateTime::WeekDay)weekday, (wxDateTime::NameFlags)flags)); CSG_String _s(&s); return( _s );
580}
581
582//---------------------------------------------------------
584{
585 return( (TSG_DateTime)wxDateTime::GetNumberOfDays(year) );
586}
587
589{
590 return( (TSG_DateTime)wxDateTime::GetNumberOfDays((wxDateTime::Month)month, year) );
591}
592
593//---------------------------------------------------------
595{
596 return( wxDateTime::IsLeapYear(year) );
597}
598
599//---------------------------------------------------------
601{
602 CSG_DateTime DateTime; *DateTime.m_pDateTime = wxDateTime::Now();
603
604 return( DateTime );
605}
606
607
609// //
611
612//---------------------------------------------------------
614{
615 time_t tUnix = Seconds;
616 struct tm t;
617
618 #ifdef _SAGA_LINUX
619 t = *gmtime(&tUnix);
620 #else
621 gmtime_s(&t, &tUnix);
622 #endif
623
624 Set(t.tm_mday, (CSG_DateTime::Month)(t.tm_mon), t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec);
625
626 return( *this );
627}
628
629//---------------------------------------------------------
631{
632 long d, n;
633 double h;
634
635 h = 1721424.0 + (Hours - 12.0) / 24.0;
636 d = (long)h; // Truncate to integral day
637 h = h - d + 0.5; // Fractional part of calendar day
638 if( h >= 1.0 ) // Is it really the next calendar day?
639 {
640 h--;
641 d++;
642 }
643
644 int day, mon, year, hour, min, sec;
645
646 h = 24.0 * (h);
647 hour = (int)h;
648 h = 60.0 * (h - hour);
649 min = (int)h;
650 h = 60.0 * (h - min);
651 sec = (int)h;
652
653 d = d + 68569;
654 n = 4 * d / 146097;
655 d = d - (146097 * n + 3) / 4;
656 year = 4000 * (d + 1) / 1461001;
657 d = d - 1461 * year / 4 + 31; // 1461 = 365.25 * 4
658 mon = 80 * d / 2447;
659 day = d - 2447 * mon / 80;
660 d = mon / 11;
661 mon = mon + 2 - 12 * d;
662 year = 100 * (n - 49) + year + d;
663
664 Set(day, (CSG_DateTime::Month)(mon - 1), year, hour, min, sec);
665
666 return( *this );
667}
668
669
671// //
673
674//---------------------------------------------------------
676{
677 CSG_String Choices;
678
679 Choices.Printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|",
680 _TL("January"), _TL("February"), _TL("March" ), _TL("April" ), _TL("May" ), _TL("June" ),
681 _TL("July" ), _TL("August" ), _TL("September"), _TL("October"), _TL("November"), _TL("December")
682 );
683
684 return( Choices );
685}
686
687
689// //
691
692//---------------------------------------------------------
696//---------------------------------------------------------
697bool SG_JulianDayNumber_To_Date(double JDN, int &y, int &m, int &d)
698{
699 double Z = floor(JDN + 0.5);
700 double F = JDN + 0.5 - Z;
701 double a = floor((Z - 1867216.25) / 36524.25);
702 double A = Z + 1 + a - floor(a / 4);
703 double B = A + 1524;
704 double C = floor((B - 122.1) / 365.25);
705 double D = floor(365.25 * C);
706 double E = floor((B - D) / 30.6001);
707
708 d = int(B - D - floor(30.6001 * E) + F);
709
710 if( E <= 13 )
711 {
712 m = E - 1; y = C - 4716;
713 }
714 else
715 {
716 m = E - 13; y = C - 4715;
717 }
718
719 return( true );
720}
721
722//---------------------------------------------------------
726//---------------------------------------------------------
728{
729 CSG_String Date; int y, m, d;
730
731 if( SG_JulianDayNumber_To_Date(JDN, y, m, d) )
732 {
733 Date.Printf("%04d-%02d-%02d", y, m, d); // yyyy-mm-dd (ISO 8601)
734 }
735
736 return( Date );
737}
738
739//---------------------------------------------------------
743//---------------------------------------------------------
745{
746 return( SG_JulianDayNumber_To_Date(JDN + 0.5) );
747}
748
749//---------------------------------------------------------
756//---------------------------------------------------------
757double SG_Date_To_JulianDayNumber(int Year, int Month, int Day)
758{
759 if( Month <= 2 ) { Year--; Month += 12; }
760
761 return( floor(365.25 * (Year + 4716)) + floor(30.6001 * (Month + 1)) + Day + 2. - floor(Year / 100) + floor(Year / 400) - 1524.5 );
762}
763
764//---------------------------------------------------------
770//---------------------------------------------------------
772{
773 if( Date.Length() >= 10 )
774 {
775 bool BC = Date[0] == '-';
776
777 CSG_Strings ymd(SG_String_Tokenize(BC ? Date.AfterFirst('-') : Date, "-./"));
778
779 if( ymd.Get_Count() >= 3 )
780 {
781 bool inv = ymd[2].Length() == 4;
782
783 int y = ymd[inv ? 2 : 0].asInt(); if( BC ) { y = -y; }
784 int m = ymd[ 1].asInt(); if( m < 1 ) { m = 1; } else if( m > 12 ) { m = 12; }
785 int d = ymd[inv ? 0 : 2].asInt(); if( d < 1 ) { d = 1; } else if( d > 31 ) { d = 31; }
786
787 return( SG_Date_To_JulianDayNumber(y, m, d) );
788 }
789 }
790
791 return( 0. );
792}
793
794
796// //
798
799//---------------------------------------------------------
804//---------------------------------------------------------
805int SG_Get_Day_MidOfMonth(int Month, bool bLeapYear)
806{
807 static const int MidOfMonth[12] =
808 // JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
809 // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
810 // 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
811 { 15, 45, 74, 105, 135, 166, 196, 227, 258, 288, 319, 349 };
812
813 int Day;
814
815 if( Month < 0 )
816 {
817 Month = (Month % 12) + 12;
818 Day = MidOfMonth[Month] - 365;
819 }
820 else if( Month >= 12 )
821 {
822 Month = (Month % 12);
823 Day = MidOfMonth[Month] + 365;
824 }
825 else
826 {
827 Day = MidOfMonth[Month];
828 }
829
830 if( bLeapYear && Month > 1 )
831 {
832 Day++;
833 }
834
835 return( Day );
836}
837
838
840// //
841// //
842// //
844
845//---------------------------------------------------------
846#include "mat_tools.h"
847
848//---------------------------------------------------------
855//---------------------------------------------------------
856bool SG_Get_Sun_Position(double JDN, double &RA, double &Dec)
857{
858 double T = (JDN - 2451545.) / 36525.; // Number of Julian centuries since 2000-01-01 at 12 UT (JDN = 2451545.0)
859
860 double M = 357.52910 + 35999.05030 * T - 0.0001559 * T*T - 0.00000048 * T*T*T; // mean anomaly
861
862 M *= M_DEG_TO_RAD;
863
864 double L = (280.46645 + 36000.76983 * T + 0.0003032 * T*T) // mean longitude
865 + ((1.914600 - 0.004817 * T - 0.000014 * T*T) * sin(M)
866 + (0.019993 - 0.000101 * T) * sin(2. * M) + 0.000290 * sin(3. * M)); // true longitude
867
868 L *= M_DEG_TO_RAD;
869
870 //-----------------------------------------------------
871 // convert ecliptic longitude to right ascension RA and declination delta
872
873 static const double Ecliptic_Obliquity = M_DEG_TO_RAD * 23.43929111;
874
875 double X = cos(L);
876 double Y = cos(Ecliptic_Obliquity) * sin(L);
877 double Z = sin(Ecliptic_Obliquity) * sin(L);
878 double R = sqrt(1. - Z*Z);
879
880 Dec = atan2(Z, R);
881 RA = 2. * atan2(Y, (X + R));
882
883 return( true );
884}
885
886//---------------------------------------------------------
887bool SG_Get_Sun_Position(const CSG_DateTime &Time, double &RA, double &Dec)
888{
889 return( SG_Get_Sun_Position(Time.Get_JDN(), RA, Dec) );
890}
891
892//---------------------------------------------------------
899//---------------------------------------------------------
900bool SG_Get_Sun_Position(double JDN, double Longitude, double Latitude, double &Height, double &Azimuth, bool bRefraction)
901{
902 //-----------------------------------------------------
903 // 1. Get right ascension RA and declination delta
904
905 double RA, Dec; SG_Get_Sun_Position(JDN, RA, Dec);
906
907 //-----------------------------------------------------
908 // 2. compute sidereal time (radians) at Greenwich local sidereal time at longitude (radians)
909
910 double T = (JDN - 2451545.) / 36525.;
911
912 double Theta = Longitude + M_DEG_TO_RAD * (280.46061837 + 360.98564736629 * (JDN - 2451545.) + T*T * (0.000387933 - T / 38710000.));
913
914 double HA = Theta - RA; // compute local hour angle (radians)
915
916 //-----------------------------------------------------
917 // 3. convert (HA, Dec) to horizon coordinates (height, azimuth) of the observer
918
919 Height = asin ( sin(Latitude) * sin(Dec) + cos(Latitude) * cos(Dec) * cos(HA));
920 Azimuth = atan2(-sin(HA) * cos(Dec), cos(Latitude) * sin(Dec) - sin(Latitude) * cos(Dec) * cos(HA));
921// 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...
922
923 if( bRefraction )
924 {
925 double Refraction = SG_Get_Sun_Refraction(Height, true);
926
927 if( Refraction >= 0. )
928 {
929 Height += Refraction;
930 }
931 }
932
933 return( Height > 0. );
934}
935
936//---------------------------------------------------------
937bool SG_Get_Sun_Position(const CSG_DateTime &Time, double Longitude, double Latitude, double &Height, double &Azimuth, bool bRefraction)
938{
939 return( SG_Get_Sun_Position(Time.Get_JDN(), Longitude, Latitude, Height, Azimuth, bRefraction) );
940}
941
942//---------------------------------------------------------
943bool SG_Get_Sun_Position(double JDN, double Longitude, double Latitude, CSG_Vector &Position, bool bRefraction)
944{
945 Position.Create(2); return( SG_Get_Sun_Position(JDN, Longitude, Latitude, Position[0], Position[1], bRefraction) );
946}
947
948//---------------------------------------------------------
949bool SG_Get_Sun_Position(const CSG_DateTime &Time, double Longitude, double Latitude, CSG_Vector &Position, bool bRefraction)
950{
951 Position.Create(2); return( SG_Get_Sun_Position(Time.Get_JDN(), Longitude, Latitude, Position[0], Position[1], bRefraction) );
952}
953
954//---------------------------------------------------------
961//---------------------------------------------------------
962double SG_Get_Sun_Refraction(double Height, bool bRadians)
963{
964 // 3.51823 = 1013.25mb / 288K
965
966 double z = bRadians ? M_RAD_TO_DEG * Height : Height;
967
968 if( z > -0.766 )
969 {
970 if( z >= 19.225 )
971 {
972 z = 3.51823 * 0.00452 / tan(z * M_DEG_TO_RAD);
973 }
974 else
975 {
976 z = 3.51823 * (0.1594 + z * (0.0196 + 0.00002 * z) ) / (1. + z * (0.505 + 0.0845 * z));
977 }
978
979 return( bRadians ? M_DEG_TO_RAD * z : z );
980 }
981
982 return( -1. );
983}
984
985
987// //
989
990//---------------------------------------------------------
991double SG_Get_Day_Length(int DayOfYear, double Latitude)
992{
993 double tanLat = tan(Latitude * M_DEG_TO_RAD);
994
995 double JD = DayOfYear * M_PI_360 / 365.;
996
997 double SunDec = 0.4093 * sin(JD - 1.405); // solar declination
998
999 double d = -tanLat * tan(SunDec); // sunset hour angle
1000
1001 return( acos(d < -1 ? -1 : d < 1 ? d : 1) * 24. / M_PI );
1002}
1003
1004//---------------------------------------------------------
1005double SG_Get_Day_Length(const CSG_DateTime &Date, double Latitude)
1006{
1007 return( SG_Get_Day_Length(Date.Get_DayOfYear(), Latitude) );
1008}
1009
1010
1012// //
1013// //
1014// //
1016
1017//---------------------------------------------------------
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:1618
TSG_DateTime Get_WeekOfMonth(void) const
Definition datetime.cpp:415
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:455
unsigned short Get_Millisecond(void) const
Definition datetime.cpp:373
sLong Get_Unix_Time(void) const
Definition datetime.cpp:436
unsigned short Get_DayOfYear(void) const
Definition datetime.cpp:405
unsigned short Get_Minute(void) const
Definition datetime.cpp:375
static CSG_String Get_EnglishWeekDayName(WeekDay weekday, NameFlags flags=Name_Full)
Definition datetime.cpp:577
CSG_DateTime & Reset_Time(void)
Definition datetime.cpp:267
static TSG_DateTime Get_NumberOfDays(int year)
Definition datetime.cpp:583
CSG_DateTime & Set_Year(int Value)
Definition datetime.cpp:243
CSG_String Format_Time(void) const
Definition datetime.cpp:474
CSG_String Format(const CSG_String &Format="") const
Definition datetime.cpp:464
static CSG_String Get_Month_Choices(void)
Definition datetime.cpp:675
CSG_DateTime & Set_Hours_AD(int Hours)
Definition datetime.cpp:630
bool Parse_ISOTime(const CSG_String &date)
Definition datetime.cpp:531
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:495
sLong Get_Value(void) const
Definition datetime.cpp:313
static CSG_String Get_MonthName(Month month, NameFlags flags=Name_Full)
Definition datetime.cpp:562
static int Get_Current_Year(void)
Definition datetime.cpp:556
Month Get_Month(void) const
Returns the month as enum CSG_DateTime::Month (zero based, Jan = 0, Feb = 1, ...),...
Definition datetime.cpp:384
double Get_MJD(void) const
Definition datetime.cpp:431
static CSG_String Get_EnglishMonthName(Month month, NameFlags flags=Name_Full)
Definition datetime.cpp:567
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:446
unsigned short Get_Second(void) const
Definition datetime.cpp:374
bool is_SameDate(const CSG_DateTime &DateTime) const
Definition datetime.cpp:451
static bool is_LeapYear(int year=Inv_Year)
Definition datetime.cpp:594
CSG_String Format_ISOCombined(char sep='T') const
Definition datetime.cpp:489
bool is_EqualTo(const CSG_DateTime &DateTime) const
Definition datetime.cpp:447
CSG_String Format_ISOTime(void) const
Definition datetime.cpp:484
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:500
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:551
static TSG_DateTime Get_Current_Day(void)
Definition datetime.cpp:542
unsigned short TSG_DateTime
Definition datetime.h:189
WeekDay Get_WeekDay(void) const
Definition datetime.cpp:410
CSG_DateTime & Set_Unix_Time(sLong Seconds)
Definition datetime.cpp:613
CSG_DateTime(void)
Definition datetime.cpp:82
CSG_String Format_ISODate(void) const
Definition datetime.cpp:479
int Get_Year(void) const
Definition datetime.cpp:399
double Get_JDN(void) const
Definition datetime.cpp:426
CSG_DateTime & Set_Month(unsigned short Value)
Definition datetime.cpp:235
CSG_String Format_Date(void) const
Definition datetime.cpp:469
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:456
bool is_Valid(void) const
Definition datetime.cpp:367
bool is_SameTime(const CSG_DateTime &DateTime) const
Definition datetime.cpp:452
static CSG_DateTime Now(void)
Definition datetime.cpp:600
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:526
bool Parse_Format(const CSG_String &date, const CSG_String &format, const CSG_DateTime &dateDef)
Definition datetime.cpp:505
bool Parse_ISOCombined(const CSG_String &date, char sep='T')
Definition datetime.cpp:521
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:448
static CSG_String Get_WeekDayName(WeekDay weekday, NameFlags flags=Name_Full)
Definition datetime.cpp:572
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:420
size_t Length(void) const
CSG_String AfterFirst(char Character) const
bool asInt(int &Value) const
CSG_String Right(size_t count) 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:725
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:962
bool SG_JulianDayNumber_To_Date(double JDN, int &y, int &m, int &d)
Definition datetime.cpp:697
double SG_Get_Day_Length(int DayOfYear, double Latitude)
Definition datetime.cpp:991
int SG_Get_Day_MidOfMonth(int Month, bool bLeapYear)
Definition datetime.cpp:805
bool SG_Get_Sun_Position(double JDN, double &RA, double &Dec)
Definition datetime.cpp:856
double SG_Date_To_JulianDayNumber(int Year, int Month, int Day)
Definition datetime.cpp:757
#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