Jump to content

Macros/ScriptForge/UniversalClockExample

From The Document Foundation Wiki


How to make a universal clock

Authored by Jean-Pierre Ledure.

Display the actual time on your computer, the actual Universal Time Coordinated (UTC), and the actual time of any city in the world selected in the combo box.

A universal clock with different times.
A universal clock with different times.

How to run it in BASIC

  1. Create a new document
  2. Open the Basic IDE
  3. Select the first available blank module
  4. Copy and paste the Basic code
  5. Verify line 14
  6. Set your own actual timezone line 19
  7. Run Main()

How to run it in Python

  1. Create a new document
  2. Run APSO
  3. Create a new module, call it 'Module1'
  4. Copy and paste the Python code below
  5. Verify line 17
  6. Set your own actual timezone line 22
  7. (Optionally) Define an alternative list of timezones on lines 97+
  8. Save and run the Main() method


Code

REM  SCRIPTFORGE WIKI EXAMPLE
REM 	How to make a universal clock ?
REM		Minimal required version: LibreOffice 7.6
REM 	Used services
REM 		SFDialogs.Dialog, SFDialogs.DialogControl, ScriptForge.Region

Option Explicit

Public dialog		As Object		'	The SFDialogs.Dialog service instance

'*******************************************************************
'***	Adjust module name if not "Module1'
'*******************************************************************
Const onaction = "vnd.sun.star.script:Standard.Module1.CloseDialog?language=Basic&location=document"

'*******************************************************************
'***	Set your timezone below
'*******************************************************************
Const mytimezone = "Europe/Brussels"


'*******************************************************************
'* Run the demo below
'*******************************************************************
Sub Main(Optional event As Object)
	GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
	SetupDialog()
	SetClocks()
	dialog.Terminate()
End Sub

'*******************************************************************
'* Specific demo code
'*******************************************************************
Sub SetClocks()
'	Compute the different clock values: computer, UTC and abroad
'	Check on https://time.is/UTC

Dim utc			As Date		'	Actual Coordinated Universal Time
Dim timezone	As String	'	The selected timezone
Dim region		As Object	'	The Region service

	If IsNull(dialog) Or IsEmpty(dialog) Then Exit Sub
	Set region = CreateScriptService("Region")

	dialog.Execute(modal := False)

	Do While dialog.Visible	'	The Close button hides the dialog box
		dialog.Controls("timeLocal").Value = Now()
		utc = region.UTCNow(mytimezone)
		dialog.Controls("timeUTC").Value = utc
		'	Get the targeted timezone at each loop
		timezone = dialog.Controls("TimeZone").Value
		dialog.Controls("timeAbroad").Value = region.LocalDateTime(utc, timezone)

		Wait 1000
	
	Loop

End Sub

'*******************************************************************
'* Fired by the Close button
'*******************************************************************
Sub CloseDialog(Optional event As Object)
'	Hide the non-modal dialog on user request. Effective closure is elsewhere

Dim closebutton		As Object		'The close button

	If IsNull(event) Then Exit Sub
	Set closebutton = CreateScriptService("DialogEvent", event)

	If Not IsNull(closebutton) Then dialog.Visible = False

End Sub

'*******************************************************************
'* May be defined with the Basic IDE
'*******************************************************************
Sub SetupDialog()
'	Build from scratch the example dialog and its controls

Dim control			As Object	'	A SFDialogs.DialogControl object

'	Define the list of the timezones made available in the combo box
Dim timezones		As Variant	'	Array of timezones to load in the TimeZone combo box

	'	Find the complete list of timezones on https://timezonedb.com/time-zones
	timezones = Array(	"America/Los_Angeles" _
							, "America/New_York" _
							, "Europe/London" _
							, "Europe/Brussels" _
							, "Asia/Shanghai" _
							, "Asia/Tokyo" _
							)


	Set dialog = CreateScriptService("NewDialog", "Clock", Array(150, 75, 180, 150))
	dialog.Caption = "Universal clock"
	With dialog
		'	The close button
		Set control = .CreateButton("btnClose", place := Array(140, 10, 30, 10))
		control.Caption = "Close"
		control.OnActionPerformed = onaction

		'	The clocks
		Set control = .CreateFixedText("lblLocal", _
							place := Array(25, 25, 75, 10))
			control.Caption = "My computer"
			Set control = .CreateTimeField("timeLocal", _
							place := Array(control.X, control.Y + control.Height + 2, control.Width, 15))
			control.Enabled = False
			control.Format = "24h long"
			control.XControlModel.Align = 1				'	Center
			control.XControlModel.FontHeight = 24			'	Font size
			control.XControlModel.FontWeight = 150.0		'	Bold
		Set control = .CloneControl("lblLocal", "lblUTC", left := control.X, top := control.Y + control.Height + 4)
			control.Caption = "UTC Time"
		Set control = .CloneControl("timeLocal", "timeUTC", left := control.X, top := control.Y + control.Height + 2)
		Set control = .CreateComboBox("TimeZone", _
							place := Array(control.X, control.Y + control.Height + 15, control.Width + 15, control.Height), _
							dropdown := True, LineCount := 8)
			control.RowSource = timezones
			control.ListIndex = 0
		Set control = .CloneControl("timeUTC", "timeAbroad", left := control.X, top := control.Y + control.Height + 4)
	
	End With

