How to use the hl7apy.base_datatypes.TM function in hl7apy

To help you get started, we’ve selected a few hl7apy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crs4 / hl7apy / tests / test_datatypes.py View on Github external
def test_TM_default_format(self):
        time = datetime.strptime('010111.1110', '%H%M%S.%f')
        tm = TM(time)
        self.assertEqual(tm.to_er7(), '010111.1110')
github crs4 / hl7apy / tests / test_datatypes.py View on Github external
def test_TM_custom_microsec_precision(self):
        time = datetime.strptime('011959.1234', '%H%M%S.%f')
        tm = TM(time, microsec_precision=1)
        self.assertEqual(tm.to_er7(), '011959.1')
        tm = TM(time, microsec_precision=2)
        self.assertEqual(tm.to_er7(), '011959.12')
        tm = TM(time, microsec_precision=3)
        self.assertEqual(tm.to_er7(), '011959.123')
        tm = TM(time, microsec_precision=4)
        self.assertEqual(tm.to_er7(), '011959.1234')
github crs4 / hl7apy / tests / test_datatypes.py View on Github external
def test_TM(self):

        time = datetime.strptime('01', '%H')
        time1 = datetime.strptime('0101', '%H%M')
        time2 = datetime.strptime('010111', '%H%M%S')
        time3 = datetime.strptime('010111.1110', '%H%M%S.%f')
        tm = TM(time, out_format='%H')
        tm1 = TM(time1, out_format='%H%M')
        tm2 = TM(time2, out_format='%H%M%S')
        tm3 = TM(time3, out_format='%H%M%S.%f')
        self.assertEqual(tm.classname, 'TM')
        self.assertEqual(tm.to_er7(), '01')
        self.assertEqual(tm1.to_er7(), '0101')
        self.assertEqual(tm2.to_er7(), '010111')
        self.assertEqual(tm3.to_er7(), '010111.1110')
github crs4 / hl7apy / tests / test_datatypes.py View on Github external
dtime = datetime.strptime('2013', '%Y')
        dtime1 = datetime.strptime('201307', '%Y%m')
        dtime2 = datetime.strptime('20130715', '%Y%m%d')
        dtime3 = datetime.strptime('01', '%H')
        dtime4 = datetime.strptime('0101', '%H%M')
        dtime5 = datetime.strptime('010111', '%H%M%S')
        dtime6 = datetime.strptime('010111.1110', '%H%M%S.%f')
        dtime7 = datetime.strptime('20130715010111.1110', '%Y%m%d%H%M%S.%f')

        dtm = DTM(dtime, out_format='%Y')
        dtm1 = DTM(dtime1, out_format='%Y%m')
        dtm2 = DTM(dtime2, out_format='%Y%m%d')
        dtm3 = TM(dtime3, out_format='%H')
        dtm4 = TM(dtime4, out_format='%H%M')
        dtm5 = TM(dtime5, out_format='%H%M%S')
        dtm6 = TM(dtime6, out_format='%H%M%S.%f')
        dtm7 = DTM(dtime7, out_format='%Y%m%d%H%M%S.%f')

        self.assertEqual(dtm.classname, 'DTM')
        self.assertEqual(dtm.to_er7(), '2013')
        self.assertEqual(dtm1.to_er7(), '201307')
        self.assertEqual(dtm2.to_er7(), '20130715')
        self.assertEqual(dtm3.to_er7(), '01')
        self.assertEqual(dtm4.to_er7(), '0101')
        self.assertEqual(dtm5.to_er7(), '010111')
        self.assertEqual(dtm6.to_er7(), '010111.1110')
        self.assertEqual(dtm7.to_er7(), '20130715010111.1110')
github crs4 / hl7apy / tests / test_datatypes.py View on Github external
def test_TM(self):

        time = datetime.strptime('01', '%H')
        time1 = datetime.strptime('0101', '%H%M')
        time2 = datetime.strptime('010111', '%H%M%S')
        time3 = datetime.strptime('010111.1110', '%H%M%S.%f')
        tm = TM(time, out_format='%H')
        tm1 = TM(time1, out_format='%H%M')
        tm2 = TM(time2, out_format='%H%M%S')
        tm3 = TM(time3, out_format='%H%M%S.%f')
        self.assertEqual(tm.classname, 'TM')
        self.assertEqual(tm.to_er7(), '01')
        self.assertEqual(tm1.to_er7(), '0101')
        self.assertEqual(tm2.to_er7(), '010111')
        self.assertEqual(tm3.to_er7(), '010111.1110')
