[IMP] sprintf is awesome, so use sprintf correctly for patterning
[odoo/odoo.git] / openerpweb / dates.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import datetime
24
25 DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d"
26 DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S"
27 DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % (
28     DEFAULT_SERVER_DATE_FORMAT,
29     DEFAULT_SERVER_TIME_FORMAT)
30
31 def str_to_datetime(str):
32     """
33     Converts a string to a datetime object using OpenERP's
34     datetime string format (exemple: '2011-12-01 15:12:35').
35     
36     No timezone information is added, the datetime is a naive instance, but
37     according to OpenERP 6.1 specification the timezone is always UTC.
38     """
39     if not str:
40         return str
41     return datetime.datetime.strptime(str, DEFAULT_SERVER_DATETIME_FORMAT)
42
43 def str_to_date(str):
44     """
45     Converts a string to a date object using OpenERP's
46     date string format (exemple: '2011-12-01').
47     """
48     if not str:
49         return str
50     return datetime.datetime.strptime(str, DEFAULT_SERVER_DATE_FORMAT).date()
51
52 def str_to_time(str):
53     """
54     Converts a string to a time object using OpenERP's
55     time string format (exemple: '15:12:35').
56     """
57     if not str:
58         return str
59     return datetime.datetime.strptime(str, DEFAULT_SERVER_TIME_FORMAT).time()
60
61 def datetime_to_str(obj):
62     """
63     Converts a datetime object to a string using OpenERP's
64     datetime string format (exemple: '2011-12-01 15:12:35').
65     
66     The datetime instance should not have an attached timezone and be in UTC.
67     """
68     if not obj:
69         return False
70     return obj.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
71
72 def date_to_str(obj):
73     """
74     Converts a date object to a string using OpenERP's
75     date string format (exemple: '2011-12-01').
76     """
77     if not obj:
78         return False
79     return obj.strftime(DEFAULT_SERVER_DATE_FORMAT)
80
81 def time_to_str(obj):
82     """
83     Converts a time object to a string using OpenERP's
84     time string format (exemple: '15:12:35').
85     """
86     if not obj:
87         return False
88     return obj.strftime(DEFAULT_SERVER_TIME_FORMAT)