End Sub
# coding: utf-8
from __future__ import unicode_literals

###	SCRIPTFORGE WIKI EXAMPLE
###		How to make a universal clock ?
###		Minimal required version: LibreOffice 7,6
###		Used services
###			SFDialogs.Dialog, SFDialogs.DialogControl, ScriptForge.Region, ScriptForge.Basic

import time, datetime
from threading import Thread
from scriptforge import CreateScriptService

# *******************************************************************
#	Adjust module name if not 'Module1'
# *******************************************************************
onaction = 'vnd.sun.star.script:Module1.py$CloseDialog?language=Python&location=document'

# *******************************************************************
# ***	Set your timezone below
# *******************************************************************
mytimezone = 'Europe/Brussels'


# *******************************************************************
# * Run the demo below
# *******************************************************************
def Main(event = None):
    #	dialog				The SFDialogs.Dialog object
    dialog = SetupDialog()
    clock = Thread(target = SetClocks, args = (dialog,))
    clock.start()


# *******************************************************************
# * Specific demo code
# *******************************************************************
def SetClocks(dialog = None):
    #	Compute the different clock values: computer, UTC and abroad
    #	Check on https://time.is/UTC

    #	utc						Actual Coordinated Universal Time
    #	timezone				The selected timezone
    #	region					The Region service

    if dialog is None:
        return
    region = CreateScriptService('Region')

    dialog.Execute(modal = False)

    while dialog.Visible:  # The Close button hides the dialog box
        now = datetime.datetime.now()
        dialog.Controls('timeLocal').Value = now
        utc = region.UTCDateTime(now, mytimezone)
        dialog.Controls('timeUTC').Value = utc
        #	Get the targeted timezone at each loop
        timezone = dialog.Controls('TimeZone').Value
        dialog.Controls('timeAbroad').Value = region.LocalDateTime(utc, timezone)

        time.sleep(1)

    dialog.Terminate()

    return


# *******************************************************************
# Fired by the Close button
# *******************************************************************
def CloseDialog(event = None):
    #	Close the non-modal dialog on user request

    #	closebutton			The close button
    #	dialog				The dialog box

    if event is None:
        return
    closebutton = CreateScriptService("DialogEvent", event)

    if closebutton is not None:
        dialog = closebutton.Parent
        dialog.Visible = False

    return


# *******************************************************************
# * May be defined with the Basic IDE
# *******************************************************************
def SetupDialog():
    #	Build from scratch the example dialog and its controls

    #   dialog         The returned SFDialogs.Dialog object
    #   control        A SFDialogs.DialogControl object

    #	Define the list of available timezones
    #	timezones		Tuple of timezones to load in the TimeZone combo box
    #		Find the complete list of timezones on https://timezonedb.com/time-zones
    timezones = ('America/Los_Angeles', 
                 'America/New_York', 
                 'Europe/London', 
                 'Europe/Brussels', 
                 'Asia/Shanghai',
                 'Asia/Tokyo')

    dialog = CreateScriptService('NewDialog', 'Clock', (150, 75, 180, 150))
    dialog.Caption = 'Universal clock'
    #	The close button
    control = dialog.CreateButton('btnClose', place = (140, 10, 30, 10))
    control.Caption = 'Close'
    control.OnActionPerformed = onaction

    #	The clocks
    control = dialog.CreateFixedText('lblLocal', place = (25, 25, 75, 10))
    control.Caption = 'My computer'
    control = dialog.CreateTimeField('timeLocal',
                                     place = (control.X, control.Y + control.Height + 2, control.Width, 15))
    control.Enabled = False
    control.Format = '24h long'
    control.XControlModel.Align = 1  # Center
    control.XControlModel.FontHeight = 24  # Font size
    control.XControlModel.FontWeight = 150.0  # Bold

    control = dialog.CloneControl('lblLocal', 'lblUTC', left = control.X, top = control.Y + control.Height + 4)
    control.Caption = 'UTC Time'
    control = dialog.CloneControl('timeLocal', 'timeUTC', left = control.X, top = control.Y + control.Height + 2)

    control = dialog.CreateComboBox('TimeZone',
                                    place = (control.X, control.Y + control.Height + 10, control.Width + 15, 15),
                                    dropdown = True, linecount = 8)
    control.RowSource = timezones
    control.ListIndex = 0
    control = dialog.CloneControl('timeUTC', 'timeAbroad', left = control.X, top = control.Y + control.Height + 4)

    return dialog


g_exportedScripts = (Main,)

if __name__ == '__main__':
    Main()

See also