コード例 #1
0
ファイル: gnrclasses.py プロジェクト: unbit/genropy
 def parse_datetime(self, txt, workdate=None):
     """Add???
         
     :param txt: TODO
     :param workdate: the :ref:`workdate`"""
     splitted = gnrstring.wordSplit(txt)
     result = datetime.datetime(*[int(el) for el in splitted])
     return result
コード例 #2
0
    def parse_time(self, txt):
        """Add???

        :param txt: TODO
        :returns: TODO
        """
        if txt:
            return datetime.time(*[int(el) for el in gnrstring.wordSplit(txt)])
コード例 #3
0
ファイル: gnrclasses.py プロジェクト: unbit/genropy
 def parse_time(self, txt):
     """Add???
         
     :param txt: TODO
     :returns: TODO
     """
     if txt:
         return datetime.time(*[int(el) for el in gnrstring.wordSplit(txt)])
コード例 #4
0
 def parse_time(self, txt):
     """Add???
         
     :param txt: add???
     :returns: add???
     """
     if txt and txt != '00:00:00':
         return datetime.time(*[int(el) for el in gnrstring.wordSplit(txt)])
コード例 #5
0
 def parse_datetime(self, txt, workdate=None):
     """Add???
         
     :param txt: TODO
     :param workdate: the :ref:`workdate`"""
     splitted = gnrstring.wordSplit(txt)
     result = datetime.datetime(*[int(el) for el in splitted])
     return result
コード例 #6
0
ファイル: gnrclasses.py プロジェクト: unbit/genropy
 def parse_date(self, txt, workdate=None):
     """Add???
         
     :param txt: TODO
     :param workdate: the :ref:`workdate`"""
     if txt != "0000-00-00":
         if txt and ISO_MATCH.match(txt):
             return datetime.date(*[int(el) for el in gnrstring.wordSplit(txt)[0:3]])
         else:
             return decodeDatePeriod(txt, workdate=workdate, returnDate=True)
コード例 #7
0
 def parse_datetime(self, txt, workdate=None):
     """Add???
         
     :param txt: add???
     :param workdate: add???. Default value is ``None``
     :returns: add???
     """
     splitted = gnrstring.wordSplit(txt)
     result = datetime.datetime(*[int(el) for el in splitted])
     return result
コード例 #8
0
 def parse_date(self, txt, workdate=None):
     """Add???
         
     :param txt: add???
     :param workdate: add???. Default value is ``None``
     :returns: add???
     """
     if txt != '0000-00-00':
         if txt and ISO_MATCH.match(txt):
             return datetime.date(*[int(el) for el in gnrstring.wordSplit(txt)[0:3]])
         else:
             return decodeDatePeriod(txt, workdate=workdate, returnDate=True)
コード例 #9
0
 def parse_date(self, txt, workdate=None):
     """Add???
         
     :param txt: TODO
     :param workdate: the :ref:`workdate`"""
     if txt != '0000-00-00':
         if txt and ISO_MATCH.match(txt):
             return datetime.date(
                 *[int(el) for el in gnrstring.wordSplit(txt)[0:3]])
         else:
             return decodeDatePeriod(txt,
                                     workdate=workdate,
                                     returnDate=True)
コード例 #10
0
def test_wordSplit():
    """docstring for test_wordSplit"""
    assert gnrstring.wordSplit('hello, my dear friend') == [
        'hello', 'my', 'dear', 'friend'
    ]
コード例 #11
0
ファイル: gnrstring_test.py プロジェクト: OpenCode/genropy
def test_wordSplit():
    """docstring for test_wordSplit"""
    assert gnrstring.wordSplit('hello, my dear friend') == ['hello', 'my', 'dear', 'friend']
