#---------------------------------------------------------------------------
# Copyright 2013 The Open Source Electronic Health Record Agent
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#---------------------------------------------------------------------------
import re
from datetime import datetime, date, time

def fmDtToPyDt(fileManDt):
  """ convert fileman time to Python datetime
      return None if format is not valid
  """
  """ check to see if datetime is valid or not"""
  validFmt = re.compile('^(?P<date>[0-9]{6,})(?P<time>\.[0-9]{0,6})?$')
  result = validFmt.search(fileManDt)
  if result:
    datePart = result.group('date')
    timePart = result.group('time')
    if not datePart: return None # must at least have a date part
    if len(datePart) < 7:
      datePart = datePart.ljust(7,'0')
    try:
      outDate = date(int(datePart[0:-4]) + 1700,
                     int(datePart[-4:-2]),
                     int(datePart[-2:]))
    except ValueError:
      return None
    outTime = time()
    if timePart and timePart[1:]: # has some time part here
      timePart = timePart[1:].ljust(6,'0')
      hour = int(timePart[0:2])
      if hour == 24: # special handling for mid-night
        outTime = time(23,59,59)
      else:
        minute = int(timePart[2:4])
        second = int(timePart[-2:])
        if minute == 60:
          minute = 59
          second = 59
        elif second == 60:
          second = 59
        try:
          outTime = time(hour, minute, second)
        except ValueError:
          return None
    return datetime.combine(outDate, outTime)
  return None

def testFmDtToPyDt():
  fileManDt = ("3121201.2308",
               "2970919.12",
               "2970919.24",
               "2930913.146",
               "2970919.082701",
               "3091203.09072",
               )
  for fmDt in fileManDt:
    print fmDtToPyDt(fmDt)

if __name__ == '__main__':
  testFmDtToPyDt()

@@ -1325,23 +1325,15 @@
     
-outDate = date(
+year = 
 int(
@@ -1351,37 +1351,22 @@
 -4%5D)
- + 1700,%0A                    
+%0A      month =
  int
@@ -1386,30 +1386,51 @@
 -2%5D)
-,
 %0A      
-       
+if month == 0: month = 1%0A
       
- 
+day =
  int
@@ -1444,16 +1444,88 @@
 rt%5B-2:%5D)
+%0A      if day == 0: day = 1%0A      outDate = date(year + 1700, month, day
 )%0A    ex
@@ -2376,24 +2376,50 @@
 203.09072%22,%0A
+               %223110000%22,%0A
             

Handle the case that both month and day are 0