Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 12-21-2012, 12:49 AM   #1
bookworm2000
Member
bookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the rough
 
Posts: 14
Karma: 7220
Join Date: Dec 2012
Device: Kindle 4NT, Android
Date formatting/template program mode

Hi everyone,

this is my first post, thank you for taking the time to look into my problem.

Which is...

I use a custom column to store the publishing dates of magazines. These come in various formats like
  • 1955.04.20 (most exact)
  • 1955.04
  • 1955
  • 1955.Spring
  • 1955.Spring/Summer (worst case)

So I use strings instead of dates or numbers.

These of course don't sort exactly but can easily be found as hierarchical items in the tag browser.

Now I want to extract a formatted date from this data:

1955.04.20
should be
1955-04-20

1955.04
should be
1955-04-00

1955
should be
1955-00-00

1955.Spring
should be
1955-00-00

1955.Spring/Summer
should be
1955-00-00

(the season data should be omitted completely).

What I have so far:

3 (hidden) columns to isolate year, month and day:
{#original_date:subitems(0,1)}
{#original_date:subitems(1,2)}
{#original_date:subitems(2,3)}

and a fourth to build the formatted date:
{#original_year:ifempty(0000)}-{#original_month:ifempty(00)}-{#original_day:ifempty(00)}

What I want to achieve:
  1. get them all into one column
  2. check the month for non-integer data and replace it with "00"

So far I haven't wrapped my brain around the template program mode, I don't find a way to combine subitems() and ifempty().

Thanks in advance!
bookworm2000 is offline   Reply With Quote
Old 12-27-2012, 07:23 AM   #2
Sabardeyn
Guru
Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.Sabardeyn ought to be getting tired of karma fortunes by now.
 
Sabardeyn's Avatar
 
Posts: 644
Karma: 1242364
Join Date: May 2009
Location: The Right Coast
Device: PC (Calibre), Nexus 7 2013 (Moon+ Pro), HTC HD2/Leo (Freda)
Ok, I'm not the best person to respond, particularly as I have not dabbled in templates and don't otherwise see the need for them. It could be my own ignorance though.

Anyway... That seems like an awful lot of work to produce 'useless' intermediary data. I say 'useless' because if the year, month and day columns are not otherwise needed, you're storing data unnecessarily.


I've just found out about subgroups and think they might be of use in this instance: http://manual.calibre-ebook.com/sub_groups.html

You might be able to create a single custom column with a data format of: YYYY.MM.DD.Q, making use of numbers with leading zeros for all segments except the last. The format is pretty much standard date field notation, adding a "Quarter #" into the mix. The only thing that would be odd is the quarterly / seasonal releases will be slightly out of true placement among other data - because day "00" is going to appear at the beginning of any listings for the month.

You could also do a YYYY.DDD format where days are # of days in the year (ex: Dec 25, 2012 = 2012.359). While accurate, this latter format is not as readable / user friendly.

Using subgroups might have the added benefit that you could output / display all of the publications in your database released in Oct 1953, for instance. Searching on #custom_column_name:"1953.10".


I'm not sure if these ideas are what you need, but I hope I've helped. (Or at least prompted someone else to jump in and offer a more viable suggestion!)
Sabardeyn is offline   Reply With Quote
Advert
Old 12-27-2012, 08:06 AM   #3
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: 11,734
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I meant to answer this sometime back, but got busy.

The following general program mode template does what I think you want.
Code:
program:
	date = field('#original_date');
# compute the components
	year = subitems(date, 0, 1);
	month = re(subitems(date, 1, 2), '^\D*$',  '00');
	day = ifempty(subitems(date, 2, 3), '00');
# compute the return value
	strcat(year, '--', month, '--', day)
The trickiest part is the call to re. The pattern matches if the source contains only non-numeric characters, replacing them with '00' otherwise leaving them alone.
chaley is offline   Reply With Quote
Old 12-28-2012, 01:47 PM   #4
bookworm2000
Member
bookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the roughbookworm2000 is a jewel in the rough
 
Posts: 14
Karma: 7220
Join Date: Dec 2012
Device: Kindle 4NT, Android
Thank you both for your answers.


Quote:
Originally Posted by Sabardeyn View Post
I've just found out about subgroups and think they might be of use in this instance: http://manual.calibre-ebook.com/sub_groups.html

The column containing the original data is already organized into subgroups.


Quote:
Originally Posted by chaley View Post
The following general program mode template does what I think you want.
Code:
program:
	date = field('#original_date');
# compute the components
	year = subitems(date, 0, 1);
	month = re(subitems(date, 1, 2), '^\D*$',  '00');
	day = ifempty(subitems(date, 2, 3), '00');
# compute the return value
	strcat(year, '--', month, '--', day)


That's exactly what I was looking for.



One last question:

I expanded the template to ISO 8601 format. Is there a way to convert this date string to datetime data type?

That would allow me to

a) sort the column by date instead of string,
b) use date formatting in further columns (e.g. used for display in the content server).

Or is that impossible because 1955-00-00T00:00:00Z+00:00 is not a valid date (with a 00 month and day)?
bookworm2000 is offline   Reply With Quote
Old 12-28-2012, 04:41 PM   #5
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: 11,734
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
A date column must contain a valid date. That is why calibre adds month and day to dates that display only a year.

Do note that ISO dates sort correctly even as strings.
chaley is offline   Reply With Quote
Advert
Reply

Tags
date format, template


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Paper mode/Two column mode in new PDF reader Fourier Onyx Boox 3 10-15-2012 10:48 AM
Syntax for nesting functions in program mode jcallaway Library Management 2 08-20-2012 02:26 AM
Request Safe mode or recovery mode for Pocket Edge? felixblackcat enTourage eDGe 25 01-08-2012 05:07 PM
Does anyone use "general program mode" template functions? chaley Library Management 0 08-16-2011 11:54 AM
Date formatting cvogan General Discussions 5 05-23-2010 05:36 PM


All times are GMT -4. The time now is 05:37 PM.


MobileRead.com is a privately owned, operated and funded community.