コード例 #12
0
ファイル: gnrdate.py プロジェクト: OpenCode/genropy
def decodeOneDate(datestr, workdate=None, months=None, days=None, quarters=None, locale=None, isEndPeriod=False):
    """Parse a string representing a date or a period. Return ``datetime.date``
    or ``tuple(year,month)`` or ``None``
    
    :param datestr: the string to be interpreted
    :param workdate: the :ref:`workdate`
    :param months: names of months according to locale (just for caching)
    :param days: names of weekdays according to locale (just for caching)
    :param quarters: names of quarters according to locale (just for caching)
    :param locale: the current locale (e.g: en, en_us, it)
    :param isEndPeriod: if the string represents a period, return the end date
                        (default return the start date)
    
    Special keywords like ``today`` or the name of a month can be translated in all languages
    and support synonimous. (e.g: this month; e.g: month)
    
    The input string can be:
    
    * a year: e.g. 2007 or 07
    * today, yesterday, tomorrow (can be translated in all languages)
        * you can specify a number of days to add to today: e.g. 'today + 3' or 'today - 15'
    * this week, next week, last week (can be translated in all languages)
    * this month, next month, last month (can be translated in all languages )
        * can be specified a number of months to add to current month: e.g. 'this month + 3' or 'this month - 24'
    * the name of a quarter: e.g. Q1 or 1st quarter
    * the name of a month: e.g. april or apr
        * can be specified a year after the month: e.g. apr 07 or april 2007
        * returns a tuple (year, month): if year is not specified in datestr, year is returned None
    * the name of a weekday: e.g. monday or mon
        * the date returned is the date of the given weekday in this week (relative to workdate)
    * an iso date: e.g. 2008-04-28
    * a date formatted according to locale (see babel doc): e.g. 4 28, 2008 (en_us) or 28-4-08 (it)
                           various separators are admitted: 28-4-08, 28/4/08, 28 4 08"""
    
    def addToDay(datestr, date):
        if '+' in datestr:
            days = int(datestr.split('+')[1].strip())
            return date + datetime.timedelta(days)
        if '-' in datestr:
            days = int(datestr.split('-')[1].strip())
            return date - datetime.timedelta(days)
        return date

    def addToMonth(datestr, date, addmonth=0):#l'errore è nel chiamate che passa addmonth sbagliato
        delta=0
        if '+' in datestr:
            delta =  int(datestr.split('+')[1].strip())
        if '-' in datestr:
            delta = -int(datestr.split('-')[1].strip())
        month = date.month + addmonth+ delta
        year = date.year
        while month <= 0:
            month = month + 12
            year = year - 1
        while month > 12:
            month = month - 12
            year = year + 1
        return datetime.date(year, month, 1)
        
    datestr = datestr or ''
    datestr = datestr.strip()
    if datestr:
        months = months or gnrlocale.getMonthNames(locale)
        def_months = gnrlocale.getMonthNames(DEFAULT_LOCALE)
        days = days or gnrlocale.getDayNames(locale)
        def_days = gnrlocale.getDayNames(DEFAULT_LOCALE)
        quarters = quarters or gnrlocale.getQuarterNames(locale)
        def_quarters = gnrlocale.getQuarterNames(DEFAULT_LOCALE)
        dateStart = None
        dateEnd = None
        workdate = workdate or datetime.date.today()
        
        if datestr.isdigit() and len(datestr) in (2, 4):                          # a full year
            year = yearDecode(datestr)
            dateStart = datetime.date(year, 1, 1)
            if isEndPeriod:
                dateEnd = datetime.date(year, 12, 31)
                
        elif checkDateKeywords('today', datestr, locale):     # today
            dateStart = addToDay(datestr, workdate)
        elif checkDateKeywords('yesterday', datestr, locale): # yesterday
            dateStart = addToDay(datestr, workdate - datetime.timedelta(1))
        elif checkDateKeywords('tomorrow', datestr, locale):  # tomorrow
            dateStart = addToDay(datestr, workdate + datetime.timedelta(1))

        elif checkDateKeywords(('this week', 'next week', 'last week'), datestr, locale): # relative week
            j = workdate.weekday()
            dateStart = workdate - datetime.timedelta(j)
            if checkDateKeywords('last week', datestr, locale):
                dateStart = dateStart - datetime.timedelta(7)
            elif checkDateKeywords('next week', datestr, locale):
                dateStart = dateStart + datetime.timedelta(7)
            if '+' in datestr:
                 dateStart = dateStart + datetime.timedelta(7*int(datestr.split('+')[1]))
            if '-' in datestr:
                 dateStart = dateStart - datetime.timedelta(7*int(datestr.split('-')[1]))
            if isEndPeriod:
                dateEnd = dateStart + datetime.timedelta(6)
        elif checkDateKeywords(('this month', 'next month', 'last month'), datestr, locale): # relative month
            if checkDateKeywords('last month', datestr, locale):  
                dateStart = addToMonth(datestr, workdate, -1)
            elif checkDateKeywords('next month', datestr, locale):
                dateStart = addToMonth(datestr, workdate, 1)
            else:
                dateStart = addToMonth(datestr, workdate)
            if isEndPeriod:
                dateEnd = monthEnd(date=dateStart)
        elif anyWordIn(quarters.keys(), datestr): # quarter
            qt, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            qt = quarters[datestr]
            dateStart = (year, qt * 3 - 2)
            if isEndPeriod:
                dateEnd = (year, qt * 3)
        elif anyWordIn(def_quarters.keys(), datestr): # quarter
            qt, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            qt = def_quarters[datestr]
            dateStart = (year, qt * 3 - 2)
            if isEndPeriod:
                dateEnd = (year, qt * 3)
        elif anyWordIn(months.keys(), datestr):                                 # month name
            month, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            month = months[month]
            dateStart = (year, month)
        elif anyWordIn(def_months.keys(), datestr):                                 # month name
            month, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            month = def_months[month]
            dateStart = (year, month)
        elif datestr in days:                                                   # weekday name
            dateStart = workdate + datetime.timedelta(days[datestr] - workdate.weekday())
        elif datestr in def_days:                                                   # weekday name
            dateStart = workdate + datetime.timedelta(def_days[datestr] - workdate.weekday())
        elif re.match('\d{4}-\d{2}-\d{2}', datestr):                            # ISO date
            date_items = [int(el) for el in wordSplit(datestr)[0:3]]
            dateStart = datetime.date(*[int(el) for el in wordSplit(datestr)[0:3]])
        else:                                                                   # a date in local format
            dateStart = gnrlocale.parselocal(datestr, datetime.date, locale)
        if isEndPeriod and dateEnd:
            return dateEnd
        else:
            return dateStart
