technonaturalist

image link to hive image link to ko-fi

theAbysmal Calendar Drupal module

posted on: Sunday, 25 March 2012 @ 9:52pm in
tagged

I think it’s done now.

  • fixed an issue where the years weren’t being calculated properly (?!)
  • I may be slightly out of sync with the official documentation, at least according to the tests I’ve done on my local so far. This year should match up, and so will key dates (New Year Day and Leap Day). I’ve shifted leap years to fall on multiples of 4 so calculating would be a simple case of if mod 4 is 0, it’s a leap year, if mod 128 is 0, it’s not a leap year, so some of my years will be a day or two out

theabysmal_calendar.info

name = theAbysmal Calendar 
description = Converts Gregorian date to theAbysmal date 
core = 7.x 
package = Date/Time

theabysmal_calendar.module

<?php
/**
* @file
* Converts date from Gregorian to theAbysmal Calendar
* 
* theAbysmal Calendar in depth: http://theabysmal.wordpress.com/theabysmal-calendar/
* Currently uses months from the Tranquility Calendar 
* (http://www.mithrandir.com/tranquility/tranquility.html) and days in
* highly informal/bastardised lojban as that's how I use it.
* 
* @author ryivhnn
*/

/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
*   Which path of the site we're using to display help
* @param arg
*   Array that holds the current path as returned from arg() function
*/

function theabysmal_calendar_help($path, $arg) {
switch ($path) {
    case "admin/help#theabysmal_calendar" :
    return '<p>' . t("Converts date to theAbysmal Calendar") . '</p><p>&nbsp;</p><p>';
    break; 
    } 
} 

function node_created_date() { 
    // grab node create date 
    $query = db_select('node', 'n') -> fields('n', array('nid', 'created')) -> condition('nid', arg(1)) -> condition('status', 1) -> execute() -> fetchAssoc(); 
    $date = $query['created']; 
    return $date; 
} 

function today() { 
    // grab today's date 
    $date = time(); 
    return $date; 
} 

function date_converter($date_ce) { 
/** 
    * doing the calendar converty magic 
    * owe the algorithm to the software engineer husband 
    */
    
// set the zero date and time for theAbysmal Calendar 
$zero_date = mktime(0, 0, 0, 12, 21, 2012); 

// this is New Year Day on theAbysmal Calendar 
$zero_day = date('z', mktime(0, 0, 0, 12, 22, 2012)); 

// this is first day which is counted from 0 
// calculate theAbysmal year 
if ((date('m', $date_ce) == 12) && (date('d', $date_ce) > 20)) { 
    $year = date('Y', $date_ce) - date('Y', $zero_date); 
} 
else { 
    $year = (date('Y', $date_ce) - date('Y', $zero_date)) - 1; 
} 

/** 
    * I think theAbysmal Calendar may be following Gregorian leap years to a degree. 
    * I couldn't find anything explicit in the documentation on the website. 
    * Because it counts from 0 every 4th number ends up being slightly weird (3, 7, 11, 15). 
    * I've decided to modify slightly to make leap years occur on multiples of 4 like they do 
    * on the Gregorian which then makes the not observing the leap year every 128 years 
    * slightly easy to remember, and also takes an operator out of the maths. 
    * Yes I can write an entire rationale in the comments but cbf putting in one extra operator. 
    */ 

if ($year % 4 == 0) { 
    $ly = 1; 
    
    if ($year % 128 == 0) 
    { $ly = 0; 
    } 
} 
else { 
    $ly = 0; 
} 

// because the leap years are different, we need to do maths with Gregorian 
$gly = date('L', $date_ce); 

// "buffers" for the leap year calculations 
if ($ly == 0 && $gly == 0) { 
    /** 
        * the way the $year_length calculation works, years where it's not a Gregorian 
        * or theAbysmal leap year come up a bit short, so this pads it out 
        */ 
        
    $leap_buffer = 1; 
}
elseif ($ly == 1 &amp;&amp; $gly == 1) { 
    /** 
        * conversely if the leap years coincide it makes the $year_length a bit longer 
        * than it should be, so this adjusts 
        */ 
        
    $leap_buffer = -1; 
} 
else { 
    $leap_buffer = 0; 
} 

/** 
    * working out how long the year is 
    * need to +1 to make it an absolute value 
    * add theAbysmal leap year and subtract the Gregorian leap year as required 
    */ 
    
$year_length = date('z', mktime(0, 0, 0, 12, 31, $year_ce)) + $ly - $gly + $leap_buffer + 1; 

// what day number are we up to in the Gregorian, + theAbysmal leap year + the leap buffer 
$gregorian_day_of_year = date('z', $date_ce) + $ly + $leap_buffer; 

// how many days have passed since theAbysmal start date, plus tA leap year day, less Gregorian leap year day 
$days_from_start = ($gregorian_day_of_year - $zero_day + $year_length) % $year_length; 

// theAbysmal month converted to an integer 
$month_number = intval($days_from_start / 28); 

// what day in the month it is 
$mday = $days_from_start % 28; 

// lojban day names assigned to month names 
$days = array( 
    'xundei' => array('0', '7', '14', '21'), 
    'najdei' => array('1', '8', '15', '22'), 
    'peldei' => array('2', '9', '16', '23'), 
    'ri\'ordei' => array('3', '10', '17', '24'), 
    'cicnydei' => array('4', '11', '18', '25'), 
    'bladei' => array('5', '12', '19', '26'), 
    'zirdei' => array('6', '13', '20', '27'), 
); 

foreach($days as $key =&gt; $value) {
    if (in_array($mday, $value)) { 
        $weekday = $key; 
    } 
} 

// Tranquility calendar month names 
// the 14th "month" is a 2 day intercalary period for New Year Day and Leap Year Day 
$months = array( 
    '0' => 'Archimedes',
    '1' => 'Brahe',
    '2' => 'Copernicus', 
    '3' => 'Darwin',
    '4' => 'Einstein', 
    '5' => 'Faraday',
    '6' => 'Galileo', 
    '7' => 'Hippocrates',
    '8' => 'Imhotep', 
    '9' => 'Jung', 
    '10' => 'Kepler', 
    '11' => 'Lavoisier', 
    '12' => 'Mendel', 
    '13' => '', 
); 

foreach ($months as $key =&gt; $value) { 
    if ($key == $month_number) { 
        $month = $value; 
    } 
} 

/* 
    * intercalary days 
    * 
    * check to see if it's December Solstice (Dec 21) 
    * if it is, it's New Year Day 
    * if not, it's a weekday 
    * 
    * if it's a leap year, Dec 20 is Leap Year Day 
    */
    
if ($month == '') { 
    $weekday = ''; 
    
    if (date('d', $date_ce) == 21 && date('m', $date_ce) == 12) { 
        $mday = "New Year Day"; 
    } 
    
    if ($ly == 1 && date('d', $date_ce) == 20 && date('m', $date_ce) == 12) { 
        $mday = "Leap Day"; 
    } 
} 

// leading zero silliness 
$yrabs = abs($year); 
$yrlen = strlen($yrabs); 

if ($date_ce < $zero_date) { 
    $era = 'btAT'; 
} 
elseif ($year == '0000') { 
    $era = 'tAT'; 
} 
else { 
    $era = 'tAT'; 
} 

if ($yrlen == 1) { 
    $year = '000' . $yrabs; 
} 
elseif ($yrlen == 2) { 
    $year = '00' . $yrabs; 
} 
elseif ($yrlen == 3) { 
    $year = '0' . $yrabs; 
} 

$daylen = strlen($mday); 

    if ($daylen == 1) { 
        $mday = '0' . $mday; 
    } 

    return $year . $era . ' ' . $month . ' ' . $mday . ' ' . $weekday; 
}

