Exemple #1
0
import os
from pyboost import date_time
from pyboost.date_time import gregorian
from pyboost.date_time import posix_time
from pyboost.date_time import local_time
from pyboost.date_time import to_simple_string

#Date programming
weekstart = gregorian.date(2002, date_time.Feb, 1)
print 'weekstart: ', to_simple_string(weekstart)

weekend = weekstart + gregorian.weeks(1)
print 'weekend: ', to_simple_string(weekend)

today = gregorian.day_clock.local_day()
d2 = weekstart + gregorian.days(5)
if d2 >= today:  #comparison operator
    pass

thisWeek = gregorian.date_period(today, d2)
if thisWeek.contains(today):
    pass

#date generator functions
d5 = gregorian.next_weekday(today, date_time.Sunday)
#calculate Sunday following d4
print 'd5: ', to_simple_string(d5)

#US labor day is first Monday in Sept
first = gregorian.nth_day_of_the_week_in_month.first
labor_day = gregorian.nth_day_of_the_week_in_month(first, date_time.Monday,
Exemple #2
0
    def test_greg_durations(self):
        m1 = gregorian.months(5)
        m2 = gregorian.months(3)
        m3 = gregorian.months(1)
        self.failUnless(gregorian.months(8) == m1 + m2)
        m1 += m2
        self.failUnless(gregorian.months(8) == m1)
        self.failUnless(gregorian.months(-5) == m2 - m1)
        m2 -= m1
        self.failUnless(gregorian.months(-5) == m2)

        d1 = gregorian.date(2005, date_time.Jan, 1)

        self.failUnless(
            d1 +
            gregorian.months(-1) == gregorian.date(2004, date_time.Dec, 1))
        d3 = gregorian.date(2005, date_time.Dec, 1)
        self.failUnless(
            d3 -
            gregorian.months(-1) == gregorian.date(2006, date_time.Jan, 1))
        m1 = gregorian.months(5)
        m2 = gregorian.months(3)
        m3 = gregorian.months(10)
        self.failUnless(gregorian.months(15) == m1 * 3)
        m1 *= 3
        self.failUnless(gregorian.months(15) == m1)
        self.failUnless(gregorian.months(12) == m2 * 4)
        self.failUnless(gregorian.months(3) == m3 / 3)
        m3 /= 3
        self.failUnless(gregorian.months(3) == m3)

        m = gregorian.months(-5)
        m_pos = gregorian.months(date_time.pos_infin)
        m_neg = gregorian.months(date_time.neg_infin)
        m_nadt = gregorian.months(date_time.not_a_date_time)
        self.failUnless(m + m_pos == m_pos)
        self.failUnless(m + m_neg == m_neg)
        self.failUnless(m_pos + m_neg == m_nadt)
        self.failUnless(m_neg + m_neg == m_neg)
        self.failUnless(m - m_pos == m_neg)
        self.failUnless(m - m_neg == m_pos)
        self.failUnless(m_pos - m_neg == m_pos)
        self.failUnless(m_pos * -1 == m_neg)
        self.failUnless(m_pos * 0 == m_nadt)
        self.failUnless(m_neg / 3 == m_neg)

        y1 = gregorian.years(2)
        y2 = gregorian.years(4)
        self.failUnless(gregorian.months(27) == m3 + y1)
        m3 += y1
        self.failUnless(gregorian.months(27) == m3)
        self.failUnless(gregorian.months(-21) == m3 - y2)
        m3 -= y2
        self.failUnless(gregorian.months(-21) == m3)

        d = gregorian.date(2001, date_time.Oct, 31)
        self.failUnless(
            gregorian.date(2002, date_time.Feb, 28) == d + gregorian.months(4))
        d += gregorian.months(4)
        self.failUnless(gregorian.date(2002, date_time.Feb, 28) == d)
        d = gregorian.date(2001, date_time.Oct, 31)
        self.failUnless(
            gregorian.date(2001, date_time.Apr, 30) == d - gregorian.months(6))
        d -= gregorian.months(6)
        self.failUnless(gregorian.date(2001, date_time.Apr, 30) == d)

        y1 = gregorian.years(2)
        y2 = gregorian.years(4)
        y3 = gregorian.years(1)
        self.failUnless(gregorian.years(3) == y3 + y1)
        y3 += y1
        self.failUnless(gregorian.years(3) == y3)
        self.failUnless(gregorian.years(-1) == y3 - y2)
        y3 -= y2
        self.failUnless(gregorian.years(-1) == y3)

        y1 = gregorian.years(5)
        y2 = gregorian.years(3)
        y3 = gregorian.years(10)

        self.failUnless(gregorian.years(15) == y1 * 3)
        y1 *= 3
        self.failUnless(gregorian.years(15) == y1)

        self.failUnless(gregorian.years(3) == y3 / 3)
        y3 /= 3
        self.failUnless(gregorian.years(3) == y3)

        m = gregorian.years(15)
        y_pos = gregorian.years(date_time.pos_infin)
        y_neg = gregorian.years(date_time.neg_infin)
        y_nadt = gregorian.years(date_time.not_a_date_time)
        self.failUnless(m + y_pos == y_pos)
        self.failUnless(m + y_neg == y_neg)
        self.failUnless(y_pos + y_neg == y_nadt)
        self.failUnless(y_neg + y_neg == y_neg)
        self.failUnless(m - y_pos == y_neg)
        self.failUnless(m - y_neg == y_pos)
        self.failUnless(y_pos - y_neg == y_pos)
        self.failUnless(y_pos * -1 == y_neg)
        self.failUnless(y_pos * 0 == y_nadt)
        self.failUnless(y_neg / 3 == y_neg)

        m1 = gregorian.months(5)
        m2 = gregorian.months(3)
        self.failUnless(gregorian.months(39) == y2 + m2)
        self.failUnless(gregorian.months(31) == y2 - m1)

        d = gregorian.date(2001, date_time.Feb, 28)
        self.failUnless(
            gregorian.date(2004, date_time.Feb, 29) == d + gregorian.years(3))
        d += gregorian.years(3)
        self.failUnless(gregorian.date(2004, date_time.Feb, 29) == d)

        d = gregorian.date(2000, date_time.Feb, 29)
        self.failUnless(
            gregorian.date(1994, date_time.Feb, 28) == d - gregorian.years(6))
        d -= gregorian.years(6)
        self.failUnless(gregorian.date(1994, date_time.Feb, 28) == d)

        w1 = gregorian.weeks(2)
        w2 = gregorian.weeks(4)
        w3 = gregorian.weeks(1)
        pi = gregorian.weeks(date_time.pos_infin)
        self.failUnless(gregorian.weeks(date_time.pos_infin) == w1 + pi)
        self.failUnless(gregorian.weeks(3) == w3 + w1)
        w3 += w1
        self.failUnless(gregorian.weeks(3) == w3)
        self.failUnless(gregorian.weeks(-1) == w3 - w2)
        w3 -= w2
        self.failUnless(gregorian.weeks(-1) == w3)

        d = gregorian.days(10)
        self.failUnless(gregorian.days(31) == d + gregorian.weeks(3))
        d += gregorian.weeks(3)
        self.failUnless(gregorian.days(31) == d)

        d = gregorian.days(10)
        self.failUnless(gregorian.days(-32) == d - gregorian.weeks(6))
        d -= gregorian.weeks(6)
        self.failUnless(gregorian.days(-32) == d)

        d = gregorian.date(2001, date_time.Feb, 28)
        self.failUnless(
            gregorian.date(2001, date_time.Mar, 21) == d + gregorian.weeks(3))
        d += gregorian.weeks(3)
        self.failUnless(gregorian.date(2001, date_time.Mar, 21) == d)

        d = gregorian.date(2001, date_time.Feb, 28)
        self.failUnless(
            gregorian.date(2001, date_time.Jan, 17) == d - gregorian.weeks(6))
        d -= gregorian.weeks(6)
        self.failUnless(gregorian.date(2001, date_time.Jan, 17) == d)

        d = gregorian.date(2000, date_time.Oct, 31)
        d2 = d + gregorian.months(4) + gregorian.years(2)
        d3 = d + gregorian.years(2) + gregorian.months(4)
        self.failUnless(gregorian.date(2003, date_time.Feb, 28) == d2)
        self.failUnless(gregorian.date(2003, date_time.Feb, 28) == d3)
        m = gregorian.years(2) + gregorian.months(4) - gregorian.months(
            4) - gregorian.years(2)
        self.failUnless(m.number_of_months() == 0)
Exemple #3
0
import os
from pyboost import date_time
from pyboost.date_time import gregorian
from pyboost.date_time import posix_time
from pyboost.date_time import local_time
from pyboost.date_time import to_simple_string

# Date programming
weekstart = gregorian.date(2002, date_time.Feb, 1)
print "weekstart: ", to_simple_string(weekstart)

weekend = weekstart + gregorian.weeks(1)
print "weekend: ", to_simple_string(weekend)

today = gregorian.day_clock.local_day()
d2 = weekstart + gregorian.days(5)
if d2 >= today:  # comparison operator
    pass

thisWeek = gregorian.date_period(today, d2)
if thisWeek.contains(today):
    pass

# date generator functions
d5 = gregorian.next_weekday(today, date_time.Sunday)
# calculate Sunday following d4
print "d5: ", to_simple_string(d5)

# US labor day is first Monday in Sept
first = gregorian.nth_day_of_the_week_in_month.first
labor_day = gregorian.nth_day_of_the_week_in_month(first, date_time.Monday, date_time.Sep)
Exemple #4
0
    def test_greg_durations(self):
        m1 = gregorian.months(5)
        m2 = gregorian.months(3)
        m3 = gregorian.months(1)
        self.failUnless( gregorian.months(8) == m1 + m2)
        m1 += m2
        self.failUnless( gregorian.months(8) == m1)
        self.failUnless( gregorian.months(-5) == m2 - m1)
        m2 -= m1;
        self.failUnless( gregorian.months(-5) == m2)
      
        d1 = gregorian.date(2005, date_time.Jan, 1)

        self.failUnless( d1 + gregorian.months(-1) 
                         == gregorian.date(2004, date_time.Dec, 1) )
        d3 = gregorian.date(2005, date_time.Dec, 1);
        self.failUnless( d3 - gregorian.months(-1) 
                         == gregorian.date(2006,date_time.Jan,1))
        m1 = gregorian.months(5)
        m2 = gregorian.months(3)
        m3 = gregorian.months(10)
        self.failUnless( gregorian.months(15) == m1 * 3)
        m1 *= 3
        self.failUnless( gregorian.months(15) == m1)
        self.failUnless( gregorian.months(12) == m2 * 4)
        self.failUnless( gregorian.months(3) == m3 / 3)
        m3 /= 3
        self.failUnless( gregorian.months(3) == m3)

        m = gregorian.months(-5)
        m_pos = gregorian.months(date_time.pos_infin)
        m_neg = gregorian.months(date_time.neg_infin)
        m_nadt = gregorian.months(date_time.not_a_date_time)
        self.failUnless( m + m_pos == m_pos)
        self.failUnless( m + m_neg == m_neg)
        self.failUnless( m_pos + m_neg == m_nadt)
        self.failUnless( m_neg + m_neg == m_neg)
        self.failUnless( m - m_pos == m_neg)
        self.failUnless( m - m_neg == m_pos)
        self.failUnless( m_pos - m_neg == m_pos)
        self.failUnless( m_pos * -1 == m_neg)
        self.failUnless( m_pos * 0 == m_nadt)
        self.failUnless( m_neg / 3 == m_neg)
       
        y1 = gregorian.years(2)
        y2 = gregorian.years(4)
        self.failUnless( gregorian.months(27) == m3 + y1)
        m3 += y1
        self.failUnless( gregorian.months(27) == m3)
        self.failUnless( gregorian.months(-21) == m3 - y2)
        m3 -= y2
        self.failUnless( gregorian.months(-21) == m3)

        d = gregorian.date(2001, date_time.Oct, 31);
        self.failUnless( gregorian.date(2002, date_time.Feb, 28) 
                         == d + gregorian.months(4))
        d += gregorian.months(4)
        self.failUnless( gregorian.date(2002, date_time.Feb, 28) == d);
        d = gregorian.date(2001, date_time.Oct, 31);
        self.failUnless( gregorian.date(2001, date_time.Apr, 30) 
                         == d - gregorian.months(6));
        d -= gregorian.months(6);
        self.failUnless( gregorian.date(2001, date_time.Apr, 30) == d);

        y1 = gregorian.years(2)
        y2 = gregorian.years(4)
        y3 = gregorian.years(1)
        self.failUnless( gregorian.years(3) == y3 + y1)
        y3 += y1
        self.failUnless( gregorian.years(3) == y3)
        self.failUnless( gregorian.years(-1) == y3 - y2)
        y3 -= y2
        self.failUnless( gregorian.years(-1) == y3)
       
        y1 = gregorian.years(5)
        y2 = gregorian.years(3)
        y3 = gregorian.years(10)
        
        self.failUnless( gregorian.years(15) == y1 * 3)
        y1 *= 3
        self.failUnless( gregorian.years(15) == y1)

        self.failUnless( gregorian.years(3) == y3 / 3)
        y3 /= 3
        self.failUnless( gregorian.years(3) == y3)

        m = gregorian.years(15)
        y_pos = gregorian.years(date_time.pos_infin)
        y_neg = gregorian.years(date_time.neg_infin)
        y_nadt = gregorian.years(date_time.not_a_date_time)
        self.failUnless( m + y_pos == y_pos)
        self.failUnless( m + y_neg == y_neg)
        self.failUnless( y_pos + y_neg == y_nadt)
        self.failUnless( y_neg + y_neg == y_neg)
        self.failUnless( m - y_pos == y_neg)
        self.failUnless( m - y_neg == y_pos)
        self.failUnless( y_pos - y_neg == y_pos)
        self.failUnless( y_pos * -1 == y_neg)
        self.failUnless( y_pos * 0 == y_nadt)
        self.failUnless( y_neg / 3 == y_neg)

        m1 = gregorian.months(5)
        m2 = gregorian.months(3)
        self.failUnless( gregorian.months(39) == y2 + m2)
        self.failUnless( gregorian.months(31) == y2 - m1)
   
        d = gregorian.date(2001, date_time.Feb, 28)
        self.failUnless( gregorian.date(2004, date_time.Feb, 29) == d + gregorian.years(3))
        d += gregorian.years(3)
        self.failUnless( gregorian.date(2004, date_time.Feb, 29) == d)
         
        d = gregorian.date(2000, date_time.Feb, 29)
        self.failUnless( gregorian.date(1994, date_time.Feb, 28) == d - gregorian.years(6))
        d -= gregorian.years(6)
        self.failUnless( gregorian.date(1994, date_time.Feb, 28) == d)

        w1 = gregorian.weeks(2)
        w2 = gregorian.weeks(4)
        w3 = gregorian.weeks(1)
        pi = gregorian.weeks(date_time.pos_infin)
        self.failUnless( gregorian.weeks(date_time.pos_infin) == w1 + pi)
        self.failUnless( gregorian.weeks(3) == w3 + w1)
        w3 += w1
        self.failUnless( gregorian.weeks(3) == w3)
        self.failUnless( gregorian.weeks(-1) == w3 - w2)
        w3 -= w2
        self.failUnless( gregorian.weeks(-1) == w3)

        d = gregorian.days(10)
        self.failUnless( gregorian.days(31) == d + gregorian.weeks(3))
        d += gregorian.weeks(3);
        self.failUnless( gregorian.days(31) == d)

        d = gregorian.days(10)
        self.failUnless( gregorian.days(-32) == d - gregorian.weeks(6))
        d -= gregorian.weeks(6)
        self.failUnless( gregorian.days(-32) == d)
        
        d = gregorian.date(2001, date_time.Feb, 28)
        self.failUnless( gregorian.date(2001, date_time.Mar, 21) == d + gregorian.weeks(3))
        d += gregorian.weeks(3)
        self.failUnless( gregorian.date(2001, date_time.Mar, 21) == d)
        
        d = gregorian.date(2001, date_time.Feb, 28);
        self.failUnless( gregorian.date(2001, date_time.Jan, 17) == d - gregorian.weeks(6))
        d -= gregorian.weeks(6)
        self.failUnless( gregorian.date(2001, date_time.Jan, 17) == d)

        d = gregorian.date(2000, date_time.Oct, 31)
        d2 = d + gregorian.months(4) + gregorian.years(2)
        d3 = d + gregorian.years(2) + gregorian.months(4)
        self.failUnless( gregorian.date(2003, date_time.Feb,28) == d2)
        self.failUnless( gregorian.date(2003,date_time.Feb,28) == d3)
        m = gregorian.years(2) + gregorian.months(4) - gregorian.months(4) - gregorian.years(2)
        self.failUnless( m.number_of_months() == 0)