The Classical Chinese Temporal Converter

Imagine that we know we're not alone in the universe. Imagine that one day, other sentient beings come in great ships across the endless sea of space. They smell rather bad, and look quite hairy, like if Chewbacca was a sanitation engineer and didn't shower for -- oh, ever.

They come bearing goods that they wish to trade with us. It turns out that they've been watching our TV broadcasts so they've heard of Pop Tarts, and Hot Pockets and Fruity Cereals. And the aliens have no psychological immunity to our suggestive Saturday morning commercials.

Now there is a terrible clamoring of demand from their homeworlds for our goods. They're cuckoo for Cocoa Puffs, and they're willing to trade their stickiest spiderwool, their clotted crème milked from the shaggifish, and their space-pieces to get it.

Perhaps you don't want to wear sticky wool from spider abdomens, or think the shaggifish crème smells disturbingly like overripe papaya mixed with wet dog and morning breath, or wonder how knowing exactly how far the sun is from you could be in any way useful? Well, it doesn't matter if you don't want the goods. They've gotta have their Pops!

Perhaps you ask for gold instead, but they value their gold and they don't want to sell it to you. Instead they enslave a "nearby" planet and convert its surface into meth labs. They then trade the slave-made meth for those sweet marshmallowy hearts stars and clovers.

You might say to these aliens that's unfair, but they certainly won't be hearing that from you! I mean, you're a degenerate meth addict, after all!

It also turns out that everything Earth's philosophers and poets have ever said is just pure nonsense deluding us from the true nature of things. Luckily, the alien philosophers have never gotten anything wrong! The Earth must count its blessings to have these benevolent wise men allow us a glimpse of their wisdom.

That metric system based on water? Cute. But they have a better system based on shaggifish crème and how long it takes to cure in the sauna. They also have a much better way to keep time, by measuring the exact distance you are from the sun and dividing the change in distance into 13ths, then 42nds, then 9ths.

After long enough exposure, and looking at how the earth's governments, economy, and environment have collapsed in response to the stress of the Count Chocula-for-Meth trade, you reason the alien's methods really are superior. What you need to do is modernize, meaning imitate the aliens. Because let's face it, being true to ourselves is sinful. All the alien philosophers say so...

Ok, thought experiment over.

This actually happened to China, only the aliens came from over the sea, not from outer space. And they were interested in tea, not Capn' Crunch. And they didn't want to actually pay for their purchases, so they conquered India and set it to producing opium to trade for tea. Then they criticized the Chinese for being atheistic opium addicts who backwardly worship their own ancestors instead of the alien's deity.

Orwell's 1984 proposes a grim future where language has been changed in such a way that it no longer can be used to express dissent. In that world, the word "Free" no longer has the usage "Free" as in "Freedom of Speech". Instead its only use is "free" as in "This room is free of pests."

China has been through that Orwellian filter, but with measurements rather than words.

The Chinese no longer reckon time the way their ancestors did. Their Ancestors didn't measure years in AD/BC, nor did they have a seven-day week, nor did the calendar rely exclusively on the sun.

They had a cyclic time system that repeated every 60 increments. It's called the Sexagenary Stem-Branch Cycle. 10 units called heavenly stems would cycle next to 12 units called earthly branches. As the two went out of phase with each other, they would generate 60 pairs to make up the complete cycle. Think of one cycle as a "Chinese Century", or the life-span of a wise and virtuous person.

I've constructed a program from JavaScript to get the Classical Chinese Sexagenary Year from any Western Year.

/* 
Takes a Gregorian Year and Era as arguments;  
Returns the name of the corresponding Sexagenary Stem/Branch Cycle Year.

The pure code minus html and side effects ==>  
*/

