Is there a Python function...

Stuart Ford stuart at forddata.com
Mon Aug 7 14:34:33 EDT 2000


Not a single function, but you can use the time.strptime function, and pass
it's result
to the time.mktime function.  The only problem is that this doesn't work on
Windows
machines.

*nix Example:

TimeStr = "Jan 12 1999 20h23"
MyTime = int(time.mktime(time.strptime(TimeStr, "%b %d %Y %Hh%M")))

For a Windows Machine, you must parse the string yourself and use the
time.mktime function to return the Time integer.

Windows Example:

import re
import time
import string

def GetMonth(str):
    str = string.upper(str)
    if str == 'JAN':
        return 1
    elif str == 'FEB':
        return 2
    elif str == 'MAR':
        return 3
    elif str == 'APR':
        return 4
    elif str == 'MAY':
        return 5
    elif str == 'JUN':
        return 6
    elif str == 'JUL':
        return 7
    elif str == 'AUG':
        return 8
    elif str == 'SEP':
        return 9
    elif str == 'OCT':
        return 10
    elif str == 'NOV':
        return 11
    elif str == 'DEC':
        return 12
    else:
        return 0

def ParseDateString(TimeStr):
    match = r'(?P<month>\D*) (?P<day>\d*) (?P<year>\d*)
(?P<hour>\d*)h(?P<minute>\d*)'
    reMatch = re.compile(match)
    DateTimeFound = reMatch.search(TimeStr)
    DateTimeDict = DateTimeFound.groupdict()

    Year = string.atoi(DateTimeDict['year'])
    Month = GetMonth(DateTimeDict['month'])
    Day = string.atoi(DateTimeDict['day'])
    Hour = string.atoi(DateTimeDict['hour'])
    Minute = string.atoi(DateTimeDict['minute'])

    return int(time.mktime((Year, Month, Day, Hour, Minute, 0, 0, 0, 0))




Hope this helps...
Stuart


----- Original Message -----
From: "Paul Sheer" <psheer at obsidian.co.za>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Monday, August 07, 2000 12:20 PM
Subject: Is there a Python function...


>
> ...to convert an ascii formatted date into integer time in seconds since
1970?
>
> for example:
> input: Jan 12 1999 20h23
> output: 916165380
>
> Thanks
>
> -paul
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list