diff babel/dates.py @ 16:ed154241c08d

Handle escape chars in datetime patterns.
author cmlenz
date Thu, 31 May 2007 14:37:31 +0000
parents 76985c08a339
children 77a68f88f6bc
line wrap: on
line diff
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -154,8 +154,15 @@
     >>> format_date(d, format='full', locale='de_DE')
     u'Sonntag, 1. April 2007'
     
+    If you don't want to use the locale default formats, you can specify a
+    custom date pattern:
+    
+    >>> format_time(d, "EEE, MMM d, ''yy", locale='en')
+    u"Sun, Apr 1, '07"
+    
     :param date: the ``date`` object
-    :param format: one of "full", "long", "medium", or "short"
+    :param format: one of "full", "long", "medium", or "short", or a custom
+                   date/time pattern
     :param locale: a `Locale` object or a locale string
     :rtype: `unicode`
     """
@@ -169,7 +176,8 @@
     """Returns a date formatted according to the given pattern.
     
     :param datetime: the ``date`` object
-    :param format: one of "full", "long", "medium", or "short"
+    :param format: one of "full", "long", "medium", or "short", or a custom
+                   date/time pattern
     :param locale: a `Locale` object or a locale string
     :rtype: `unicode`
     """
@@ -184,8 +192,15 @@
     >>> format_time(t, format='short', locale='de_DE')
     u'15:30'
     
+    If you don't want to use the locale default formats, you can specify a
+    custom time pattern:
+    
+    >>> format_time(t, "hh 'o''clock' a", locale='en')
+    u"03 o'clock PM"
+    
     :param time: the ``time`` object
-    :param format: one of "full", "long", "medium", or "short"
+    :param format: one of "full", "long", "medium", or "short", or a custom
+                   date/time pattern
     :param locale: a `Locale` object or a locale string
     :rtype: `unicode`
     """
@@ -323,9 +338,18 @@
     u'%(MMMM)s%(d)s'
     >>> parse_pattern("MMM d, yyyy").format
     u'%(MMM)s %(d)s, %(yyyy)s'
+    
+    Pattern can contain literal strings in single quotes:
+    
     >>> parse_pattern("H:mm' Uhr 'z").format
     u'%(H)s:%(mm)s Uhr %(z)s'
     
+    An actual single quote can be used by using two adjacent single quote
+    characters:
+    
+    >>> parse_pattern("hh' o''clock'").format
+    u"%(hh)s o'clock"
+    
     :param pattern: the formatting pattern to parse
     """
     if type(pattern) is DateTimePattern:
@@ -350,7 +374,7 @@
         fieldchar[0] = ''
         fieldnum[0] = 0
 
-    for idx, char in enumerate(pattern):
+    for idx, char in enumerate(pattern.replace("''", '\0')):
         if quotebuf is None:
             if char == "'": # quote started
                 if fieldchar[0]:
@@ -374,7 +398,7 @@
                 charbuf.append(char)
 
         elif quotebuf is not None:
-            if char == "'": # quote ended
+            if char == "'": # end of quote
                 charbuf.extend(quotebuf)
                 quotebuf = None
             else: # inside quote
@@ -385,4 +409,4 @@
     elif charbuf:
         append_chars()
 
-    return DateTimePattern(pattern, u''.join(result))
+    return DateTimePattern(pattern, u''.join(result).replace('\0', "'"))
Copyright (C) 2012-2017 Edgewall Software