~jfb-tempo-consulting/unifield-wm/sync-env-py3

« back to all changes in this revision

Viewing changes to openerplib103/dates.py

  • Committer: Cecile Tonglet
  • Date: 2013-10-17 14:40:19 UTC
  • Revision ID: cto@openerp.com-20131017144019-grjgxx09qz1q311p
[FIX] Version problem with openerplib

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (C) Stephane Wirtel
 
5
# Copyright (C) 2011 Nicolas Vanhoren
 
6
# Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
 
7
# All rights reserved.
 
8
 
9
# Redistribution and use in source and binary forms, with or without
 
10
# modification, are permitted provided that the following conditions are met:
 
11
#     * Redistributions of source code must retain the above copyright
 
12
#       notice, this list of conditions and the following disclaimer.
 
13
#     * Redistributions in binary form must reproduce the above copyright
 
14
#       notice, this list of conditions and the following disclaimer in the
 
15
#       documentation and/or other materials provided with the distribution.
 
16
#     * Neither the name of the <organization> nor the
 
17
#       names of its contributors may be used to endorse or promote products
 
18
#       derived from this software without specific prior written permission.
 
19
 
20
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
21
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
22
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
23
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 
24
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
25
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
26
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
27
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
28
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
29
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
30
#
 
31
##############################################################################
 
32
 
 
33
import datetime
 
34
 
 
35
DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d"
 
36
DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S"
 
37
DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % (
 
38
    DEFAULT_SERVER_DATE_FORMAT,
 
39
    DEFAULT_SERVER_TIME_FORMAT)
 
40
 
 
41
def str_to_datetime(str):
 
42
    """
 
43
    Converts a string to a datetime object using OpenERP's
 
44
    datetime string format (exemple: '2011-12-01 15:12:35').
 
45
    
 
46
    No timezone information is added, the datetime is a naive instance, but
 
47
    according to OpenERP 6.1 specification the timezone is always UTC.
 
48
    """
 
49
    if not str:
 
50
        return str
 
51
    return datetime.datetime.strptime(str, DEFAULT_SERVER_DATETIME_FORMAT)
 
52
 
 
53
def str_to_date(str):
 
54
    """
 
55
    Converts a string to a date object using OpenERP's
 
56
    date string format (exemple: '2011-12-01').
 
57
    """
 
58
    if not str:
 
59
        return str
 
60
    return datetime.datetime.strptime(str, DEFAULT_SERVER_DATE_FORMAT).date()
 
61
 
 
62
def str_to_time(str):
 
63
    """
 
64
    Converts a string to a time object using OpenERP's
 
65
    time string format (exemple: '15:12:35').
 
66
    """
 
67
    if not str:
 
68
        return str
 
69
    return datetime.datetime.strptime(str, DEFAULT_SERVER_TIME_FORMAT).time()
 
70
 
 
71
def datetime_to_str(obj):
 
72
    """
 
73
    Converts a datetime object to a string using OpenERP's
 
74
    datetime string format (exemple: '2011-12-01 15:12:35').
 
75
    
 
76
    The datetime instance should not have an attached timezone and be in UTC.
 
77
    """
 
78
    if not obj:
 
79
        return False
 
80
    return obj.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
 
81
 
 
82
def date_to_str(obj):
 
83
    """
 
84
    Converts a date object to a string using OpenERP's
 
85
    date string format (exemple: '2011-12-01').
 
86
    """
 
87
    if not obj:
 
88
        return False
 
89
    return obj.strftime(DEFAULT_SERVER_DATE_FORMAT)
 
90
 
 
91
def time_to_str(obj):
 
92
    """
 
93
    Converts a time object to a string using OpenERP's
 
94
    time string format (exemple: '15:12:35').
 
95
    """
 
96
    if not obj:
 
97
        return False
 
98
    return obj.strftime(DEFAULT_SERVER_TIME_FORMAT)
 
99