Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76342 > unrolled thread
| Started by | luofeiyu <elearn2014@gmail.com> |
|---|---|
| First post | 2014-08-15 10:24 +0800 |
| Last post | 2014-08-15 09:36 +0000 |
| Articles | 11 — 5 participants |
Back to article view | Back to comp.lang.python
timedelta problem luofeiyu <elearn2014@gmail.com> - 2014-08-15 10:24 +0800
Re: timedelta problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-15 03:51 +0000
Re: timedelta problem Chris Angelico <rosuav@gmail.com> - 2014-08-15 13:59 +1000
Re: timedelta problem Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-14 22:00 -0600
Re: timedelta problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-15 07:39 +0000
Re: timedelta problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-15 08:44 +0000
Re: timedelta problem Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-15 09:23 -0600
Re: timedelta problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-15 17:43 +0000
Re: timedelta problem Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-15 12:05 -0600
Re: timedelta problem Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-15 17:29 +0100
Re: timedelta problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-15 09:36 +0000
| From | luofeiyu <elearn2014@gmail.com> |
|---|---|
| Date | 2014-08-15 10:24 +0800 |
| Subject | timedelta problem |
| Message-ID | <mailman.13018.1408069502.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
In the python doc , https://docs.python.org/3.4/library/datetime.html
A timedelta
<https://docs.python.org/3.4/library/datetime.html#datetime.timedelta>
object represents a duration, the difference between two dates or times.
/class /datetime.timedelta(/days=0/, /seconds=0/, /microseconds=0/,
/milliseconds=0/, /minutes=0/, /hours=0/, /weeks=0/)
All arguments are optional and default to 0. Arguments may be
integers or floats, and may be positive or negative.
Only /days/, /seconds/ and /microseconds/ are stored internally.
import datetime
t1='Sat, 09 Aug 2014 07:36:46 -0700'
t2='Sat, 09 Aug 2014 07:36:46 +0700'
>>> datetime.datetime.strptime(t1,"%a, %d %b %Y %H:%M:%S %z")
datetime.datetime(2014, 8, 9, 7, 36, 46,
tzinfo=datetime.timezone(datetime.timed
elta(-1, 61200)))
>>> datetime.datetime.strptime(t2,"%a, %d %b %Y %H:%M:%S %z")
datetime.datetime(2014, 8, 9, 7, 36, 46,
tzinfo=datetime.timezone(datetime.timed
elta(0, 25200)))
problem :
t1 is GMT time 2014 00:36:46
t2 is GMT time 2014 14:36:46
datetime.datetime.strptime do not give me the right answer.
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-08-15 03:51 +0000 |
| Message-ID | <lsk03m$osa$8@dont-email.me> |
| In reply to | #76342 |
On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: > import datetime > t1='Sat, 09 Aug 2014 07:36:46 -0700' > t2='Sat, 09 Aug 2014 07:36:46 +0700' > datetime.datetime.strptime(t1,"%a, %d %b %Y %H:%M:%S %z") Are you sure? When I try this I get: ValueError: 'z' is a bad directive in format '%a, %d %b %Y %H:%M:%S %z' > datetime.datetime(2014, 8, 9, 7, 36, 46, > tzinfo=datetime.timezone(datetime.timedelta(-1, 61200))) And this: AttributeError: 'module' object has no attribute 'timezone' -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-08-15 13:59 +1000 |
| Subject | Re: timedelta problem |
| Message-ID | <mailman.13025.1408075186.18130.python-list@python.org> |
| In reply to | #76350 |
On Fri, Aug 15, 2014 at 1:51 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote: > On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: > >> import datetime >> t1='Sat, 09 Aug 2014 07:36:46 -0700' >> t2='Sat, 09 Aug 2014 07:36:46 +0700' >> datetime.datetime.strptime(t1,"%a, %d %b %Y %H:%M:%S %z") > > Are you sure? When I try this I get: > > ValueError: 'z' is a bad directive in format '%a, %d %b %Y %H:%M:%S %z' > >> datetime.datetime(2014, 8, 9, 7, 36, 46, >> tzinfo=datetime.timezone(datetime.timedelta(-1, 61200))) > > And this: > > AttributeError: 'module' object has no attribute 'timezone' Both fail as you describe in 2.7, but in 3.4/3.5ish (my 'python3' is a bit of a mess, but it's something between those two I think), both work as per the OP's description. You both need to be clearer about version numbers, I think :) The OP did have a link to docs with 3.4 in the name, although that isn't proof necessarily. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-14 22:00 -0600 |
| Subject | Re: timedelta problem |
| Message-ID | <mailman.13026.1408075301.18130.python-list@python.org> |
| In reply to | #76350 |
[Multipart message — attachments visible in raw view] — view raw
On Thu, Aug 14, 2014 at 9:51 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote: > > On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: > > > import datetime > > t1='Sat, 09 Aug 2014 07:36:46 -0700' > > t2='Sat, 09 Aug 2014 07:36:46 +0700' > > datetime.datetime.strptime(t1,"%a, %d %b %Y %H:%M:%S %z") > > Are you sure? When I try this I get: > > ValueError: 'z' is a bad directive in format '%a, %d %b %Y %H:%M:%S %z' > > > datetime.datetime(2014, 8, 9, 7, 36, 46, > > tzinfo=datetime.timezone(datetime.timedelta(-1, 61200))) > > And this: > > AttributeError: 'module' object has no attribute 'timezone' The datetime.timezone class was added in Python 3.2.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-08-15 07:39 +0000 |
| Message-ID | <lskdfb$6bg$1@dont-email.me> |
| In reply to | #76342 |
On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote:
On further inspection, it seems that strptime() in 2.7 doesn't handle %z
at all. In 3.2, it ignores the value it gets, because there's no
practical way to select the "right" tz string from the offset.
For example, a dictionary of offset minutes to tzstrings looks something
like this:
{
-720: ['BIT'],
-660: ['NUT', 'SST'],
-600: ['CKT', 'HAST', 'HST', 'TAHT'],
-570: ['MART', 'MIT'],
-540: ['AKST', 'GAMT', 'GIT', 'HADT'],
-480: ['AKDT', 'CIST', 'PST'],
-420: ['MST', 'PDT'],
-360: ['CST', 'EAST', 'GALT', 'MDT'],
-300: ['CDT', 'COT', 'CST', 'EASST', 'ECT', 'EST', 'PET'],
-270: ['VET'],
-240: ['AMT', 'AST', 'BOT', 'CDT', 'CLT', 'COST', 'ECT', 'EDT', 'FKT',
'GYT', 'PYT'],
-210: ['NST', 'NT'],
-180: ['ADT', 'AMST', 'ART', 'BRT', 'CLST', 'FKST', 'FKST', 'GFT', 'PMST',
'PYST', 'ROTT', 'SRT', 'UYT'],
-150: ['NDT'],
-120: ['FNT', 'GST', 'PMDT', 'UYST'],
-60: ['AZOST', 'CVT', 'EGT'],
0: ['GMT', 'UCT', 'UTC', 'WET', 'Z', 'EGST'],
60: ['BST', 'CET', 'DFT', 'IST', 'MET', 'WAT', 'WEDT', 'WEST'],
120: ['CAT', 'CEDT', 'CEST', 'EET', 'HAEC', 'IST', 'MEST', 'SAST',
'WAST'],
180: ['AST', 'EAT', 'EEDT', 'EEST', 'FET', 'IDT', 'IOT', 'SYOT'],
210: ['IRST'],
240: ['AMT', 'AZT', 'GET', 'GST', 'MSK', 'MUT', 'RET', 'SAMT', 'SCT',
'VOLT'],
270: ['AFT', 'IRDT'],
300: ['AMST', 'HMT', 'MAWT', 'MVT', 'ORAT', 'PKT', 'TFT', 'TJT', 'TMT',
'UZT'],
330: ['IST', 'SLST'],
345: ['NPT'],
360: ['BIOT', 'BST', 'BTT', 'KGT', 'VOST', 'YEKT'],
390: ['CCT', 'MMT', 'MST'],
420: ['CXT', 'DAVT', 'HOVT', 'ICT', 'KRAT', 'OMST', 'THA', 'WIT'],
480: ['ACT', 'AWST', 'BDT', 'CHOT', 'CIT', 'CST', 'CT', 'HKT', 'MST',
'MYT', 'PST', 'SGT', 'SST', 'ULAT', 'WST'],
525: ['CWST'],
540: ['AWDT', 'EIT', 'IRKT', 'JST', 'KST', 'TLT'],
570: ['ACST', 'CST'],
600: ['AEST', 'CHUT', 'DDUT', 'EST', 'PGT', 'VLAT', 'YAKT'],
630: ['ACDT', 'CST', 'LHST'],
660: ['AEDT', 'KOST', 'LHST', 'MIST', 'NCT', 'PONT', 'SAKT', 'SBT',
'VUT'],
690: ['NFT'],
720: ['FJT', 'GILT', 'MAGT', 'MHT', 'NZST', 'PETT', 'TVT', 'WAKT'],
765: ['CHAST']
780: ['NZDT', 'PHOT', 'TKT', 'TOT'],
825: ['CHADT'],
840: ['LINT'],
}
I've patched my 2.7 to set a tz string of "UTC[+-]nnnn" from the [+-]nnnn
%z value.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-08-15 08:44 +0000 |
| Message-ID | <lskh8k$8u4$1@dont-email.me> |
| In reply to | #76355 |
On Fri, 15 Aug 2014 07:39:23 +0000, Denis McMahon wrote: > I've patched my 2.7 to set a tz string of "UTC[+-]nnnn" from the > [+-]nnnn %z value. ... but that doesn't do much, because time.struct_time in 2.7 doesn't recognise anything that strptime passes in as a tz at all, as it expects the dst flag in that position and seems to have no concept of tz at all. So in 2.7 you can probably forget any question of reading in tz info with datetime.strptime(). See another reply to the OP regarding Python 3.2 -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-15 09:23 -0600 |
| Subject | Re: timedelta problem |
| Message-ID | <mailman.13032.1408116226.18130.python-list@python.org> |
| In reply to | #76355 |
On Fri, Aug 15, 2014 at 1:39 AM, Denis McMahon <denismfmcmahon@gmail.com> wrote: > On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: > > On further inspection, it seems that strptime() in 2.7 doesn't handle %z > at all. In 3.2, it ignores the value it gets, because there's no > practical way to select the "right" tz string from the offset. I'm not sure when %z was added, but it's worth noting that it doesn't seem to be documented earlier than 3.3.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-08-15 17:43 +0000 |
| Subject | Re: timedelta problem |
| Message-ID | <lslgrd$5hh$1@dont-email.me> |
| In reply to | #76374 |
On Fri, 15 Aug 2014 09:23:02 -0600, Ian Kelly wrote: > On Fri, Aug 15, 2014 at 1:39 AM, Denis McMahon > <denismfmcmahon@gmail.com> wrote: >> On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: >> >> On further inspection, it seems that strptime() in 2.7 doesn't handle >> %z at all. In 3.2, it ignores the value it gets, because there's no >> practical way to select the "right" tz string from the offset. > > I'm not sure when %z was added, but it's worth noting that it doesn't > seem to be documented earlier than 3.3. There may be some confusion because %z is in the table of strftime and strptime format chars on the 2.7.8 docs at https://docs.python.org/2/library/datetime.html#strftime-and-strptime- behavior but I suspect it's only applicable to strftime in that release. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-15 12:05 -0600 |
| Subject | Re: timedelta problem |
| Message-ID | <mailman.13036.1408125988.18130.python-list@python.org> |
| In reply to | #76379 |
On Fri, Aug 15, 2014 at 11:43 AM, Denis McMahon <denismfmcmahon@gmail.com> wrote: > On Fri, 15 Aug 2014 09:23:02 -0600, Ian Kelly wrote: > >> On Fri, Aug 15, 2014 at 1:39 AM, Denis McMahon >> <denismfmcmahon@gmail.com> wrote: >>> On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: >>> >>> On further inspection, it seems that strptime() in 2.7 doesn't handle >>> %z at all. In 3.2, it ignores the value it gets, because there's no >>> practical way to select the "right" tz string from the offset. >> >> I'm not sure when %z was added, but it's worth noting that it doesn't >> seem to be documented earlier than 3.3. > > There may be some confusion because %z is in the table of strftime and > strptime format chars on the 2.7.8 docs at > > https://docs.python.org/2/library/datetime.html#strftime-and-strptime- > behavior > > but I suspect it's only applicable to strftime in that release. Ah, interesting. I was looking at the time.strptime docs. I incorrectly assumed that since help(datetime.strptime) defers to help(time.strptime), the docs would also. I note it says that "datetime.strptime(date_string, format) is equivalent to datetime(*(time.strptime(date_string, format)[0:6]))", but clearly that's not the case when the format includes %z.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-08-15 17:29 +0100 |
| Subject | Re: timedelta problem |
| Message-ID | <mailman.13033.1408120179.18130.python-list@python.org> |
| In reply to | #76355 |
On 15/08/2014 16:23, Ian Kelly wrote: > On Fri, Aug 15, 2014 at 1:39 AM, Denis McMahon <denismfmcmahon@gmail.com> wrote: >> On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: >> >> On further inspection, it seems that strptime() in 2.7 doesn't handle %z >> at all. In 3.2, it ignores the value it gets, because there's no >> practical way to select the "right" tz string from the offset. > > I'm not sure when %z was added, but it's worth noting that it doesn't > seem to be documented earlier than 3.3. > Looks like this http://bugs.python.org/issue16667 -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-08-15 09:36 +0000 |
| Message-ID | <lskkb7$8u4$2@dont-email.me> |
| In reply to | #76342 |
On Fri, 15 Aug 2014 10:24:47 +0800, luofeiyu wrote: > problem : > > t1 is GMT time 2014 00:36:46 t2 is GMT time 2014 14:36:46 > > datetime.datetime.strptime do not give me the right answer. As far as I can tell from running the following, it all seems to work as expected in python 3.2 (and hence I expect in 3.4). If the expected output doesn't match yours, it would be interesting to see what your output is. If the expected output does match yours, and you think it's wrong, it would be interesting to know which bits you think are wrong and why you think they are wrong, because having a fair bit of the night looking at this, it all looks good to me. #!/usr/bin/python3 from datetime import tzinfo, timedelta, datetime, timezone # define timedelta based timezones UTCm7 = timezone(timedelta(0,-7*3600),"UTC-07:00") UTCp7 = timezone(timedelta(0,+7*3600),"UTC+07:00") UTC = timezone(timedelta(0), "UTC+00:00") # some timestrings t1 = 'Sat, 09 Aug 2014 07:36:46 -0700' t2 = 'Sat, 09 Aug 2014 07:36:46 +0700' t3 = 'Sat, 09 Aug 2014 07:36:46 +0000' # make some datetime objects # these are both utc -7 a1 = datetime.strptime(t1,"%a, %d %b %Y %H:%M:%S %z") b1 = datetime(2014, 8, 9, 7, 36, 46, tzinfo = UTCm7) # these are both utc +7 a2 = datetime.strptime(t2,"%a, %d %b %Y %H:%M:%S %z") b2 = datetime(2014, 8, 9, 7, 36, 46, tzinfo = UTCp7) # these are both utc a3 = datetime.strptime(t3,"%a, %d %b %Y %H:%M:%S %z") b3 = datetime(2014, 8, 9, 7, 36, 46, tzinfo = UTC) # print them out as stored print( "UTC -7:" ) print( t1 ) print( a1 ) print( b1 ) print( "UTC +7:" ) print( t2 ) print( a2 ) print( b2 ) print( "UTC:" ) print( t3 ) print( a3 ) print( b3 ) # print them out converted to UTC print( "UTC -7 as UTC:" ) print( a1.astimezone( UTC ) ) print( b1.astimezone( UTC ) ) print( "UTC +7 as UTC:" ) print( a2.astimezone( UTC ) ) print( b2.astimezone( UTC ) ) print( "UTC as UTC:" ) print( a3.astimezone( UTC ) ) print( b3.astimezone( UTC ) ) # expected output """ UTC -7: Sat, 09 Aug 2014 07:36:46 -0700 2014-08-09 07:36:46-07:00 2014-08-09 07:36:46-07:00 UTC +7: Sat, 09 Aug 2014 07:36:46 +0700 2014-08-09 07:36:46+07:00 2014-08-09 07:36:46+07:00 UTC: Sat, 09 Aug 2014 07:36:46 +0000 2014-08-09 07:36:46+00:00 2014-08-09 07:36:46+00:00 UTC -7 as UTC: 2014-08-09 14:36:46+00:00 2014-08-09 14:36:46+00:00 UTC +7 as UTC: 2014-08-09 00:36:46+00:00 2014-08-09 00:36:46+00:00 UTC as UTC: 2014-08-09 07:36:46+00:00 2014-08-09 07:36:46+00:00 """ -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web