How to get user timezone/time locale information?

I need a way for a macro in written in OpenOffice/LibreOffice Basic to be able to get the user’s current timezone and/or time locale whether that be a UTC offset, time zone abbreviation, tzdata (e.g. Africa/Addis_Ababa), or anything equivalent that can be identified or cross-referenced as a timezone. I need a solution that doesn’t require third party macros aside from ScriptForge and other macro libraries that are part of the default LibreOffice installation nor use a language that isn’t OpenOffice/LibreOffice Basic.

https://docs.python.org/3/library/time.html

1 Like

I need something that can be done in OpenOffice/LibreOffice Basic as I said in the OP.

Like this Threads?
https://forum.openoffice.org/en/forum/viewtopic.php?t=104910

Or possibly LibreOffice Calc Macro, how do I get UTC hour?

This just returns a time for a given timezone. I need something that returns the timezone identifier/locale for the current user.

Hallo

The answer was already given by @Villeroy , you may ignore it …or you can arrive in the year 2022 and accept that basic is unsuitable for this.

python_timezone

from time import strftime

print("i´m tired of honks who insist on Basic")
print(f"abbreviated Timezone: »{strftime('%Z')}«")
print(f"Timezone-offset in »[+-]HHMM«: »{strftime('%z')}«")

I need high portability and least likely to cause hassle on different operating systems. I know that python is built in, but I need the script to be able to run without needing to worry about maintenance burden and/or need user to install other packages such as libreoffice-script-provider-python on Ubuntu 18.04+.

Hallo

sudo apt install libreoffice-script-provider-python

is done in 10seconds!

This allows you to get a time in UTC and in current timezone (when you construct a calendar with no specific timezone), and get the time offset (which is specified in your question as something that satisfies you, even though does not define a single possible TZ).

The documentation on the API is here.

… and a requirement to have in functional in OpenOffice would make the task impossible, unless you follow the good advise of using Python. OTOH, ScriptForge isn’t part of OOo/AOO either…

You can write Python code that works outside the context of any office suite, on all kinds of computers.

Thank you for the reference. I think the problem is resolved; The script has .loadDefaultCalendarTZ with “UTC” string in TimeZone field when if string were just “” it would return local timezone as desired. It returns UTC time with offset, not the offset itself, so I guess difference has to be calculated.

The TimeZone string is something you need to pass as an argument when using the respective methods.

Clever guess.

Function localDateTimeOffset(Optional pMode As Long)
If (pMode<0) OR (pMode>4) Then Exit Function
Const utcPrefix = " UTC"
Dim theLocale As New com.sun.star.lang.Locale
Dim theCalendar As Object
theCalendar = CreateUnoService("com.sun.star.i18n.LocaleCalendar")
With theCalendar 
 .loadDefaultCalendar(theLocale) REM Uses Unix NullDate!
 offs = .LocalDateTime - .DateTime
End With
out = offs                                  REM Unit is 1 d ("calendaric day")
If pMode>0 Then out = offs * 24             REM Unit is now 1 h.
If pMode>1 Then
 signChar = IIf(sgn(out)<0,"-", "+")
 out = signCahr & Format(Abs(offs), "H:MM") REM out now formatted to a string.
EndIf
If pMode>2 Then
 If offs=0 Then out = ""
 out = utcPrefix & out
EndIf
If pMode=4 Then out = utcPrefix & signChar & Format(Abs(offs * 24), "0.00""h""")
localDateTimeOffset = out
End Function

BTW: What about your profile data? We don’t play Hide’n Seek here.

The TimeZone string is something you need to pass as an argument when using the respective methods.

I don’t know what I said to imply that it wasn’t something that needed to be passed.

For the code:

  1. Why use LocaleCalendar rather than LocaleCalendar2?
  2. Where are LocalDateTime and DateTime documented as I can’t find them in LibreOffice: LocaleCalendar Service Reference

Search the included interfaces. Probably XCalendarXCalendar4
However, the API documentation won’t have a lot to say. If you inspect an instance of the localeCalendar service in the Basic IDE or with one of the famous tools, you can see these properties.

If you inspect an instance of the localeCalendar service in the Basic IDE

How do you do this? I can’t find the inspect feature in the documentation. What famous tools are you talking about?

@quix
There is the built in inspector → Development Tools
.
And then MRI and XRay → Apache OpenOffice Community Forum - [Solved] Object Inspection Tool: XRAY or MRI - (View topic) and need help to get MRI to work on LO 7x - #2 by Ratslinger

@Ratslinger
Thanks for info. That toggle does not appear under tools in the Basic IDE (I’ve been assuming the editor that comes up when selecting to edit macros (Tools->Macros->Edit Macros…) that has LibreOffice Basic in the window title is the Basic IDE) so I added it to the toolbar, but it’s greyed out. Is there a reason why it’s greyed out? If I open development tools in a different LibreOffice flavor it just shows objects limited to the “LibreOffice application Supported objects” table in that development tools link. I don’t know what object can be placed that would use com.sun.star.i18n.LocaleCalendar service let alone the sought after functions. I’ll try XRAY or MRI to see if it’s more useful.

@quix
Don’t know what LO version you are using. From the on-line help:

Screenshot at 2022-11-28 16-41-00
My Calc sample:


.
My personal choice of the three is MRI.

Edit:

Using MRI & @Lupp code you would inspect theCalendar
There is a tutorial link in the Apache post-> Apache OpenOffice Community Forum - [Tutorial] Introduction into object inspection with MRI - (View topic)
.
To obtain MRI use the link in my post

@Ratslinger
I had to inspect via MRI 1.3.4 library in the macro because the menu bar in its gui doesn’t seem to work. MRI shows LocalDateTime and DateTime in the properties tab as double data types, but they don’t show up in methods. So I’m still not exactly where it’s coming from because of both services being loaded (more on that later). For future people that come across this thread, formatting the output of each of the functions shows LocalDateTime gives time local to the user and DateTime gives UTC standard time.
.
I think LocalDateTime the same as getLocalDateTime (in XCalendar4 which LocaleCalendar2 inherits) and DateTime is the same as getDateTime (in XCalendar which both LocaleCalendar and LocaleCalendar inherit), but I’m not sure if LocalDateTime accounts for “The actual timezone and daylight saving time offsets effective at the given date and time are considered and added to the UTC time at the calendar.” part of documentation.
.
Question about API:
I am inspecting theCalendar with (InitializeMRI function outside scope)

Call InitializeMri 
Dim theLocale As New com.sun.star.lang.Locale
Dim theCalendar As Object
theCalendar = CreateUnoService("com.sun.star.i18n.LocaleCalendar")
theCalendar.loadDefaultCalendar(theLocale)
oMRI.inspect(theCalendar)

.
Despite LocaleCalendar service being called, MRI is showing LocaleCalendar and LocaleCalendar2 as supported service names as well as getting all the methods from both services. It seems this isn’t just MRI showing it this way as functions from LocaleCalendar2 can be called. Why are both services being created/imported when only one of them is specified?