// A switchboard conversion chart; Takes a %60 code number and returns a Sexagenary Stem/Branch name.
function numberToStemBranch(number) {  
    stemNumber = number % 10;
    branchNumber = number % 12;
    stem = "";
    branch = "";
    switch (stemNumber) {
        case 0:
            stem = "Gui";
            break;  
        case 1:
            stem = "Jia";
            break;
        case 2:
            stem = "Yi";
            break;
        case 3:
            stem = "Bing";
            break;
        case 4:
            stem = "Ding";
            break;
        case 5:
            stem = "Wu";
            break;
        case 6:
            stem = "Ji";
            break;
        case 7:
            stem = "Geng";
            break;
        case 8:
            stem = "Xin";
            break;
        case 9:
            stem = "Ren";
            break;
        case 10:
            stem = "Gui";
            break;
        case -1:
            stem = "Ren";
            break;
        case -2:
            stem = "Xin";
            break;
        default:
            stem = "numberToStemBranch Error.";
            break;
    }
    switch (branchNumber) {
        case 0:
            branch = "Hai";
            break;  
        case 1:
            branch = "Zi";
            break;
        case 2:
            branch = "Chou";
            break;
        case 3:
            branch = "Yin";
            break;
        case 4:
            branch = "Mao";
            break;
        case 5:
            branch = "Chen";
            break;
        case 6:
            branch = "Si";
            break;
        case 7:
            branch = "Wu";
            break;
        case 8:
            branch = "Wei";
            break;
        case 9:
            branch = "Shen";
            break;
        case 10:
            branch = "You";
            break;
        case 11:
            branch = "Xu";
            break;
        case 12:
            branch = "Hai";
            break;
        case -1:
            branch = "Xu";
            break;
        case -2:
            branch = "You";
            break;
        default:
            stem = "numberToStemBranch Error.";
    }
    return stem + branch;
}



// Controls the calibration of Gregorian Years to a %60 code number for numberToStemBranch to convert.
// Rounds and ignores finer date/time information coming in the form of floats to get "just the year at that time".
// Accepts a single parameter (year) and assumes that to mean "AD" or "CE".
// era input is non-case-sensitive.
function yearConverter(year, era) {  
    var number = 0;
    if (isNaN(year)) {
        return "Please enter an Indian Numeral for the year without quotation marks.";
    } else if ( era === undefined || (era.toUpperCase() === "AD" || era.toUpperCase() === "CE") && year >= 1) {
        number = (year - 3) % 60;
        return numberToStemBranch(Math.floor(number));
     } else if ((era === "BC" || era === "BCE") && year >= 1) {
        number = 60 - ((year + 2) % 60);
        return numberToStemBranch(Math.ceil(number));
    } else if ((era !== "AD" && era !== "BC") && (era !== "CE" && era !== "BCE")) {
        return "Please enter either \"AD\", \"BC\", \"CE\", or \"BCE\" for the era.";
    } else {
        return "Please enter a positive non-zero number for the year.";
      }
}

As soon as I learned what modulo was, I immediately saw a use for it. For those who don't know, modulo is the remainder of a division after whole numbers have been taken away. If 9/3 represents the turn of a three-spoked wheel 3 times, modulo lets you know which of the three spokes are facing up when the wheel completes its revolutions.

9 % 3 == 0 //spoke "0" on the wheel is up.  
10 % 3 == 1 //now, spoke "1"  
11 % 3 == 2 //spoke "2"  
12 % 3 == 0 //spoke "0" again, one revolution.  
13 % 3 == 1 //...and so on.  

This works perfectly for converting the linear time of western reckoning into the cyclical time of Classical Chinese Culture. Western time measures the road. Chinese time measures the orientation of the wheel. It turns years into seasons, and reflects the subjective nature of the world we live in rather than reflecting a objective march forward, disconnected from experience.

So there it is. I intend to port this code into Python, Ruby and Java as an exercise in learning those languages and comparing their operations.

Future versions of this code will include more precise timekeeping information. It should surprise no one that the Traditional Chinese year and the Western Year do not sync perfectly. Nor does the start of a new day. Even Hours, Quarter Hours, Minutes and Seconds are not the same things. But I have other plans for these...

In some small way I intend to know what it's like to think of time the way Classical Chinese Culture did. How we carve up our world has a great deal to do with how we ultimately interpret it.