Getting time from localcalendar using python and uno only

Good Morning,

I am having some trouble trying to get the timestamp using the local calendar. I would like to avoid using the datetime or any other third party object. i would like to remain strictly with in UNO:

import uno
from com.sun.star.lang import Locale
from com.sun.star.util import DateTime
def getUTCTime():
	aLocale =Locale()
	oLocaleCalendar2=createUnoService("com.sun.star.i18n.LocaleCalendar2")
	oLocaleCalendar2.loadDefaultCalendarTZ(aLocale, "GMT+3")
	localeTime = oLocaleCalendar2.getLocalDateTime
	print(localeTime)
	return localeTime-int(localeTime)

def main():
	print(getUTCTime())

def createUnoService(serviceName):
	cc = uno.getComponentContext()
	sm = cc.ServiceManager
	return sm.createInstanceWithContext(serviceName, cc)

this is based off of the information in

Apache OpenOffice Community Forum - How to get UTC DateTime in a spreadsheet or in Basic - (View topic)
and
LibreOffice Calc Macro, how do I get UTC hour? - English - Ask LibreOffice

Hmm, you likely forgot to specify the some trouble. Your code works, it seems?

You are right. Here is what I get:


Error during invoking function main in module file:///D:/Program%20Files/LibreOfficePortable/Data/settings/user/Scripts/python/LibraryMisc/ModuleMisc.py (<class 'TypeError'>: int() argument must be a string, a bytes-like object or a number, not 'PyUNO_callable'
  File "D:\Program Files\LibreOfficePortable\App\libreoffice\program\pythonscript.py", line 915, in invoke
    ret = self.func( *args )
  File "D:\Program Files\LibreOfficePortable\Data\settings\user\Scripts\python\LibraryMisc\ModuleMisc.py", line 15, in main
    print(getUTCTime())
  File "D:\Program Files\LibreOfficePortable\Data\settings\user\Scripts\python\LibraryMisc\ModuleMisc.py", line 12, in getUTCTime
    return localeTime-int(localeTime)
)

The error occurs at:

return localeTime-int(localeTime)

On the line

print(localeTime)

The python shell returns:

<PyUNO_callable object at 0x33FF32C0>

neither datetime nor zoneinfo is thirdparty both are builtins in python

from datetime import datetime as dt
from zoneinfo import ZoneInfo

moscow = ZoneInfo("Europe/Moscow")

print(f" ⇒ {dt.now(tz=moscow) = :%Y-%m-%dT%H:%M:%S%z_%Z}")

⇒ dt.now(tz=moscow) = 2024-01-16T11:41:05+0300_MSK

2 Likes

getLocalDateTime is a function. You use Python, not Basic (which allows to call functions without ()). You need

    localeTime = oLocaleCalendar2.getLocalDateTime()
2 Likes

Thank you very much. That works great!

would you know the python equivalient of the basic:

Format(utcTime, "hh:mm:ss")

Thank you once again

Check NumberFormatter service.

1 Like
1 Like

exactly: Basic cannot distinguish between assignment and call

Thank you very much