das hier ist so ein Proof of Concept für die Darstellung eines Datums und der Zeit mittels locales. Es funktioniert bisher gut, aber es gibt auch einen Crash, der als Beispiel ganz unten auskommentiert ist und den ich nicht verstehe. Vielleicht hat da jemand eine Idee.
Ich möchte das ganze auch noch auf Zahlen und Geldbeträge ausweiten. Für Linux kein Problem, aber ich könnte Hilfe für die anderen Betriebssysteme gebrauchen. Und dann mache ich ein Module daraus. Wie gefällt euch die Parameterübergabe? Soll ich vielleicht das setlocale() sogar auslagern, sodass man es einmal setzt und dann kann man beliebig viele Date-Funktionen aufrufen?
Wichtig ist, dass es nur mit locales funktioniert, die auch installiert sind. Ihr findet das unter Linux heraus, indem ihr im Terminal "locale -a" eingibt. Neu hinzufügen für z.B. Russisch geht so:
Code: Alles auswählen
sudo locale-gen ru_RU.UTF-8
sudo update-locale
Hier aber nun der Code.
Code: Alles auswählen
Structure tm
tm_sec.l ; seconds
tm_min.l ; minutes
tm_hour.l ; hours
tm_mday.l ; day of the month
tm_mon.l ; month
tm_year.l ; number of years since 1900
tm_wday.l ; day of the week
tm_yday.l ; day in the year
tm_isdst.l ; daylight saving time
EndStructure
Enumeration 1
#LC_ALL
#LC_COLLATE
#LC_CTYPE
#LC_MONETARY
#LC_NUMERIC
#LC_TIME
EndEnumeration
Import ""
time(*rawtime)
localtime.i(*rawtime)
strftime(*dest, maxsize.l, format.p-utf8, *timeprt.tm)
setlocale(t.i, locale.p-ascii)
EndImport
Procedure.s localDateTime(date.q, mask.s = "%x %X", locale.s = "")
Protected bufferSize.i = 12
Protected *result = AllocateMemory(bufferSize), *tResult
Protected tm.tm, *oldLocale, oldLocale.s, result.s
tm\tm_sec = Second(date)
tm\tm_min = Minute(date)
tm\tm_hour = Hour(date)
tm\tm_mday = Day(date)
tm\tm_mon = Month(date) - 1
tm\tm_year = Year(date) - 1900
tm\tm_wday = DayOfWeek(date)
tm\tm_yday = DayOfYear(date) - 1
tm\tm_isdst = 0
*oldLocale = setlocale(#LC_TIME, locale)
If Not *oldLocale
DebuggerWarning("Locale could not be set: " + locale)
Else
oldLocale = PeekS(*oldLocale, -1, #PB_Ascii)
EndIf
While Not strftime(*result, bufferSize, mask, @tm)
bufferSize * 2
*tResult = ReAllocateMemory(*result, bufferSize, #PB_Memory_NoClear)
If Not *result
DebuggerWarning("Could not allocate enough memory for datetime conversion.")
Break
EndIf
*result = *tResult
Wend
result = PeekS(*result, -1, #PB_UTF8)
FreeMemory(*result)
If *oldLocale
setlocale(#LC_TIME, oldLocale)
EndIf
ProcedureReturn result
EndProcedure
Debug localDateTime(Date(2018, 3, 3, 0, 0, 0), "Heute ist %A. Wir haben %X Uhr und es ist der %x. Der Monat ist %B.", "de_DE.UTF8")
; Das hier crasht :(
;Debug localDateTime(Date(), "%c", "de_DE.UTF8")