time utilities #43

Parent #79Owner #36Flags readSource RPG Core/rpgcore-patched.db

Aliases: time utilities, time

16 verbs · 18 properties · 0 children

Verbs

VerbSpecFlagsDefinerLines
daynone none nonerxd#4310
monthnone none nonerxd#4311
ampmnone none nonerxd#4322
to_secondsthis none thisrxd#434
sunthis none thisrxd#4310
from_ctimethis none thisrxd#4313
dhms dayshoursminutessecondsthis none thisrxd#4321
english_timethis none thisrxd#4340
from_daythis none thisrxd#4318
from_monththis none thisrxd#4321
dst_midnightthis none thisrxd#433
time_subthis none thisrxd#4386
mmddyy ddmmyythis none thisrxd#4320
parse_english_time_intervalthis none thisrxd#4349
seconds_until_datethis none thisrx#4320
seconds_until_timethis none thisrx#437

Properties

PropertyDefinerFlagsOwnerValue
monthlens#43rc#36
list of 12{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
timezones#43rc#36
list of 15{{"AuEST", -10}, {"AuCST", -9}, {"AuWST", -8}, {"WET", -1}, {"GMT", 0}, {"AST", 4}, {"EDT", 4}, {"EST", 5}, {"CDT", 5}, {"CST", 6}, {"MDT", 6}, {"MST", 7}, {"PDT", 7}, {"PST", 8}, {"HST", 10}}
stsd#43rc#362427
ctcd#43rc#367276
ct#43rc#367934
corr#43rc#36-122
dayabbrs#43rc#36{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
days#43rc#36{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
months#43rc#36
list of 12{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
monthabbrs#43rc#36
list of 12{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
zones#43rc#36
list of 5{{{"est", "edt", "Massachusetts", "MA"}, 10800}, {{"cst", "cdt"}, 7200}, {{"mst", "mdt"}, 3600}, {{"pst", "pdt", "California", "CA", "Lambda"}, 0}, {{"gmt"}, 28800}}
time_units#43rc#36
list of 7{{31536000, "year", "years", "yr", "yrs"}, {2628000, "month", "months", "mo", "mos"}, {604800, "week", "weeks", "wk", "wks"}, {86400, "day", "days", "dy", "dys"}, {3600, "hour", "hours", "hr", "hrs"}, {60, "minute", "minutes", "min", "mins"}, {1, "second", "seconds", "sec", "secs"}}
help_msg#79rc#36
list of 30{" Converting from seconds-since-1970 ", "dhms (time) => string ...DD:HH:MM:SS", "english_time (time[, reference time)=> string of y, m, d, h, m, s", "", " Converting to seconds", "to_seconds (\"hh:mm:ss\") => seconds since 00:00:00", "from_ctime (ctime) => corresponding time-since-1970", "from_day (day_of_week, which) => time-since-1970 for the given day*", "from_month (month, which) => time-since-1970 for the given month*", " (* the first midnight of that day/month)", "parse_english_time_interval(\"n1 u1 n2 u2...\")", " => seconds in interval", "seconds_until_time(\"hh:mm:ss\") => number of seconds from now until then", "seconds_until_date(\"month\",day,\"hh:mm:ss\",flag ", " => number of seconds from now until then", " (see verb help for details)", "", " Converting to some standard English formats", "day ([c]time) => what day it is", "month ([c]time) => what month it is", "ampm ([c]time[, precision]) => what time it is, with am or pm", "mmddyy ([c]time) => date in format MM/DD/YY", "ddmmyy ([c]time) => date in format DD/MM/YY", "", " Substitution", "time_sub (string, time) => substitute time information", "", " Miscellaneous", "sun ([time]) => angle between sun and zenith", "dst_midnight (time) "}
key#1c#36<clear>
aliases#1rc#36{"time utilities", "time"}
description#1rc#36{"This is the time utilities utility package. See `help $time_utils' for more details."}
object_size#1r#36{24603, -1090650497}
html#1rc#36<clear>

Ancestry

Ancestors (nearest first): #79 Generic Utilities Package#1 Root Class

Children: none

Call graph

calls n43_0 #43:day n20_38 #20:explode n43_0->n20_38 n43_1 #43:month n43_1->n20_38 n43_2 #43:ampm n43_2->n20_38 n43_4 #43:sun n26_31 #26:cos n43_4->n26_31 n26_30 #26:sin n43_4->n26_30 n43_5 #43:from_ctime n43_5->n20_38 n55_17 #55:assoc n43_5->n55_17 n43_7 #43:english_time n20_6 #20:english_list n43_7->n20_6 n43_8 #43:from_day n20_22 #20:find_prefix n43_8->n20_22 n43_9 #43:from_month n43_9->n20_22 n43_11 #43:time_sub n43_11->n43_0 n43_11->n20_38 n43_11->n43_1 n6_18 #6:tell n43_11->n6_18 n20_9 #20:trim n43_11->n20_9 n20_71 #20:redirect_ansi n43_11->n20_71 n43_12 #43:mmddyy n43_12->n20_38 n43_13 #43:parse_english_time_interval n43_13->n43_13 n20_39 #20:words n43_13->n20_39 n55_9 #55:setremove_all n43_13->n55_9 n20_24 #20:is_integer n43_13->n20_24 n43_14 #43:seconds_until_date n43_14->n43_9 n43_15 #43:seconds_until_time n43_14->n43_15 n43_3 #43:to_seconds n43_15->n43_3

Source

day

Spec none none noneFlags rxdOwner #36Definer #43

Referenced by

Source

1"Given a time() or ctime()-style date, this returns the full name of the day.";
2if (typeof(args[1]) == INT)
3time = ctime(args[1]);
4elseif (typeof(args[1]) == STR)
5time = args[1];
6else
7return E_TYPE;
8endif
9dayabbr = $string_utils:explode(time)[1];
10return this.days[dayabbr in this.dayabbrs];

month

Spec none none noneFlags rxdOwner #36Definer #43

Referenced by

Source

1"Given a time() or ctime()-style date, this returns the full name";
2"of the month.";
3if (typeof(args[1]) == INT)
4time = ctime(args[1]);
5elseif (typeof(args[1]) == STR)
6time = args[1];
7else
8return E_TYPE;
9endif
10monthabbr = $string_utils:explode(time)[2];
11return this.months[monthabbr in this.monthabbrs];

ampm

Spec none none noneFlags rxdOwner #36Definer #43

Referenced by

none

Source

1"Return a time in the form [h]h[:mm[:ss]] {a.m.|p.m.}.  Args are";
2"[1]   either a time()- or a ctime()-style date, and";
3"[2]   (optional) the precision desired--1 for hours, 2 for minutes,";
4"        3 for seconds.  If not given, precision defaults to minutes";
5{time, ?precision = 2} = args;
6if (typeof(time) == INT)
7time = ctime(time);
8elseif (typeof(time) != STR)
9return E_TYPE;
10endif
11time = $string_utils:explode(time)[4];
12hour = toint(time[1..2]);
13if (hour == 0)
14time = ("12" + time[3..(precision * 3) - 1]) + " a.m.";
15elseif (hour == 12)
16time = time[1..(precision * 3) - 1] + " p.m.";
17elseif (hour > 12)
18time = (tostr(hour - 12) + time[3..(precision * 3) - 1]) + " p.m.";
19else
20time = (tostr(hour) + time[3..(precision * 3) - 1]) + " a.m.";
21endif
22return time;

to_seconds

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"Given string hh:mm:ss ($string_utils:explode(ctime(time))[4]), this returns";
2"the number of seconds elapsed since 00:00:00.  I can't remember why I";
3"created this verb, but I'm sure it serves some useful purpose.";
4return (((60 * 60) * toint(args[1][1..2])) + (60 * toint(args[1][4..5]))) + toint(args[1][7..8]);

sun

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

none

Source

1{?time = time()} = args;
2r = 10000;
3h = (r * r) + (r / 2);
4t = ((time + 120) % 86400) / 240;
5s = (5 * ((time - 14957676) % 31556952)) / 438291;
6phi = (s + t) + this.corr;
7cs = $trig_utils:cos(s);
8spss = ((($trig_utils:sin(phi) * $trig_utils:sin(s)) + h) / r) - r;
9cpcs = ((($trig_utils:cos(phi) * cs) + h) / r) - r;
10return (((((this.stsd * cs) - (this.ctcd * cpcs)) - (this.ct * spss)) + h) / r) - r;

from_ctime

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"Given a string such as returned by ctime(), return the corresponding time-in-seconds-since-1970 time returned by time(), or E_DIV if the format is wrong in some essential way.";
2words = $string_utils:explode(args[1]);
3if (length(words) == 5)
4"Arrgh!  the old ctime() didn't return a time zone, yet it arbitrarily decides whether it's standard or daylight savings time.  URK!!!!!";
5words = listappend(words, "PST");
6endif
7if ((((length(words) != 6) || (length(hms = $string_utils:explode(words[4], ":")) != 3)) || (!(month = words[2] in this.monthabbrs))) || (!(zone = $list_utils:assoc(words[6], this.timezones))))
8return E_DIV;
9endif
10year = toint(words[5]);
11day = ({-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}[month] + toint(words[3])) + (year * 366);
12zone = zone[2];
13return (((((((((((((day - ((day + 1038) / 1464)) - ((day + 672) / 1464)) - ((day + 306) / 1464)) - ((day + 109740) / 146400)) - ((day + 73140) / 146400)) - ((day + 36540) / 146400)) - 719528) * 24) + toint(hms[1])) + zone) * 60) + toint(hms[2])) * 60) + toint(hms[3]);

dhms dayshoursminutesseconds

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1s = args[1];
2if (s < 0)
3return "-" + this:(verb)(-s);
4endif
5m = s / 60;
6s = s % 60;
7if (m)
8ss = tostr((s < 10) ? ":0" | ":", s);
9h = m / 60;
10m = m % 60;
11if (h)
12ss = tostr((m < 10) ? ":0" | ":", m, ss);
13d = h / 24;
14h = h % 24;
15return tostr(@d ? {d, (h < 10) ? ":0" | ":"} | {}, h, ss);
16else
17return tostr(m, ss);
18endif
19else
20return tostr(s);
21endif

english_time

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"english_time(time [,reference time]): returns the time as a string of";
2"years, months, days, hours, minutes and seconds using the reference time as";
3"the start time and incrementing forwards. it can be given in either ctime()";
4"or time() format. if a reference time is not given, it is set to time().";
5{_time, ?reftime = time()} = args;
6if (_time < 1)
7return "0 seconds";
8endif
9_ctime = (typeof(reftime) == INT) ? ctime(reftime) | reftime;
10seclist = {60, 60, 24};
11units = {"year", "month", "day", "hour", "minute", "second"};
12timelist = {};
13for unit in (seclist)
14timelist = {_time % unit, @timelist};
15_time = _time / unit;
16endfor
17months = 0;
18month = _ctime[5..7] in $time_utils.monthabbrs;
19year = toint(_ctime[21..24]);
20"attribution: the algorithm used is from the eminently eminent g7.";
21while (_time >= (days = this.monthlens[month] + (((month == 2) && ((year % 4) == 0)) && (!((year % 400) in {100, 200, 300})))))
22_time = _time - days;
23months = months + 1;
24if ((month = month + 1) > 12)
25year = year + 1;
26month = 1;
27endif
28(ticks_left() < 4000) && suspend(0);
29endwhile
30timelist = {months / 12, months % 12, _time, @timelist};
31for unit in (units)
32i = unit in units;
33if (timelist[i] > 0)
34units[i] = ((tostr(timelist[i]) + " ") + units[i]) + ((timelist[i] == 1) ? "" | "s");
35else
36units = listdelete(units, i);
37timelist = listdelete(timelist, i);
38endif
39endfor
40return $string_utils:english_list(units);

from_day

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"from_day(day_of_week,which)";
2"numeric time (seconds since 1970) corresponding to midnight (PST) of the given weekday.  Use either the name of the day or a 1..7 number (1==Sunday,...)";
3"  which==-1 => use most recent such day.";
4"  which==+1 => use first upcoming such day.";
5"  which==0  => use closest such day.";
6"larger (absolute) values for which specify a certain number of weeks into the future or past.";
7{day, ?dir = 0} = args;
8if (!(toint(day) || (day = $string_utils:find_prefix(day, this.days))))
9return E_DIV;
10endif
11delta = {288000, 374400, 460800, 547200, 28800, 115200, 201600}[toint(day)];
12time = time() - delta;
13if (dir)
14time = (time / 604800) + ((dir > 0) ? dir | (dir + 1));
15else
16time = (time + 302400) / 604800;
17endif
18return (time * 604800) + delta;

from_month

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"from_month(month,which[,d])";
2"numeric time (seconds since 1970) corresponding to midnight (PST) of the dth (first) day of the given month.  Use either the month name or a 1..12 number (1==January,...)";
3"  which==-1 => use most recent such month.";
4"  which==+1 => use first upcoming such month.";
5"  which==0  => use closest such month.";
6"larger (absolute) values for which specify a certain number of years into the future or past.";
7{month, ?dir = 0, ?dth = 1} = args;
8if (!(toint(month) || (month = $string_utils:find_prefix(month, this.months))))
9return E_DIV;
10endif
11delta = ({0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}[month] + dth) - 1;
12day = (time() - 28800) / 86400;
13day = (day - ((day + 672) / 1461)) - delta;
14if (dir)
15day = ((day / 365) + dir) + (dir <= 0);
16else
17day = ((2 * day) + 365) / 730;
18endif
19day = (day * 365) + delta;
20day = day + ((day + 671) / 1460);
21return (day * 86400) + 28800;

dst_midnight

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"Takes a time that is midnight PST and converts it to the nearest PDT midnight time if it's during that part of the year where we use PDT.";
2time = args[1];
3return time - (3600 * (((toint(ctime(time)[12..13]) + 12) % 24) - 12));

time_sub

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"Works like pronoun substitution, but substitutes time stuff.";
2"Call with time_sub(string, time). returns a string.";
3"time is an optional integer in time() format.  If omitted, time() is used.";
4"Macros which are unknown are ignored. $Q -> the empty string.";
5"Terminal $ are ignored.";
6"$H -> hour #. $M -> min #. $S -> second #. 24-hour format, fixed width.";
7"$h, $m, $s same x/c have not-fixed format. 00:03:24 vs. 0:3:24";
8"$O/$o -> numeric hour in 12-hour format.";
9"$D -> long day name. $d -> short day name.";
10"$N -> long month name. $n -> short month name.";
11"$Y -> long year # (e.g. '1991'). $y -> short year # (e.g. '91')";
12"$Z -> the time zone    (added in by r'm later)";
13"$P/$p -> AM/PM, or am/pm.";
14"$T -> date number. $t -> date number with no extra whitespace etc.";
15"$1 -> Month in fixed-width numeric format (01-12)   (added by dpk)";
16"$2 -> Month in nonfixed numeric format (1-12)";
17"$3 -> Date in fixed-width format, 0-fill";
18"$$ -> $.";
19"";
20"This verb stolen from Ozymandias's #4835:time_subst.";
21res = "";
22{thestr, ?thetime = time()} = args;
23if ((typeof(thestr) != STR) || (typeof(thetime) != INT))
24player:tell("Bad arguments to time_subst.");
25return;
26endif
27itslength = length(thestr);
28if (!itslength)
29return "";
30endif
31done = 0;
32cctime = ctime(thetime);
33while (dollar = index(thestr, "$"))
34res = res + thestr[1..dollar - 1];
35if (dollar == length(thestr))
36return res;
37endif
38thechar = thestr[dollar + 1];
39thestr[1..dollar + 1] = "";
40if (thechar == "$")
41res = res + "$";
42elseif (!strcmp(thechar, "h"))
43res = res + $string_utils:trim(tostr(toint(cctime[12..13])));
44elseif (thechar == "H")
45res = res + cctime[12..13];
46elseif (!strcmp(thechar, "m"))
47res = res + $string_utils:trim(tostr(toint(cctime[15..16])));
48elseif (thechar == "M")
49res = res + cctime[15..16];
50elseif (!strcmp(thechar, "s"))
51res = res + $string_utils:trim(tostr(toint(cctime[18..19])));
52elseif (thechar == "S")
53res = res + cctime[18..19];
54elseif (!strcmp(thechar, "D"))
55res = res + $time_utils:day(thetime);
56elseif (thechar == "d")
57res = res + cctime[1..3];
58elseif (!strcmp(thechar, "N"))
59res = res + $time_utils:month(thetime);
60elseif (thechar == "n")
61res = res + cctime[5..7];
62elseif (!strcmp(thechar, "T"))
63res = res + cctime[9..10];
64elseif (thechar == "t")
65res = res + $string_utils:trim(cctime[9..10]);
66elseif (thechar == "O")
67res = res + tostr(((toint(cctime[12..13]) + 11) % 12) + 1);
68elseif (!strcmp(thechar, "p"))
69res = res + ((toint(cctime[12..13]) >= 12) ? "pm" | "am");
70elseif (thechar == "P")
71res = res + ((toint(cctime[12..13]) >= 12) ? "PM" | "AM");
72elseif (!strcmp(thechar, "y"))
73res = res + cctime[23..24];
74elseif (thechar == "Y")
75res = res + cctime[21..24];
76elseif (thechar == "Z")
77res = res + cctime[26..28];
78elseif (thechar == "1")
79res = res + $string_utils:right(tostr($string_utils:explode(cctime)[2] in this.monthabbrs), 2, "0");
80elseif (thechar == "2")
81res = res + tostr($string_utils:explode(cctime)[2] in this.monthabbrs);
82elseif (thechar == "3")
83res = res + $string_utils:subst(cctime[9..10], {{" ", "0"}});
84endif
85endwhile
86return res + thestr;

mmddyy ddmmyy

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"Copied from Archer (#52775):mmddyy Tue Apr  6 17:04:26 1993 PDT";
2"Given a time() or ctime()-style date and an optional separator, this returns the MM/DD/YY or DD/MM/YY form of the date (depending on the verb called.)  The default seperator is '/'";
3{time, ?divstr = "/"} = args;
4if (typeof(time) == INT)
5time = ctime(time);
6elseif (typeof(time) != STR)
7return E_TYPE;
8endif
9date = $string_utils:explode(time);
10day = toint(date[3]);
11month = date[2] in $time_utils.monthabbrs;
12year = date[5];
13daystr = (day < 10) ? "0" + tostr(day) | tostr(day);
14monthstr = (month < 10) ? "0" + tostr(month) | tostr(month);
15yearstr = tostr(year)[3..4];
16if (verb == "mmddyy")
17return tostr(monthstr, divstr, daystr, divstr, yearstr);
18else
19return tostr(daystr, divstr, monthstr, divstr, yearstr);
20endif

parse_english_time_interval

Spec this none thisFlags rxdOwner #36Definer #43

Referenced by

Source

1"$time_utils:parse_english_time_interval(n1,u1,n2,u2,...)";
2"or $time_utils:parse_english_time_interval(\"n1 u1[,] [and] n2[,] u2 [and] ...\")";
3"There must be an even number of arguments, all of which must be strings,";
4" or there must be just one argument which is the entire string to be parsed.";
5"The n's are are numeric strings, and the u's are unit names.";
6"The known units are in $time_utils.time_units,";
7" which must be kept sorted with bigger times at the head.";
8"Returns the time represented by those words.";
9"For example,";
10" $time_utils:parse_english_time_interval(\"30\",\"secs\",\"2\",\"minutes\",\"31\",\"seconds\") => 181";
11if ((length(args) == 1) && index(args[1], " "))
12return $time_utils:parse_english_time_interval(@$string_utils:words(args[1]));
13endif
14a = $list_utils:setremove_all(args, "and");
15nargs = length(a);
16if (nargs % 2)
17return E_ARGS;
18endif
19nsec = 0;
20n = 0;
21for i in [1..nargs]
22if ((i % 2) == 1)
23if ($string_utils:is_numeric(a[i]))
24n = toint(a[i]);
25elseif (a[i] in {"a", "an"})
26n = 1;
27elseif (a[i] in {"no"})
28n = 0;
29else
30return E_INVARG;
31endif
32else
33unit = a[i];
34if (unit[$] == ",")
35unit = unit[1..$ - 1];
36endif
37ok = 0;
38for entry in ($time_utils.time_units)
39if ((!ok) && (unit in entry[2..$]))
40nsec = nsec + (entry[1] * n);
41ok = 1;
42endif
43endfor
44if (!ok)
45return E_INVARG;
46endif
47endif
48endfor
49return nsec;

seconds_until_date

Spec this none thisFlags rxOwner #36Definer #43

Referenced by

none

Source

1"Copied from Ballroom Complex (#29992):from_date by Keelah! (#30246) Tue Jul 13 19:42:32 1993 PDT";
2":seconds_until_date(month,day,time,which)";
3"month is a string or the numeric representation of the month, day is a number, time is a string in the following format, hh:mm:ss.";
4"which==-1 => use most recent such month.";
5"which==+1 => use first upcoming such month.";
6"which==0 => use closest such month.";
7"This will return the number of seconds until the month, day and time given to it.";
8"Written by Keelah, on July 5, 1993.";
9{month, day, time, which} = args;
10converted = 0;
11converted = converted + $time_utils:from_month(month, which, day);
12current = this:seconds_until_time("12:00:00");
13get_seconds = this:seconds_until_time(time);
14if (get_seconds < 0)
15get_seconds = (get_seconds + 39600) - current;
16else
17get_seconds = (get_seconds + 39600) - current;
18endif
19converted = (converted + get_seconds) - time();
20return converted;

seconds_until_time

Spec this none thisFlags rxOwner #36Definer #43

Referenced by

Source

1"Copied from Ballroom Complex (#29992):seconds_until by Keelah! (#30246) Tue Jul 13 19:42:37 1993 PDT";
2":seconds_until_time(hh:mm:ss)";
3"Given the string hh:mm:ss, this returns the number of seconds until that hh:mm:ss. If the hh:mm:ss is before the current time(), the number returned is a negative, else the number is a positive.";
4"Written by Keelah, on July 4, 1993.";
5current = $time_utils:to_seconds(ctime()[12..19]);
6time = $time_utils:to_seconds(args[1]);
7return toint(time) - toint(current);