import Calendar; #include "pagesetup.h" mapping zone_list=0; int zone_list_age; mapping zone_times; mapping zone_abbr; array zones_o,zones_a,zones_n; string linkvar(string a,string b) { return a+"="+replace(b, "%/:&=;\\\"'?#"/1, "%25%2f%3a%26%3d%3b%5c%22%27%3f%23"/3); } string link_time(string time, string from_zone, array to_zone, string zsort, string contents) { string link="timezone.pike?"; link+=linkvar("time",time); if (from_zone) link+="&"+linkvar("from_zone",from_zone); if (to_zone) foreach (to_zone;;string z) link+="&"+linkvar("to_zone",z); if (zsort) link+="&"+linkvar("zone_sort",zsort); return ""+contents+""; } void make_zone_list() { zone_list_age=time(); zone_list=([]); zone_times=([]); zone_abbr=([]); zones_n=({}); zones_a=({}); zones_o=({}); foreach (TZnames.zones;string a;array v) foreach (v;;string b) { string z=a+"/"+b; string info; object now=set_timezone(z)->now(); int i=-now->utc_offset(); if (!i) info="±0:00"; else if (!(i%60)) info=sprintf("%+d:%02d",i/3600,i/60%60); else info=sprintf("%+d:%02d:%02d",i/3600,i/60%60,i%60); info=info; zone_list[z]=(["info":z+" "+info, "tzname":now->tzname(), "id":z, "offset":i, "offsetnice":info]); if (!zone_times[i]) zone_times[i]=({z}); else zone_times[i]+=({z}); if (!zone_abbr[now->tzname()]) zone_abbr[now->tzname()]=({z}); else zone_abbr[now->tzname()]+=({z}); zones_n+=({({z, z+" "+now->tzname()+" "+info })}); zones_a+=({({z, now->tzname()+" "+z+" "+info, now->tzname() })}); zones_o+=({({z, info+" "+z+" "+now->tzname(), i })}); } sort(column(zones_a,2),zones_a); sort(column(zones_o,2),zones_o); sort(column(zones_n,0),zones_n); } mixed parse(object id) { if (!zone_list || time()-zone_list_age>3600) make_zone_list(); string res=""; if (!id->variables->time) { object now=now(); id->variables->time= sprintf("%s %s %s %s", now->month_day_name(),now->month_name(),now->year_name(), now->format_todz()); } if (!id->variables->zonesort) id->variables->zonesort="o"; object cal=set_timezone(id->variables->from_zone||"Europe/Stockholm"); object got=0; string from_zone=id->variables->from_zone; if (id->variables->time) { mixed err=catch { got=cal->dwim_time(id->variables->time); if (got->timezone()->zoneid=="Europe/Tirane" && id->variables->from_zone!="Europe/Tirane") got=got->set_timezone("Europe/Stockholm"); if (got->timezone()->zoneid!=from_zone && from_zone) { object got2=got->set_timezone(from_zone); if (got->tzname() == got2->tzname()) got=got2; } string z=(id->variables->time/" ")[-1]; if (z!=got->tzname()) { object got2=got+Hour(); if (z==got2->tzname() && got2->hour_no()==got->hour_no() && got2->minute_no()==got->minute_no()) got=got2; } if (got->timezone()->zoneid) id->variables->from_zone=got->timezone()->zoneid; }; } res+= "
\n" "Time: " "variables->time||"","\"",""")+ "\">" "" "
" "(ex: 9:00 pm, August 12 2002 9:00 pm PDT)
\n" "\n" "" "
From timezone:
\n" "" "
To timezone(s):
\n" "" "
\n"+ "" "\n" "
\n"+ hr(); int table=0; if (id->variables->time) { if (!got) { res+="Failed to parse "+htmlquote(id->variables->time)+".\n"; } else { if (table) res+=""; else res+="
";

	 to_zone=column(zones,0)&(to_zone|({got->timezone()->zoneid}));

// 	 to_zone=({got->timezone()->zoneid})+
// 	    (to_zone-({got->timezone()->zoneid}));

	 string utc="UTC";
	 if (got->unix_time()<0) utc="GMT";

	 foreach (to_zone;;string z)
	 {
	    string b,b0,ta;
	    object t=z?got->set_timezone(z):got;

	    if (t==got || z==got->timezone()->zoneid) 
	       b="",b0="";
	    else
	       b=b0="";

	    if (table)
	       res+=
		  "
" "" "" "" "" "" ""; else { string ta=sprintf( "%-3s %2s %-3s %-4s %8s", t->week_day_shortname(), t->month_day_name(), t->month_shortname(), t->year_name(), t->format_tod()); string tb=ta+" "+t->tzname(); res+=b+link_time(ta,z,to_zone,zsort,tb)+" "*(30-strlen(tb)); } if (z) { string info,uinfo; int i=got->utc_offset()-t->utc_offset(); if (!i) info="±0:00"; else if (!(i%60)) info=sprintf("%+d:%02d",i/3600,i/60%60); else info=sprintf("%+d:%02d:%02d",i/3600,i/60%60,i%60); i=-t->utc_offset(); if (!i) uinfo="±0:00"; else if (!(i%60)) uinfo=sprintf("%+d:%02d",i/3600,i/60%60); else uinfo=sprintf("%+d:%02d:%02d",i/3600,i/60%60,i%60); object dz=Events.tzshift->next(t); string ninfo=""; if (dz) { object ds=dz-Second()*5; ninfo=sprintf("%-3s %2s %-3s %s%d:%02d %s", ds->week_day_shortname(), ds->month_day_name(), ds->month_shortname(), (ds->julian_day()-t->julian_day()>360) ?ds->year_no()+" ":"", ds->hour_no(), ds->minute_no(), ds->tzname()); dz+=Second()*5; ninfo="(to "+ link_time(sprintf("%-3s %2s %-3s %s %d:%02d %s", dz->week_day_shortname(), dz->month_day_name(), dz->month_shortname(), dz->year_name(), dz->hour_no(), dz->minute_no(), dz->tzname()), ds->timezone()->zoneid, to_zone,zsort,ninfo)+ ")"; } if (table) res+= "" "" "" ""; else res+=sprintf( "%-20s %-9s %-10s %s", z, utc+uinfo, got->tzname()+info, ninfo); } else { if (table) res+= "" ""; else res+=sprintf("%-20s %-9s %-9s", "?","?","?"); } if (table) res+="\n"; else res+=b0+"\n"; } if (table) res+="
"+b+t->week_day_name()+b0+""+b+t->month_day_name()+b0+""+b+t->month_name()+b0+""+b+t->format_tod()+b0+""+b+t->year_name()+b0+""+b+t->tzname()+b0+""+b+z+b0+""+b+utc+uinfo+b0+""+b+got->tzname()+info+b0+""+b+got->tzname()+info+b0+"??
\n"; else res+=""; } res+=hr(); } res+= ""+ sizeof(zone_list)+" geographic timezones with "+ sizeof(zone_times)+" different time offsets and "+ sizeof(zone_abbr)+" different abbreviations - right now"; "\n"; return mirar_layout("Mirar's time zone converter",res); }