Answering the question https://ask.libreoffice.org/en/question ... -utc-hour/ in a different site, a user "sokol92" posted a function returning UTC (or something nearly equivalent). Below you find what I made of it for my own use:
Code: Select all
Function nowUTC(Optional pNullDate As String) As Double
REM The function shall regard the document's NullDate if it is called from Calc.
REM without passing a pNullDate (factually "0"). 
REM For non-Calc documents the omitted pNullDate defaults to officeNulldate.
REM Otherwise the pNullDate must be passed in ISO 8601 extended.
REM Be sure to understand that a result based on a pNullDate different from
REM ThisComponent.NullDate can't reasonably be formatted as a date in Calc.
  Const unixNullDate    = "1970-01-01"
  Const officeNullDate  = "1899-12-30"
  Const starDivNullDate = "1900-01-01"
  Const  fancyNullDate  = "1904-01-01"
  Dim aLocale As New com.sun.star.lang.Locale
  Dim oLocaleCalendar As Object
  Dim nOffset As Double
  If IsMissing(pNullDate) Then pNullDate = "0" REM use ThisComponent.NullDate
  If pNullDate = "0" Then
    If NOT ThisComponent.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then
      pNullDate = officeNullDate
    Else
      With ThisComponent.NullDate
        pNullDate = "" & .Year & "-" & .Month & "-" & .Day
      End With
    End If
  End If
  nOffset = DateValue(pNulldate)
  oLocaleCalendar=CreateUnoService("com.sun.star.i18n.LocaleCalendar")
  With oLocaleCalendar
    .loadDefaultCalendar(aLocale) REM Uses Unix NullDate!
    nowUTC=.DateTime +  DateValue(unixNullDate) - nOffset
  End With
End FunctionInterested in criticism.
The much simpler time-only function clockUTC() is contained in my next Post below.
Editing: I inserted a letter "n" which had vanished for unknown reasons since my original testing from "com.sun.star.sheet" in the code.
|  Edit: 
 dt=2022-10-31 10:40 UTC Due to questions in a different site I came back to this thread and first time checked the demo attached to the fourth post (by @Villeroy). Be careful: There only is set the Unix-Nulldate (1970-01-01), and this setting is regarded when formatting the date-part of a date-time-stamp. However (at least under Win) the NOW() function of Calc takes its result from the system's zone-time. You won't get UTC (or GMT) this way. In addition: Don't tamper with the document's NullDate if you not are absolutely sure to understand the implications concerning different functions and API means. Testing them anew the UDFs I published in this thread (nowUTC() and clockUTC()) proved correct so far.  |