- Offizieller Beitrag
Im Modul times sind alle notwendigen Funktionen für Datumsoperationen enthalten. Ich habe mir nun eine Prozedur erstellt, mit der ich variabel die Funktion für die Datumsoperation übergeben kann ('years', 'months', 'weeks', 'days') und Integer als Werte für Monat und Tag übergeben kann. Das ist deshalb sinnvoll, damit ich mit der Vorbelegung '-1' den aktuellen Datumswert übernehmen kann. -1 ist aber kein gültiger Wert für die Datentypen 'Month' und 'MonthdayRange'.
Das funktioniert soweit zufriedenstellend, nur werden die Funktionen 'weeks' und 'days' als Parameter nicht erkannt. Da habe ich keine Erklärung für.
Code
import times, tables
# ------------------------------------------------------------------------------
type
ConditionPair[T] = object
ifTrue, ifFalse: T
# ternary operator: (cond ? if-true ! if-false)
proc `!`*[T](a, b: T): ConditionPair[T] {.inline.} =
ConditionPair[T](ifTrue: a, ifFalse: b)
template `?`*[T](cond: bool; p: ConditionPair[T]): T =
(if cond: p.ifTrue else: p.ifFalse)
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# dateFunc: The procedure to use ('years', 'months', 'weeks', 'days')
# count: The count of units to add. Negative value for subtraction.
# year: The year for the start date. -1 (default) - the current year
# mon: The month for the start date. -1 (default) - the current month
# day: The day for the start date. -1 (default) - the current day
# ------------------------------------------------------------------------------
proc dateAdd(datefunc: proc, count: int, year: int = -1,
mon: int = -1, day: int = -1): DateTime =
let dtToday = now() # date Today
let # if default (-1) - Today-value
y = year == -1 ? dtToday.year ! year
m: Month = mon == -1 ? dtToday.month ! cast[Month](mon)
d: MonthdayRange = day == -1 ? dtToday.monthday ! cast[MonthdayRange](day)
let dtStart = initDateTime(d, m, y, 0, 0, 0, local())
result = dtStart + (count.datefunc)
# ------------------------------------------------------------------------------
let dateFormat = initTimeFormat("dd'.'MM'.'yyyy")
echo "Heute: ", now().format(dateFormat)
var d = dateAdd(years, -2)
echo "Heute -2 Jahre: ", d.format(dateFormat)
d = dateAdd(years, 2)
echo "Heute +2 Jahre: ", d.format(dateFormat)
d = dateAdd(months, -3, 2016, 2, 28)
echo "28.02.2016 -3 Monate: ", d.format(dateFormat)
d = dateAdd(months, -5)
echo "Heute -5 Monate: ", d.format(dateFormat)
d = dateAdd(months, 5)
echo "Heute +5 Monate: ", d.format(dateFormat)
# 'weeks' und 'days' werden nicht als Funktion erkannt:
# d = dateAdd(weeks, -3)
# echo "Heute -3 Wochen: ", d.format(dateFormat)
# d = dateAdd(weeks, 3)
# echo "Heute +3 Wochen: ", d.format(dateFormat)
# d = dateAdd(days, -21)
# echo "Heute -21 Tage: ", d.format(dateFormat)
# d = dateAdd(days, 14)
# echo "Heute +14 Tage: ", d.format(dateFormat)
# im direkten Aufruf keine Probleme:
echo "Heute - 3 Wochen: ", (now() - 3.weeks).format(dateFormat)
echo "Heute +10 Tage: ", (now() + 10.days).format(dateFormat)
Alles anzeigen