Skip to content

Commit 7e24cc2

Browse files
committed
Rewrite void wxPLTimeAxis::RecalculateTicksAndLabel() to use non-leapyear
1 parent ed70db2 commit 7e24cc2

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

src/plot/plaxis.cpp

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -668,34 +668,26 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
668668

669669
//We need to figure out whether we are looking at hours, days, or months, and label the graph appropriately.
670670
wxDateTime timeKeeper(1, wxDateTime::Jan, 1971, 0, 0, 0); // works all time zones
671-
wxDateTime timeKeeperRef(1, wxDateTime::Jan, 1971, 0, 0, 0); // works all time zones
672-
673-
// leap year for min and max values
674-
// both are tracked as hours since Jan 1, 1971
675-
int m_min_days_to_add = 0;
676-
int m_min_num_years = m_min / 8760;
677-
int m_max_num_years = m_max / 8760;
678-
679-
for (size_t i = 0; i < m_min_num_years; i++) {
680-
//wxDateTime dt = timeKeeper.Add() later than Feb 28
681-
// auto ndays = timeKeeper.GetNumberOfDays(i);
682-
if (timeKeeperRef.IsLeapYear(i))// && m_min_dt.IsLaterThan(dt))
683-
m_min_days_to_add++;
684-
// m_min_dt.Add(wxTimeSpan::Days(ndays+1));
685-
}
686-
687671

688672

689673
double world_len = m_max - m_min;
690674
double time = m_min;
675+
676+
// to skip leap years (SAM issue 289) - scale to first year
677+
int m_min_offset = m_min / 8760;
678+
double m_min_scaled = (double)((int)m_min % 8760); // m_min and m_max always based on 8760 (365 days with no leap years)
679+
int m_max_offset = m_max / 8760;
680+
double m_max_scaled = (double)((int)m_max % 8760); // m_min and m_max always based on 8760 (365 days with no leap years)
681+
if (m_max_scaled <= m_min_scaled) m_max_scaled += 8760; // handle year end points
682+
683+
time = m_min_scaled;
684+
691685
timeKeeper.Add(wxTimeSpan::Minutes(60 * time));
692686

693687
// Handle DST.
694688
if (timeKeeper.IsDST())
695689
timeKeeper.Subtract(wxTimeSpan::Hour());
696690

697-
timeKeeper.Add(wxTimeSpan::Days(m_min_days_to_add));
698-
699691

