Using pypizza datetime

This example assumes you have installed the pyceda module and installed the pypizza module.

The pytest module defines some simple date and time data types in the Xc++ language:


// DateTime.h

@import "Pizza.h"

namespace dt
{
    $enum+ class TMonth
    {
        Jan,
        Feb,
        Mar,
        Apr,
        May,
        Jun,
        Jul,
        Aug,
        Sep,
        Oct,
        Nov,
        Dec
    };

    $model+ TDate <<-os>>
    {
        int32 Year;
        TMonth Month;   // 1-12
        int32 Day;      // 1-31
    };

    inline ceda::xostream& operator<<(ceda::xostream& os, const TDate& d)
    {
        os << ceda::setfill('0') 
           << d.Month << ' ' 
           << ceda::setw(2) << d.Day << ' ' 
           << ceda::setw(4) << d.Year;
        return os;
    }

    $model+ TTime <<-os>>
    {
        int32 Hour;     // 0-23
        int32 Minute;   // 0-59
        int32 Second;   // 0-59
    };

    inline ceda::xostream& operator<<(ceda::xostream& os, const TTime& t)
    {
        os << ceda::setfill('0') 
           << ceda::setw(2) << t.Hour << ':' 
           << ceda::setw(2) << t.Minute;
        return os;
    }

    $model+ TDateTime <<-os>>
    {
        TDate Date;
        TTime Time;
    };
    
    inline ceda::xostream& operator<<(ceda::xostream& os, const TDateTime& dt)
    {
        os << dt.Date << ' ' << dt.Time; 
        return os;
    }
} // namespace dt

Using your favorite text editor, write a file named datetime-example.py as follows:


# datetime-example.py
# This is python

import pyceda
import pypizza

dt = pyceda.cns.dt

date = dt.TDate(2017,dt.TMonth.Aug,24)
print('date = ' + `date`)
print('date.Year = ' + `date.Year`)
print('date.Month = ' + `date.Month`)
print('date.Day = ' + `date.Day`)

time = dt.TTime(20,30,00)
print('time = ' + `time`)

datetime = dt.TDateTime(date,time)
print('datetime = ' + `datetime`)

print('datetime.Date = ' + `datetime.Date`)
print('datetime.Time = ' + `datetime.Time`)

In a bash terminal run datetime-example.py as follows:


# bash
python datetime-example.py

This will produce the following output

    date = dt::TDate
    {
        Year[int32] = 2017
        Month[enum dt::TMonth] = Aug
        Day[int32] = 24
    }

    date.Year = 2017
    date.Month = 7
    date.Day = 24
    time = dt::TTime
    {
        Hour[int32] = 20
        Minute[int32] = 30
        Second[int32] = 0
    }

    datetime = dt::TDateTime
    {
        Date[class dt::TDate].Year[int32] = 2017
                             .Month[enum dt::TMonth] = Aug
                             .Day[int32] = 24
        Time[class dt::TTime].Hour[int32] = 20
                             .Minute[int32] = 30
                             .Second[int32] = 0
    }

    datetime.Date = dt::TDate
    {
        Year[int32] = 2017
        Month[enum dt::TMonth] = Aug
        Day[int32] = 24
    }

    datetime.Time = dt::TTime
    {
        Hour[int32] = 20
        Minute[int32] = 30
        Second[int32] = 0
    }