VirtualBox

Ignore:
Timestamp:
Apr 30, 2020 11:39:11 AM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
137689
Message:

TestManager/base.py: Reworked the timestamp valdation a little and allow timestamps without a sub-second value.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/testmanager/core/base.py

    r83384 r84100  
    606606        return (lValue, None);
    607607
     608    kdTimestampRegex = {
     609        len('2012-10-08 01:54:06'):           r'(\d{4})-([01]\d)-([0123]\d)[ Tt]([012]\d):[0-5]\d:([0-6]\d)$',
     610        len('2012-10-08 01:54:06.00'):        r'(\d{4})-([01]\d)-([0123]\d)[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{2}$',
     611        len('2012-10-08 01:54:06.000'):       r'(\d{4})-([01]\d)-([0123]\d)[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{3}$',
     612        len('999999-12-31 00:00:00.00'):      r'(\d{6})-([01]\d)-([0123]\d)[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{2}$',
     613        len('9999-12-31 23:59:59.999999'):    r'(\d{4})-([01]\d)-([0123]\d)[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{6}$',
     614        len('9999-12-31T23:59:59.999999999'): r'(\d{4})-([01]\d)-([0123]\d)[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{9}$',
     615    };
     616
    608617    @staticmethod
    609618    def validateTs(sValue, aoNilValues = tuple([None, '']), fAllowNull = True):
     
    614623            return (sValue, None);
    615624
     625        # Validate and strip off the timezone stuff.
     626        if sValue[-1] in 'Zz':
     627            sStripped = sValue[:-1];
     628            sValue = sStripped + 'Z';
     629        elif len(sValue) >= 19 + 3:
     630            oRes = re.match(r'^.*[+-](\d\d):(\d\d)$', sValue);
     631            if oRes is not None:
     632                if int(oRes.group(6)) > 12 or int(oRes.group(7)) >= 60:
     633                    return (sValue, 'Invalid timezone offset.');
     634                sStripped = sValue[:-6];
     635            else:
     636                sStripped = sValue;
     637        else:
     638            sStripped = sValue;
     639
     640        # Used the stripped value length to find regular expression for validating and parsing the timestamp.
    616641        sError = None;
    617         if len(sValue) == len('2012-10-08 01:54:06.364207+02:00'):
    618             oRes = re.match(r'(\d{4})-([01]\d)-([0123])\d ([012]\d):[0-5]\d:([0-6]\d).\d{6}[+-](\d\d):(\d\d)', sValue);
    619             if    oRes is not None \
    620               and (   int(oRes.group(6)) >  12 \
    621                    or int(oRes.group(7)) >= 60):
    622                 sError = 'Invalid timezone offset.';
    623         elif len(sValue) == len('2012-10-08 01:54:06.00'):
    624             oRes = re.match(r'(\d{4})-([01]\d)-([0123])\d[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{2}', sValue);
    625         elif len(sValue) == len('2012-10-08 01:54:06.00Z'):
    626             oRes = re.match(r'(\d{4})-([01]\d)-([0123])\d[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{2}[Zz]', sValue);
    627         elif len(sValue) == len('9999-12-31 23:59:59.999999'):
    628             oRes = re.match(r'(\d{4})-([01]\d)-([0123])\d[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{6}', sValue);
    629         elif len(sValue) == len('9999-12-31 23:59:59.999999Z'):
    630             oRes = re.match(r'(\d{4})-([01]\d)-([0123])\d[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{6}[Zz]', sValue);
    631         elif len(sValue) == len('999999-12-31 00:00:00.00'):
    632             oRes = re.match(r'(\d{6})-([01]\d)-([0123])\d[ Tt]([012]\d):[0-5]\d:([0-6]\d).\d{2}', sValue);
    633         elif len(sValue) == len('9999-12-31T23:59:59.999999Z'):
    634             oRes = re.match(r'(\d{4})-([01]\d)-([0123])\d[Tt]([012]\d):[0-5]\d:([0-6]\d).\d{6}[Zz]', sValue);
    635         elif len(sValue) == len('9999-12-31T23:59:59.999999999Z'):
    636             oRes = re.match(r'(\d{4})-([01]\d)-([0123])\d[Tt]([012]\d):[0-5]\d:([0-6]\d).\d{9}[Zz]', sValue);
     642        sRegExp = ModelDataBase.kdTimestampRegex.get(len(sStripped), None);
     643        if sRegExp:
     644            oRes = re.match(sRegExp, sStripped);
     645            if oRes is not None:
     646                iYear  = int(oRes.group(1));
     647                if iYear % 4 == 0 and (iYear % 100 != 0  or iYear % 400 == 0):
     648                    acDaysOfMonth = [31, 29, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31];
     649                else:
     650                    acDaysOfMonth = [31, 28, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31];
     651                iMonth = int(oRes.group(2));
     652                iDay   = int(oRes.group(3));
     653                iHour  = int(oRes.group(4));
     654                iSec   = int(oRes.group(5));
     655                if iMonth > 12 or iMonth <= 0:
     656                    sError = 'Invalid timestamp month.';
     657                elif iDay > acDaysOfMonth[iMonth - 1]:
     658                    sError = 'Invalid timestamp day-of-month (%02d has %d days).' % (iMonth, acDaysOfMonth[iMonth - 1]);
     659                elif iHour > 23:
     660                    sError = 'Invalid timestamp hour.'
     661                elif iSec >= 61:
     662                    sError = 'Invalid timestamp second.'
     663                elif iSec >= 60:
     664                    sError = 'Invalid timestamp: no leap seconds, please.'
     665            else:
     666                sError = 'Invalid timestamp (validation regexp: %s).' % (sRegExp,);
    637667        else:
    638             return (sValue, 'Invalid timestamp length.');
    639 
    640         if oRes is None:
    641             sError = 'Invalid timestamp (format: 2012-10-08 01:54:06.364207+02:00).';
    642         else:
    643             iYear  = int(oRes.group(1));
    644             if iYear % 4 == 0 and (iYear % 100 != 0  or iYear % 400 == 0):
    645                 acDaysOfMonth = [31, 29, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31];
    646             else:
    647                 acDaysOfMonth = [31, 28, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31];
    648             iMonth = int(oRes.group(2));
    649             iDay   = int(oRes.group(3));
    650             iHour  = int(oRes.group(4));
    651             iSec   = int(oRes.group(5));
    652             if iMonth > 12:
    653                 sError = 'Invalid timestamp month.';
    654             elif iDay > acDaysOfMonth[iMonth - 1]:
    655                 sError = 'Invalid timestamp day-of-month (%02d has %d days).' % (iMonth, acDaysOfMonth[iMonth - 1]);
    656             elif iHour > 23:
    657                 sError = 'Invalid timestamp hour.'
    658             elif iSec >= 61:
    659                 sError = 'Invalid timestamp second.'
    660             elif iSec >= 60:
    661                 sError = 'Invalid timestamp: no leap seconds, please.'
     668            sError = 'Invalid timestamp length.';
    662669        return (sValue, sError);
    663670
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette