Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / api / common / time.php
blob9d2020efef514d8e55e473da1af9615f7999038d
1 <?php
2 /* Copyright (C) 2009 Winch Gate Property Limited
4 * This file is part of ryzom_api.
5 * ryzom_api is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * ryzom_api is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with ryzom_api. If not, see <http://www.gnu.org/licenses/>.
19 /* Basic constants */
20 /* 1 IG hour = 3 IRL minutes = 1800 ticks */
21 define('RYTIME_HOUR_TICKS', 1800);
22 define('RYTIME_DAY_HOURS', 24);
23 define('RYTIME_SEASON_DAYS', 90);
24 define('RYTIME_MONTH_DAYS', 30);
25 define('RYTIME_CYCLE_MONTHS', 12);
26 define('RYTIME_JY_CYCLES', 4);
27 define('RYTIME_WEEK_DAYS', 6);
28 /* 0 = spring, 1 = summer, 2 = automn, 3 = winter */
29 define('RYTIME_CYCLE_SEASONS', 4);
30 /* Tick is offset on server of 61 days. */
31 define('RYTIME_TICK_OFFSET', 61 * RYTIME_DAY_HOURS * RYTIME_HOUR_TICKS);
33 define('RYTIME_START_JY', 2568);
35 /* Helpers */
36 define('RYTIME_CYCLE_DAYS',RYTIME_CYCLE_MONTHS * RYTIME_MONTH_DAYS);
37 define('RYTIME_JY_DAYS', RYTIME_CYCLE_DAYS * RYTIME_JY_CYCLES);
38 define('RYTIME_JY_MONTHS', RYTIME_CYCLE_MONTHS * RYTIME_JY_CYCLES);
40 // Takes a server tick and returns a computed array
41 function ryzom_time_array($tick) {
42 $out = array();
43 $out["server_tick"] = $tick;
45 $time_in_hours = ($tick-RYTIME_TICK_OFFSET) / RYTIME_HOUR_TICKS;
46 $day = $time_in_hours / RYTIME_DAY_HOURS;
48 $out["jena_year"] = floor($day / RYTIME_JY_DAYS) + RYTIME_START_JY;
49 if ($day < 0) $day = RYTIME_JY_DAYS - abs($day) % RYTIME_JY_DAYS;
50 $out["day_of_jy"] = $day % RYTIME_JY_DAYS;
51 $out["month_of_jy"] = floor($out["day_of_jy"] / RYTIME_MONTH_DAYS);
53 $out["cycle"] = floor($out["day_of_jy"] / RYTIME_CYCLE_DAYS);
54 $out["day_of_cycle"] = $day % RYTIME_CYCLE_DAYS;
55 $out["month_of_cycle"] = $out["month_of_jy"] % RYTIME_CYCLE_MONTHS;
57 $out["day_of_month"] = $out["day_of_jy"] % RYTIME_MONTH_DAYS;
58 $out["day_of_week"] = $day % RYTIME_WEEK_DAYS;
60 $out["season"] = ($day / RYTIME_SEASON_DAYS) % RYTIME_CYCLE_SEASONS;
61 $out["day_of_season"] = $day % RYTIME_SEASON_DAYS;
63 $out["time_of_day"] = abs($time_in_hours) % RYTIME_DAY_HOURS;
64 if ($time_in_hours < 0 && $out["time_of_day"]) $out["time_of_day"] = RYTIME_DAY_HOURS - $out["time_of_day"];
66 return $out;
69 function ryzom_time_xml_without_cache($rytime) {
70 $out = new SimpleXMLElement('<shard_time/>');
71 foreach($rytime as $key => $value) {
72 $out->addChild($key, $value);
74 return $out;
77 /**
78 * Take number of the month (0-11) and returns its name
80 function ryzom_month_name($month_number) {
81 if ($month_number < 0 || $month_number > 11) return "bad month";
83 $RYTIME_MONTHS = array(
84 'Winderly', 'Germinally', 'Folially', 'Floris',
85 'Medis', 'Thermis', 'Harvestor', 'Frutor',
86 'Fallenor', 'Pluvia', 'Mystia', 'Nivia'
89 return $RYTIME_MONTHS[(int)$month_number];
93 /**
94 * Take number of the day of week (0-5) and returns its name
96 function ryzom_day_name($day_number) {
97 if ($day_number < 0 || $day_number > 5) return "bad day of week";
99 $RYTIME_DAYS = array(
100 'Prima', 'Dua', 'Tria',
101 'Quarta', 'Quinteth', 'Holeth'
104 return $RYTIME_DAYS[(int)$day_number];
108 * Take a computed ryzom time array and returns the formatted date
109 * (Official 2004 Format without trailing JY)
111 function ryzom_time_txt($rytime, $lang = "en") {
112 if ($lang != "en" && $lang != "fr" && $lang != "de") $lang = "en";
114 $RYTIME_AC = array(
115 "de" => array("1. AZ", "2. AZ", "3. AZ", "4. AZ"),
116 "en" => array("1st AC", "2nd AC", "3rd AC", "4th AC"),
117 "fr" => array("1er CA", "2e CA", "3e CA", "4e CA")
120 # Day, Month DayOfMonth, CycleNth AC JY
121 return sprintf("%sh - %s, %s %d, %s %d",
122 $rytime["time_of_day"],
123 ryzom_day_name($rytime["day_of_week"]),
124 ryzom_month_name($rytime["month_of_cycle"]),
125 $rytime["day_of_month"] + 1,
126 $RYTIME_AC[$lang][$rytime["cycle"]],
127 $rytime["jena_year"]);