Skip to content
Snippets Groups Projects
Commit ed7e36e6 authored by Mirar (Pontus Hagland)'s avatar Mirar (Pontus Hagland)
Browse files

I have no idea why Hubbe reverted the Stardate.pmod to the Calendar I one, but...

I have no idea why Hubbe reverted the Stardate.pmod to the Calendar I one, but I'll recheck in the 1.1 version of the Calendar II stardate again...

Rev: lib/modules/Calendar.pmod/Stardate.pmod:1.3
parent 74db119b
Branches
Tags
No related merge requests found
//! module Calendar
//! submodule Stardate //! submodule Stardate
//! time unit: TNGDate //! This implements TNG stardates.
//! class TNGDate import ".";
//! implements ST:TNG stardates
//! can be used as create argument to Day
#pike __VERSION__ inherit TimeRanges;
class TNGDate static constant TNGSTARPERJULIAN=1000.0/365.2425;
{ static constant TNGSTARPERSECOND=TNGSTARPERJULIAN/86400;
inherit Calendar._TimeUnit; static constant TNG0JULIAN=2569518.5;
static constant TNG0UNIX=11139552000;
// 40759.5 2363-10-05 2584405 string calendar_name() { return "Stardate"; }
// 47391.2 2370-05-23 2586827
// 50893.5 2373-11-23 2588107 function(mixed...:cTick) Tick=cTick;
class cTick
{
inherit TimeRange;
// 6631.7 ---------- 2422 constant is_stardate=1;
// 10134.0 ---------- 3702
// 1000.0 ---------- 365.2425
// 0.0 - - 2569519
#define TNGSTARPERJULIAN (1000.0/365.2425)
//-- variables ------------------------------------------------------ float tick;
float len;
float jd; //! method void create(mixed ...)
float tics; //! method void create(int|float date)
//! method void create()
//! Apart from the standard creation methods
//! (julian day, etc), you can create a stardate
//! from the stardate number. The length
//! of the period will then be zero.
//!
//! You can also omit any arguments to create now.
//!
//! known bugs:
//! Since the precision is limited to the float type
//! of pike you can get non-precise results:
//!
//! <pre>
//! > Calendar.Second(Calendar.Stardate.Day(Calendar.Year()));
//! Result: Second(Fri 31 Dec 1999 23:59:18 CET - Sun 31 Dec 2000 23:59:18 CET)
//! </pre>
//-- standard methods -----------------------------------------------
void create(int|float|object ... day) void create(mixed ...args)
{
switch (sizeof(args))
{ {
float jd; case 4:
if (!sizeof(day)) // internal
day=({Calendar.Gregorian.Second()}); if (args[0]=="stardate")
else if (floatp(day[0]))
{ {
from_stardate(day[0]); rules=args[1];
tick=args[2];
len=args[3];
return; return;
} }
if (!intp(day[0])) break;
case 1:
if (intp(args[0]) || floatp(args[0]))
{ {
object o=day[0]; rules=default_rules;
tick=(float)args[0];
len=0.0;
return;
}
break;
case 0:
rules=default_rules;
create_unixtime_default(time());
return;
}
rules=default_rules;
::create(@args);
}
if (o->julian_day || o->julian_day_f) static void create_unixtime(int unixtime,int seconds)
jd=(float)(o->julian_day_f||o->julian_day)();
else // dig
if (o->day) // larger
{ {
o=o->day(0); tick=(unixtime-TNG0UNIX)*TNGSTARPERSECOND;
if (o->julian_day_f) len=seconds*TNGSTARPERSECOND;
jd=o->julian_day_f(); }
else if (o->julian_day)
jd=(float)o->julian_day(); static void create_unixtime_default(int unixtime)
{
tick=(unixtime-TNG0UNIX)*TNGSTARPERSECOND;
len=0.0;
}
static void create_julian_day(int|float jd)
{
tick=(jd-TNG0JULIAN)*TNGSTARPERJULIAN;
len=0.0;
}
//! method float tics()
//! This gives back the number of stardate tics
//! in the period.
float tics()
{
return len;
}
//! method int number_of_seconds()
//! method int number_of_days()
//! This gives back the Gregorian/Earth/ISO number of seconds
//! and number of days, for convinience and conversion to
//! other calendars.
int number_of_seconds()
{
return (int)(len/TNGSTARPERSECOND);
}
int number_of_days()
{
return (int)(len/TNGSTARPERJULIAN);
}
int unix_time()
{
return ((int)(tick/TNGSTARPERSECOND))+TNG0UNIX;
}
float julian_day()
{
return ((int)(tick/TNGSTARPERJULIAN))+TNG0JULIAN;
}
TimeRange add(int n,void|this_program step)
{
float x;
if (!step)
x=len;
else else
; // error, like {
if (!step->is_stardate)
error("add: Incompatible type %O\n",step);
x=step->len;
} }
else // smaller
if (n&&x)
return Tick("stardate",rules,tick+x,len);
return this_object();
}
static void convert_from(TimeRange other)
{ {
float z=1.0; if (other->unix_time)
while (sizeof(o->greater())) create_unixtime_default(other->unix_time());
else
::convert_from(other);
if (other->is_stardate)
{
tick=other->tick;
len=other->len;
}
else if (other->number_of_seconds)
len=TNGSTARPERSECOND*other->number_of_seconds();
else if (other->number_of_days)
len=TNGSTARPERJULIAN*other->number_of_days();
else
len=0.0;
}
static TimeRange _set_size(int n,TimeRange x)
{ {
string name=o->is(); if (!x->is_stardate)
o=o[o->greater()[0]](); error("distance: Incompatible type %O\n",x);
z*=o["number_of_"+name+"s"](); return Tick("stardate",rules,tick,x->len);
if (o->julian_day_f || o->julian_day) }
TimeRange place(TimeRange what,void|int force)
{ {
jd=(o->julian_day||o->julian_day_f)()/z; // can't do this
break; return this_object();
}
array(TimeRange) split(int n)
{
if (!n) return ({this_object()}); // foo
float z=tick;
float l=len/n;
array(TimeRange) res=({});
while (n--)
res+=({Tick("stardate",rules,z,l)}),z+=l;
return res;
} }
TimeRange beginning()
{
if (!len) return this_object();
return Tick("stardate",rules,tick,0.0);
} }
TimeRange end()
{
if (!len) return this_object();
return Tick("stardate",rules,tick+len,0.0);
} }
TimeRange distance(TimeRange to)
{
if (!to->is_stardate)
error("distance: Incompatible type %O\n",to);
if (to->tick<tick)
error("negative distance\n");
return Tick("stardate",rules,tick,to->tick-tick);
} }
array _compare(TimeRange with)
{
float b1=tick;
float e1=tick+len;
float b2,e2;
if (with->is_stardate)
b2=with->tick,e2=b2+with->len;
else else
jd=(float)day[0]; ::_compare(with);
from_julian_day(jd); #define CMP(A,B) ( ((A)<(B))?-1:((A)>(B))?1:0 )
return ({ CMP(b1,b2),CMP(b1,e2),CMP(e1,b2),CMP(e1,e2) });
}
int __hash() { return (int)tick; }
cTick set_ruleset(Ruleset r)
{
return Tick("stardate",r,tick,len);
}
string _sprintf(int t)
{
switch (t)
{
case 'O':
if (len!=0.0)
return sprintf("Tick(%s)",nice_print_period());
return sprintf("Tick(%s)",nice_print());
default:
return 0;
}
} }
static void from_stardate(float f) string nice_print_period()
{ {
tics=f; if (len>0.010)
jd=f/TNGSTARPERJULIAN+2569518.5; return sprintf("%s..%s",nice_print(),end()->nice_print());
else
return sprintf("%s..%+g",nice_print(),len);
} }
static void from_julian_day(float f) string nice_print()
{ {
jd=f; return sprintf("%.3f",tick);
tics=(f-2569518.5)*TNGSTARPERJULIAN;
} }
//-- nonstandard methods -------------------------------------------- //! string format_long()
//! string format_short()
//! string format_vshort()
//! Format the stardate tick nicely.
//! <pre>
//! long "-322537.312"
//! short "77463.312" (w/o >100000-component)
//! short "7463.312" (w/o >10000-component)
//! </pre>
float number() string format_long()
{ {
return tics; return sprintf("%.3f",tick);
} }
int julian_day() string format_short()
{ {
return (int)jd; return sprintf("%.3f",tick-((int)tick/100000)*100000);
} }
float julian_day_f() string format_vshort()
{ {
return jd; return sprintf("%.3f",tick-((int)tick/10000)*10000);
} }
} }
// compat
function(mixed...:cTick) TNGDate=cTick;
// for events
function(mixed...:cTick) Day=cTick;
//------------------------------------------------------------------------
// global convinience functions
//------------------------------------------------------------------------
//! method TimeofDay now()
//! Give the zero-length time period of the
//! current time.
TimeofDay now()
{
return Tick();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment