timefuncs.pwn 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. stock ConvertToTwelveHour(tHour)
  2. {
  3. new string[56], suffix[3], cHour;
  4. if(tHour > 12 && tHour < 24)
  5. {
  6. cHour = tHour - 12;
  7. suffix = "PM";
  8. }
  9. else if(tHour == 12)
  10. {
  11. cHour = 12;
  12. suffix = "PM";
  13. }
  14. else if(tHour > 0 && tHour < 12)
  15. {
  16. cHour = tHour;
  17. suffix = "AM";
  18. }
  19. else if(tHour == 0)
  20. {
  21. cHour = 12;
  22. suffix = "AM";
  23. }
  24. format(string, sizeof(string), "%d%s", cHour, suffix);
  25. return string;
  26. }
  27. forward FixServerTime();
  28. public FixServerTime()
  29. {
  30. gettime(ghour, gminute, gsecond);
  31. FixHour(ghour);
  32. ghour = shifthour;
  33. new tmphour;
  34. new tmpminute;
  35. new tmpsecond;
  36. gettime(tmphour, tmpminute, tmpsecond);
  37. FixHour(tmphour);
  38. tmphour = shifthour;
  39. new iTempHour = CalculateWorldGameTime(hour, minuite);
  40. SetWorldTime(iTempHour);
  41. print("Adjusted the server time...");
  42. return 1;
  43. }
  44. GetWeekday(display = 0, day = 0, month = 0, year = 0)
  45. {
  46. if(!day) getdate(year, month, day);
  47. new weekday_str[10], j, e;
  48. if(month <= 2)
  49. {
  50. month += 12;
  51. --year;
  52. }
  53. j = year % 100;
  54. e = year / 100;
  55. if(display == 1)
  56. {
  57. switch((day + (month+1)*26/10 + j + j/4 + e/4 - 2*e) % 7)
  58. {
  59. case 0: weekday_str = "sat";
  60. case 1: weekday_str = "sun";
  61. case 2: weekday_str = "mon";
  62. case 3: weekday_str = "tues";
  63. case 4: weekday_str = "wed";
  64. case 5: weekday_str = "thurs";
  65. case 6: weekday_str = "fri";
  66. }
  67. }
  68. else
  69. {
  70. switch((day + (month+1)*26/10 + j + j/4 + e/4 - 2*e) % 7)
  71. {
  72. case 0: weekday_str = "saturday";
  73. case 1: weekday_str = "sunday";
  74. case 2: weekday_str = "monday";
  75. case 3: weekday_str = "tuesday";
  76. case 4: weekday_str = "wednesday";
  77. case 5: weekday_str = "thursday";
  78. case 6: weekday_str = "friday";
  79. }
  80. }
  81. return weekday_str;
  82. }
  83. stock ConvertTimeS(seconds, TYPE = 0)
  84. {
  85. new string[64], minutes;
  86. if(TYPE == 0) {
  87. if(seconds > 86400)
  88. {
  89. if(floatround((seconds/86400), floatround_floor) > 1) format(string, sizeof(string), "%d days", floatround((seconds/86400), floatround_floor));
  90. else format(string, sizeof(string), "%d day", floatround((seconds/86400), floatround_floor));
  91. seconds=seconds-((floatround((seconds/86400), floatround_floor))*86400);
  92. }
  93. if(seconds > 3600)
  94. {
  95. if(strlen(string) > 0) format(string, sizeof(string), "%s, ", string);
  96. if(floatround((seconds/3600), floatround_floor) > 1) format(string, sizeof(string), "%s%d hours", string, floatround((seconds/3600), floatround_floor));
  97. else format(string, sizeof(string), "%s%d hour", string, floatround((seconds/3600), floatround_floor));
  98. seconds=seconds-((floatround((seconds/3600), floatround_floor))*3600);
  99. }
  100. if(seconds > 60)
  101. {
  102. if(strlen(string) > 0) format(string, sizeof(string), "%s, ", string);
  103. if(floatround((seconds/60), floatround_floor) > 1) format(string, sizeof(string), "%s%d minutes", string, floatround((seconds/60), floatround_floor));
  104. else format(string, sizeof(string), "%s%d minute", string, floatround((seconds/60), floatround_floor));
  105. seconds=seconds-((floatround((seconds/60), floatround_floor))*60);
  106. }
  107. if(strlen(string) > 0) format(string, sizeof(string), "%s, ", string);
  108. if(seconds > 1) format(string, sizeof(string), "%s%d seconds", string, seconds);
  109. else if(seconds != 0) format(string, sizeof(string), "%s%d second", string, seconds);
  110. }
  111. else {
  112. if(seconds > 60)
  113. {
  114. minutes = floatround((seconds/60), floatround_floor);
  115. if(minutes > 9) format(string, sizeof(string), "%d", minutes);
  116. else format(string, sizeof(string), "0%d", minutes);
  117. seconds = seconds - (minutes * 60);
  118. }
  119. if(minutes > 0) {
  120. if(seconds > 9) format(string, sizeof(string), "%s:%d", string, seconds);
  121. else format(string, sizeof(string), "%s:0%d", string, seconds);
  122. }
  123. else {
  124. if(seconds > 9) format(string, sizeof(string), "00:%d", seconds);
  125. else format(string, sizeof(string), "00:0%d", seconds);
  126. }
  127. }
  128. return string;
  129. }
  130. stock ConvertTime(cts, &ctm=-1,&cth=-1,&ctd=-1,&ctw=-1,&ctmo=-1,&cty=-1)
  131. {
  132. //Defines to drastically reduce the code..
  133. #define PLUR(%0,%1,%2) (%0),((%0) == 1)?((#%1)):((#%2))
  134. #define CTM_cty 31536000
  135. #define CTM_ctmo 2628000
  136. #define CTM_ctw 604800
  137. #define CTM_ctd 86400
  138. #define CTM_cth 3600
  139. #define CTM_ctm 60
  140. #define CT(%0) %0 = cts / CTM_%0; cts %= CTM_%0
  141. new strii[128];
  142. if(cty != -1)
  143. {
  144. CT(cty); CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
  145. format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(cty,"year","years"),PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
  146. return strii;
  147. }
  148. if(ctmo != -1)
  149. {
  150. CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
  151. format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
  152. return strii;
  153. }
  154. if(ctw != -1)
  155. {
  156. CT(ctw); CT(ctd); CT(cth); CT(ctm);
  157. format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
  158. return strii;
  159. }
  160. if(ctd != -1)
  161. {
  162. CT(ctd); CT(cth); CT(ctm);
  163. format(strii, sizeof(strii), "%d %s, %d %s, %d %s, and %d %s",PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
  164. return strii;
  165. }
  166. if(cth != -1)
  167. {
  168. CT(cth); CT(ctm);
  169. format(strii, sizeof(strii), "%d %s, %d %s, and %d %s",PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
  170. return strii;
  171. }
  172. if(ctm != -1)
  173. {
  174. CT(ctm);
  175. format(strii, sizeof(strii), "%d %s, and %d %s",PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
  176. return strii;
  177. }
  178. format(strii, sizeof(strii), "%d %s", PLUR(cts,"second","seconds"));
  179. return strii;
  180. }
  181. TimeConvert(time) {
  182. new jhour;
  183. new jmin;
  184. new jdiv;
  185. new jsec;
  186. new string[128];
  187. if(time > 3599){
  188. jhour = floatround(time / (60*60));
  189. jdiv = floatround(time % (60*60));
  190. jmin = floatround(jdiv / 60, floatround_floor);
  191. jsec = floatround(jdiv % 60, floatround_ceil);
  192. format(string,sizeof(string),"%02d:%02d:%02d",jhour,jmin,jsec);
  193. }
  194. else if(time > 59 && time < 3600){
  195. jmin = floatround(time/60);
  196. jsec = floatround(time - jmin*60);
  197. format(string,sizeof(string),"%02d:%02d",jmin,jsec);
  198. }
  199. else{
  200. jsec = floatround(time);
  201. format(string,sizeof(string),"%02d seconds",jsec);
  202. }
  203. return string;
  204. }
  205. forward FixHour(hourt);
  206. public FixHour(hourt)
  207. {
  208. hourt = timeshift+hourt;
  209. if (hourt < 0)
  210. {
  211. hourt = hourt+24;
  212. }
  213. else if (hourt > 23)
  214. {
  215. hourt = hourt-24;
  216. }
  217. shifthour = hourt;
  218. return 1;
  219. }
  220. #if defined zombiemode
  221. forward SyncPlayerTime(playerid);
  222. public SyncPlayerTime(playerid)
  223. {
  224. if(zombieevent == 0)
  225. {
  226. new
  227. iTempHour = CalculateWorldGameTime(hour, minuite),
  228. iTempMinute = CalculateGameMinute(minuite, second);
  229. SetPlayerTime(playerid, iTempHour, iTempMinute);
  230. }
  231. else SetPlayerTime(playerid, 0, 0);
  232. return 1;
  233. }
  234. forward SyncMinTime(i);
  235. public SyncMinTime(i)
  236. {
  237. if(GetPlayerVirtualWorld(i) == 133769)
  238. {
  239. SetPlayerWeather(i, 45);
  240. SetPlayerTime(i, 0, 0);
  241. }
  242. else
  243. {
  244. if(zombieevent == 0)
  245. {
  246. new
  247. iTempHour = CalculateWorldGameTime(hour, minuite),
  248. iTempMinute = CalculateGameMinute(minuite, second);
  249. SetPlayerTime(i, iTempHour, iTempMinute);
  250. }
  251. else SetPlayerTime(i, 0, 0);
  252. }
  253. return 1;
  254. }
  255. #else
  256. forward SyncPlayerTime(playerid);
  257. public SyncPlayerTime(playerid)
  258. {
  259. new
  260. iTempHour = CalculateWorldGameTime(hour, minuite),
  261. iTempMinute = CalculateGameMinute(minuite, second);
  262. SetPlayerTime(playerid, iTempHour, iTempMinute);
  263. return 1;
  264. }
  265. forward SyncMinTime(i);
  266. public SyncMinTime(i)
  267. {
  268. new
  269. iTempHour = CalculateWorldGameTime(hour, minuite),
  270. iTempMinute = CalculateGameMinute(minuite, second);
  271. if(GetPlayerVirtualWorld(i) == 133769)
  272. {
  273. SetPlayerWeather(i, 45);
  274. SetPlayerTime(i, 0, 0);
  275. }
  276. else
  277. {
  278. SetPlayerTime(i, iTempHour, iTempMinute);
  279. }
  280. return 1;
  281. }
  282. #endif
  283. stock date( timestamp, _form=0 )
  284. {
  285. /*
  286. date( 1247182451 ) will print >> 09.07.2009-23:34:11
  287. date( 1247182451, 1) will print >> 09/07/2009, 23:34:11
  288. date( 1247182451, 2) will print >> July 09, 2009, 23:34:11
  289. date( 1247182451, 3) will print >> 9 Jul 2009, 23:34
  290. */
  291. new year=1970, day=0, month=0, hourt=0, mins=0, sec=0;
  292. new days_of_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  293. new names_of_month[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
  294. new returnstring[32];
  295. while(timestamp>31622400){
  296. timestamp -= 31536000;
  297. if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp -= 86400;
  298. year++;
  299. }
  300. if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
  301. days_of_month[1] = 29;
  302. else
  303. days_of_month[1] = 28;
  304. while(timestamp>86400){
  305. timestamp -= 86400, day++;
  306. if(day==days_of_month[month]) day=0, month++;
  307. }
  308. while(timestamp>60){
  309. timestamp -= 60, mins++;
  310. if( mins == 60) mins=0, hourt++;
  311. }
  312. sec=timestamp;
  313. switch( _form ){
  314. case 1: format(returnstring, 31, "%02d/%02d/%d %02d:%02d:%02d", day+1, month+1, year, hourt, mins, sec);
  315. case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hourt, mins, sec);
  316. case 3: format(returnstring, 31, "%d %c%c%c %d, %02d:%02d", day+1,names_of_month[month][0],names_of_month[month][1],names_of_month[month][2], year,hourt,mins);
  317. case 4: format(returnstring, 31, "%s %02d, %d", names_of_month[month],day+1,year);
  318. default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hourt, mins, sec);
  319. }
  320. return returnstring;
  321. }
  322. CalculateWorldGameTime(iTempSVHour, iTempSVMinute)
  323. {
  324. // Note that 1 hour in-game time is equivalent to half an hour server-time. By logic this means the clock
  325. // advances 2x as fast as it should.
  326. new iTime = 0;
  327. switch(iTempSVHour)
  328. {
  329. case 0, 12: iTime = 0; // 00:00
  330. case 1, 13: iTime = 2; // 02:00
  331. case 2, 14: iTime = 4; // 04:00
  332. case 3, 15: iTime = 6; // 06:00
  333. case 4, 16: iTime = 8; // 08:00
  334. case 5, 17: iTime = 10; // 10:00
  335. case 6, 18: iTime = 12; // 12:00
  336. case 7, 19: iTime = 14; // 14:00
  337. case 8, 20: iTime = 16; // 16:00
  338. case 9, 21: iTime = 18; // 18:00
  339. case 10, 22: iTime = 20; // 20:00
  340. case 11, 23: iTime = 22;// 22:00
  341. }
  342. if(iTempSVMinute >= 30) iTime +=1;
  343. return iTime;
  344. }
  345. /*CalculateGameMinute(iTempSVSec)
  346. {
  347. new iTime = 0;
  348. if(iTempSVSec == 0) iTime = 0;
  349. else
  350. {
  351. iTime = iTempSVSec/30;
  352. }
  353. return iTime;
  354. }*/
  355. CalculateGameMinute(iMinute, iSecond)
  356. {
  357. new iTime = 0;
  358. if(iMinute == 0 || iMinute == 30) iTime = 0;
  359. else if(iMinute > 30) iTime = (iMinute -30) * 2;
  360. else iTime = iMinute * 2;
  361. if(iSecond >= 30) iTime += 1;
  362. return iTime;
  363. }