Serializing / Unserializing datetime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brendan

    Serializing / Unserializing datetime

    Hi All

    I can't find the "Python Way" of writing a datetime instance to a
    string so that it can be easily parsed back again. time.strptime is
    apparantly not supported on some platforms, and time.time <==>
    datetime.utcfro mtimestamp will cause problems come 2038. Unfortunately
    there don't seem to be "fromstring " equivalents for datetime.ctime or
    datetime.isofor mat.

    Ideally the serialized datetime should be human readable, and
    potentially parseable from other languages. Any suggestions?

    Brendan
    --
    Brendan Simons

  • John Machin

    #2
    Re: Serializing / Unserializing datetime

    On 28/05/2006 3:37 AM, Brendan wrote:[color=blue]
    > Hi All
    >
    > I can't find the "Python Way" of writing a datetime instance to a
    > string so that it can be easily parsed back again. time.strptime is
    > apparantly not supported on some platforms, and time.time <==>
    > datetime.utcfro mtimestamp will cause problems come 2038. Unfortunately
    > there don't seem to be "fromstring " equivalents for datetime.ctime or
    > datetime.isofor mat.
    >
    > Ideally the serialized datetime should be human readable, and
    > potentially parseable from other languages. Any suggestions?
    >
    >[/color]

    It's not that hard to DIY; the simple code at the end of this posting
    (1) handles fractions of a second [which you may or may not want]
    without burdening the network or the readers' eyeballs with excess
    trailing zeroes (2) doesn't handle TZs [which you may or may not want].

    Some further validation on input strings may be a good idea, if you need
    to eat other software's strings as well as yours. E.g. check that where
    the '-' characters should be that you find nothing stranger than '.' or
    '/'; certainly not digits.

    HTH,
    John

    8<---
    import datetime

    def datetime_from_s tr(s):
    # YYYY-MM-DD HH:MM:SS.TTT
    # 012345678901234 56789012
    year = int(s[0:4])
    month = int(s[5:7])
    day = int(s[8:10])
    hour = int(s[11:13])
    minute = int(s[14:16])
    microseconds = int(float(s[17:]) * 1000000.0)
    second, microsecond = divmod(microsec onds, 1000000)
    return datetime.dateti me(year, month, day, hour, minute, second,
    microsecond)

    def datetime_as_str (dtm):
    part1 = dtm.strftime("% Y-%m-%d %H:%M:%S")
    micros = dtm.microsecond
    if not micros:
    return part1
    part2 = (".%06d" % micros).rstrip( '0')
    return part1 + part2

    if __name__ == "__main__":
    tests = [
    '1999-12-31 23:59:59.999999 ',
    '1999-12-31 23:59:59.999999 99',
    '1999-12-31 23:59:59.999',
    '1999-12-31 23:59:59',
    '2000-01-01 00:00:00.000',
    '2000-01-01 00:00:00',
    '2000-01-01 00:00:00.000001 ',
    '2000-01-01 00:00:00.001',
    ]
    for test in tests:
    dtm = datetime_from_s tr(test)
    print "input str: ", repr(test)
    print "datetime: ", repr(dtm)
    round_trip = datetime_as_str (dtm)
    print "output str:", repr(round_trip ), ["not same",
    ""][round_trip == test]
    print
    8<---

    Comment

    • Brendan

      #3
      Re: Serializing / Unserializing datetime

      Thanks John. I've discovered that datetime.strpti me will be available
      in 2.5, (http://docs.python.org/dev/whatsnew/modules.html) but your
      example will work in the meantime.

      BJ

      Comment

      • Gerard Flanagan

        #4
        Re: Serializing / Unserializing datetime

        Brendan wrote:[color=blue]
        > Thanks John. I've discovered that datetime.strpti me will be available
        > in 2.5, (http://docs.python.org/dev/whatsnew/modules.html) but your
        > example will work in the meantime.
        >
        > BJ[/color]

        I don't think it's what you want but I had the following on file - it
        uses time.strptime() which I didn't know there was a problem with. Not
        tested beyond what you see.

        --------------------------------------------------------------------------------
        import time
        import datetime

        DDMMYY = ['%d %m %Y', '%d %m %y', '%d/%m/%Y', '%d/%m/%y', '%d-%m-%Y',
        '%d-%m-%y' ]

        def yearmonthday(da testring, fmts=DDMMYY):
        ymd = None
        for f in fmts:
        try:
        ymd = time.strptime( datestring, f )
        break
        except ValueError:
        continue
        if ymd is None:
        raise ValueError
        return ymd[0], ymd[1], ymd[2]

        def is_valid_date(d atestring, fmts=DDMMYY):
        try:
        yearmonthday(da testring, fmts)
        return True
        except ValueError:
        return False

        def string_to_date( datestring, fmts=DDMMYY):
        return datetime.date( *yearmonthday(d atestring, fmts) )

        assert string_to_date( '1/2/01', DDMMYY) == datetime.date(2 001,2,1)
        assert string_to_date( '1 2 01', DDMMYY) == datetime.date(2 001,2,1)
        assert string_to_date( '01/02/01', DDMMYY) == datetime.date(2 001,2,1)
        assert string_to_date( '1/02/2001', DDMMYY) == datetime.date(2 001,2,1)
        assert string_to_date( '29/02/2008', DDMMYY) ==
        datetime.date(2 008,2,29)
        assert string_to_date( '01/2/99', DDMMYY) == datetime.date(1 999,2,1)

        for d in [ '', '32/1/01', '01/13/01', '29/2/07', '1/2', 'abcdef' ]:
        assert not is_valid_date(d , DDMMYY)
        ------------------------------------------------------------------------------------

        Gerard

        Comment

        • John Machin

          #5
          Re: Serializing / Unserializing datetime

          On 29/05/2006 5:23 AM, Brendan wrote:[color=blue]
          > Thanks John. I've discovered that datetime.strpti me will be available
          > in 2.5, (http://docs.python.org/dev/whatsnew/modules.html) but your
          > example will work in the meantime.
          >[/color]

          Only in the meantime? I would thought there was a good chance it would
          continue to work in 2.5 -- especially as I tested it with 2.5a2 :-)

          Hmmm ... now you've got me thinking, maybe there's an anti-DIY
          enforced-use-of-official-functionality gadget in 2.5. Just because I'm
          paranoid doesn't mean the PSU isn't out to ~;{[]=]|\ <no connection>

          Comment

          • John Machin

            #6
            Re: Serializing / Unserializing datetime

            On 29/05/2006 6:27 AM, Gerard Flanagan wrote:
            [color=blue]
            > I don't think it's what you want but I had the following on file - it
            > uses time.strptime() which I didn't know there was a problem with.[/color]

            In 2.3, time.strptime became a pure-Python routine, thus losing its
            dependency on the existence and correctness of the strptime in the C
            library.

            However, the whole time module has a problem: the range of years it
            handles -- 1970 to 2038 (maybe). This is documented on the first page of
            The Fine Manual. Not very useful for customer's date of birth, and (as
            Brendan indicated), likely to cause Y2.038K problems.

            Cheers,
            John

            Comment

            Working...