70f88302a0b77418f36b7900eeb3538acbca5c58
[sfa.git] / sfa / util / sfatime.py
1 #----------------------------------------------------------------------\r
2 # Copyright (c) 2008 Board of Trustees, Princeton University\r
3 #\r
4 # Permission is hereby granted, free of charge, to any person obtaining\r
5 # a copy of this software and/or hardware specification (the "Work") to\r
6 # deal in the Work without restriction, including without limitation the\r
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,\r
8 # and/or sell copies of the Work, and to permit persons to whom the Work\r
9 # is furnished to do so, subject to the following conditions:\r
10 #\r
11 # The above copyright notice and this permission notice shall be\r
12 # included in all copies or substantial portions of the Work.\r
13 #\r
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\r
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\r
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS\r
21 # IN THE WORK.\r
22 #----------------------------------------------------------------------\r
23 from types import StringTypes\r
24 import dateutil.parser\r
25 import datetime\r
26 import time\r
27 \r
28 from sfa.util.sfalogging import logger\r
29 \r
30 DATEFORMAT = "%Y-%m-%dT%H:%M:%SZ"\r
31 \r
32 def utcparse(input):\r
33     """ Translate a string into a time using dateutil.parser.parse but make sure it's in UTC time and strip\r
34 the timezone, so that it's compatible with normal datetime.datetime objects.\r
35 \r
36 For safety this can also handle inputs that are either timestamps, or datetimes\r
37 """\r
38     # prepare the input for the checks below by\r
39     # casting strings ('1327098335') to ints\r
40     if isinstance(input, StringTypes):\r
41         try:\r
42             input = int(input)\r
43         except ValueError:\r
44             pass\r
45 \r
46     if isinstance (input, datetime.datetime):\r
47         logger.warn ("argument to utcparse already a datetime - doing nothing")\r
48         return input\r
49     elif isinstance (input, StringTypes):\r
50         t = dateutil.parser.parse(input)\r
51         if t.utcoffset() is not None:\r
52             t = t.utcoffset() + t.replace(tzinfo=None)\r
53         return t\r
54     elif isinstance (input, (int,float,long)):\r
55         return datetime.datetime.fromtimestamp(input)\r
56     else:\r
57         logger.error("Unexpected type in utcparse [%s]"%type(input))\r
58 \r
59 def datetime_to_string(input):\r
60     return datetime.datetime.strftime(input, DATEFORMAT)\r
61 \r
62 def datetime_to_utc(input):\r
63     return time.gmtime(datetime_to_epoch(input))\r
64 \r
65 def datetime_to_epoch(input):\r
66     return int(time.mktime(input.timetuple()))\r