/
CDC-1.2b/
CDC-1.2b/src/
parent $time_root
object $time

var $root child_index 0
var $root owners [$time]
var $root fertile 0
var $root inited 1
var $time_root secs_per_min 60
var $time_root secs_per_hour 3600
var $time_root secs_per_day 86400
var $time_root secs_per_week 604800
var $time_root secs_per_year 31536000
var $time_root created_on 0
var $time_root mins_per_hour 60
var $time_root hours_per_day 24
var $time_root days_per_year 365
var $time_root year_begin 0
var $time_root days ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var $time_root months #[["Jan", ["January", 1, 31]], ["Feb", ["February", 2, 28]], ["Mar", ["March", 3, 31]], ["Apr", ["April", 4, 30]], ["May", ["May", 5, 31]], ["Jun", ["June", 6, 30]], ["Jul", ["July", 7, 31]], ["Aug", ["August", 8, 31]], ["Sep", ["September", 9, 30]], ["Oct", ["October", 10, 31]], ["Nov", ["November", 11, 30]], ["Dec", ["December", 12, 31]]]
var $time_root standard 1
var $root owned [$time]
var $root manager $time
var $root writable [$time]
var $root readable ['parameters, 'methods, 'code]
var $root dbref 'time

method elapsed
    arg time, [flag];
    var str, t, p;
    
    // compares args[1] with time() and returns hh:mm elapsed
    // will eventually make flags do things like 'long etc.  For now its
    // just your own time, rather than time().
    flag = [@flag, 'stopwatch][1];
    str = "";
    switch (flag) {
        case 'long:
            return .to_english(time);
        default:
            if (time > 356400)
                p = 3;
            else
                p = 2;
            str = str + pad(tostr(time / 3600), -p, "0");
            str = (str + ":") + pad(tostr((time % 3600) / 60), -2, "0");
            return str;
    }
.

method dhms
    arg secs, [long];
    var ret_str, x;
    
    if (long)
        long = 1;
    if (secs > 86400) {
        x = secs / 86400;
        ret_str = tostr(x) + (long ? " day" + ((x < 2) ? "" | "s") | "d");
    } else if (secs > 3600) {
        x = secs / 3600;
        ret_str = tostr(x) + (long ? " hr" + ((x < 2) ? "" | "s") | "h");
    } else if (secs > 60) {
        x = secs / 60;
        ret_str = tostr(x) + (long ? " min" + ((x < 2) ? "" | "s") | "m");
    } else {
        ret_str = tostr(secs) + (long ? " sec" + ((secs < 2) ? "" | "s") | "s");
    }
    return ret_str;
.

method month_str
    arg [args];
    var month, m, time, option;
    
    // args[1] = time (opt); args[2] == 'num, 'long, 'short;
    if (args && (type(args[1]) != 'integer)) {
        time = time();
        option = [@args, 'long][1];
    } else {
        time = [@args, time()][1];
        option = [@args, 'long, 'long][2];
    }
    if (type(time) == 'integer)
        month = substr(ctime(time), 5, 3);
    else
        month = substr(time, 5, 3);
    
    // special case:
    switch (option) {
        case 'short:
            return month;
        case 'num:
            return tostr(((.months())[month])[2]);
        default:
            return ((.months())[month])[1];
    }
.

method year_str
    arg [time];
    var how;
    
    // (time, how) time defaults to time(), how defaults to long
    how = [@time, 'long, 'long][2];
    time = [@time, .time()][1];
    if (how == 'long)
        return substr(ctime(time), 21);
    return substr(ctime(time), 23);
.

method ldate
    arg [args];
    var time, ctime, how, options, sep, hrs, mins, secs;
    
    // takes a bunch of numbers and returns info depending upon what the
    // symbol is.  Time is optional, so is how for that matter.
    // options are listed to the right of the descriptions.
    // How:     'long     -- Monday, January 10th, 1994 (default)
    //          'noyear   -- 'long without the year.
    //          'short    -- Mon, Jan 10, 94
    //          'date     -- 01-Dec-95 (ie: DD-MMM-YY)
    //          'dmy      -- 10/01/94 (ie: DD/MM/YY) ['slash | 'dash]
    //          'mdy      -- 01/10/94 (ie: MM/DD/YY) ['slash | 'dash]
    //          'ymd      -- 94/10/01 (ie: YY/MM/DD) ['slash | 'dash]
    args = [@args, 'long];
    if (type(args[1]) != 'integer) {
        time = time();
        how = args[1];
        args = delete(args, 1);
    } else {
        time = args[1];
        how = args[2];
        args = sublist(args, 3);
    }
    
    // figure options first
    options = [@args, 0][1];
    sep = "/";
    if (options) {
        switch (options) {
            case 'dash:
                sep = "-";
            case 'slash:
                sep = "/";
        }
    }
    
    // figure actual time
    switch (how) {
        case 'long:
            return ((((((.day_str(time)) + ", ") + (.month_str(time))) + " ") + (.month_day_str(time, 'num))) + ", ") + (.year_str(time));
        case 'noyear:
            return ((((.day_str(time)) + ", ") + (.month_str(time))) + " ") + (.month_day_str(time, 'num));
        case 'short:
            ctime = ctime(time);
            return (((pad(ctime, 3) + ",") + pad(substr(ctime, 4), 7)) + ", `") + (.year_str(time, 'short));
        case 'date:
            return ((((.month_day_str(time)) + "-") + (.month_str(time, 'short))) + "-") + (.year_str(time, 'short));
        case 'dmy:
            return ((((.month_day_str(time)) + sep) + (.month_str(time, 'num))) + sep) + (.year_str(time, 'short));
        case 'mdy:
            return ((((.month_str(time, 'num)) + sep) + (.month_day_str(time))) + sep) + (.year_str(time, 'short));
        case 'ymd:
            return ((((.year_str(time, 'short)) + sep) + (.month_str(time, 'num))) + sep) + (.month_day_str(time, 'num));
    }
.

method ltime
    arg [args];
    var time, ctime, how, options, sep1, sep2, ampm, hrs, mins, secs;
    
    // takes a bunch of numbers and returns info depending upon what the symbol is
    // time is optional, so is how for that matter.  Uses '12hr '_ampm for default
    // options are listed after the descriptions.
    //          '24hr     -- 24:00 ['a_m_p_m|'ampm|'_ampm|'ap|'no_ampm]
    //          '12hr     -- 12:00 ['a_m_p_m|'ampm|'_ampm|'ap|'no_ampm]
    //          '24hr_sec -- 24:00:00 ['a_m_p_m|'ampm|'_ampm|'ap|'no_ampm]
    //          '12hr_sec -- 12:00:00 ['a_m_p_m|'ampm|'_ampm|'ap|'no_ampm]
    //          'long     -- twelve thirty four pm['a_m_p_m|'ampm|'_ampm|'no_ampm]
    //          'hour     -- Twelve o'clock
    // BTW, incase your wondering, ltime stands for Lynx Time; i'm a bastard 8b
    args = [@args, '12hr];
    if (type(args[1]) != 'integer) {
        time = time();
        how = args[1];
        args = delete(args, 1);
    } else {
        time = args[1];
        how = args[2];
        args = sublist(args, 3);
    }
    
    // figure options first
    options = [@args, '_ampm][1];
    sep1 = "/";
    sep2 = ":";
    ampm = ["", ""];
    if (options) {
        switch (options) {
            case 'dash:
                sep1 = "-";
            case 'slash:
                sep1 = "/";
            case 'a_m_p_m:
                ampm = [" a.m.", " p.m."];
            case 'ampm:
                ampm = ["am", "pm"];
            case '_ampm:
                ampm = [" am", " pm"];
            case 'ap:
                ampm = ["a", "p"];
            case 'no_ampm:
                ampm = ["", ""];
        }
    }
    
    // figure actual time
    switch (how) {
        case '24hr:
            return substr(.ctime(time), 12, 5);
        case '24hr_sec:
            return substr(.ctime(time), 12, 8);
        case '12hr:
            time = .hr_min_sec(time);
            hrs = toint(time[1]);
            ampm = (hrs < 12) ? ampm[1] | (ampm[2]);
            hrs = hrs % 12;
            hrs = (hrs == 0) ? 12 | hrs;
            return ((tostr(abs(hrs)) + sep2) + (time[2])) + ampm;
        case '12hr_sec:
            time = .hr_min_sec(time);
            hrs = toint(time[1]);
            ampm = (hrs < 12) ? ampm[1] | (ampm[2]);
            hrs = hrs % 12;
            hrs = (hrs == 0) ? 12 | hrs;
            return ((((tostr(hrs) + sep2) + (time[2])) + sep2) + (time[3])) + ampm;
    }
.

method day_str
    arg [args];
    var day, days, d, time, option;
    
    if (args && (type(args[1]) != 'integer)) {
        time = time();
        option = [@args, 'long][1];
    } else {
        time = [@args, time()][1];
        option = [@args, 'long, 'long][2];
    }
    if (type(time) == 'integer)
        day = explode(ctime(time))[1];
    else
        day = explode(time)[1];
    days = .days();
    switch (option) {
        case 'num:
            return .month_day_str();
        case 'short:
            return day;
        default:
            for d in (days) {
                if (match_begin(d, day))
                    return d;
            }
    }
.

method month_day_str
    arg [time];
    
    time = .ctime([@time, .time()][1]);
    return strsub(substr(time, 9, 2), " ", "0");
.

method hr_min_sec
    arg [time];
    var ctime;
    
    ctime = .ctime(@time);
    return [substr(ctime, 12, 2), substr(ctime, 15, 2), substr(ctime, 18, 2)];
.

method time_stamp
    return ((.ldate('date)) + "/") + ($time.ltime('24hr_sec));
.

method to_english
    arg time, [reftime];
    var times, words, x, seconds, ctime, months, month, year, days, out;
    
    // most of this was stolen from MOO (und ve are evil)
    if (time < 1)
        return "0 seconds";
    reftime = reftime || time();
    ctime = (type(reftime) == 'integer) ? ctime(reftime) | reftime;
    words = ["year", "month", "day", "hour", "minute", "second"];
    times = [];
    seconds = [60, 60, 24];
    for x in (seconds) {
        times = [time % x, @times];
        time = time / x;
    }
    months = 0;
    month = ((.months())[substr(ctime, 5, 3)])[2];
    year = toint(.year_str(reftime));
    days = ((.months())[substr(ctime, 5, 3)])[3];
    while (time >= (days + ((month == 2) && (((year % 4) == 0) && (!((year % 400) in [100, 200, 300])))))) {
        time = time - days;
        months = months + 1;
        month = month + 1;
        if (month > 12) {
            year = year + 1;
            month = 1;
        }
    }
    times = [months / 12, months % 12, time, @times];
    out = [];
    for x in [1 .. listlen(words)] {
        if ((times[x]) > 0)
            out = [@out, ((tostr(times[x]) + " ") + (words[x])) + (((times[x]) == 1) ? "" | "s")];
    }
    return $list.to_english(out);
.

method date
    arg [args];
    var time, opt;
    
    time = [@args, time()][1];
    opt = [@args, 'long, 'long][2];
    if (type(time) != 'integer)
        throw(~type, "Time must be submitted as an integer.");
    switch (opt) {
        case 'short:
            return ctime(time);
        default:
            return ((.ltime(time)) + ", ") + (.ldate(time));
    }
.