dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
091c41 | 2012-05-04 | Henrik Grubbström (Grubba) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
a580e1 | 2000-09-27 | Fredrik Hübinette (Hubbe) | | #pike __REAL_VERSION__
|
a20af6 | 2000-09-26 | Fredrik Hübinette (Hubbe) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | inherit Calendar.TimeRanges:TimeRanges;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
489a70 | 2001-01-11 | Mirar (Pontus Hagland) | | #include "constants.h"
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
function(mixed...:TimeRange) Day;
|
e5230a | 2004-09-05 | Martin Nilsson | | #ifndef __AUTO_BIGNUM__
#error Calendar.Time needs bignums (Gmp.mpz)
#endif
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | Calendar.Rule.Timezone Timezone_UTC=
Calendar.Rule.Timezone(0,"UTC");
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
74db11 | 2000-09-27 | Mirar (Pontus Hagland) | | string calendar_name() { return "Time"; }
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
4a46a6 | 2003-09-08 | Johan Sundström | |
|
04e1d8 | 2004-01-11 | Martin Nilsson | |
|
55f4ec | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
class TimeofDay
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | inherit TimeRange;
constant is_timeofday=1;
TimeRange base;
int ux;
int len;
int ls;
int utco=CALUNKNOWN;
string tzn;
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
void create(mixed ...args)
{
if (!sizeof(args))
{
rules=default_rules;
create_now();
return;
}
switch (args[0])
{
case "timeofday":
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | rules=[object(Calendar.Ruleset)]args[1];
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | ux=[int]args[2];
len=[int]args[3];
ls=CALUNKNOWN;
return;
case "timeofday_sd":
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | rules=[object(Calendar.Ruleset)]args[1];
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | ux=[int]args[2];
len=[int]args[3];
ls=[int]args[4];
utco=[int]args[5];
return;
default:
if (intp(args[0]) && sizeof(args)==1)
{
rules=default_rules;
create_unixtime_default([int]args[0]);
return;
}
}
rules=default_rules;
if (create_backtry(@args)) return;
::create(@args);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected int(0..1) create_backtry(mixed ...args)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (sizeof(args)>1 && objectp(args[0]))
{
base=args[0];
rules=base->ruleset();
args=args[1..];
}
if (base)
{
switch (sizeof(args))
{
case 3:
if (intp(args[0]) && intp(args[1]) && intp(args[2]))
{
create_unixtime_default(base->second(@args)->unix_time());
return 1;
}
break;
case 2:
if (intp(args[0]) && intp(args[1]))
{
create_unixtime_default(base->second(@args,0)->unix_time());
return 1;
}
break;
}
}
return 0;
}
void create_unixtime(int unixtime,int _len)
{
ux=unixtime;
len=_len;
ls=CALUNKNOWN;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_now();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
void create_julian_day(int|float jd)
{
jd-=2440588;
float fjd=(jd-(int)jd)-0.5;
ls=CALUNKNOWN;
|
ed81a9 | 2001-06-09 | Mirar (Pontus Hagland) | | create_unixtime_default(((int)jd)*86400+(int)(fjd*86400));
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
TimeRange make_base()
{
base=Day("unix_r",ux,rules);
if (len) base=base->range(Day("unix_r",ux+len,rules));
return base;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void make_local()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (!base) make_base();
ls=ux-base->unix_time();
if (rules->timezone->is_dst_timezone)
{
if (utco==CALUNKNOWN)
[utco,tzn]=rules->timezone->tz_ux(ux);
if (utco!=base->utc_offset())
ls+=base->utc_offset()-utco;
}
}
|
adbafd | 2006-01-11 | Martin Bähr | | TimeRange `*(int|float n)
{
if(intp(n))
return set_size(n,this);
else
return second()*(int)(how_many(Second)*n);
}
array(TimeRange) split(int|float n, void|function|TimeRange with)
{
if(!with)
with=Second;
if (functionp(with)) with=promote_program(with);
float length=how_many(with)/(float)n;
TimeRange start=beginning();
TimeRange end=end();
array result=({});
while(start+with*length < end)
{
result += ({ start->distance(start+with*length) });
start=start+with*length;
}
result += ({ start->distance(end) });
return result;
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
ed3e4c | 2004-03-30 | Martin Nilsson | | this_program autopromote()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
array(int(-1..1)) _compare(TimeRange with)
{
#define CMP(A,B) ( ((A)<(B))?-1:((A)>(B))?1:0 )
|
9f3c3f | 2001-07-18 | Mirar (Pontus Hagland) | | if (!objectp(with))
error("_compare: illegal argument 1, expected TimeRange: %O\n",with);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | if (with->is_timeofday_f)
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | array(int(-1..1)) cmp=with->_compare(this);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
return ({-cmp[0],
-cmp[2],
-cmp[1],
-cmp[3]});
}
else if (with->is_timeofday)
return ({ CMP(ux,with->ux),CMP(ux,with->ux+with->len),
CMP(ux+len,with->ux),CMP(ux+len,with->ux+with->len) });
else if (with->is_supertimerange ||
with->is_nulltimerange ||
!with->unix_time)
return ::_compare(with);
else
{
int e2=with->end()->unix_time();
int b2=with->unix_time();
return ({ CMP(ux,b2),CMP(ux,e2),CMP(ux+len,b2),CMP(ux+len,e2) });
}
}
|
87786d | 2006-11-30 | Henrik Grubbström (Grubba) | | TimeRange beginning()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return Second("timeofday_sd",rules,ux,0,ls,utco)->autopromote();
}
|
87786d | 2006-11-30 | Henrik Grubbström (Grubba) | | TimeRange end()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return Second("timeofday",rules,ux+len,0)->autopromote();
}
TimeRange distance(TimeRange to)
{
if (to->is_ymd)
return distance(to->hour());
if (!to->is_timeofday)
error("distance: incompatible class %O\n",
object_program(to));
if (to->is_timeofday_f)
return
Fraction("timeofday_f",rules,ux,0,len,0)
->distance(to);
int m;
if ( (m=to->unix_time()-unix_time())<0)
|
563bd7 | 2004-01-11 | Martin Nilsson | | error("Negative distance %O .. %O\n", this,to);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
return
Second("timeofday_sd",rules,ux,m,ls,utco)
->autopromote();
}
TimeRange range(TimeRange to)
{
if (to->is_timeofday_f)
{
return distance(to->end());
}
if (to->is_timeofday)
{
int m;
if ( (m=to->unix_time()+to->len-unix_time())<0 )
error("Negative range\n");
return Second("timeofday_sd",rules,ux,m,ls,utco)
->autopromote();
}
return ::range(to);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void convert_from(TimeRange other)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
::convert_from(other);
if (other->is_timeofday)
len=other->len;
else if (other->number_of_seconds)
len=other->number_of_seconds();
else if (other->number_of_days)
len=86400*other->number_of_days();
else
len=0;
}
TimeRange _set_size(int n,TimeRange t)
{
if (!t->is_timeofday)
return distance(add(n,t));
return Second("timeofday_sd",rules,ux,n*(t->len),ls,utco)->autopromote();
}
TimeRange _move(int n,int m);
|
c4a0d9 | 2001-06-09 | Mirar (Pontus Hagland) | | TimeRange _add(float|int n,TimeRange step)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (step->is_timeofday_f)
return Fraction("timeofday_f",rules,ux,0,len,0)
->add(n,step);
if (step->is_timeofday)
return _move(n,step->number_of_seconds());
if (!base) make_base();
|
563bd7 | 2004-01-11 | Martin Nilsson | | return base->add(n,step)->place(this,1);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
cHour hour(void|int n)
{
if (ls==CALUNKNOWN) make_local();
int zx=ux-ls%3600;
if (!n || (n==-1 && !len))
return Hour("timeofday",rules,zx,3600);
if (n<0) n=number_of_hours()+n;
if (n<0 || n*3600>=len+ux-zx)
error("hour not in timerange (hour 0..%d exist)\n",(len-1)/3600);
return Hour("timeofday",rules,zx+3600*n,3600)->autopromote();
}
int number_of_hours()
{
if (ls==CALUNKNOWN) make_local();
int zx=ux-ls%3600;
return ((ux-zx)+len+3599)/3600;
}
array(cSecond) hours(int ...range)
{ return get_timeofday("hours",0,3600,Hour,@range); }
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
cMinute minute(void|int n)
{
if (ls==CALUNKNOWN) make_local();
int zx=ux-ls%60;
if (!n || (n==-1 && !len))
return Minute("timeofday",rules,zx,60);
if (n<0) n=number_of_minutes()+n;
if (n<0 || n*60>=len+ux-zx)
error("minute not in timerange (minute 0..%d exist)\n",(len-1)/60);
return Minute("timeofday",rules,zx+60*n,60)->autopromote();
}
int number_of_minutes()
{
if (ls==CALUNKNOWN) make_local();
int zx=ux-ls%60;
return ((ux-zx)+len+59)/60;
}
array(cSecond) minutes(int ...range)
{ return get_timeofday("minutes",0,60,Minute,@range); }
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
cSecond second(void|int n)
{
int len=number_of_seconds();
if (!n || (n==-1 && !len))
return Second("timeofday",rules,ux,1);
if (n<0) n=len+n;
if (n<0 || n>=len)
error("second not in timerange (second 0..%d exist)\n",len);
return Second("timeofday",rules,ux+n,1)->autopromote();
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected array(TimeRange) get_timeofday(string unit,
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | int start,int step,program p,
int ... range)
{
int from=0,n=::`[]("number_of_"+unit)(),to=n-1;
if (sizeof(range))
if (sizeof(range)<2)
error("Illegal numbers of arguments to "+unit+"()\n");
else
{
[from,to]=range;
if (from>=n) return ({}); else if (from<0) from=0;
if (to>=n) to=n-1; else if (to<from) return ({});
}
from*=step;
to*=step;
to-=from-step;
if (ls==CALUNKNOWN) make_local();
from+=ux-ls%step;
return
map(enumerate(to/step,step,from),
lambda(int x)
{ return p("timeofday",rules,x,step); });
}
array(cSecond) seconds(int ...range)
{ return get_timeofday("seconds",0,1,Second,@range); }
int number_of_seconds()
{
return len;
}
float number_of_fractions() { return (float)number_of_seconds(); }
cSecond fraction(float|int ... n)
{
if (sizeof(n))
{
float m=n[0];
if (m<=-1.0) m=1+number_of_fractions()+m;
array q=fractions(m,m);
if (!sizeof(q))
error("fraction not in range (0..%.1f)\n",number_of_fractions());
return q[0];
}
else
return fractions()[0];
}
array(cSecond) fractions(int|float ...range)
{
float from,to,n=number_of_fractions();
if (sizeof(range)==2)
from=(float)range[0],to=(float)range[1];
else if (sizeof(range)==0)
from=0.0,to=n;
else
error("Illegal arguments\n");
if (from<0.0) from=0.0;
if (to>n) to=n;
if (to<from)
return ({});
to-=from;
return ({Fraction("timeofday_f",rules,
ux+(int)from,(int)(inano*(from-(int)from)),
(int)to,(int)(inano*(to-(int)to)))
->autopromote()});
}
string nice_print();
string nice_print_period()
{
if (!base) make_base();
if (!len)
return sprintf("%s %s sharp",
base->nice_print(),nice_print());
string a=base->nice_print();
object e=end();
string b=e->day()->nice_print();
string c=e->nice_print();
if (a==b)
return a+" "+nice_print()+" - "+end()->nice_print();
if (c==b)
return a+" "+nice_print()+" - "+b+" 0:00";
return a+" "+nice_print()+" - "+b+" "+end()->nice_print();
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
TimeRange set_size_seconds(int seconds)
{
if (seconds<0) error("Negative size\n");
return Second("timeofday_sd",rules,ux,seconds,ls,utco)
->autopromote();
}
TimeRange set_size_ns(int nanoseconds)
{
if (nanoseconds<0) error("Negative size\n");
return Fraction("timeofday_f",rules,ux,0,0,nanoseconds)
->autopromote();
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
TimeRange move_seconds(int seconds)
{
return _move(seconds,1);
}
TimeRange move_ns(int nanoseconds)
{
return Fraction("timeofday_f",rules,ux,nanoseconds,len,0)
->autopromote();
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | int unix_time() { return ux; }
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | float julian_day()
{
return 2440588+ux/86400.0-0.5;
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
int hour_no()
{
if (ls==CALUNKNOWN) make_local();
return ls/3600;
}
int minute_no()
{
if (ls==CALUNKNOWN) make_local();
return (ls/60)%60;
}
int second_no()
{
if (ls==CALUNKNOWN) make_local();
return ls%60;
}
float fraction_no()
{
return 0.0;
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
mapping datetime()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (ls==CALUNKNOWN) make_local();
if (!base) make_base();
return base->datetime(1) |
(["hour":ls/3600,
"minute":(ls/60)%60,
"second":ls%60,
"fraction":0.0,
"timezone":utc_offset(),
"julian":julian_day(),
"unix":ux
]);
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
9f3c3f | 2001-07-18 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
9f3c3f | 2001-07-18 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
3e5dc6 | 2003-06-19 | Johan Sundström | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
string format_ext_ymd();
string format_iso_ymd();
string format_ymd();
string format_ymd_short();
string format_ymd_xshort();
string format_iso_week();
string format_iso_week_short();
string format_week();
string format_week_short();
string format_month();
string format_month_short();
|
2c364e | 2000-11-13 | Mirar (Pontus Hagland) | | string format_nice();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | string format_iso_time()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_iso_ymd()+" "+format_todz_iso();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
string format_ext_time()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_ext_ymd()+" "+format_tod();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
string format_time()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_ymd()+" "+format_tod();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
string format_time_short()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_ymd_short()+" "+format_tod();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
9f3c3f | 2001-07-18 | Mirar (Pontus Hagland) | | string format_iso_short()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_ymd_short()+"T"+format_tod();
|
9f3c3f | 2001-07-18 | Mirar (Pontus Hagland) | | }
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | string format_time_xshort()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_ymd_xshort()+" "+format_tod();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
string format_mtime()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_ymd()+" "+format_mod();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
string format_xtime()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this->format_ymd()+" "+format_xtod();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
string format_ctime()
{
if (!base) make_base();
return replace(base->format_ctime(),
"00:00:00",format_tod());
}
string format_http()
{
if (utc_offset())
return set_timezone(Timezone_UTC)->format_http();
if (!base) make_base();
return replace(base->format_http(),
"00:00:00",format_tod());
}
|
16901a | 2007-05-06 | Martin Bähr | | string format_ext_time_short()
{
|
a813bd | 2007-05-06 | Martin Bähr | | if (!base) make_base();
return replace(base->format_ext_time_short(),
"00:00:00 GMT",format_todz());
|
16901a | 2007-05-06 | Martin Bähr | | }
|
3e5dc6 | 2003-06-19 | Johan Sundström | | string format_commonlog()
{
if (!base) make_base();
return replace(base->format_commonlog(),
"00:00:00 ",format_tod()+" ");
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | string format_tod()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%02d:%02d:%02d", ls/3600, (ls/60)%60, ls%60);
}
string format_tod_short()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%02d%02d%02d", ls/3600, (ls/60)%60, ls%60);
}
string format_todz()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%02d:%02d:%02d %s",
ls/3600,
(ls/60)%60,
ls%60,
tzname());
}
string format_todz_iso()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%02d:%02d:%02d %s",
ls/3600,
(ls/60)%60,
ls%60,
tzname_iso());
}
string format_mod()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%02d:%02d",ls/3600,(ls/60)%60);
}
string format_xtod()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%02d:%02d:%02d.%06d",
ls/3600,
(ls/60)%60,
ls%60,
0);
}
|
2c364e | 2000-11-13 | Mirar (Pontus Hagland) | | string format_nice();
string format_nicez()
{
return format_nice()+" "+tzname();
}
string format_smtp()
{
if (ls==CALUNKNOWN) make_local();
int u=utc_offset();
|
87786d | 2006-11-30 | Henrik Grubbström (Grubba) | | return sprintf("%s, %s %s %s %02d:%02d:%02d %+03d%02d",
|
376f90 | 2000-12-12 | Mirar (Pontus Hagland) | | base->week_day_shortname(),
|
2c364e | 2000-11-13 | Mirar (Pontus Hagland) | | base->month_day_name(),
base->month_shortname(),
base->year_name(),
ls/3600, (ls/60)%60, ls%60,
-u/3600,max(u,-u)/60%60);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | string format_elapsed()
{
string res="";
|
563bd7 | 2004-01-11 | Martin Nilsson | | object left=this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | int x;
|
563bd7 | 2004-01-11 | Martin Nilsson | | if ( (x=(this/Day)) )
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
res+=sprintf("%dd",x);
|
563bd7 | 2004-01-11 | Martin Nilsson | | left=this->add(x,Day)->range(this->end());
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
return sprintf("%s%d:%02d:%02d",
res,left->len/3600,
(left->len/60)%60,
left->len%60);
}
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | this_program set_ruleset(Calendar.Ruleset r)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return
Second("timeofday",r,ux,len)
->autopromote();
}
int utc_offset()
{
if (utco==CALUNKNOWN)
return [utco,tzn]=rules->timezone->tz_ux(ux),utco;
else
return utco;
}
string tzname()
{
if (tzn) return tzn;
if (rules->timezone->is_dst_timezone)
return [utco,tzn]=rules->timezone->tz_ux(ux),tzn;
return tzn=rules->timezone->name;
}
string tzname_iso()
{
int u=utc_offset();
if (!(u%3600))
return sprintf("UTC%+d",-u/3600);
if (!(u%60))
return
(u<0)
?sprintf("UTC+%d:%02d",-u/3600,(-u/60)%60)
:sprintf("UTC-%d:%02d",u/3600,(u/60)%60);
return
(u<0)
?sprintf("UTC+%d:%02d:%02d",-u/3600,(-u/60)%60,(-u)%60)
:sprintf("UTC-%d:%02d:%02d",u/3600,(u/60)%60,u%60);
}
|
3e5dc6 | 2003-06-19 | Johan Sundström | | string tzname_utc_offset()
{
int u=utc_offset();
return sprintf("%+03d%02d", -u/3600, abs(u)/60%60);
}
string tzname_location()
{
string location = rules->timezone->zoneid;
if (location || tzn) return location || tzn;
return [utco,tzn]=rules->timezone->tz_ux(ux), tzn;
}
|
c4a0d9 | 2001-06-09 | Mirar (Pontus Hagland) | |
TimeRange place(TimeRange what,void|int force)
{
if (!base) make_base();
if (what->is_ymd)
return base->place(what,force);
error("place: Incompatible type %O\n",what);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
0dd5c6 | 2004-01-21 | Mirar (Pontus Hagland) | |
void call_out(function fun,mixed ...args)
{
if (ux-time(1)>10000)
predef::call_out(call_out,ux-time(1)-100,fun,@args);
else if (ux-time(1)<0)
predef::call_out(fun,0,@args);
else
predef::call_out(fun,-time(ux),@args);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
#ifdef TIME_OPERATOR_DEBUG
#define DEBUG_OVERLOAD_OPERATOR(OP,NAME,IND) \
TimeRange OP(mixed ...args) \
{ \
_ind+=IND; \
TimeRange x=::OP(@args); \
|
8a531a | 2006-11-04 | Martin Nilsson | | _ind=_ind[..<sizeof(IND)]; \
|
563bd7 | 2004-01-11 | Martin Nilsson | | werror(_ind+" %O\n",this); \
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | foreach (args,TimeRange t) werror(_ind+NAME+" %O\n",t); \
werror(_ind+"= %O\n",x); \
return x; \
}
DEBUG_OVERLOAD_OPERATOR(`&,"&","| ");
DEBUG_OVERLOAD_OPERATOR(`^,"^","| ");
DEBUG_OVERLOAD_OPERATOR(`|,"|","| ");
DEBUG_OVERLOAD_OPERATOR(subtract,"-","| ");
#endif
}
string _ind="* ";
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
55f4ec | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
class cSuperTimeRange
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
inherit TimeRanges::cSuperTimeRange;
#ifdef TIME_OPERATOR_DEBUG
DEBUG_OVERLOAD_OPERATOR(`&,"&","S ");
DEBUG_OVERLOAD_OPERATOR(`^,"^","S ");
DEBUG_OVERLOAD_OPERATOR(`|,"|","S ");
DEBUG_OVERLOAD_OPERATOR(subtract,"-","S ");
#endif
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
array(cHour) hours(int ...range) { return get_units("hours",@range); }
cHour hour(void|int n) { return get_unit("hours",n); }
int number_of_hours() { return num_units("hours"); }
array(cMinute) minutes(int ...range) { return get_units("minutes",@range); }
cMinute minute(void|int n) { return get_unit("minutes",n); }
int number_of_minutes() { return num_units("minutes"); }
array(cSecond) seconds(int ...range) { return get_units("seconds",@range); }
cSecond second(void|int n) { return get_unit("seconds",n); }
int number_of_seconds() { return num_units("seconds"); }
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected array(TimeRange) get_units(string unit,int ... range)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
int from=0,to=0x7fffffff,pos=0;
array res=({});
TimeRange last=0;
|
8a531a | 2006-11-04 | Martin Nilsson | | string ums=unit[..<1];
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
if (sizeof(range)==2)
[from,to]=range;
else if (sizeof(range)==1)
error("Illegal numbers of arguments to "+unit+"()\n");
foreach (parts,TimeRange part)
{
if (pos>=from)
{
if (pos>to) break;
array tmp=part[unit](from-pos,to-pos);
pos+=sizeof(tmp);
if (tmp[0]==last) tmp=tmp[1..];
res+=tmp;
if (sizeof(tmp)) last=tmp[-1];
}
else
{
int n=part["number_of_"+unit]();
TimeRange l=part[ums]();
if (l==last) n--;
if (n+pos>from)
res=part[unit](from-pos,to-pos);
pos+=n;
last=l;
}
}
return res;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected int num_units(string unit)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
int pos=0;
TimeRange last=0;
|
8a531a | 2006-11-04 | Martin Nilsson | | string ums=unit[..<1];
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
foreach (parts,TimeRange part)
{
int n=part["number_of_"+unit]();
TimeRange l=part[ums]();
if (l==last) n--;
pos+=n;
last=l;
}
return pos;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected TimeRange get_unit(string unit,int n)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
array(TimeRange) res=get_units(unit,n,n);
if (sizeof(res)==1) return res[0];
error("not in range ("+unit+" 0..%d exist)\n",
num_units(unit)-1);
}
#define RBASE parts[0]
string format_ext_ymd();
string format_iso_ymd();
string format_ymd();
string format_ymd_short();
string format_ymd_xshort();
string format_iso_week();
string format_iso_week_short();
string format_week();
string format_week_short();
string format_month();
string format_month_short();
string format_iso_time() { return RBASE->format_iso_time(); }
string format_ext_time() { return RBASE->format_ext_time(); }
|
b8dee2 | 2007-05-06 | Martin Bähr | | string format_ext_time_short() { return RBASE->format_ext_time_short(); }
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | string format_time() { return RBASE->format_time(); }
string format_time_short() { return RBASE->format_time_short(); }
string format_time_xshort() { return RBASE->format_time_xshort(); }
string format_mtime() { return RBASE->format_mtime(); }
string format_xtime() { return RBASE->format_xtime(); }
string format_ctime() { return RBASE->format_ctime(); }
string format_http() { return RBASE->format_http(); }
string format_tod() { return RBASE->format_tod(); }
string format_todz() { return RBASE->format_todz(); }
string format_todz_iso() { return RBASE->format_todz_iso(); }
string format_mod() { return RBASE->format_mod(); }
string format_xtod() { return RBASE->format_xtod(); }
int hour_no() { return RBASE->hour_no(); }
int minute_no() { return RBASE->minute_no(); }
int second_no() { return RBASE->second_no(); }
#undef RBASE
string format_elapsed()
{
TimeRange z=parts[0];
foreach (parts,TimeRange y) z+=y;
return parts[0]->distance(z)->format_elapsed();
}
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | |
|
06212d | 2002-11-27 | Martin Nilsson | | string _sprintf(int t,mapping m)
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | {
if (t=='t')
return "Calendar."+calendar_name()+".TimeofDay";
|
091120 | 2002-11-27 | Marcus Comstedt | | return ::_sprintf(t,m);
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | }
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
adb9c5 | 2000-10-18 | Mirar (Pontus Hagland) | | class cNullTimeRange
{
inherit TimeRanges::cNullTimeRange;
array(cHour) hours(int ...range) { return ({}); }
cHour hour(void|int n) { error("no hours in nulltimerange\n"); }
int number_of_hours() { return 0; }
array(cMinute) minutes(int ...range) { return ({}); }
cMinute minute(void|int n) { error("no minutes in nulltimerange\n"); }
int number_of_minutes() { return 0; }
array(cSecond) seconds(int ...range) { return ({}); }
cSecond second(void|int n) { error("no seconds in nulltimerange\n"); }
int number_of_seconds() { return 0; }
string format_elapsed() { return "0:00:00"; }
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
55f4ec | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
function(mixed...:cHour) Hour=cHour;
class cHour
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
constant is_hour=1;
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | inherit TimeofDay;
|
afa3de | 2003-12-30 | Martin Nilsson | | int __hash() { return ux/(60*60); }
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | void create_unixtime_default(int unixtime)
{
create_unixtime(unixtime,3600);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_now()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
create_unixtime(time(),3600);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_unixtime(int _ux,int _len)
|
ed81a9 | 2001-06-09 | Mirar (Pontus Hagland) | | {
::create_unixtime(_ux,_len);
if (ls==CALUNKNOWN) make_local();
if (ls%3600) ux-=ls%3600,ls=CALUNKNOWN;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_julian_day(int|float jd)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
::create_julian_day(jd);
len=3600;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void convert_from(TimeRange other)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
::convert_from(other);
len-=len%3600;
}
TimeofDay _move(int n,int m)
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | if (m==0 || n==0) return this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | if (m%3600)
return Second("timeofday",rules,ux,len)->_move(n,m);
return Hour("timeofday",rules,ux+n*m,len)->autopromote();
}
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | string _sprintf(int t,mapping m)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (catch {
switch (t)
{
case 'O':
if (!base) make_base();
if (len!=3600)
return sprintf("Hour(%s)",
nice_print_period());
return sprintf("Hour(%s %s)",
base->nice_print(),
nice_print());
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | case 't':
return "Calendar."+calendar_name()+".Hour";
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | default:
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | return ::_sprintf(t,m);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
})
return "error";
}
string nice_print()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%d:00 %s",ls/3600,tzname());
}
|
2c364e | 2000-11-13 | Mirar (Pontus Hagland) | |
string format_nice()
{
if (ls==CALUNKNOWN) make_local();
if (!base) make_base();
return base->format_nice()+" "+sprintf("%d:00",ls/3600);
}
|
c4a0d9 | 2001-06-09 | Mirar (Pontus Hagland) | |
TimeRange place(TimeRange what,void|int force)
{
if (what->is_hour)
return Hour("timeofday",rules,ux,what->len);
if (what->is_minute)
return minute()+(what->hour()->distance(what))->number_of_minutes();
if (what->is_fraction)
{
TimeRange t=what->hour()->distance(what);
int s=t->len_s;
int ns=t->len_ns;
return
Fraction("timeofday_f",rules,ux,0,what->len_s,what->len_s)
->_move(1,s,ns);
}
if (what->is_second)
return second()+(what->hour()->distance(what))->number_of_seconds();
::place(what,force);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
55f4ec | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
function(mixed...:cMinute) Minute=cMinute;
class cMinute
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
constant is_minute=1;
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | inherit TimeofDay;
|
afa3de | 2003-12-30 | Martin Nilsson | | int __hash() { return ux/60; }
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_unixtime(int _ux,int _len)
|
ed81a9 | 2001-06-09 | Mirar (Pontus Hagland) | | {
::create_unixtime(_ux,_len);
if (ls==CALUNKNOWN) make_local();
ux-=ls%60;
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | void create_unixtime_default(int unixtime)
{
create_unixtime(unixtime,60);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_now()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
create_unixtime(time()/60*60,60);
}
TimeofDay autopromote()
{
if (ls==CALUNKNOWN) make_local();
if (!(ls%3600) && !(len%3600))
return Hour("timeofday_sd",rules,ux,len,ls,utco)->autopromote();
return ::autopromote();
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void convert_from(TimeRange other)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
::convert_from(other);
len-=len%60;
}
TimeofDay _move(int n,int m)
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | if (m==0 || n==0) return this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | if (m%60) return Second("timeofday",rules,ux,len)->_move(n,m);
return Minute("timeofday",rules,ux+n*m,len)->autopromote();
}
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | string _sprintf(int t,mapping m)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
switch (t)
{
case 'O':
if (!base) make_base();
if (len!=60)
return sprintf("Minute(%s)",
nice_print_period());
return sprintf("Minute(%s %s)",
base->nice_print(),
nice_print());
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | case 't':
return "Calendar."+calendar_name()+".Minute";
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | default:
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | return ::_sprintf(t,m);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
}
string nice_print()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%d:%02d %s",ls/3600,(ls/60)%60,tzname());
}
|
2c364e | 2000-11-13 | Mirar (Pontus Hagland) | |
string format_nice()
{
if (ls==CALUNKNOWN) make_local();
if (!base) make_base();
return base->format_nice()+" "+sprintf("%d:%02d",ls/3600,(ls/60)%60);
}
|
c4a0d9 | 2001-06-09 | Mirar (Pontus Hagland) | |
TimeRange place(TimeRange what,void|int force)
{
if (what->is_hour)
return hour()->place(what);
if (what->is_minute)
return Minute("timeofday",rules,ux,what->len);
if (what->is_fraction)
{
TimeRange t=what->minute()->distance(what);
int s=t->len_s;
int ns=t->len_ns;
return
Fraction("timeofday_f",rules,ux,0,what->len_s,what->len_s)
->_move(1,s,ns);
}
if (what->is_second)
return second()+(what->minute()->distance(what))->number_of_seconds();
::place(what,force);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
55f4ec | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
function(mixed...:cSecond) Second=cSecond;
class cSecond
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
constant is_second=1;
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | inherit TimeofDay;
|
afa3de | 2003-12-30 | Martin Nilsson | | int __hash() { return ux; }
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | void create_unixtime_default(int unixtime)
{
create_unixtime(unixtime,1);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_now()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
create_unixtime(time(),1);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_julian_day(int|float jd)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
::create_julian_day(jd);
len=1;
}
TimeofDay autopromote()
{
if (ls==CALUNKNOWN) make_local();
if (!(ls%60) && !(len%60))
return Minute("timeofday",rules,ux,len)->autopromote();
return ::autopromote();
}
TimeofDay _move(int n,int m)
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | if (m==0 || n==0) return this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | return Second("timeofday",rules,ux+n*m,len)->autopromote();
}
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | string _sprintf(int t,mapping m)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
switch (t)
{
case 'O':
if (len!=1)
return sprintf("Second(%s)",
nice_print_period());
if (!base) make_base();
return sprintf("Second(%s %s)",
base->nice_print(),
nice_print());
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | case 't':
return "Calendar."+calendar_name()+".Second";
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | default:
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | return ::_sprintf(t,m);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
}
string nice_print()
{
if (ls==CALUNKNOWN) make_local();
|
4999cc | 2001-01-12 | Mirar (Pontus Hagland) | | return sprintf("%d:%02d:%02d %s",ls/3600,ls/60%60,ls%60,tzname()||"?");
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
2c364e | 2000-11-13 | Mirar (Pontus Hagland) | | string format_nice()
{
if (ls==CALUNKNOWN) make_local();
if (!base) make_base();
return base->format_nice()+" "+
sprintf("%d:%02d:%02d",ls/3600,ls/60%60,ls%60);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
string iso_name()
|
a2ce53 | 2010-04-15 | Henrik Grubbström (Grubba) | | { return this->format_ymd()+"T"+format_tod(); }
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | string iso_short_name()
|
a2ce53 | 2010-04-15 | Henrik Grubbström (Grubba) | | { return this->format_ymd_short()+"T"+(format_tod()-":"); }
|
c4a0d9 | 2001-06-09 | Mirar (Pontus Hagland) | |
TimeRange place(TimeRange what,void|int force)
{
if (what->is_hour)
return hour()->place(what);
if (what->is_minute)
return minute()->place(what);
if (what->is_fraction)
{
TimeRange t=what->second()->distance(what);
int s=t->len_s;
int ns=t->len_ns;
return
Fraction("timeofday_f",rules,ux,0,what->len_s,what->len_s)
->_move(1,s,ns);
}
if (what->is_second)
return second()+(what->second()->distance(what))->number_of_seconds();
::place(what,force);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
091c41 | 2012-05-04 | Henrik Grubbström (Grubba) | |
|
55f4ec | 2001-04-27 | Henrik Grubbström (Grubba) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
function(mixed...:cFraction) Fraction=cFraction;
class cFraction
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
inherit cSecond;
constant is_timeofday_f=1;
constant is_fraction=1;
int ns;
int len_ns;
int len_s;
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
ed3076 | 2004-03-30 | David Gourdelier | |
|
0dd5c6 | 2004-01-21 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
0dd5c6 | 2004-01-21 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
void create(mixed ...args)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (!sizeof(args))
{
rules=default_rules;
create_now();
return;
}
else if (args[0]=="timeofday_f")
{
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | rules=[object(Calendar.Ruleset)]args[1];
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | ux=[int]args[2];
ns=[int]args[3];
len_s=[int]args[4];
len_ns=[int]args[5];
ls=CALUNKNOWN;
if (ns<0)
|
563bd7 | 2004-01-11 | Martin Nilsson | | error("Can't create negative ns: %O\n",this);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
if (!rules) error("no rules\n");
if (len_ns>=inano) len_s+=(len_ns/inano),len_ns%=inano;
if (ns>=inano) ux+=(ns/inano),ns%=inano;
len=len_s+(ns+len_ns+inano-1)/inano;
if (ns<0)
|
563bd7 | 2004-01-11 | Martin Nilsson | | error("Can't create negative ns: %O\n",this);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
return;
}
else if (intp(args[0]) && sizeof(args)==1)
{
rules=default_rules;
create_unixtime(args[0]);
return;
}
else switch (args[0])
{
|
4a46a6 | 2003-09-08 | Johan Sundström | | case "timeofday":
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | rules=[object(Calendar.Ruleset)]args[1];
|
4a46a6 | 2003-09-08 | Johan Sundström | | ux=[int]args[2];
len=[int]args[3];
ls=CALUNKNOWN;
ns=[int]args[4];
return;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | case "unix":
rules=default_rules;
create_unixtime(@args[1..]);
return;
case "julian":
rules=default_rules;
create_julian_day(args[1]);
return;
case "unix_r":
rules=args[-1];
|
8a531a | 2006-11-04 | Martin Nilsson | | create_unixtime(@args[..<1]);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | return;
case "julian_r":
rules=args[2];
create_julian_day(args[1]);
return;
}
rules=default_rules;
if (create_backtry(@args)) return;
error("Fraction: illegal argument(s) %O,%O,%O,...\n",
@args,0,0,0);
}
|
4a46a6 | 2003-09-08 | Johan Sundström | | int create_backtry(mixed ...args)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (sizeof(args)>1 && objectp(args[0]) && args[0]->is_day)
{
base=args[0];
rules=base->ruleset();
args=args[1..];
}
ls=CALUNKNOWN;
if (base)
{
switch (sizeof(args))
{
case 4:
if (intp(args[0]) && intp(args[1]) && intp(args[2]) &&
intp(args[3]))
{
ux=base->second(@args[..2])->unix_time();
ns=args[3];
return 1;
}
break;
case 3:
if (intp(args[0]) && intp(args[1]) && floatp(args[2]))
{
ux=base->second(args[0],args[1],(int)args[2])->unix_time();
ns=(int)(inano*(args[3]-(int)args[3]));
return 1;
}
break;
}
}
return ::create_backtry(@args);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_now()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
ux=time();
ns=(int)(inano*time(ux));
if (ns>=inano) ux+=(ns/inano),ns%=inano;
len=len_s=len_ns=0;
ls=CALUNKNOWN;
}
int unix_time() { return ux; }
float f_unix_time() { return ux+ns*(1.0/inano); }
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_unixtime(int|float unixtime,
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | void|int|float _len)
{
ux=(int)unixtime;
ns=(int)(inano*(unixtime-(int)unixtime));
len_s=(int)_len;
len_ns=(int)(inano*(_len-(int)_len));
ls=CALUNKNOWN;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_unixtime_default(int|float unixtime)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
|
c0dbee | 2000-08-01 | Mirar (Pontus Hagland) | | create_unixtime(unixtime);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
float julian_day()
{
return 2440588+(ux+ns*(1.0/inano))*(1/86400.0)+0.5;
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void create_julian_day(int|float jd)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
jd-=2440588;
float fjd=((jd-(int)jd)+0.5)*86400;
ux=((int)jd)*86400+(int)(fjd);
ns=(int)(inano*(fjd-(int)fjd));
ls=CALUNKNOWN;
}
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | string _sprintf(int t,mapping m)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
switch (t)
{
case 'O':
if (!base) make_base();
if (len_ns || len_s)
return sprintf("Fraction(%s)",
nice_print_period());
return sprintf("Fraction(%s %s)",
base->nice_print(),
nice_print());
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | case 't':
return "Calendar."+calendar_name()+".Fraction";
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | default:
|
61ec5e | 2001-06-09 | Mirar (Pontus Hagland) | | return ::_sprintf(t,m);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
}
int __hash() { return ux; }
string nice_print()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%d:%02d:%02d.%06d %s",
ls/3600,ls/60%60,ls%60,ns/1000,tzname());
}
|
2c364e | 2000-11-13 | Mirar (Pontus Hagland) | | string format_nice()
{
if (ls==CALUNKNOWN) make_local();
if (!base) make_base();
return base->format_nice()+" "+
sprintf("%d:%02d:%02d.%06d",
ls/3600,ls/60%60,ls%60,ns/1000);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | TimeofDay autopromote()
{
if (!ns && !len_ns)
return Second("timeofday",rules,ux,len_s)->autopromote();
|
563bd7 | 2004-01-11 | Martin Nilsson | | return this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | this_program set_ruleset(Calendar.Ruleset r)
|
4a46a6 | 2003-09-08 | Johan Sundström | | {
return
Fraction("timeofday",r,ux,len,ns)
->autopromote();
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | TimeofDay _move(int n,int z_s,void|int z_ns)
{
if ((z_s==0 && z_ns==0) || n==0) return autopromote();
int nns=ns+n*z_ns;
int nux=ux+n*z_s;
if (nns<0 || nns>=inano) nux+=nns/inano,nns%=inano;
return Fraction("timeofday_f",rules,nux,nns,len_s,len_ns)
->autopromote();
}
TimeRange _add(int n,TimeRange step)
{
if (step->is_timeofday_f)
return _move(n,step->len_s,step->len_ns);
return ::_add(n,step);
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected void convert_from(TimeRange other)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
::convert_from(other);
if (other->is_timeofday_f)
{
ns=other->ns;
len_s=other->len_s;
len_ns=other->len_ns;
}
}
|
9eaf1d | 2008-06-28 | Martin Nilsson | | protected TimeRange _set_size(int n,TimeRange t)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
if (t->is_timeofday_f)
return Fraction("timeofday_f",rules,ux,ns,
n*t->len_s,n*t->len_ns)
->autopromote();
if (t->is_timeofday)
return Fraction("timeofday_f",rules,ux,ns,n*t->len,0)
->autopromote();
return distance(add(n,t));
}
TimeRange distance(TimeRange to)
{
int s1=ux;
int s2;
if (to->is_timeofday)
s2=to->ux;
else
s2=to->unix_time();
if (s2<s1 ||
(s2==s1 && to->ns<ns))
|
563bd7 | 2004-01-11 | Martin Nilsson | | error("Negative distance %O .. %O\n", this,to);
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
return
Fraction("timeofday_f",rules,ux,ns,s2-s1-1,to->ns-ns+inano)
->autopromote();
}
TimeRange range(TimeRange to)
{
return distance(to->end());
}
array(int(-1..1)) _compare(TimeRange with)
{
#define CMP2(AS,ANS,BS,BNS) \
(((AS)<(BS))?-1: \
((AS)>(BS))?1: \
((ANS)<(BNS))?-1: \
((ANS)>(BNS))?1:0)
if (with->is_timeofday_f)
return ({ CMP2(ux,ns,with->ux,with->ns),
CMP2(ux,ns,with->ux+with->len_s,with->ns+with->len_ns),
CMP2(ux+len_s,ns+len_ns,with->ux,with->ns),
CMP2(ux+len_s,ns+len_ns,with->ux+with->len_s,
with->ns+with->len_ns) });
else if (with->is_timeofday)
return ({ CMP2(ux,ns,with->ux,0),
CMP2(ux,ns,with->ux+with->len,0),
CMP2(ux+len_s,ns+len_ns,with->ux,0),
CMP2(ux+len_s,ns+len_ns,with->ux+with->len, 0) });
return ::_compare(with);
}
TimeofDay beginning()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | if (len_s==0.0 && len_ns==0.0) return this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | return Fraction("timeofday_f",rules,ux,ns,0,0)
->autopromote();
}
TimeofDay end()
{
|
563bd7 | 2004-01-11 | Martin Nilsson | | if (len_s==0 && len_ns==0) return this;
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | object q=Fraction("timeofday_f",rules,ux+len_s,ns+len_ns,0,0)
->autopromote();
return q;
}
TimeRange set_size_seconds(int seconds)
{
if (seconds<0) error("Negative size\n");
return Fraction("timeofday_f",rules,ux,ns,seconds,0)
->autopromote();
}
TimeRange set_size_ns(int nanoseconds)
{
if (nanoseconds<0) error("Negative size\n");
return Fraction("timeofday_f",rules,ux,ns,0,nanoseconds)
->autopromote();
}
TimeRange move_seconds(int seconds)
{
return _move(1,seconds);
}
TimeRange move_ns(int nanoseconds)
{
return _move(1,0,nanoseconds);
}
int number_of_seconds()
{
if (len_ns || ns) return len_s+1;
else return len_s||1;
}
float number_of_fractions()
{
return len_s+1e-9*len_ns;
}
array(cFraction) fractions(int|float ...range)
{
float from,to,n=number_of_fractions();
if (sizeof(range)==2)
from=(float)range[0],to=(float)range[1];
else if (sizeof(range)==0)
from=0.0,to=n;
else
error("Illegal arguments\n");
if (from<0.0) from=0.0;
if (to>n) to=n;
if (to<from)
return ({});
|
563bd7 | 2004-01-11 | Martin Nilsson | | if (from==0.0 && to==n) return ({this});
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
to-=from;
return ({Fraction("timeofday_f",rules,
ux+(int)from,ns+(int)(inano*(from-(int)from)),
(int)to,(int)(inano*(to-(int)to)))});
}
mapping datetime()
{
return ::datetime()|(["fraction":ns*(1.0/inano)]);
}
string format_xtod()
{
if (ls==CALUNKNOWN) make_local();
return sprintf("%02d:%02d:%02d.%06d",
ls/3600,
(ls/60)%60,
ls%60,
ns/1000);
}
string format_elapsed()
{
int x;
|
563bd7 | 2004-01-11 | Martin Nilsson | | if ( (x=(this/Day)) )
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
|
563bd7 | 2004-01-11 | Martin Nilsson | | object left=this->add(x,Day)->range(this->end());
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | return sprintf("%dd%d:%02d:%02d.%03d",
x,left->len_s/3600,
(left->len_s/60)%60,
left->len_s%60,
left->len_ns/1000000);
}
if (len_s>=3600)
return sprintf("%d:%02d:%02d.%03d",
len_s/3600,(len_s/60)%60,
len_s%60,len_ns/1000000);
if (len_s>=60)
return sprintf("%d:%02d.%03d",
len_s/60,len_s%60,len_ns/1000000);
if (len_s || len_ns>inano/1000)
return sprintf("%d.%06d",
len_s,len_ns/1000);
return sprintf("0.%09d",len_ns);
}
|
257aa6 | 2003-09-08 | Johan Sundström | |
float fraction_no()
{
return ns / 1e9;
}
|
0dd5c6 | 2004-01-21 | Mirar (Pontus Hagland) | |
void call_out(function fun,mixed ...args)
{
if (ux-time(1)>10000)
predef::call_out(call_out,ux-time(1)-100,fun,@args);
else if (ux-time(1)<0)
predef::call_out(fun,0,@args);
else
predef::call_out(fun,-time(ux)+ns*1e-9,@args);
}
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | }
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | TimeofDay now()
{
return Fraction();
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | this_program set_timezone(string|Calendar.Rule.Timezone tz)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return set_ruleset(default_rules->set_timezone(tz));
}
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | Calendar.Rule.Timezone timezone()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return default_rules->timezone;
}
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | this_program set_language(string|Calendar.Rule.Language lang)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return set_ruleset(default_rules->set_language(lang));
}
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | Calendar.Rule.Language language()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return default_rules->language;
}
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
dc21d4 | 2001-05-05 | Mirar (Pontus Hagland) | |
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | |
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | this_program set_ruleset(Calendar.Ruleset r)
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
|
547955 | 2003-03-13 | Martin Nilsson | | this_program c=this_program();
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | c->default_rules=r;
return c;
}
|
9f0b6c | 2008-02-07 | Martin Stjernholm | | Calendar.Ruleset ruleset()
|
78fd53 | 2000-07-12 | Mirar (Pontus Hagland) | | {
return default_rules;
}
|