Jump to content

Parse Unix Time to Date/Time


Wulfie Reanimator
 Share

You are about to reply to a thread that has been inactive for 1701 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Random curiosity caught me, so I made this:

list unix2date(integer unix)
{
    integer sec = unix % 60;
    integer min = (unix/60) % 60;
    integer hour = (unix/60/60) % 24;

    // Epoch started on Thursday. 0 is Monday.
    integer weekday = (unix/60/60/24 + 3) % 7;

    integer year = 1970;                       // Epoch started on 1970.
    year += (integer)((float)(unix/60/60/24) / 365.25); // +49 for 2019.

    integer month;
    // http://howardhinnant.github.io/date_algorithms.html#civil_from_days
    // doe/yoe = day/year of era (era = 400 years), doy = day of year
    integer z = unix / 86400 + 719468;
    integer era;

    if (z >= 0) era = z / 146097;
    else        era = z - 146096 / 146097;

    integer doe = (z - era * 146097);
    integer yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365;
    integer doy =  doe - (365*yoe + yoe/4 - yoe/100);
    integer m   = (5*doy + 2) / 153;

    if (m < 10) m += 3;
    else        m += -9;
    month = m;

    // llOwnerSay(llList2CSV([unix, year, month, weekday, hour, min, sec]));
    // Example: 2019, 8, 4, 0, 46, 13
    return [year, month, weekday, hour, min, sec];
}

Calculation of the month was only translated by me from the Github page. I still couldn't figure out how to get the day of the month, instead of day of the week.

This is how you might use the function:

list days = [
    "Mon",
    "Tue",
    "Wed",
    "Thu",
    "Fri",
    "Sat",
    "Sun"
];

list months = [
    "Error",
    "Jan",
    "Feb",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
];

list unix2date(integer unix)
{
    // ...
}

default
{
    state_entry()
    {
        list out = unix2date(llGetUnixTime());

        llOwnerSay(
            "Year " + llList2String(out, 0) +
            ", " + llList2String(months, llList2Integer(out, 1)) +
            ", " + llList2String(days, llList2Integer(out, 2)) +
            ", at " + llList2String(out, 3) +
            ":" + llList2String(out, 4) +
            ":" + llList2String(out, 5)
        );
        // Example: Year 2019, Jul, Fri, at 0:56:14
    }
}

 

Edited by Wulfie Reanimator
  • Like 1
Link to comment
Share on other sites

Very nice work.  Compare this with Void Singer's solution at  http://wiki.secondlife.com/wiki/Unix2StampLst , which also calculates the day of the month, using a different algorithm.    It's lovely when independent methods lead to the same result.  There's magic in both of your methods.  I'd try to figure out how they work, but it would make my head hurt. 

Link to comment
Share on other sites

My first exposure to calendar algorithms was from programming an old HP-67 calculator. There was a little book of calendar programs that I faithfully keyed in, after which I went looking for dates in history to figure out if they fell on the weekend, when people should have been playing/praying.

Here's a link to one of those programs...
https://www.hpmuseum.org/software/41td2/daydte.htm

I think that program is based on Zeller's Congruence...
https://en.wikipedia.org/wiki/Zeller's_congruence

The HP calculator didn't have a "mod" operator, so rounding and more careful constant selection were used instead.

That HP program was one of many little epiphanies I've had along the way to understanding that math really is nature's language.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1701 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...