function date_shorten($date) { 
    /** 
     * can't help but feel this may be unnecessarily long winded :) 
     * 
     * Months and days are put into an array 
     * the first letter in each is grabbed 
     * the $date string is searched and the full name replaced with the letter 
     * then spaces are stripped 
     * 
     * I also continued with negative years in the array even though Not Jack 
     * isn't a huge fan og negative years as the $era was making my short dates 
     * longer :) 
     */
     
    if ($era = 'btAT') { 
        $neg = '-'; 
    } 
    
    $month = array( 
        'Archimedes', 
        'Brahe', 
        'Copernicus', 
        'Darwin', 
        'Einstein', 
        'Faraday', 
        'Galileo', 
        'Hippocrates', 
        'Imhotep', 
        'Jung', 
        'Kepler', 
        'Lavoisier', 
        'Mendel', 
    ); 
    
    foreach ($month as $value) { 
        $m = $value[0]; 
        $date = str_replace($value, $m, $date); 
    } 
    
    $wday = array( 
        'xundei', 
        'najdei', 
        'peldei', 
        'ri\'ordei', 
        'cicnydei', 
        'bladei', 
        'zirdei', 
    ); 
    
    foreach ($wday as $value) { 
        $wd = $value[0]; 
        $date = str_replace($value, $wd, $date); 
    } 
    
    $date = str_replace(' ', '', $date); 
    $date = str_replace('btAT', '', $date); 
    
    return $neg . $date; 
}

/** 
 * implements hook_block_info() 
 */
 
 function theabysmal_calendar_block_info() { 
    $blocks['today'] = array( 
        'info' => t('Today\'s theAbysmal date'), 
        'cache' => DRUPAL_NO_CACHE, 
    ); 
    
    $blocks['posted'] = array( 
        'info' => t('Converted post date'), 
    ); 
    
    return $blocks;
}

/** 
 * implements hook_block_view() 
 */ 
 
 function theabysmal_calendar_block_view($delta = '') { 
    $block = array(); 
    
    switch ($delta) { 
        case 'today': $block['subject'] = t('Today\'s theAbysmal date'); 
            if (user_access('access content')) { 
                $date_ce = today(); 
                $block['content'] = date_converter($date_ce); 
            } 
        break; 
        case 'posted': $block['subject'] = t('Converted post date'); 
            if (user_access('access content')) { 
                $date_ce = node_created_date(); 
                $date = date_converter($date_ce); 
                $block['content'] = date_shorten($date); 
            } 
        break; 
    } 
    
    return $block; 
}
?>

Usage instructions

  • create a folder called theabysmal_calendar in sites/all/modules
  • copy theabysmal_calendar.info and theabysmal_calendar.module to files of the same name and save them into the folder you created. Dicth the closing php tags in the module file
  • if you don’t like the month and week days I’m using, change them to whatever you like in the days and month arrays (two of each, just be aware that the date shortening as it is may look odd with week day and month names that start with the same letter, the date formats can be changed easily enough)
  • go to the modules page (I think it’s Admin >> Modules, I use the admin toolbar and it’s just there), scroll down to Date/Time and check theAbysmal Calendar on
  • go to Admin >> Structure >> Blocks and activate and position the blocks you want. There are two provided, one converts today’s date and the other converts and shortens a post date. I’m using <?php echo render[$block['content']; ?> to spit the shortened date into the top of the blog posts

If anyone knows how to write Wordpress plugins and feels like converting it, go nuts.  Then tell Not Jack so theAbysmal Calendar blog can have an Abysmal Calendar :)