Python : Get current time and add seconds
#1
Hi.

I'm trying to get current time and add some minutes in python script for an addon.

Here is my code :

Code:
import xbmc, xbmcaddon, xbmcgui
from datetime import datetime, timedelta, time

def _get_end_time(minutes_string):
    now = datetime.now().time()
    full_minutes = int(minutes_string)
    fulldate = datetime(2000, 1, 1, now.hour, now.minute, now.second)
    fulldate = fulldate + datetime.timedelta(seconds=full_minutes)
    return fulldate.time()

But I run the addon, I have this error :

Code:
10:55:04 T:4860   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.ImportError'>
                                            Error Contents: Failed to import time because the import lockis held by another thread.
                                            Traceback (most recent call last):
                                              File "C:\Kodi-14.0\portable_data\addons\script.duration\default.py", line 98, in <module>
                                                Main()
                                              File "C:\Kodi-14.0\portable_data\addons\script.duration\default.py", line 52, in __init__
                                                self.run_backend()
                                              File "C:\Kodi-14.0\portable_data\addons\script.duration\default.py", line 81, in run_backend
                                                self.display_duration()
                                              File "C:\Kodi-14.0\portable_data\addons\script.duration\default.py", line 91, in display_duration
                                                end_time = _get_end_time(self.duration)
                                              File "C:\Kodi-14.0\portable_data\addons\script.duration\default.py", line 36, in _get_end_time
                                                now = datetime.now().time()
                                            ImportError: Failed to import time because the import lockis held by another thread.
                                            -->End of Python script error report<--

I have made an ugly workaround by calculating by myself :

Code:
def _get_end_time(minutes_string):
    try:
        now = xbmc.getInfoLabel("System.Time(hh:mm)")
        # Extract hours and minutes from time and convert to integer
        hh = int(now[0:2])
        mm = int(now[3:5])
        # Convert minutes to integer
        full_minutes = int(minutes_string)
        # Get minutes and hours from duration with modulo division
        minutes = full_minutes % 60
        hours   = full_minutes // 60
        # Add hours and minutes to current time
        mm = mm + minutes
        hh = hh + hours
        # Convert minutes to hours with minutes greater than 60
        hh = hh + (mm // 60)
        mm = mm % 60
        # Convert hours greater than 24
        hh = hh % 24
        return str(hh).zfill(2)+":"+str(mm).zfill(2)
    except:
        return '--:--'

Because ListItem.EndTime is only available for TV programms or PVR :

http://kodi.wiki/view/InfoLabels#Listitem

We can't use time function in addon ?
Sorry for my english, but, you know, I'm French so ...

Main HTPC : Odroid-C2 running CoreELEC with Titan Bingie
Secondary HTPC : Freebox Mini 4K running Android TV with Titan Bingie
Reply
#2
I seem to recall running into the same error when trying to get the current time using the datetime module a while ago ("import lockis held by another thread"). Have you tried using the arrow module instead of datetime?
Reply
#3
Thanks but arrow is in Kodi ?
Sorry for my english, but, you know, I'm French so ...

Main HTPC : Odroid-C2 running CoreELEC with Titan Bingie
Secondary HTPC : Freebox Mini 4K running Android TV with Titan Bingie
Reply
#4
Oh, no I guess it isn't. It would have to be added, but I am not quite sure how to add specific modules to your kodi addon. Sorry I couldn't be of more help =)
Reply
#5
As end time will be added to next stable release :

https://github.com/xbmc/xbmc/pull/6973

I won't spend more time on this script. The workaround work so it's enough for me Wink

BTW, thanks for yours replies.
Sorry for my english, but, you know, I'm French so ...

Main HTPC : Odroid-C2 running CoreELEC with Titan Bingie
Secondary HTPC : Freebox Mini 4K running Android TV with Titan Bingie
Reply
#6
My solution for the lock message was to try except loop until the lock read released.
Reply

Logout Mark Read Team Forum Stats Members Help
Python : Get current time and add seconds0