Re: timedelta problem
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[+-] from the [+-] %z value. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On 15Aug2014 13:59, Chris Angelico ros...@gmail.com wrote: On Fri, Aug 15, 2014 at 1:51 PM, Denis McMahon denismfmcma...@gmail.com wrote: 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. At least one of the OP's posts showed 3.4.0 as the Python version. On some flavour of Windows I believe. Cheers, Cameron Simpson c...@zip.com.au Our friend Trefayne is really most intuitive; you may trust that anything he says is absolutely true.- Aylebourne -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Fri, 15 Aug 2014 07:39:23 +, Denis McMahon wrote: I've patched my 2.7 to set a tz string of UTC[+-] from the [+-] %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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
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 +' # 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 + 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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Fri, Aug 15, 2014 at 1:39 AM, Denis McMahon denismfmcma...@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. -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On 15/08/2014 16:23, Ian Kelly wrote: On Fri, Aug 15, 2014 at 1:39 AM, Denis McMahon denismfmcma...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Fri, 15 Aug 2014 09:23:02 -0600, Ian Kelly wrote: On Fri, Aug 15, 2014 at 1:39 AM, Denis McMahon denismfmcma...@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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Fri, Aug 15, 2014 at 11:43 AM, Denis McMahon denismfmcma...@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 denismfmcma...@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. -- https://mail.python.org/mailman/listinfo/python-list
timedelta problem
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
luofeiyu elearn2...@gmail.com writes: 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))) (Your examples will be easier to read – and you will help those who are helping you – if you conform to PEP 8 in the code, and don't break up the output when you copy it here.) problem : t1 is GMT time 2014 00:36:46 t2 is GMT time 2014 14:36:46 Not true; t1 and t2 are not in the “GMT” zone, they are in UTC-0700 and UTC+0700 respectively. Their value includes that. The instants they *represent* may be equal to the times-plus-timezone you've listed here, but their value is different. The value includes the timezone information, so they're different from what you show. datetime.datetime.strptime do not give me the right answer. What do you think the right answer would be in each case? Why? -- \ “The aim of science is not to open the door to infinite wisdom, | `\but to set some limit on infinite error.” —Bertolt Brecht, | _o__)_Leben des Galilei_, 1938 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Thu, Aug 14, 2014 at 8:24 PM, luofeiyu elearn2...@gmail.com 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) 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 You have it backwards. t1 is a later time than t2. datetime.datetime.strptime do not give me the right answer. dt1 - dt2 datetime.timedelta(0, 50400) _.seconds // 3600 14 Looks correct to me. -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Thu, Aug 14, 2014 at 9:37 PM, Ian Kelly ian.g.ke...@gmail.com wrote: On Thu, Aug 14, 2014 at 8:24 PM, luofeiyu elearn2...@gmail.com wrote: t1 is GMT time 2014 00:36:46 t2 is GMT time 2014 14:36:46 You have it backwards. t1 is a later time than t2. datetime.datetime.strptime do not give me the right answer. dt1 - dt2 datetime.timedelta(0, 50400) _.seconds // 3600 14 Looks correct to me. Also: dt1.astimezone(datetime.timezone.utc) datetime.datetime(2014, 8, 9, 14, 36, 46, tzinfo=datetime.timezone.utc) dt2.astimezone(datetime.timezone.utc) datetime.datetime(2014, 8, 9, 0, 36, 46, tzinfo=datetime.timezone.utc) -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Fri, Aug 15, 2014 at 1:51 PM, Denis McMahon denismfmcma...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: timedelta problem
On Thu, Aug 14, 2014 at 9:51 PM, Denis McMahon denismfmcma...@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. -- https://mail.python.org/mailman/listinfo/python-list