Skip to content

Instantly share code, notes, and snippets.

@jesusbagpuss
Created August 6, 2018 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusbagpuss/c0815d36095f4e4786ae686143c81370 to your computer and use it in GitHub Desktop.
Save jesusbagpuss/c0815d36095f4e4786ae686143c81370 to your computer and use it in GitHub Desktop.
EPrints - render event dates in a human-friendly way (when they are stored in a database in 'YYYY-MM-DD - YYYY-MM-DD' format)
# Render event dates when used in citation
# This will not affect them when exported/in the database.
{
# Write into EPrints::Script::Compiled
package EPrints::Script::Compiled;
use strict;
sub run_custom_human_event_dates
{
my( $self, $state, $value ) = @_;
my $session = $state->{session};
my $event_dates = $value->[0]; #[1] = MetaField, [2] = EPrint
my $r;
if( $event_dates =~ m/^\s*\d{4}\-\d{2}\-\d{2}\s*$/ ) {
# YYYY-MM-DD : Just one date - render it in short style
$r = EPrints::Utils::tree_to_utf8( EPrints::Time::render_short_date( $session, $event_dates ) );
} elsif( $event_dates =~ m/^\s*(\d{4}\-\d{2}\-\d{2})\s*\-\s*(\d{4}\-\d{2}\-\d{2})\s*$/ ) {
# YYYY-MM-DD - YYYY-MM-DD - start and finish dates.
if( $1 eq $2 ){
# start date = finish date : render it in short style
$r = EPrints::Utils::tree_to_utf8( EPrints::Time::render_short_date( $session, $1 ) );
} else {
my @start_date = EPrints::Time::split_value( $1 );
my @finish_date = EPrints::Time::split_value( $2 );
if( $start_date[0] != $finish_date[0] ){
#different years: 30 Dec 2014 - 2 Jan 2015
$r = EPrints::Utils::tree_to_utf8( EPrints::Time::render_short_date( $session, $1 ) ) .
' - ' .
EPrints::Utils::tree_to_utf8( EPrints::Time::render_short_date( $session, $2 ) );
} elsif( $start_date[1] != $finish_date[1] ){
#different months: 31 Jan - 2 Feb 2015
$r = $start_date[2].' '.EPrints::Time::short_month_label( $session, $start_date[1] ).' - ' .
EPrints::Utils::tree_to_utf8( EPrints::Time::render_short_date( $session, $2 ) );
} else {
# same year, same month, different days.
$r = $start_date[2] . '-' .
EPrints::Utils::tree_to_utf8( EPrints::Time::render_short_date( $session, $2 ) );
}
}
} else {
#something that doesn't match YYYY-MM-DD - render it as-is.
$r = $event_dates;
}
return [ $r, "STRING" ];
}
} # END EPrints::Script::Compiled additions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment