View Single Post
Old 02-18-2023, 01:19 PM   #513
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
You don't say what a "bad date" looks like when showing in the booklist or book details. Because you are using a printable date (the value of '#column name') the month names will be localized. The date functions in calibre are localized to accept 3-letter month names in English, French, and German. See replace_months() in calibre's source calibre.utils.date.

I expect in your case the month abbreviations are in Spanish. In that language many 3-letter month abbreviations are the same as in English, but several differ from their English equivalents such as "abr" instead of "Apr"

You avoid this problem by using raw_field to get the date in ISO-format instead of localized. Something like this, replacing '#mydate' with the real lookup name:
Code:
{:'ifempty(format_date(raw_field('#mydate', ''), 'MMM'), 'Without date')'}
or this, which is more like your original.
Code:
{:'d = raw_field('#mydate', ''); test(d, strcat(format_date(d,'MMM')),'Without date')'}
Note: if this is the only template in the composite column then I suggest you use General Program Mode, as that will be faster because of template caching. Something like this:
Code:
program: ifempty(format_date(raw_field('#mydate', ''), 'MMM'), 'Without date')
Or even faster: a Python template:
Code:
python:
def evaluate(book, context):
	d = book.get('#mydate')
	if d is None:
		return 'Without date'
	# Localize these as you wish
	months = ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 
		  'jul', 'ago', 'sep', 'oct', 'nov', 'dic']
	return months[d.month-1]

Last edited by chaley; 02-18-2023 at 01:59 PM. Reason: Grammar
chaley is offline   Reply With Quote