|
| 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 | +# |
| 12 | +# 1. Redistributions of source code must retain the above copyright notice, this |
| 13 | +# list of conditions and the following disclaimer. |
| 14 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +# this list of conditions and the following disclaimer in the documentation |
| 16 | +# and/or other materials provided with the distribution. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | +############################################################################## |
| 30 | + |
| 31 | +import datetime |
| 32 | + |
| 33 | +DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d" |
| 34 | +DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S" |
| 35 | +DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % ( |
| 36 | + DEFAULT_SERVER_DATE_FORMAT, |
| 37 | + DEFAULT_SERVER_TIME_FORMAT) |
| 38 | + |
| 39 | +def str_to_datetime(str): |
| 40 | + """ |
| 41 | + Converts a string to a datetime object using OpenERP's |
| 42 | + datetime string format (exemple: '2011-12-01 15:12:35'). |
| 43 | + |
| 44 | + No timezone information is added, the datetime is a naive instance, but |
| 45 | + according to OpenERP 6.1 specification the timezone is always UTC. |
| 46 | + """ |
| 47 | + if not str: |
| 48 | + return str |
| 49 | + return datetime.datetime.strptime(str.split(".")[0], DEFAULT_SERVER_DATETIME_FORMAT) |
| 50 | + |
| 51 | +def str_to_date(str): |
| 52 | + """ |
| 53 | + Converts a string to a date object using OpenERP's |
| 54 | + date string format (exemple: '2011-12-01'). |
| 55 | + """ |
| 56 | + if not str: |
| 57 | + return str |
| 58 | + return datetime.datetime.strptime(str, DEFAULT_SERVER_DATE_FORMAT).date() |
| 59 | + |
| 60 | +def str_to_time(str): |
| 61 | + """ |
| 62 | + Converts a string to a time object using OpenERP's |
| 63 | + time string format (exemple: '15:12:35'). |
| 64 | + """ |
| 65 | + if not str: |
| 66 | + return str |
| 67 | + return datetime.datetime.strptime(str.split(".")[0], DEFAULT_SERVER_TIME_FORMAT).time() |
| 68 | + |
| 69 | +def datetime_to_str(obj): |
| 70 | + """ |
| 71 | + Converts a datetime object to a string using OpenERP's |
| 72 | + datetime string format (exemple: '2011-12-01 15:12:35'). |
| 73 | + |
| 74 | + The datetime instance should not have an attached timezone and be in UTC. |
| 75 | + """ |
| 76 | + if not obj: |
| 77 | + return False |
| 78 | + return obj.strftime(DEFAULT_SERVER_DATETIME_FORMAT) |
| 79 | + |
| 80 | +def date_to_str(obj): |
| 81 | + """ |
| 82 | + Converts a date object to a string using OpenERP's |
| 83 | + date string format (exemple: '2011-12-01'). |
| 84 | + """ |
| 85 | + if not obj: |
| 86 | + return False |
| 87 | + return obj.strftime(DEFAULT_SERVER_DATE_FORMAT) |
| 88 | + |
| 89 | +def time_to_str(obj): |
| 90 | + """ |
| 91 | + Converts a time object to a string using OpenERP's |
| 92 | + time string format (exemple: '15:12:35'). |
| 93 | + """ |
| 94 | + if not obj: |
| 95 | + return False |
| 96 | + return obj.strftime(DEFAULT_SERVER_TIME_FORMAT) |
| 97 | + |
0 commit comments