700692
if (world_len <= 72) {
701693
if (floor(time) != time) {
@@ -714,31 +706,18 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
714706
// Less than 2 days. Need to label time.
715707

716708
wxDateTime timeKeeper2(timeKeeper.GetTicks());
717-
timeKeeper2.Add(wxTimeSpan::Minutes(60 * world_len));
709+
timeKeeper2.Add(wxTimeSpan::Minutes(60 * world_len)); // TODO 289 - check scaling
718710
timeKeeper2.Subtract(wxTimeSpan::Minute()); //If it is 0:00 the next day, its really the same day.
719711
if (timeKeeper.IsDST() && !timeKeeper2.IsDST()) {
720712
timeKeeper.Add(wxTimeSpan::Hour());
721713
timeKeeper2.Add(wxTimeSpan::Hour());
722714
}
723-
724-
// TODO condition needs to be if leap year and day of year > 2/28 then add a day...
725-
if ((timeKeeper.GetDayOfYear() > (31 + 27)) && (timeKeeperRef.IsLeapYear(m_min/8760)))
726-
// if ((timeKeeper.GetMonth() == wxDateTime::Feb && timeKeeper.GetDay() == 29))
727-
timeKeeper.Add(wxTimeSpan::Hours(24));
728-
// if ((timeKeeper2.GetMonth() == wxDateTime::Feb && timeKeeper2.GetDay() == 29))
729-
if ((timeKeeper2.GetDayOfYear() > (31 + 27)) && (timeKeeperRef.IsLeapYear(m_min / 8760)))
730-
timeKeeper2.Add(wxTimeSpan::Hours(24));
731-
732-
//if (!(timeKeeper.GetMonth() == wxDateTime::Feb && timeKeeper.GetDay() == 29)
733-
// && !(timeKeeper2.GetMonth() == wxDateTime::Feb && timeKeeper2.GetDay() == 29)) {
734-
// auto x = timeKeeper.GetMonth();
735-
// auto y = timeKeeper.GetDay();
736-
// auto z = x + y;
737715
if (timeKeeper.GetDay() == timeKeeper2.GetDay())
738716
m_timeLabel = timeKeeper.Format("%b %d");
717+
else if (timeKeeper.GetMonth() == timeKeeper2.GetMonth())
718+
m_timeLabel = timeKeeper.Format("%b %d") + "-" + timeKeeper2.Format("%d"); // TODO fix year 2 Feb 28-01 (always been the case)
739719
else
740-
m_timeLabel = timeKeeper.Format("%b %d") + "-" + timeKeeper2.Format("%d");
741-
//}
720+
m_timeLabel = timeKeeper.Format("%b %d") + "-" + timeKeeper2.Format("%b %d");
742721

743722
do {
744723
m_tickList.push_back(TickData(time, timeKeeper.Format("%H"), TickData::LARGE));
@@ -775,16 +754,18 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
775754
}
776755

777756
do {
778-
if ((timeKeeper.GetMonth() == wxDateTime::Feb && timeKeeper.GetDay() == 29))
779-
timeKeeper.Add(wxTimeSpan::Hours(24));
780757
auto str = timeKeeper.Format("%b %d");
781-
m_tickList.push_back(TickData(time, str, TickData::NONE));
758+
// m_tickList.push_back(TickData(time, str, TickData::NONE));
759+
m_tickList.push_back(TickData(time + m_min_offset*8760, str, TickData::NONE));
782760
time += 12;
783-
if (time < m_max)
784-
m_tickList.push_back(TickData(time, wxEmptyString, TickData::LARGE)); // midnight
761+
// if (time < m_max)
762+
if (time < m_max_scaled)
763+
m_tickList.push_back(TickData(time + m_min_offset * 8760, wxEmptyString, TickData::LARGE)); // midnight
764+
// m_tickList.push_back(TickData(time, wxEmptyString, TickData::LARGE)); // midnight
785765
time += 12;
786766
timeKeeper.Add(wxTimeSpan::Hours(24));
787-
} while (time < m_max);
767+
} while (time < m_max_scaled);
768+
// } while (time < m_max);
788769
} else {
789770
//Assume day endpoints.
790771
//Only Label Months
@@ -800,13 +781,16 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
800781
timeKeeper2.Add(wxTimeSpan::Day());
801782
}
802783

803-
m_tickList.push_back(TickData(time + daysVisibleInMonth * 24.0f, wxEmptyString, TickData::LARGE));
784+
// m_tickList.push_back(TickData(time + daysVisibleInMonth * 24.0f, wxEmptyString, TickData::LARGE));
785+
m_tickList.push_back(TickData(time + daysVisibleInMonth * 24.0f + m_min_offset * 8760, wxEmptyString, TickData::LARGE));
804786
int endMonthHours = time + daysVisibleInMonth * 24.0f; //00:00 on first of next month.
805787

