wxInterval

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
dirkmausf
In need of some credit
In need of some credit
Posts: 7
Joined: Tue May 15, 2007 9:05 pm
Location: Freiburg
Contact:

wxInterval

Post by dirkmausf » Sat Jan 01, 2011 2:02 pm

Hi all!

This is a little class representing an interval (range of time). It might be usefull for you...


Happy new year!

Code: Select all


#pragma once
#include "wx/wx.h"

class wxInterval : public wxObject
{

public:
	wxInterval(wxDateTime s, wxDateTime e) : m_dtStart(s), m_dtEnd(e) { }
    wxInterval() { }
	wxInterval(const wxInterval& iv) : m_dtStart(iv.GetStart()), m_dtEnd(iv.GetEnd()) { m_lType=iv.m_lType; }
    virtual ~wxInterval() { }

    bool IsNull() const { return !(GetStart() < GetEnd()); }


    bool Contains(wxDateTime date) const
    {
		return date.IsBetween(GetStart(),GetEnd());
    }

    bool Contains(const wxInterval& i) const
    {
        return (GetStart() <= i.GetStart()) && (i.GetEnd() <= GetEnd());
    }

    bool Overlap(const wxInterval& i)
    {
        // Sets the interval to the overlapping interval.
        if (GetEnd() <= i.GetStart() || GetStart() >= i.GetEnd())
        {
            // Intervals do not overlap.
			m_dtStart = GetStart()-wxTimeSpan::Day();
            return false;
        }

        if (GetStart() < i.GetStart())
            m_dtStart = i.GetStart();
        if (GetEnd() > i.GetEnd())
            m_dtEnd = i.GetEnd();

        return true;
    }
    

    bool Overlaps(const wxInterval& i) const
    {
        return (GetStart() <= GetEnd() && i.GetStart() <= i.GetEnd() &&
                ((GetStart() <= i.GetStart() && i.GetStart() <= GetEnd()) ||
                 (i.GetStart() <= GetStart() && GetStart() <= i.GetEnd())));
    }
    
    bool Prepend(const wxInterval& i)
    {
        if (((i.GetEnd() + wxTimeSpan::Day()) == GetStart()) && (i.GetStart() < GetStart()))
        {
            m_dtStart = i.GetStart();
            return true;
        }
        return false;
    }
    
    bool Append(const wxInterval& i)
    {
        if (((GetEnd() + wxTimeSpan::Day()) == i.GetStart()) && ((GetEnd() + wxTimeSpan::Day()) <= i.GetEnd()))
        {
            m_dtEnd = i.GetEnd();
            return true;
        }
        return false;
    }
    
    int Compare(const wxInterval& i) const
    {
        if (GetEnd() < i.GetStart())
            return -1;  // interval is below i
        else if (i.GetEnd() < GetStart())
            return 1;   // interval above below i
        else
            return 0;   // interval overlap
    }
    
    bool operator==(const wxInterval& i) const
    {
        return GetStart() == i.GetStart() && GetEnd() == i.GetEnd();
    }
    
    bool operator!=(const wxInterval& i) const
    {
        return GetStart() != i.GetStart() || GetEnd() != i.GetEnd();
    } 

    inline wxDateTime GetStart() const { return m_dtStart; }
    void SetStart(wxDateTime s) { m_dtStart = s; }

    inline wxDateTime GetEnd() const { return m_dtEnd; }
    void SetEnd(wxDateTime e) { m_dtEnd = e; }

    wxTimeSpan GetDuration() const { return GetEnd() - GetStart(); }

    const wxInterval& FirstDay()
    {
        m_dtStart.SetDay(1);
        m_dtEnd = GetStart()+wxTimeSpan::Day();
        return *this;
    }

    const wxInterval& FirstWeek()
    {
		m_dtStart.SetToPrevWeekDay(wxDateTime::WeekDay::Mon); //, weekStartsMonday);
		m_dtEnd = m_dtStart.GetLastWeekDay(wxDateTime::WeekDay::Mon);
        return *this;
    }

    const wxInterval& FirstMonth()
    {
        m_dtStart.SetDay(1);
		m_dtEnd = m_dtStart.GetLastMonthDay();
        return *this;
    }

    const wxInterval& FirstQuarter()
    {
        m_dtStart.SetMonth(((wxDateTime::Month)((GetStart().GetMonth()/3)*3)));
		m_dtStart.SetDay(1);
        m_dtEnd = m_dtStart;
		m_dtEnd.SetMonth(((wxDateTime::Month)(GetStart().GetMonth()+3)));
		m_dtEnd.SetToLastMonthDay();
        return *this;
    }

    const wxInterval& FirstYear()
    {
        m_dtStart.SetDay(1);
		m_dtStart.SetMonth(wxDateTime::Month::Jan);
        m_dtEnd = m_dtStart;
		m_dtEnd.SetMonth(wxDateTime::Month::Dec);
		m_dtEnd.SetToLastMonthDay();
        return *this;
    }

protected:
    /// The start of the time interval.
	wxDateTime m_dtStart;
    /// The end of the time interval. This value is part of the interval.
	wxDateTime m_dtEnd;

};

Greetings,

Dirk

Post Reply