UTC vs. local time (And samba addition)

Asked by Justin

0.26 of mythtv has changed to reporting UTC time from the backend. I'm not a python person, and some google searches seemed to show some confusion as to utc and local time handling in python, so I haven't put a lot of effort into trying to implement this so far. I was hoping you could extend the scripts to perhaps query the version of mythtv and translate the time accordingly?

Also, I use my pc also as a samba server, so I added a samba activity monitor that I wanted to share in case others might find it useful.

import subprocess
import activity
from activity import Activity
from globals import log, run

class SambaActivity(Activity):
    """
    Checks for any locked files from samba server.
    """

    def is_idle(self):
        locks=subprocess.check_output(["smbstatus", "-L"])
        if "No locked files" in locks:
            return True
        return False

activity.register_activity_class(SambaActivity)

if __name__ == "__main__":
    mon = SambaActivity()
    print "idle:", mon.is_idle()

Question information

Language:
English Edit question
Status:
Answered
For:
TurnOff Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Pekka Jääskeläinen (pekka-jaaskelainen) said :
#1

Hi, samba activity checker is certainly useful. I committed it with modifications to make it work with Python 2.6.

For MythTV 0.26 compatibility, unfortunately I'm still running MythTV 0.24 in my HTPC. So you better implement the check with MythTV 0.26 and I can then test it still works with MythTV 0.24.

Revision history for this message
Thomas (twar) said :
#2

I have checked what is reported by the MythTV 0.26 bindings and it seems that they have now added timezone awareness to their Program results. This results in the following output from turnoff.

activity MythBackendActivity will be active at 2012-11-21 21:00:00+01:00
total of idle 1 activities will be active
Traceback (most recent call last):
  File "/usr/local/bin/turnoff_system.py", line 46, in <module>
    main()
  File "/usr/local/bin/turnoff_system.py", line 41, in main
    shutdown = set_wakeup_time(config)
  File "/usr/local/bin/set_wakeup_time.py", line 93, in set_wakeup_time
    wakeup_delta = wakeup_time - datetime.datetime.now()
TypeError: can't subtract offset-naive and offset-aware datetimes

This happens because datetime.datetime.now() by default, does not provide timezone awareness and just provides local time.

On top of this, I think that the Program results might be incorrect. I live in the UK and a program scheduled for 20:00:00 (8pm) UTC/GMT is reported as 21:00:00+01:00 by the MythTV bindings. The UK currently observes GMT, not BST (GMT+1). So MythTV should not apply a correction?

I was thinking of reversing the starttime's offset and then removing the tzinfo attribute using recording.starttime.replace(tzinfo=None). But this is all dependent on the consistency of what is returned by the bindings.

I have no idea why my local time is being adjusted by MythTV to be GMT+1. I'm also not skilled in Python and don't really understand how the MythTV bindings work because of their style.

Revision history for this message
Justin (koopjm) said :
#3

I don't think the samba_activity file got checked in, I just see .conf file and comments changes.

Revision history for this message
Pekka Jääskeläinen (pekka-jaaskelainen) said :
#4

Yep I forgot to add it. Now it's there.

Revision history for this message
Justin (koopjm) said :
#5

I had hacked this sometime before I entered this question, attempting to convert everything in the script to utc (since there didn't seem to be anything else other than mythtv using times). I didn't include it as I had no idea what I was doing, and although it did kind of work, it was off by an hour.
I worked around it by just adjusting the .conf wakeup time to wake an extra hour early. I'm on the east coast and we just had daylight savings time shift, and now it seems to program the wakeup time correctly (i.e. I had to remove my +60 minutes in the .conf file).

I haven't had any time to try to get back to this (especially since it's kind of working), but I don't see why it should be difficult. Surely someone can suggest a simple python command to convert from utc to local time? Or correctly do what I had tried of converting the rest of the scripts to use utc?

If I do ever find time to go back to this and have any better luck, I'll update.

Revision history for this message
Pekka Jääskeläinen (pekka-jaaskelainen) said :
#6

I remember trying to figure out an easy way for this before and it seems it's not totally trivial with Python:
http://stackoverflow.com/questions/4770297/python-convert-utc-datetime-string-to-local-datetime

Revision history for this message
Pekka Jääskeläinen (pekka-jaaskelainen) said :
#7

I finally upgraded my MythTV to 0.27 so I could test this. It should now work with 0.26+ (only).

Can you help with this problem?

Provide an answer of your own, or ask Justin for more information if necessary.

To post a message you must log in.