Changeset 3879

Show
Ignore:
Timestamp:
01/17/08 13:05:11 (7 months ago)
Author:
paul
Message:

Added settings.loadUserLocale, default True. This makes the
locale.setlocale(locale.LC_ALL, ) call, which I think is a nice
thing to do. However, if it causes problems people can turn it
off and make the call themselves when needed.

Added settings for dateFormat, dateTimeFormat, and timeFormat. At
this time, the only one implemented is dateFormat. A value of None
means that Dabo will delegate to time.strftime("%x").

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/paul/dabo/__init__.py

    r3810 r3879  
    103103import os 
    104104import sys 
     105import locale 
    105106try: 
    106107    import pysqlite2 
     
    151152from settings import * 
    152153 
     154if settings.loadUserLocale: 
     155    locale.setlocale(locale.LC_ALL, '') 
     156 
    153157from __version__ import version 
    154158import dColors 
  • branches/paul/dabo/lib/dates.py

    r3877 r3879  
    77import re 
    88import time 
     9import dabo 
    910 
    1011_dregex = {} 
     
    2930        exp = "^%(month)s%(day)s$" 
    3031    else: 
    31         return None 
     32        conv = {"%d": "%(day)s", 
     33                "%m": "%(month)s", 
     34                "%y": "%(shortyear)s", 
     35                "%Y": "%(year)s"} 
     36        if "%d" in format and "%m" in format and ("%y" in format or "%Y" in format): 
     37            for k in conv.keys(): 
     38                format = format.replace(k, conv[k]) 
     39                format.replace(".", "\.") 
     40                exp = "^%s$" % format 
     41        else: 
     42            return None 
     43 
    3244    return re.compile(exp % elements) 
    3345 
     
    7587 
    7688 
     89def getStringFromDate(dateVal): 
     90    """Given a datetime.date, convert to string in dabo.settings.dateFormat style.""" 
     91    dateFormat = dabo.settings.dateFormat 
     92    if dateFormat is None: 
     93        # Delegate formatting to the time module, which will take the 
     94        # user's locale into account. 
     95        dateFormat = "%x" 
     96    return dateVal.strftime(dateFormat) 
     97 
     98 
    7799def getDateFromString(strVal, formats=None): 
    78100    """Given a string in a defined format, return a date object or None.""" 
    79101    global _dregex 
    80102 
     103    dateFormat = dabo.settings.dateFormat 
    81104    ret = None 
     105 
    82106    if formats is None: 
    83107        formats = ["ISO8601"] 
     108 
     109    if dateFormat is not None: 
     110        # Take the date format as set in dabo into account, when trying  
     111        # to make a date out of the string. 
     112        formats.append(dateFormat) 
    84113 
    85114    # Try each format in order: 
     
    113142            break    
    114143    if ret is None: 
    115         ## Fall back to the current locale setting: 
    116         try: 
    117             ret = datetime.date(*time.strptime(strVal, "%x")[:3]) 
    118         except: 
    119             ret = None 
     144        if dateFormat is None: 
     145            # Fall back to the current locale setting in user's os account: 
     146            try: 
     147                ret = datetime.date(*time.strptime(strVal, "%x")[:3]) 
     148            except: 
     149                pass 
    120150    return ret 
    121151 
  • branches/paul/dabo/settings.py

    r3877 r3879  
    112112checkForWebUpdates = True 
    113113 
     114# Date and Time formats. None will use the os user's settings, but 
     115# your code can easily override these. Example: 
     116#   dabo.settings.dateFormat = "%d.%m.%Y" -> "31.12.2008". 
     117dateFormat = None 
     118dateTimeFormat = None 
     119timeFormat = None 
     120 
     121# Do we load the os user's locale settings automatically?  
     122# Pythonista note: this executes:  
     123#    locale.setlocale(locale.LC_ALL, '') 
     124loadUserLocale = True 
    114125 
    115126### Settings - end 
  • branches/paul/dabo/ui/uiwx/dGrid.py

    r3877 r3879  
    2222from dabo.dObject import dObject 
    2323from dabo.ui import makeDynamicProperty 
     24import dabo.lib.dates 
    2425 
    2526# from dabo.lib.profilehooks import profile 
     
    349350        """Get the string value to display in the grid.""" 
    350351        if isinstance(val, datetime.date): 
    351             return time.strftime("%x", (val.year, val.month, val.day, 0, 0, 0, 0, 0, 0)
     352            return dabo.lib.dates.getStringFromDate(val
    352353        return val 
    353354 
  • branches/paul/dabo/ui/uiwx/dTextBoxMixin.py

    r3877 r3879  
    514514            strVal = value.isoformat(" ") 
    515515        elif isinstance(value, datetime.date): 
    516             strVal = time.strftime("%x", (value.year, value.month, value.day, 0, 0, 0, 0, 0, 0)
     516            strVal = dabo.lib.dates.getStringFromDate(value
    517517        elif isinstance(value, datetime.time): 
    518518            # Use the ISO 8601 time string format 
     
    533533        datetime.date object, or None. 
    534534        """ 
    535         formats = [dabo.settings.dateFormat, "ISO8601"] 
     535        formats = ["ISO8601"] 
    536536        if not self.StrictDateEntry: 
    537537            # Add some less strict date-entry formats: