[Solved] How can I get GMT (UTC) time into my macro (Basic)?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
aacsus
Posts: 4
Joined: Sat Jun 29, 2019 8:55 am

[Solved] How can I get GMT (UTC) time into my macro (Basic)?

Post by aacsus »

Hello everybody. My first topic. Here we go:

I'm a beginner in this, yet I used to do programming for a while in VB, C, and C++.
I wrote a macro in OO Basic that deals with TIME. Since our time is changing with DST, I'd like to put the present GMT time in a separate cell.
Since I cannot see the contents (types, descriptions, variables, etc) of the system calls (is there a way?), I am struggling. I searched the whole forum & internet, in vein.

So, my question is:
Is there a way to get GMT in real time, with OO Basic calls?

Thank you all in advance,
AL
Last edited by MrProgrammer on Fri Aug 02, 2019 6:11 pm, edited 2 times in total.
Reason: Moved from Calc forum to OpenOffice Basic, Python, BeanShell, JavaScript
OpenOffice 4.1.6 on Windows 10 64-Bit
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How can I get GMT (UTC) time into my macro (OO Basic)?

Post by Zizi64 »

If I know it exactly, the AOO and the LO can get the time and date from the PC operating system. If the time/date of the operating system is set to GMT, then the NOW() function will get GMT in the Calc cell and in the StarBasic too.

I do not know, if it is possible to get the type of the datetime from the PC (from operating system)

Similar topics:
viewtopic.php?f=9&t=49417
https://ask.libreoffice.org/en/question ... -timezone/
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How can I get GMT (UTC) time into my macro (OO Basic)?

Post by FJCC »

I do not know how to do this in Basic. The python function below writes the current UTC into A1 of Sheet1. If you do not want to translate your entire macro to python, you could call a python routine from Basic that returns the UTC as a string. I am not on a computer where I can test that. A forum search on getScriptProvider should find some examples.

Code: Select all

from datetime import datetime

def getUTC():
  doc = XSCRIPTCONTEXT.getDocument()
  sheets = doc.Sheets
  sheet1 = sheets.getByName('Sheet1')
  oCell = sheet1.getCellRangeByName("A1")
  UTC = datetime.utcnow()
  oCell.String = UTC.isoformat()
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How can I get GMT (UTC) time into my macro (OO Basic)?

Post by Villeroy »

Code: Select all

Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
(Windows only)
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
aacsus
Posts: 4
Joined: Sat Jun 29, 2019 8:55 am

[Solved] How can I get GMT (UTC) time into my macro (OO Basi

Post by aacsus »

Thank you everybody, especially Villeroy. You gave me enough inspiration to find my way.

Here is what I came up with (and it works). I hope it helps the next person who'd needs this. Please feel free to perfect it further.

Code: Select all

Option Explicit

Private Declare Sub GetSystemTime Lib "Kernel32" (ByRef lpSystemTime As SYSTEMTIME)

Private Type SYSTEMTIME
  wYear As Integer
  wMonth As Integer
  wDayOfWeek As Integer
  wDay As Integer
  wHour As Integer
  wMinute As Integer
  wSecond As Integer
  wMilliseconds As Integer
End Type

Sub GetUTCtime

	Dim UTCnow As SYSTEMTIME

	Call GetSystemTime(UTCnow)
	MsgBox(UTCnow.wHour & ":" & UTCnow.wMinute & " Z", 0, "GMT/UTC/Zulu Time")

End Sub
Keep good work rolling,
AL
OpenOffice 4.1.6 on Windows 10 64-Bit
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] How can I get GMT (UTC) time into my macro (Bas

Post by Villeroy »

Thank you for sharing your solution.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply