1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"SF_Region" script:
language=
"StarBasic" script:
moduleType=
"normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === Full documentation is available on https://help.libreoffice.org/ ===
6 REM =======================================================================================================================
11 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
12 ''' SF_Region
13 ''' =========
14 ''' Singleton class implementing the
"ScriptForge.Region
" service
15 ''' Implemented as a usual Basic module
17 ''' A collection of functions about languages, countries and timezones
18 ''' - Locales
19 ''' - Currencies
20 ''' - Numbers and dates formatting
21 ''' - Calendars
22 ''' - Timezones conversions
23 ''' - Numbers transformed to text
25 ''' Definitions:
26 ''' Locale or Region
27 ''' A combination of a language (
2 or
3 lower case characters) and a country (
2 upper case characters)
28 ''' Most properties and methods require a locale as argument.
29 ''' Some of them accept either the complete locale or only the language or country parts.
30 ''' When absent, the considered locale is the locale used in the LibreOffice user interface.
31 ''' (see the SF_Platform.OfficeLocale property)
32 ''' Timezone
33 ''' Specified as
"Region/City
" name like
"Europe/Berlin
", or a custom time zone ID such as
"UTC
" or
"GMT-
8:
00".
34 ''' The time offset between the timezone and the Greenwich Meridian Time (GMT) is expressed in minutes.
35 ''' The Daylight Saving Time (DST) is an additional offset.
36 ''' Both offsets can be positive or negative.
37 ''' More info on
38 ''' https://timezonedb.com/time-zones
39 ''' https://en.wikipedia.org/wiki/Time_zone
41 ''' Service invocation example:
42 ''' Dim regio As Object
43 ''' Set regio = CreateScriptService(
"Region
")
45 ''' Detailed user documentation:
46 ''' https://help.libreoffice.org/latest/en-US/text/sbasic/shared/
03/sf_region.html?DbPAR=BASIC
47 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
49 REM ================================================================== EXCEPTIONS
51 REM ============================================================= PRIVATE MEMBERS
53 Private UserLocale As String
' platform.OfficeLocale
55 ' Reference tables
56 Private LocaleData As Variant
' com.sun.star.i18n.LocaleData
57 Private LocaleNames As Variant
' Array of all available
"la-CO
" strings
59 Private UserIndex As Integer
' Index of UserLocale in reference tables
61 REM ============================================================ MODULE CONSTANTS
63 REM ===================================================== CONSTRUCTOR/DESTRUCTOR
65 REM -----------------------------------------------------------------------------
66 Public Function Dispose() As Variant
68 End Function
' ScriptForge.SF_Region Explicit destructor
70 REM ================================================================== PROPERTIES
72 REM -----------------------------------------------------------------------------
73 Property Get Country(Optional ByVal Region As Variant) As String
74 ''' Returns the english country name applicable in the given region.
75 ''' The region expressed either as a
76 ''' - locale combining language-COUNTRY (la-CO)
77 ''' - country only (CO)
78 ''' Example:
79 ''' MsgBox Regio.Country(
"IT
")
' Italy
80 Country = _PropertyGet(
"Country
", Region)
81 End Property
' ScriptForge.SF_Region.Country (get)
83 REM -----------------------------------------------------------------------------
84 Property Get Currency(Optional ByVal Region As Variant) As String
85 ''' Returns the currency applicable in the given region.
86 ''' The region is expressed either as a
87 ''' - locale combining language-COUNTRY (la-CO)
88 ''' - country only (CO)
89 ''' Example:
90 ''' MsgBox Regio.Currency(
"IT
")
' EUR
91 Currency = _PropertyGet(
"Currency
", Region)
92 End Property
' ScriptForge.SF_Region.Currency (get)
94 REM -----------------------------------------------------------------------------
95 Public Function DatePatterns(Optional ByVal Region As Variant) As Variant
' Function better than Property when return value is an array
96 ''' Returns list of date acceptance patterns for the given region.
97 ''' Patterns with input combinations that are accepted as incomplete date input, such as M/D or D.M
98 ''' The region is expressed as a locale combining language-COUNTRY (la-CO)
99 ''' The list is zero-based.
100 ''' Example:
101 ''' MsgBox Join(Regio.DatePatterns(
"it-IT
"),
",
")
' D/M/Y,D/M
102 DatePatterns = _PropertyGet(
"DatePatterns
", Region)
103 End Function
' ScriptForge.SF_Region.DatePatterns (get)
105 REM -----------------------------------------------------------------------------
106 Property Get DateSeparator(Optional ByVal Region As Variant) As String
107 ''' Returns the separator used in dates applicable in the given region.
108 ''' The region is expressed as a locale combining language-COUNTRY (la-CO)
109 ''' Example:
110 ''' MsgBox Regio.DateSeparator(
"it-IT
")
' /
111 DateSeparator = _PropertyGet(
"DateSeparator
", Region)
112 End Property
' ScriptForge.SF_Region.DateSeparator (get)
114 REM -----------------------------------------------------------------------------
115 Public Function DayAbbrevNames(Optional ByVal Region As Variant) As Variant
' Function better than Property when return value is an array
116 ''' Returns list of abbreviated names of weekdays applicable in the given region.
117 ''' The region expressed as a
118 ''' - locale combining language-COUNTRY (la-CO)
119 ''' - language only (la)
120 ''' The list is zero-based. The
1st in the list [
0] is the Monday.
121 ''' Example:
122 ''' MsgBox Join(Regio.DayAbbrevNames(
"it-IT
"),
",
")
' lun,mar,mer,gio,ven,sab,dom
123 DayAbbrevNames = _PropertyGet(
"DayAbbrevNames
", Region)
124 End Function
' ScriptForge.SF_Region.DayAbbrevNames (get)
126 REM -----------------------------------------------------------------------------
127 Public Function DayNames(Optional ByVal Region As Variant) As Variant
' Function better than Property when return value is an array
128 ''' Returns list of names of weekdays applicable in the given region.
129 ''' The region expressed as a
130 ''' - locale combining language-COUNTRY (la-CO)
131 ''' - language only (la)
132 ''' The list is zero-based. The
1st in the list [
0] is the Monday.
133 ''' Example:
134 ''' MsgBox Join(Regio.DayNames(
"it-IT
"),
",
")
' lunedì,martedì,mercoledì,giovedì,venerdì,sabato,domenica
135 DayNames = _PropertyGet(
"DayNames
", Region)
136 End Function
' ScriptForge.SF_Region.DayNames (get)
138 REM -----------------------------------------------------------------------------
139 Public Function DayNarrowNames(Optional ByVal Region As Variant) As Variant
' Function better than Property when return value is an array
140 ''' Returns list of initials of weekdays applicable in the given region.
141 ''' The region expressed as a
142 ''' - locale combining language-COUNTRY (la-CO)
143 ''' - language only (la)
144 ''' The list is zero-based. The
1st in the list [
0] is the Monday.
145 ''' Example:
146 ''' MsgBox Join(Regio.DayNarrowNames(
"it-IT
"),
",
")
' l,m,m,g,v,s,d
147 DayNarrowNames = _PropertyGet(
"DayNarrowNames
", Region)
148 End Function
' ScriptForge.SF_Region.DayNarrowNames (get)
150 REM -----------------------------------------------------------------------------
151 Property Get DecimalPoint(Optional ByVal Region As Variant) As String
152 ''' Returns the decimal separator used in numbers applicable in the given region.
153 ''' The region is expressed as a locale combining language-COUNTRY (la-CO)
154 ''' Example:
155 ''' MsgBox Regio.DecimalPoint(
"it-IT
")
' .
156 DecimalPoint = _PropertyGet(
"DecimalPoint
", Region)
157 End Property
' ScriptForge.SF_Region.DecimalPoint (get)
159 REM -----------------------------------------------------------------------------
160 Property Get Language(Optional ByVal Region As Variant) As String
161 ''' Returns the english Language name applicable in the given region.
162 ''' The region expressed as a
163 ''' - locale combining language-COUNTRY (la-CO)
164 ''' - language only (la)
165 ''' Example:
166 ''' MsgBox Regio.Language(
"it-IT
")
' Italian
167 Language = _PropertyGet(
"Language
", Region)
168 End Property
' ScriptForge.SF_Region.Language (get)
170 REM -----------------------------------------------------------------------------
171 Property Get ListSeparator(Optional ByVal Region As Variant) As String
172 ''' Returns the separator used in lists applicable in the given region.
173 ''' The region is expressed as a locale combining language-COUNTRY (la-CO)
174 ''' Example:
175 ''' MsgBox Regio.ListSeparator(
"it-IT
")
' ;
176 ListSeparator = _PropertyGet(
"ListSeparator
", Region)
177 End Property
' ScriptForge.SF_Region.ListSeparator (get)
179 REM -----------------------------------------------------------------------------
180 Public Function MonthAbbrevNames(Optional ByVal Region As Variant) As Variant
' Function better than Property when return value is an array
181 ''' Returns list of abbreviated names of months applicable in the given region.
182 ''' The region expressed as a
183 ''' - locale combining language-COUNTRY (la-CO)
184 ''' - language only (la)
185 ''' The list is zero-based.
186 ''' Example:
187 ''' MsgBox Join(Regio.MonthAbbrevNames(
"it-IT
"),
",
")
' gen,feb,mar,apr,mag,giu,lug,ago,set,ott,nov,dic
188 MonthAbbrevNames = _PropertyGet(
"MonthAbbrevNames
", Region)
189 End Function
' ScriptForge.SF_Region.MonthAbbrevNames (get)
191 REM -----------------------------------------------------------------------------
192 Public Function MonthNames(Optional ByVal Region As Variant) As Variant
' Function better than Property when return value is an array
193 ''' Returns list of names of months applicable in the given region.
194 ''' The region expressed as a
195 ''' - locale combining language-COUNTRY (la-CO)
196 ''' - language only (la)
197 ''' The list is zero-based.
198 ''' Example:
199 ''' MsgBox Join(Regio.MonthNames(
"it-IT
"),
",
")
' gennaio,febbraio,marzo,aprile,maggio,giugno,luglio,agosto,settembre,ottobre,novembre,dicembre
200 MonthNames = _PropertyGet(
"MonthNames
", Region)
201 End Function
' ScriptForge.SF_Region.MonthNames (get)
203 REM -----------------------------------------------------------------------------
204 Public Function MonthNarrowNames(Optional ByVal Region As Variant) As Variant
' Function better than Property when return value is an array
205 ''' Returns list of initials of months applicable in the given region.
206 ''' The region expressed as a
207 ''' - locale combining language-COUNTRY (la-CO)
208 ''' - language only (la)
209 ''' The list is zero-based.
210 ''' Example:
211 ''' MsgBox Join(Regio.MonthNarrowNames(
"it-IT
"),
",
")
' g,f,m,a,m,g,l,a,s,o,n,d
212 MonthNarrowNames = _PropertyGet(
"MonthNarrowNames
", Region)
213 End Function
' ScriptForge.SF_Region.MonthNarrowNames (get)
215 REM -----------------------------------------------------------------------------
216 Property Get ObjectType As String
217 ''' Only to enable object representation
218 ObjectType =
"SF_Region
"
219 End Property
' ScriptForge.SF_Region.ObjectType
221 REM -----------------------------------------------------------------------------
222 Property Get ServiceName As String
223 ''' Internal use
224 ServiceName =
"ScriptForge.Region
"
225 End Property
' ScriptForge.SF_Region.ServiceName
227 REM -----------------------------------------------------------------------------
228 Property Get ThousandSeparator(Optional ByVal Region As Variant) As String
229 ''' Returns the thousands separator used in numbers applicable in the given region.
230 ''' The region is expressed as a locale combining language-COUNTRY (la-CO)
231 ''' Example:
232 ''' MsgBox Regio.ThousandSeparator(
"it-IT
")
' .
233 ThousandSeparator = _PropertyGet(
"ThousandSeparator
", Region)
234 End Property
' ScriptForge.SF_Region.ThousandSeparator (get)
236 REM -----------------------------------------------------------------------------
237 Property Get TimeSeparator(Optional ByVal Region As Variant) As String
238 ''' Returns the separator used to format times applicable in the given region.
239 ''' The region is expressed as a locale combining language-COUNTRY (la-CO)
240 ''' Example:
241 ''' MsgBox Regio.TimeSeparator(
"it-IT
")
' :
242 TimeSeparator = _PropertyGet(
"TimeSeparator
", Region)
243 End Property
' ScriptForge.SF_Region.TimeSeparator (get)
245 REM ===================================================================== METHODS
247 REM -----------------------------------------------------------------------------
248 Public Function DSTOffset(Optional ByVal LocalDateTime As Variant _
249 , Optional ByVal TimeZone As Variant _
250 , Optional ByVal Locale As Variant _
252 ''' Computes the additional offset due to daylight saving (
"summer time
")
253 ''' Args
254 ''' LocalDateTime: local date and time as a Date. DST offset varies during the year.
255 ''' TimeZone: specified as
"Region/City
" name like
"Europe/Berlin
", or a custom time zone ID such as
"UTC
" or
"GMT-
8:
00"
256 ''' Locale: expressed as a locale combining language-COUNTRY (la-CO), or COUNTRY alone (CO)
257 ''' Return:
258 ''' The offset in minutes
259 ''' Examples:
260 ''' regio.DSTOffset(DateSerial(
2022,
8,
20) + TimeSerial(
16,
58,
17),
"Europe/Brussels
",
"fr-BE
")
' 60
262 Dim iDSTOffset As Integer
' Return value
263 Dim oLocale As Object
' com.sun.star.lang.Locale
264 Dim oCalendarImpl As Object
' com.sun.star.i18n.CalendarImpl
265 Const cstThisSub =
"Region.DSTOffset
"
266 Const cstSubArgs =
"LocalDateTime, TimeZone, [Locale=
""""]
"
268 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
272 If IsMissing(Locale) Or IsEmpty(Locale) Then Locale =
""
273 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
274 If Not SF_Utils._Validate(LocalDateTime,
"LocalDateTime
", V_DATE) Then GoTo Finally
275 If Not SF_Utils._Validate(TimeZone,
"TimeZone
", V_STRING) Then GoTo Finally
276 If Not SF_Utils._Validate(Locale,
"Locale
", V_STRING) Then GoTo Finally
279 Set oLocale = SF_Region._GetLocale(Locale, pbCountry := True)
280 If IsNull(oLocale) Then GoTo Finally
283 Set oCalendarImpl = SF_Utils._GetUNOService(
"CalendarImpl
")
285 .loadDefaultCalendarTZ(oLocale, TimeZone)
286 .setLocalDateTime(LocaldateTime)
287 iDSTOffset = .getValue(com.sun.star.i18n.CalendarFieldIndex.DST_OFFSET)
291 DSTOffset = iDSTOffset
292 SF_Utils._ExitFunction(cstThisSub)
296 End Function
' ScriptForge.SF_Region.DSTOffset
298 REM -----------------------------------------------------------------------------
299 Public Function GetProperty(Optional ByVal PropertyName As Variant _
300 , Optional Region As Variant _
302 ''' Return the actual value of the given property
303 ''' Args:
304 ''' PropertyName: the name of the property as a string
305 ''' Region: the language-COUNTRY combination (la-CO) or the country (CO- or the language (la)
306 ''' Returns:
307 ''' The actual value of the property
308 ''' Exceptions:
309 ''' ARGUMENTERROR The property does not exist
311 Const cstThisSub =
"Region.GetProperty
"
312 Const cstSubArgs =
""
314 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
318 If IsMissing(Region) Or IsEmpty(Region) Then Region =
""
319 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
320 If Not ScriptForge.SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
321 If Not ScriptForge.SF_Utils._Validate(Region,
"Region
", V_STRING) Then GoTo Catch
325 If Len(Region) =
0 Then
326 GetProperty = _PropertyGet(PropertyName)
328 GetProperty = _PropertyGet(PropertyName, Region)
332 SF_Utils._ExitFunction(cstThisSub)
336 End Function
' ScriptForge.SF_Region.GetProperty
338 REM -----------------------------------------------------------------------------
339 Public Function LocalDateTime(Optional ByVal UTCDateTime As Variant _
340 , Optional ByVal TimeZone As Variant _
341 , Optional ByVal Locale As Variant _
343 ''' Computes the local date and time from a UTC date and time
344 ''' Args
345 ''' UTCDateTime: the universal date and time to be converted to local time
346 ''' TimeZone: specified as
"Region/City
" name like
"Europe/Berlin
", or a custom time zone ID such as
"UTC
" or
"GMT-
8:
00"
347 ''' Locale: expressed as a locale combining language-COUNTRY (la-CO), or COUNTRY alone (CO)
348 ''' Return:
349 ''' The local time converted from the corresponding UTC date and time as a Date
350 ''' If the returned value is before
1900, it is likely that the Locale is not recognized
351 ''' If the returned value matches the local time, it is likely that the timezone is not recognized
352 ''' Examples:
353 ''' regio.LocalDateTime(DateSerial(
2022,
3,
20) + TimeSerial(
16,
58,
17),
"Europe/Brussels
",
"fr-BE
")
354 ''' ' 2022-
03-
20 17:
58:
17
356 Dim dLocalDateTime As Double
' Return value
357 Dim oLocale As Object
' com.sun.star.lang.Locale
358 Dim oCalendarImpl As Object
' com.sun.star.i18n.CalendarImpl
359 Const cstThisSub =
"Region.LocalDateTime
"
360 Const cstSubArgs =
"UTCDateTime, TimeZone, [Locale=
""""]
"
362 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
366 If IsMissing(Locale) Or IsEmpty(Locale) Then Locale =
""
367 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
368 If Not SF_Utils._Validate(LocalDateTime,
"LocalDateTime
", V_DATE) Then GoTo Finally
369 If Not SF_Utils._Validate(TimeZone,
"TimeZone
", V_STRING) Then GoTo Finally
370 If Not SF_Utils._Validate(Locale,
"Locale
", V_STRING) Then GoTo Finally
373 Set oLocale = SF_Region._GetLocale(Locale, pbCountry := True)
374 If IsNull(oLocale) Then GoTo Finally
377 Set oCalendarImpl = SF_Utils._GetUNOService(
"CalendarImpl
")
379 .loadDefaultCalendarTZ(oLocale, TimeZone)
380 .setDateTime(UTCDateTime)
381 dLocalDateTime = .getLocalDateTime()
385 LocalDateTime = CDate(dLocalDateTime)
386 SF_Utils._ExitFunction(cstThisSub)
390 End Function
' ScriptForge.SF_Region.LocalDateTime
392 REM -----------------------------------------------------------------------------
393 Public Function Methods() As Variant
394 ''' Return the list of public methods of the Region class as an array
397 "DSTOffset
" _
398 ,
"LocalDateTime
" _
399 ,
"Number2Text
" _
400 ,
"TimeZoneOffset
" _
401 ,
"UTCDateTime
" _
402 ,
"UTCNow
" _
405 End Function
' ScriptForge.SF_Region.Methods
407 REM -----------------------------------------------------------------------------
408 Public Function Number2Text(Optional ByVal Number As Variant _
409 , Optional ByVal Locale As Variant _
411 ''' Convert numbers and money amounts in many languages into words
412 ''' Args
413 ''' Number: the number to spell out
414 ''' Accepted types: strings or numeric values (integer or real numbers)
415 ''' When a string, a variety of prefixes is supported
416 ''' The string
"help
" provides helpful tips about allowed prefixes by language
417 ''' Example for french
418 ''' un, deux, trois
419 ''' feminine: une, deux, trois
420 ''' masculine: un, deux, trois
421 ''' ordinal: premier, deuxième, troisième
422 ''' ordinal-feminine: première, deuxième, troisième
423 ''' ordinal-masculine: premier, deuxième, troisième
424 ''' informal: onze-cents, douze-cents, treize-cents
425 ''' Numbers may be prefixed by ISO currency codes (EUR, USD, ...)
426 ''' Locale: expressed as a locale combining language-COUNTRY (la-CO), or language alone (la)
427 ''' The list of supported languages can be found on
428 ''' https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1linguistic2_1_1XNumberText.html
429 ''' Return:
430 ''' The number or amount transformed in words
431 ''' Examples:
432 ''' regio.Number2Text(
"help
",
"fr
")
' See above
433 ''' regio.Number2Text(
"79,
93",
"fr-BE
")
' septante-neuf virgule nonante-trois
434 ''' regio.Number2Text(Pi(),
"pt-BR
")
' três vírgula um quatro um cinco nove dois seis cinco três cinco oito nove sete nove
435 ''' regio.Number2Text(
"EUR
1234.56",
"it
")
' milleduecentotrentaquattro euro cinquantasei centesimi
437 Dim sNumber2Text As String
' Return value
438 Dim oLocale As Object
' com.sun.star.lang.Locale
439 Dim oNumber2Text As Object
' com.sun.star.linguistic2.NumberText
440 Const cstThisSub =
"Region.Number2Text
"
441 Const cstSubArgs =
"Number, [Locale=
""""]
"
443 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
444 sNumber2Text =
""
447 If IsMissing(Locale) Or IsEmpty(Locale) Then Locale =
""
448 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
449 If Not SF_Utils._Validate(Number,
"Number
", Array(V_STRING, V_NUMERIC)) Then GoTo Finally
450 If Not SF_Utils._Validate(Locale,
"Locale
", V_STRING) Then GoTo Finally
453 Set oLocale = SF_Region._GetLocale(Locale, pbLanguage := True)
454 If IsNull(oLocale) Then GoTo Finally
457 Set oNumber2Text = SF_Utils._GetUNOService(
"Number2Text
")
458 sNumber2Text = oNumber2Text.getNumberText(Number, oLocale)
461 Number2Text = sNumber2Text
462 SF_Utils._ExitFunction(cstThisSub)
466 End Function
' ScriptForge.SF_Region.Number2Text
468 REM -----------------------------------------------------------------------------
469 Public Function Properties() As Variant
470 ''' Return the list or properties of the Region class as an array
472 Properties = Array( _
473 "Country
" _
474 ,
"Currency
" _
475 ,
"DatePatterns
" _
476 ,
"DateSeparator
" _
477 ,
"DayAbbrevNames
" _
478 ,
"DayNames
" _
479 ,
"DayNarrowNames
" _
480 ,
"DecimalPoint
" _
481 ,
"Language
" _
482 ,
"ListSeparator
" _
483 ,
"MonthAbbrevNames
" _
484 ,
"MonthNames
" _
485 ,
"MonthNarrowNames
" _
486 ,
"ThousandSeparator
" _
487 ,
"TimeSeparator
" _
490 End Function
' ScriptForge.SF_Region.Properties
492 REM -----------------------------------------------------------------------------
493 Public Function TimeZoneOffset(Optional ByVal TimeZone As Variant _
494 , Optional ByVal Locale As Variant _
496 ''' Computes the offset between GMT and the given timezone and locale
497 ''' Args
498 ''' TimeZone: specified as
"Region/City
" name like
"Europe/Berlin
", or a custom time zone ID such as
"UTC
" or
"GMT-
8:
00"
499 ''' Locale: expressed as a locale combining language-COUNTRY (la-CO), or COUNTRY alone (CO)
500 ''' Return:
501 ''' The offset in minutes
502 ''' Examples:
503 ''' regio.TimeZoneOffset(
"Europe/Brussels
",
"fr-BE
")
' 60
505 Dim iTimeZoneOffset As Integer
' Return value
506 Dim oLocale As Object
' com.sun.star.lang.Locale
507 Dim oCalendarImpl As Object
' com.sun.star.i18n.CalendarImpl
508 Const cstThisSub =
"Region.TimeZoneOffset
"
509 Const cstSubArgs =
"TimeZone, [Locale=
""""]
"
511 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
515 If IsMissing(Locale) Or IsEmpty(Locale) Then Locale =
""
516 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
517 If Not SF_Utils._Validate(TimeZone,
"TimeZone
", V_STRING) Then GoTo Finally
518 If Not SF_Utils._Validate(Locale,
"Locale
", V_STRING) Then GoTo Finally
521 Set oLocale = SF_Region._GetLocale(Locale, pbCountry := True)
522 If IsNull(oLocale) Then GoTo Finally
525 Set oCalendarImpl = SF_Utils._GetUNOService(
"CalendarImpl
")
527 .loadDefaultCalendarTZ(oLocale, TimeZone)
528 iTimeZoneOffset = .getValue(com.sun.star.i18n.CalendarFieldIndex.ZONE_OFFSET)
532 TimeZoneOffset = iTimeZoneOffset
533 SF_Utils._ExitFunction(cstThisSub)
537 End Function
' ScriptForge.SF_Region.TimeZoneOffset
539 REM -----------------------------------------------------------------------------
540 Public Function UTCDateTime(Optional ByVal LocalDateTime As Variant _
541 , Optional ByVal TimeZone As Variant _
542 , Optional ByVal Locale As Variant _
544 ''' Computes the UTC date and time of a given local date and time
545 ''' Args
546 ''' LocalDateTime: the date and time measured in a given timezone
547 ''' TimeZone: specified as
"Region/City
" name like
"Europe/Berlin
", or a custom time zone ID such as
"UTC
" or
"GMT-
8:
00"
548 ''' Locale: expressed as a locale combining language-COUNTRY (la-CO), or COUNTRY alone (CO)
549 ''' Return:
550 ''' The local time converted to the corresponding UTC date and time as a Date
551 ''' If the returned value is before
1900, it is likely that the Locale is not recognized
552 ''' If the returned value matches the local time, it is likely that the the timezone is not recognized
553 ''' Examples:
554 ''' regio.UTCDateTime(DateSerial(
2022,
3,
20) + TimeSerial(
17,
58,
17),
"Europe/Brussels
",
"fr-BE
")
555 ''' ' 2022-
03-
20 16:
58:
17
557 Dim dUTCDateTime As Double
' Return value
558 Dim oLocale As Object
' com.sun.star.lang.Locale
559 Dim oCalendarImpl As Object
' com.sun.star.i18n.CalendarImpl
560 Const cstThisSub =
"Region.UTCDateTime
"
561 Const cstSubArgs =
"LocalDateTime, TimeZone, [Locale=
""""]
"
563 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
567 If IsMissing(Locale) Or IsEmpty(Locale) Then Locale =
""
568 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
569 If Not SF_Utils._Validate(LocalDateTime,
"LocalDateTime
", V_DATE) Then GoTo Finally
570 If Not SF_Utils._Validate(TimeZone,
"TimeZone
", V_STRING) Then GoTo Finally
571 If Not SF_Utils._Validate(Locale,
"Locale
", V_STRING) Then GoTo Finally
574 Set oLocale = SF_Region._GetLocale(Locale, pbCountry := True)
575 If IsNull(oLocale) Then GoTo Finally
578 Set oCalendarImpl = SF_Utils._GetUNOService(
"CalendarImpl
")
580 .loadDefaultCalendarTZ(oLocale, TimeZone)
581 .setLocalDateTime(LocalDateTime)
582 dUTCDateTime = .getDateTime()
586 UTCDateTime = CDate(dUTCDateTime)
587 SF_Utils._ExitFunction(cstThisSub)
591 End Function
' ScriptForge.SF_Region.UTCDateTime
593 REM -----------------------------------------------------------------------------
594 Public Function UTCNow(Optional ByVal TimeZone As Variant _
595 , Optional ByVal Locale As Variant _
597 ''' Computes the actual UTC date and time
598 ''' Args
599 ''' TimeZone: specified as
"Region/City
" name like
"Europe/Berlin
", or a custom time zone ID such as
"UTC
" or
"GMT-
8:
00"
600 ''' Locale: expressed as a locale combining language-COUNTRY (la-CO), or COUNTRY alone (CO)
601 ''' Return:
602 ''' The actual UTC date and time as a Date
603 ''' If the returned value is before
1900, it is likely that the Locale is not recognized
604 ''' If the returned value matches the local time, it is likely that the the timezone is not recognized
605 ''' Examples:
606 ''' regio.UTCNow(
"Europe/Brussels
",
"fr-BE
")
' 2022-
03-
20 16:
58:
17
608 Dim dUTCNow As Double
' Return value
609 Dim oLocale As Object
' com.sun.star.lang.Locale
610 Dim oCalendarImpl As Object
' com.sun.star.i18n.CalendarImpl
611 Const cstThisSub =
"Region.UTCNow
"
612 Const cstSubArgs =
"TimeZone, [Locale=
""""]
"
614 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
618 If IsMissing(Locale) Or IsEmpty(Locale) Then Locale =
""
619 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
620 If Not SF_Utils._Validate(TimeZone,
"TimeZone
", V_STRING) Then GoTo Finally
621 If Not SF_Utils._Validate(Locale,
"Locale
", V_STRING) Then GoTo Finally
624 Set oLocale = SF_Region._GetLocale(Locale, pbCountry := True)
625 If IsNull(oLocale) Then GoTo Finally
628 Set oCalendarImpl = SF_Utils._GetUNOService(
"CalendarImpl
")
630 .loadDefaultCalendarTZ(oLocale, TimeZone)
631 .setLocalDateTime(Now())
632 dUTCNow = .getDateTime()
636 UTCNow = CDate(dUTCNow)
637 SF_Utils._ExitFunction(cstThisSub)
641 End Function
' ScriptForge.SF_Region.UTCNow
643 REM =========================================================== PRIVATE FUNCTIONS
645 REM -----------------------------------------------------------------------------
646 Private Function _GetLocale(ByVal psLocale As String _
647 , Optional ByVal pbCountry As Variant _
648 , Optional ByVal pbLanguage As Variant _
650 ''' Convert a locale given as a string to a com.sun.star.lang.Locale object
651 ''' Args:
652 ''' psLocale: the input string, as
"la-CO
",
"la
" or
"CO
"
653 ''' pbCountry: True when
"CO
" only is admitted
654 ''' pbLanguage: True when
"la
" only is admitted
655 ''' At most one out of pbLanguage or pbCountry may be True
656 ''' Returns:
657 ''' com.sun.star.lang.Locale
659 Dim sLocale As String
' "la-CO
"
660 Dim iLocale As Integer
' Index in reference tables
661 Dim oLocale As Object
' Return value com.sun.star.lang.Locale
664 If IsMissing(pbCountry) Or IsEmpty(pbCountry) Then pbCountry = False
665 If IsMissing(pbLanguage) Or IsEmpty(pbLanguage) Then pbLanguage = False
667 _LoadAllLocales()
' Initialize locale reference tables
670 ' The argument may be a language
"la
", a country
"CO
" or a Locale
"la-CO
"
671 ' Scan the reference tables to find a valid locale as a com.sun.star.lang.Locale
672 Set oLocale = Nothing : sLocale =
"" : iLocale = -
1
673 If Len(psLocale) =
0 Then
' Default value is the office com.sun.star.i18n.Locale
676 ElseIf InStr(psLocale,
"-
") =
0 Then
' Language only or country only
679 ' Find any locale having the argument as language
680 For i =
0 To UBound(LocaleNames)
681 ' A language is presumed
2 or
3 characters long
682 If Split(LocaleNames(i),
"-
")(
0) = LCase(psLocale) Then
683 sLocale = LocaleNames(i)
689 ' Find any locale having the argument as country
690 For i =
0 To UBound(LocaleNames)
691 ' A country is presumed exactly
2 characters long
692 If Right(LocaleNames(i),
2) = UCase(psLocale) Then
693 sLocale = LocaleNames(i)
700 Else
' A full locale is given
701 iLocale = SF_Array.IndexOf(LocaleNames, psLocale, CaseSensitive := False)
702 If iLocale
>=
0 Then sLocale = LocaleNames(iLocale)
706 ' Build error message when relevant
707 If iLocale
< 0 Then
708 If Not SF_Utils._Validate(psLocale,
"Locale
", V_STRING, LocaleNames) Then GoTo Finally
710 Set oLocale = CreateUnoStruct(
"com.sun.star.lang.Locale
")
711 oLocale.Language = Split(sLocale,
"-
")(
0)
' A language is
2 or
3 characters long
712 oLocale.Country = Right(sLocale,
2)
716 Set _GetLocale = oLocale
718 End Function
' ScriptForge.SF_Region._GetLocale
720 REM -----------------------------------------------------------------------------
721 Private Sub _LoadAllLocales()
722 ''' Initialize the LocaleNames array = the list of all available locales in the LibreOffice installation
724 Dim oOffice As Object
' com.sun.star.lang.Locale
725 Dim vLocales As Variant
' Array of com.sun.star.lang.Locale
726 Dim iTop As Integer
' Upper bound of LocaleNames
731 If Len(UserLocale) =
0 Then
732 Set oOffice = SF_Utils._GetUNOService(
"OfficeLocale
")
733 UserLocale = oOffice.Language
& "-
" & oOffice.Country
736 ' LocaleData, localeNames and UserIndex
737 If IsEmpty(LocaleData) Or IsNull(LocaleData) Or Not IsArray(LocaleNames) Then
738 LocaleData = SF_Utils._GetUNOService(
"LocaleData
")
739 vLocales = LocaleData.getAllInstalledLocaleNames()
740 LocaleNames = Array()
741 iTop = UBound(vLocales)
742 ReDim LocaleNames(
0 To iTop)
744 LocaleNames(i) = vLocales(i).Language
& "-
" & vLocales(i).Country
745 If LocaleNames(i) = UserLocale Then UserIndex = i
749 End Sub
' ScriptForge.SF_Region._LoadAllLocales
751 REM -----------------------------------------------------------------------------
752 Private Function _PropertyGet(Optional ByVal psProperty As String _
753 , Optional ByVal pvLocale As Variant) As Variant
754 ''' Return the value of the named property
755 ''' Args:
756 ''' psProperty: the name of the property
757 ''' pvLocale: a locale in the form language-COUNTRY (la-CO) or language only, or country only
758 ''' When language or country only, any locale matching either the language or the country is selected
760 Dim oLocale As Object
' com.sun.star.lang.Locale
761 Dim vCurrencies As Variant
' Array of com.sun.star.i18n.Currency
762 Dim oCurrency As Object
' com.sun.star.i18n.Currency
763 Dim oLanguageCountryInfo As Object
' com.sun.star.i18n.LanguageCountryInfo
764 Dim oLocaleDataItem2 As Object
' com.sun.star.i18n.LocaleDataItem2
765 Dim oCalendarImpl As Object
' com.sun.star.i18n.CalendarImpl
766 Dim oCalItem As Object
' com.sun.star.i18n.CalendarItem2
767 Dim vCalItems() As Variant
' Array of days/months
768 Dim i As Integer, j As Integer
770 Dim cstThisSub As String
771 Const cstSubArgs =
""
773 cstThisSub =
"Region.Get
" & psProperty
774 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
777 If IsMissing(pvLocale) Or IsEmpty(pvLocale) Then pvLocale =
""
778 If Not SF_Utils._Validate(pvLocale,
"Locale
", V_STRING) Then GoTo Finally
780 Select Case psProperty
781 Case
"Currency
",
"Country
"
782 Set oLocale = SF_Region._GetLocale(pvLocale, pbCountry := True)
' Country only is admitted
783 Case
"Language
",
"DayNames
",
"DayAbbrevNames
",
"DayNarrowNames
" _
784 ,
"MonthNames
",
"MonthAbbrevNames
",
"MonthNarrowNames
"
785 Set oLocale = SF_Region._GetLocale(pvLocale, pbLanguage := True)
' Language only is admitted
787 Set oLocale = SF_Region._GetLocale(pvLocale)
789 If IsNull(oLocale) Then GoTo Finally
792 Select Case psProperty
793 Case
"Country
",
"Language
"
794 Set oLanguageCountryInfo = LocaleData.getLanguageCountryInfo(oLocale)
795 With oLanguageCountryInfo
796 If psProperty =
"Country
" Then _PropertyGet = .CountryDefaultName Else _PropertyGet = .LanguageDefaultName
798 Case
"Currency
"
799 vCurrencies = LocaleData.getAllCurrencies(oLocale)
800 _PropertyGet =
""
801 For Each oCurrency In vCurrencies
802 If oCurrency.Default Then
803 _PropertyGet = oCurrency.BankSymbol
807 Case
"DatePatterns
"
808 _PropertyGet = LocaleData.getDateAcceptancePatterns(oLocale)
809 Case
"DateSeparator
",
"DecimalPoint
",
"ListSeparator
",
"ThousandSeparator
",
"TimeSeparator
"
810 Set oLocaleDataItem2 = LocaleData.getLocaleItem2(oLocale)
811 With oLocaleDataItem2
812 Select Case psProperty
813 Case
"DateSeparator
" : _PropertyGet = .dateSeparator
814 Case
"DecimalPoint
" : _PropertyGet = .decimalSeparator
815 Case
"ListSeparator
" : _PropertyGet = .listSeparator
816 Case
"ThousandSeparator
" : _PropertyGet = .thousandSeparator
817 Case
"TimeSeparator
" : _PropertyGet = .timeSeparator
820 Case
"DayAbbrevNames
",
"DayNames
",
"DayNarrowNames
"
821 Set oCalendarImpl = SF_Utils._GetUNOService(
"CalendarImpl
")
823 .loadDefaultCalendar(oLocale)
824 vCalItems = Array() : ReDim vCalItems(
0 To
6)
825 For i =
0 To UBound(.Days2)
826 Set oCalItem = .Days2(i)
827 j = Iif(i =
0,
6, i -
1)
828 Select Case psProperty
829 Case
"DayNames
" : vCalItems(j) = oCalItem.FullName
830 Case
"DayAbbrevNames
" : vCalItems(j) = oCalItem.AbbrevName
831 Case
"DayNarrowNames
" : vCalItems(j) = oCalItem.NarrowName
834 _PropertyGet = vCalItems
836 Case
"MonthAbbrevNames
",
"MonthNames
",
"MonthNarrowNames
"
837 Set oCalendarImpl = SF_Utils._GetUNOService(
"CalendarImpl
")
839 .loadDefaultCalendar(oLocale)
840 vCalItems = Array() : ReDim vCalItems(
0 To
11)
841 For i =
0 To UBound(.Months2)
842 Set oCalItem = .Months2(i)
843 Select Case psProperty
844 Case
"MonthNames
" : vCalItems(i) = oCalItem.FullName
845 Case
"MonthAbbrevNames
" : vCalItems(i) = oCalItem.AbbrevName
846 Case
"MonthNarrowNames
" : vCalItems(i) = oCalItem.NarrowName
849 _PropertyGet = vCalItems
852 _PropertyGet =
""
856 SF_Utils._ExitFunction(cstThisSub)
858 End Function
' ScriptForge.SF_Region._PropertyGet
860 REM ================================================ END OF SCRIPTFORGE.SF_REGION