File: datetime_strftime.py

package info (click to toggle)
tryton-server 3.4.0-3%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 4,600 kB
  • ctags: 3,933
  • sloc: python: 28,679; xml: 3,996; sql: 328; sh: 150; makefile: 82
file content (50 lines) | stat: -rw-r--r-- 1,475 bytes parent folder | download | duplicates (2)
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
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
#Copyright (c) 2002-2007 John D. Hunter; All Rights Reserved
import time


def datetime_strftime(date, fmt):
    '''
    Allow datetime strftime formatting for years before 1900.
    See http://bugs.python.org/issue1777412
    '''
    if date.year > 1900:
        return date.strftime(fmt)

    def _findall(text, substr):
        # Also finds overlaps
        sites = []
        i = 0
        while True:
            j = text.find(substr, i)
            if j == -1:
                break
            sites.append(j)
            i = j + 1
        return sites

    year = date.year
    # For every non-leap year century, advance by
    # 6 years to get into the 28-year repeat cycle
    delta = 2000 - year
    off = 6 * (delta // 100 + delta // 400)
    year = year + off
    # Move to around the year 2000
    year = year + ((2000 - year) // 28) * 28
    timetuple = date.timetuple()
    string1 = time.strftime(fmt, (year,) + timetuple[1:])
    sites1 = _findall(string1, str(year))

    string2 = time.strftime(fmt, (year + 28,) + timetuple[1:])
    sites2 = _findall(string2, str(year + 28))

    sites = []
    for site in sites1:
        if site in sites2:
            sites.append(site)

    syear = "%4d" % (date.year,)
    for site in sites:
        string1 = string1[:site] + syear + string1[site + 4:]
    return string1