コード例 #13
0
def decodeOneDate(datestr, workdate=None, months=None, days=None, quarters=None, locale=None, isEndPeriod=False):
    """Parse a string representing a date or a period. Return ``datetime.date``
    or ``tuple(year,month)`` or ``None``
    
    :param datestr: the string to be interpreted
    :param workdate: the :ref:`workdate`
    :param months: names of months according to locale (just for caching)
    :param days: names of weekdays according to locale (just for caching)
    :param quarters: names of quarters according to locale (just for caching)
    :param locale: the current locale (e.g: en, en_us, it)
    :param isEndPeriod: if the string represents a period, return the end date
                        (default return the start date)
    
    Special keywords like ``today`` or the name of a month can be translated in all languages
    and support synonimous. (e.g: this month; e.g: month)
    
    The input string can be:
    
    * a year: e.g. 2007 or 07
    * today, yesterday, tomorrow (can be translated in all languages)
        * you can specify a number of days to add to today: e.g. 'today + 3' or 'today - 15'
    * this week, next week, last week (can be translated in all languages)
    * this month, next month, last month (can be translated in all languages )
        * can be specified a number of months to add to current month: e.g. 'this month + 3' or 'this month - 24'
    * the name of a quarter: e.g. Q1 or 1st quarter
    * the name of a month: e.g. april or apr
        * can be specified a year after the month: e.g. apr 07 or april 2007
        * returns a tuple (year, month): if year is not specified in datestr, year is returned None
    * the name of a weekday: e.g. monday or mon
        * the date returned is the date of the given weekday in this week (relative to workdate)
    * an iso date: e.g. 2008-04-28
    * a date formatted according to locale (see babel doc): e.g. 4 28, 2008 (en_us) or 28-4-08 (it)
                           various separators are admitted: 28-4-08, 28/4/08, 28 4 08"""
    
    def addToDay(datestr, date):
        if '+' in datestr:
            days = int(datestr.split('+')[1].strip())
            return date + datetime.timedelta(days)
        if '-' in datestr:
            days = int(datestr.split('-')[1].strip())
            return date - datetime.timedelta(days)
        return date

    def addToMonth(datestr, date, addmonth=0):#l'errore è nel chiamate che passa addmonth sbagliato
        delta=0
        if '+' in datestr:
            delta =  int(datestr.split('+')[1].strip())
        if '-' in datestr:
            delta = -int(datestr.split('-')[1].strip())
        month = date.month + addmonth+ delta
        year = date.year
        while month <= 0:
            month = month + 12
            year = year - 1
        while month > 12:
            month = month - 12
            year = year + 1
        return datetime.date(year, month, 1)
        
    datestr = datestr or ''
    datestr = datestr.strip()
    if datestr:
        months = months or gnrlocale.getMonthNames(locale)
        def_months = gnrlocale.getMonthNames(DEFAULT_LOCALE)
        days = days or gnrlocale.getDayNames(locale)
        def_days = gnrlocale.getDayNames(DEFAULT_LOCALE)
        quarters = quarters or gnrlocale.getQuarterNames(locale)
        def_quarters = gnrlocale.getQuarterNames(DEFAULT_LOCALE)
        dateStart = None
        dateEnd = None
        workdate = workdate or datetime.date.today()
        
        if datestr.isdigit() and len(datestr) in (2, 4):                          # a full year
            year = yearDecode(datestr)
            dateStart = datetime.date(year, 1, 1)
            if isEndPeriod:
                dateEnd = datetime.date(year, 12, 31)
                
        elif checkDateKeywords('today', datestr, locale):     # today
            dateStart = addToDay(datestr, workdate)
        elif checkDateKeywords('yesterday', datestr, locale): # yesterday
            dateStart = addToDay(datestr, workdate - datetime.timedelta(1))
        elif checkDateKeywords('tomorrow', datestr, locale):  # tomorrow
            dateStart = addToDay(datestr, workdate + datetime.timedelta(1))

        elif checkDateKeywords(('this week', 'next week', 'last week'), datestr, locale): # relative week
            j = workdate.weekday()
            dateStart = workdate - datetime.timedelta(j)
            if checkDateKeywords('last week', datestr, locale):
                dateStart = dateStart - datetime.timedelta(7)
            elif checkDateKeywords('next week', datestr, locale):
                dateStart = dateStart + datetime.timedelta(7)
            if '+' in datestr:
                 dateStart = dateStart + datetime.timedelta(7*int(datestr.split('+')[1]))
            if '-' in datestr:
                 dateStart = dateStart - datetime.timedelta(7*int(datestr.split('-')[1]))
            if isEndPeriod:
                dateEnd = dateStart + datetime.timedelta(6)
        elif checkDateKeywords(('this month', 'next month', 'last month'), datestr, locale): # relative month
            if checkDateKeywords('last month', datestr, locale):  
                dateStart = addToMonth(datestr, workdate, -1)
            elif checkDateKeywords('next month', datestr, locale):
                dateStart = addToMonth(datestr, workdate, 1)
            else:
                dateStart = addToMonth(datestr, workdate)
            if isEndPeriod:
                dateEnd = monthEnd(date=dateStart)
        elif anyWordIn(quarters.keys(), datestr): # quarter
            qt, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            qt = quarters[qt]
            dateStart = (year, qt * 3 - 2)
            if isEndPeriod:
                dateEnd = (year, qt * 3)
        elif anyWordIn(def_quarters.keys(), datestr): # quarter
            qt, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            qt = def_quarters[datestr]
            dateStart = (year, qt * 3 - 2)
            if isEndPeriod:
                dateEnd = (year, qt * 3)
        elif anyWordIn(months.keys(), datestr):                                 # month name
            month, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            month = months[month]
            dateStart = (year, month)
        elif anyWordIn(def_months.keys(), datestr):                                 # month name
            month, year = splitAndStrip(datestr, sep=' ', n=1, fixed=2)
            year = yearDecode(year)
            month = def_months[month]
            dateStart = (year, month)
        elif datestr in days:                                                   # weekday name
            dateStart = workdate + datetime.timedelta(days[datestr] - workdate.weekday())
        elif datestr in def_days:                                                   # weekday name
            dateStart = workdate + datetime.timedelta(def_days[datestr] - workdate.weekday())
        elif re.match('\d{4}-\d{2}-\d{2}', datestr):                            # ISO date
            date_items = [int(el) for el in wordSplit(datestr)[0:3]]
            dateStart = datetime.date(*[int(el) for el in wordSplit(datestr)[0:3]])
        else:                                                                   # a date in local format
            dateStart = gnrlocale.parselocal(datestr, datetime.date, locale)
        if isEndPeriod and dateEnd:
            return dateEnd
        else:
            return dateStart