1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Copyright (C) Stephane Wirtel
5
# Copyright (C) 2011 Nicolas Vanhoren
6
# Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
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.
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.
31
##############################################################################
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)
41
def str_to_datetime(str):
43
Converts a string to a datetime object using OpenERP's
44
datetime string format (exemple: '2011-12-01 15:12:35').
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.
51
return datetime.datetime.strptime(str, DEFAULT_SERVER_DATETIME_FORMAT)
55
Converts a string to a date object using OpenERP's
56
date string format (exemple: '2011-12-01').
60
return datetime.datetime.strptime(str, DEFAULT_SERVER_DATE_FORMAT).date()
64
Converts a string to a time object using OpenERP's
65
time string format (exemple: '15:12:35').
69
return datetime.datetime.strptime(str, DEFAULT_SERVER_TIME_FORMAT).time()
71
def datetime_to_str(obj):
73
Converts a datetime object to a string using OpenERP's
74
datetime string format (exemple: '2011-12-01 15:12:35').
76
The datetime instance should not have an attached timezone and be in UTC.
80
return obj.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
84
Converts a date object to a string using OpenERP's
85
date string format (exemple: '2011-12-01').
89
return obj.strftime(DEFAULT_SERVER_DATE_FORMAT)
93
Converts a time object to a string using OpenERP's
94
time string format (exemple: '15:12:35').
98
return obj.strftime(DEFAULT_SERVER_TIME_FORMAT)