Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

# -*- coding: UTF-8 -*- 

# Copyright 2009-2015 by Luc Saffre. 

# License: BSD, see LICENSE for more details. 

 

""" 

Date formatting functions 

------------------------- 

 

To run the Lino test suite on this module:: 

 

  $ python setup.py test -s tests.UtilsTests.test_format_date 

 

This module provides shortcuts to `python-babel`'s `date formatting 

functions <http://babel.pocoo.org/docs/dates/>`_. 

 

>>> from lino import startup 

>>> startup('lino.projects.min1.settings') 

>>> import datetime 

>>> d = datetime.date(2013,8,26) 

>>> print(fds(d)) # short 

8/26/13 

>>> print(fdm(d)) # medium 

Aug 26, 2013 

>>> print(fdl(d)) # long 

August 26, 2013 

>>> print(fdf(d)) # full 

Monday, August 26, 2013 

>>> print(fdmy(d)) # full 

August 2013 

 

 

The :func:`lino.core.format_date.format_date` function is a thin 

wrapper to the corresponding function in `babel.dates`, filling the 

`locale` parameter according to Django's current language (and doing 

the conversion). 

 

The major advantage over using `date_format` from 

`django.utils.formats` is that Babel offers a "full" format: 

 

>>> today = datetime.date(2013,01,18) 

 

>>> print(format_date(today,'full')) 

Friday, January 18, 2013 

 

>>> with translation.override('fr'): 

...    print(format_date(today,'full')) 

vendredi 18 janvier 2013 

 

>>> with translation.override('de'): 

...    print(format_date(today,'full')) 

Freitag, 18. Januar 2013 

 

You can use this also for languages that aren't on your site: 

 

>>> with translation.override('et'): 

...    print(format_date(today,'full')) 

reede, 18. jaanuar 2013 

 

>>> with translation.override('nl'): 

...    print(format_date(today,'full')) 

vrijdag 18 januari 2013 

 

 

>>> print(fds('')) # empty string is tolerated 

<BLANKLINE> 

>>> print(fds('2014-10-12')) # not tolerated 

Traceback (most recent call last): 

  ... 

Exception: Not a date: u'2014-10-12' 

 

""" 

 

from __future__ import unicode_literals, print_function 

 

import datetime 

from babel.dates import format_date as babel_format_date 

 

from django.conf import settings 

from django.utils import translation 

from django.template import defaultfilters 

 

from lino.core.site import to_locale 

from lino.utils import IncompleteDate 

 

 

def monthname(n): 

    """ 

    Return the monthname for month # n in current language. 

    """ 

    d = datetime.date(2013, n, 1) 

    return defaultfilters.date(d, 'F') 

 

 

def fdmy(d): 

    """ 

    "format date as month and year" : 

    return the specified date as a localized string of type 'June 2011'.""" 

    if d is None: 

        return '' 

    return defaultfilters.date(d, 'F Y') 

 

 

def format_date(d, format='medium'): 

    """Return the given date `d` formatted with `Babel's date formatting 

    <http://babel.edgewall.org/wiki/Documentation/dates.html>`_ and 

    using Django's current language. 

 

    """ 

    if not d: 

        return '' 

    if isinstance(d, IncompleteDate): 

        d = d.as_date() 

    if not isinstance(d, datetime.date): 

        raise Exception("Not a date: {0!r}".format(d)) 

    lng = translation.get_language() 

    if lng is None:  # occured during syncdb 

        lng = settings.SITE.languages[0].django_code 

    return babel_format_date(d, format=format, locale=to_locale(lng)) 

 

 

def fdf(d): 

    return format_date(d, format='full') 

 

 

def fdl(d): 

    return format_date(d, format='long') 

 

 

def fdm(d): 

    return format_date(d, format='medium') 

 

 

def fds(d): 

    return format_date(d, format='short') 

 

 

# backwards compatibility: 

dtosl = fdf 

dtosm = fdm 

dtos = fds 

dtomy = fdmy  # backward compat 

 

 

def day_and_month(d): 

    return format_date(d, "dd. MMMM")