github crs4 / hl7apy / tests / test_datatypes.py View on Github external
def test_TM(self):

        time = datetime.strptime('01', '%H')
        time1 = datetime.strptime('0101', '%H%M')
        time2 = datetime.strptime('010111', '%H%M%S')
        time3 = datetime.strptime('010111.1110', '%H%M%S.%f')
        tm = TM(time, out_format='%H')
        tm1 = TM(time1, out_format='%H%M')
        tm2 = TM(time2, out_format='%H%M%S')
        tm3 = TM(time3, out_format='%H%M%S.%f')
        self.assertEqual(tm.classname, 'TM')
        self.assertEqual(tm.to_er7(), '01')
        self.assertEqual(tm1.to_er7(), '0101')
        self.assertEqual(tm2.to_er7(), '010111')
        self.assertEqual(tm3.to_er7(), '010111.1110')
github crs4 / hl7apy / tests / test_datatypes.py View on Github external
def test_TM_offset(self):
        time = datetime.strptime('0101', '%H%M')
        time2 = datetime.strptime('010111.1110', '%H%M%S.%f')
        tm = TM(time, out_format='%H%M', offset='+0100')
        tm2 = TM(time2, out_format='%H%M%S.%f', offset='-0300')
        TM(time, offset='+0000')
        TM(time, offset='-0000')
        TM(time, offset='+1200')
        TM(time, offset='-1200')
        self.assertEqual(tm.offset, '+0100')
        self.assertEqual(tm2.offset, '-0300')
        self.assertEqual(tm.to_er7(), '0101+0100')  # check if a space is needed between time and offset, or not
        self.assertEqual(tm2.to_er7(), '010111.1110-0300')
github crs4 / hl7apy / tests / test_datatypes.py View on Github external
def test_DTM(self):
        dtime = datetime.strptime('2013', '%Y')
        dtime1 = datetime.strptime('201307', '%Y%m')
        dtime2 = datetime.strptime('20130715', '%Y%m%d')
        dtime3 = datetime.strptime('01', '%H')
        dtime4 = datetime.strptime('0101', '%H%M')
        dtime5 = datetime.strptime('010111', '%H%M%S')
        dtime6 = datetime.strptime('010111.1110', '%H%M%S.%f')
        dtime7 = datetime.strptime('20130715010111.1110', '%Y%m%d%H%M%S.%f')

        dtm = DTM(dtime, out_format='%Y')
        dtm1 = DTM(dtime1, out_format='%Y%m')
        dtm2 = DTM(dtime2, out_format='%Y%m%d')
        dtm3 = TM(dtime3, out_format='%H')
        dtm4 = TM(dtime4, out_format='%H%M')
        dtm5 = TM(dtime5, out_format='%H%M%S')
        dtm6 = TM(dtime6, out_format='%H%M%S.%f')
        dtm7 = DTM(dtime7, out_format='%Y%m%d%H%M%S.%f')

        self.assertEqual(dtm.classname, 'DTM')
        self.assertEqual(dtm.to_er7(), '2013')
        self.assertEqual(dtm1.to_er7(), '201307')
        self.assertEqual(dtm2.to_er7(), '20130715')
        self.assertEqual(dtm3.to_er7(), '01')
        self.assertEqual(dtm4.to_er7(), '0101')
        self.assertEqual(dtm5.to_er7(), '010111')
        self.assertEqual(dtm6.to_er7(), '010111.1110')
        self.assertEqual(dtm7.to_er7(), '20130715010111.1110')
github crs4 / hl7apy / hl7apy / base_datatypes.py View on Github external
raise InvalidDateOffset(offset)

        if offset and offset[0] not in ('+', '-'):
            raise InvalidDateOffset(offset)

        self.offset = offset

    def to_er7(self, encoding_chars=None):
        date_value = super(TM, self).to_er7()
        if self.format.find('%f') != -1:
            index = 6 - self.microsec_precision
            date_value = date_value[:-index]
        return '{0}{1}'.format(date_value, self.offset)


class DTM(TM):
    """
    Class for DTM base datatype. It extends TM and it represents classes DT and DTM combined.
    Thus it represents year, month, day, hours, minutes, seconds and microseconds.
    Parameters are the same of the superclass.

    The :attr:`allowed_formats` tuple is
    ``('%Y', '%Y%m', '%Y%m%d', '%Y%m%d%H', '%Y%m%d%H%M', '%Y%m%d%H%M%S', '%Y%m%d%H%M%S.%f')``
    """

    allowed_formats = ('%Y', '%Y%m', '%Y%m%d', '%Y%m%d%H', '%Y%m%d%H%M',
                       '%Y%m%d%H%M%S', '%Y%m%d%H%M%S.%f')

    def __init__(self, value=None, out_format='%Y%m%d%H%M%S.%f', offset='', microsec_precision=4):
        super(DTM, self).__init__(value, out_format, offset, microsec_precision)
github crs4 / hl7apy / hl7apy / base_datatypes.py View on Github external
def to_er7(self, encoding_chars=None):
        date_value = super(TM, self).to_er7()
        if self.format.find('%f') != -1:
            index = 6 - self.microsec_precision
            date_value = date_value[:-index]
        return '{0}{1}'.format(date_value, self.offset)