Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pynotify screws up datetime, why?

The Problem

This code

#!/usr/bin/env python
import pynotify
import time
import datetime

c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

produces

5/1/12 1:15 PM
Traceback (most recent call last):
  File "tmp.py", line 9, in <module>
    dt = time.strptime(c, "%d/%m/%y %H:%M %p")
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: PM

Removing the import pynotify,

#!/usr/bin/env python
import time
import datetime

c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

Removes the error.

5/1/12 1:15 PM

WHY?!!?!

Python Version

Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2

pynotify.file

I added print calls for pynotify.__file__ and datetime.__file__

/usr/lib/python2.7/lib-dynload/datetime.so
/usr/lib/python2.7/dist-packages/gtk-2.0/pynotify/__init__.pyc
5/1/12 1:15 PM
Traceback (most recent call last):
  File "a.py", line 11, in <module>
    dt = time.strptime(c, "%d/%m/%y %H:%M %p")
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: PM

PDB

5/1/12 1:15 PM
> /usr/lib/python2.7/_strptime.py(324)_strptime()
-> found = format_regex.match(data_string)
(Pdb) format
'%d/%m/%y %H:%M %p'
(Pdb) continue
> /usr/lib/python2.7/_strptime.py(329)_strptime()
-> if len(data_string) != found.end():
(Pdb) continue
> /usr/lib/python2.7/_strptime.py(331)_strptime()
-> raise ValueError("unconverted data remains: %s" %
(Pdb) len(data_string)
14
(Pdb) found.end()
12
(Pdb) found.group(0)
'5/1/12 1:15 '

It would appear that '%d/%m/%y %H:%M %p' isn't capturing ALL of '5/1/12 1:15 PM'

like image 983
EricR Avatar asked May 01 '12 18:05

EricR


1 Answers

That's a fun problem. I'd wager what's happening is that pynotify is changing your locale settings, which is breaking strptime's interpretation of your timestamp string.

Here's your code with a couple of debugging print statements that will illustrate the theory:

#!/usr/bin/env python

import time
import datetime
import locale

print locale.getlocale()
import pynotify
print locale.getlocale()
c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

On my system, I get the following:

(None, None)
('en_US', 'UTF8')
5/1/12 1:15 PM

I am not getting your error, but it's possible pynotify is setting your locale to something completely silly which is confusing strptime.

Maybe have a look at that and fiddle a bit with your locale settings, either unset it before calling strptime (and set it back after, no knowing what kind of assumptions pynotify makes) or set it to something sane if you discover it's set to something silly.

like image 142
JosefAssad Avatar answered Oct 07 '22 04:10

JosefAssad