806788
if (daysVisibleInMonth >= 7) {
807789
//Label the month if it has more than seven days visible.
790+
// m_tickList.push_back(
791+
// TickData(time + (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
808792
m_tickList.push_back(
809-
TickData(time + (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
793+
TickData(time + (daysVisibleInMonth * 24.0f / 2.0f) + m_min_offset * 8760, timeKeeper.Format("%b first"), TickData::NONE));
810794
}
811795

812796
timeKeeper2.SetDay(wxDateTime::GetNumberOfDays(timeKeeper2.GetMonth()) / 2); //Middle of the second month.
@@ -816,24 +800,27 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
816800
//Loop, labeling months
817801
//While end of month is visible.
818802
do {
819-
m_tickList.push_back(TickData(time, timeKeeper2.Format("%b"), TickData::NONE));
803+
// m_tickList.push_back(TickData(time, timeKeeper2.Format("%b"), TickData::NONE));
804+
m_tickList.push_back(TickData(time + m_min_offset * 8760, timeKeeper2.Format("%b second"), TickData::NONE));
820805
timeKeeper.Set(timeKeeper2.GetTicks()); //timeKeeper is position of last label.
821806
timeKeeper2.Add(wxDateSpan::Month());
822807
timeKeeper2.SetDay(1); //timeKeeper2 is day 1 of next month.
823808

824-
m_tickList.push_back(TickData(time + timeKeeper2.Subtract(timeKeeper).GetHours(), wxEmptyString,
825-
TickData::LARGE)); //Adds tick at this pos. (start-month)
809+
// m_tickList.push_back(TickData(time + timeKeeper2.Subtract(timeKeeper).GetHours(), wxEmptyString,
810+
// TickData::LARGE)); //Adds tick at this pos. (start-month)
811+
m_tickList.push_back(TickData(time + timeKeeper2.Subtract(timeKeeper).GetHours() + m_min_offset * 8760, wxEmptyString,
812+
TickData::LARGE)); //Adds tick at this pos. (start-month)
826813
timeKeeper2.SetDay(wxDateTime::GetNumberOfDays(timeKeeper2.GetMonth()) / 2); //timeKeeper2 mid month
827814
time += timeKeeper2.Subtract(timeKeeper).GetHours(); //hours midMonth.
828815
endMonthHours += 24 * wxDateTime::GetNumberOfDays(timeKeeper2.GetMonth());
816+
// } while (endMonthHours < m_max_scaled);
829817
} while (endMonthHours < m_max);
830818

831819
//timeKeeper holds the middle of last month we actually labelled.
832820
//We still need to label the last month if it has more than 7 days showing.
833821
timeKeeper.Add(wxDateSpan::Month());
834822
timeKeeper.SetDay(1); // First not-yet-labeled month
835-
time = endMonthHours -
836-
24 * wxDateTime::GetNumberOfDays(timeKeeper.GetMonth()); //00:00 on first of not-yet-labeled month.
823+
time = endMonthHours - 24 * wxDateTime::GetNumberOfDays(timeKeeper.GetMonth()); //00:00 on first of not-yet-labeled month.
837824

838825
//Take care of fractional days at the max.
839826
timeKeeper2 = wxDateTime(01, wxDateTime::Jan, 1970, 00, 00, 00);
@@ -844,16 +831,17 @@ void wxPLTimeAxis::RecalculateTicksAndLabel() {
844831

845832
time += daysVisibleInMonth * 24.0f;
846833
timeKeeper2.Add(wxTimeSpan::Minutes(daysVisibleInMonth * 24.0f * 60.0f));
847-
while (time < m_max && timeKeeper2.GetMonth() == timeKeeper.GetMonth()) {
834+
// while (time < m_max && timeKeeper2.GetMonth() == timeKeeper.GetMonth()) {
835+
while (time < m_max_scaled && timeKeeper2.GetMonth() == timeKeeper.GetMonth()) {
848836
daysVisibleInMonth += 1;
849837
timeKeeper2.Add(wxTimeSpan::Day());
850838
time += 24;
851839
}
852840

853841
if (daysVisibleInMonth >= 7) {
854842
//Label the month if it has more than seven days visible.
855-
m_tickList.push_back(
856-
TickData(time - (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
843+
// m_tickList.push_back(TickData(time - (daysVisibleInMonth * 24.0f / 2.0f), timeKeeper.Format("%b"), TickData::NONE));
844+
m_tickList.push_back(TickData(time - (daysVisibleInMonth * 24.0f / 2.0f) + m_min_offset * 8760, timeKeeper.Format("%b third"), TickData::NONE));
857845
}
858846
}
859847

0 commit comments

Comments
 (0)