|
1 |
|
2 Dates functions |
|
3 --------------- |
|
4 |
|
5 Dates functions are used to convert dates from/to string representation: |
|
6 |
|
7 >>> from datetime import datetime |
|
8 >>> from pyams_utils import date |
|
9 >>> now = datetime.fromtimestamp(1205000000) |
|
10 >>> now |
|
11 datetime.datetime(2008, 3, 8, 19, 13, 20) |
|
12 |
|
13 You can get an unicode representation of a date in ASCII format using 'unidate' fonction ; date is |
|
14 converted to GMT: |
|
15 |
|
16 >>> udate = date.unidate(now) |
|
17 >>> udate |
|
18 '2008-03-08T19:13:20+00:00' |
|
19 |
|
20 'parse_date' can be used to convert ASCII format into datetime: |
|
21 |
|
22 >>> ddate = date.parse_date(udate) |
|
23 >>> ddate |
|
24 datetime.datetime(2008, 3, 8, 19, 13, 20, tzinfo=<StaticTzInfo 'GMT'>) |
|
25 |
|
26 'date_to_datetime' can be used to convert a 'date' type to a 'datetime' value ; if a 'datetime' value |
|
27 is used as argument, it is returned 'as is': |
|
28 |
|
29 >>> ddate.date() |
|
30 datetime.date(2008, 3, 8) |
|
31 >>> date.date_to_datetime(ddate) |
|
32 datetime.datetime(2008, 3, 8, 19, 13, 20, tzinfo=<StaticTzInfo 'GMT'>) |
|
33 >>> date.date_to_datetime(ddate.date()) |
|
34 datetime.datetime(2008, 3, 8, 0, 0) |
|
35 |
|
36 |
|
37 Timezones handling |
|
38 ------------------ |
|
39 |
|
40 Timezones handling gave me headaches at first. I finally concluded that the best way (for me !) to handle |
|
41 TZ data was to store every datetime value in GMT timezone. |
|
42 As far as I know, there is no easy way to know the user's timezone from his request settings. So you can: |
|
43 - store this timezone in user's profile, |
|
44 - define a static server's timezone |
|
45 - create and register a ServerTimezoneUtility to handle server default timezone. |
|
46 |
|
47 My current default user's timezone is set to 'Europe/Paris' ; you should probably update this setting in |
|
48 'timezone.py' if you are located elsewhere. |
|
49 |
|
50 >>> from pyams_utils import timezone |
|
51 >>> timezone.tztime(ddate) |
|
52 datetime.datetime(2008, 3, 8, 19, 13, 20, tzinfo=<StaticTzInfo 'GMT'>) |
|
53 |
|
54 'gmtime' function can be used to convert a datetime to GMT: |
|
55 |
|
56 >>> timezone.gmtime(now) |
|
57 datetime.datetime(2008, 3, 8, 19, 13, 20, tzinfo=<StaticTzInfo 'GMT'>) |