php_date.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Derick Rethans <derick@derickrethans.nl> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "php_streams.h"
  18. #include "php_main.h"
  19. #include "php_globals.h"
  20. #include "php_ini.h"
  21. #include "ext/standard/info.h"
  22. #include "ext/standard/php_versioning.h"
  23. #include "ext/standard/php_math.h"
  24. #include "php_date.h"
  25. #include "zend_interfaces.h"
  26. #include "zend_exceptions.h"
  27. #include "lib/timelib.h"
  28. #include "lib/timelib_private.h"
  29. #ifndef PHP_WIN32
  30. #include <time.h>
  31. #else
  32. #include "win32/time.h"
  33. #endif
  34. #ifdef PHP_WIN32
  35. static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
  36. #elif defined(__GNUC__) && __GNUC__ < 3
  37. static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
  38. #else
  39. static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
  40. #endif
  41. #ifdef PHP_WIN32
  42. #define DATE_I64_BUF_LEN 65
  43. # define DATE_I64A(i, s, len) _i64toa_s(i, s, len, 10)
  44. # define DATE_A64I(i, s) i = _atoi64(s)
  45. #else
  46. #define DATE_I64_BUF_LEN 65
  47. # define DATE_I64A(i, s, len) \
  48. do { \
  49. int st = snprintf(s, len, "%lld", i); \
  50. s[st] = '\0'; \
  51. } while (0);
  52. #define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
  53. #endif
  54. PHPAPI time_t php_time(void)
  55. {
  56. #ifdef HAVE_GETTIMEOFDAY
  57. struct timeval tm;
  58. if (UNEXPECTED(gettimeofday(&tm, NULL) != SUCCESS)) {
  59. /* fallback, can't reasonably happen */
  60. return time(NULL);
  61. }
  62. return tm.tv_sec;
  63. #else
  64. return time(NULL);
  65. #endif
  66. }
  67. #include "php_date_arginfo.h"
  68. static char* guess_timezone(const timelib_tzdb *tzdb);
  69. static void date_register_classes(void);
  70. /* }}} */
  71. ZEND_DECLARE_MODULE_GLOBALS(date)
  72. static PHP_GINIT_FUNCTION(date);
  73. /* True global */
  74. timelib_tzdb *php_date_global_timezone_db;
  75. int php_date_global_timezone_db_enabled;
  76. #define DATE_DEFAULT_LATITUDE "31.7667"
  77. #define DATE_DEFAULT_LONGITUDE "35.2333"
  78. /* on 90'50; common sunset declaration (start of sun body appear) */
  79. #define DATE_SUNSET_ZENITH "90.833333"
  80. /* on 90'50; common sunrise declaration (sun body disappeared) */
  81. #define DATE_SUNRISE_ZENITH "90.833333"
  82. static PHP_INI_MH(OnUpdate_date_timezone);
  83. /* {{{ INI Settings */
  84. PHP_INI_BEGIN()
  85. STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
  86. PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL)
  87. PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL)
  88. PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL)
  89. PHP_INI_ENTRY("date.sunrise_zenith", DATE_SUNRISE_ZENITH, PHP_INI_ALL, NULL)
  90. PHP_INI_END()
  91. /* }}} */
  92. zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
  93. zend_class_entry *date_ce_immutable, *date_ce_interface;
  94. PHPAPI zend_class_entry *php_date_get_date_ce(void)
  95. {
  96. return date_ce_date;
  97. }
  98. PHPAPI zend_class_entry *php_date_get_immutable_ce(void)
  99. {
  100. return date_ce_immutable;
  101. }
  102. PHPAPI zend_class_entry *php_date_get_interface_ce(void)
  103. {
  104. return date_ce_interface;
  105. }
  106. PHPAPI zend_class_entry *php_date_get_timezone_ce(void)
  107. {
  108. return date_ce_timezone;
  109. }
  110. PHPAPI zend_class_entry *php_date_get_interval_ce(void)
  111. {
  112. return date_ce_interval;
  113. }
  114. PHPAPI zend_class_entry *php_date_get_period_ce(void)
  115. {
  116. return date_ce_period;
  117. }
  118. static zend_object_handlers date_object_handlers_date;
  119. static zend_object_handlers date_object_handlers_immutable;
  120. static zend_object_handlers date_object_handlers_timezone;
  121. static zend_object_handlers date_object_handlers_interval;
  122. static zend_object_handlers date_object_handlers_period;
  123. #define DATE_CHECK_INITIALIZED(member, class_name) \
  124. if (!(member)) { \
  125. zend_throw_error(NULL, "The " #class_name " object has not been correctly initialized by its constructor"); \
  126. RETURN_THROWS(); \
  127. }
  128. static void date_object_free_storage_date(zend_object *object);
  129. static void date_object_free_storage_timezone(zend_object *object);
  130. static void date_object_free_storage_interval(zend_object *object);
  131. static void date_object_free_storage_period(zend_object *object);
  132. static zend_object *date_object_new_date(zend_class_entry *class_type);
  133. static zend_object *date_object_new_timezone(zend_class_entry *class_type);
  134. static zend_object *date_object_new_interval(zend_class_entry *class_type);
  135. static zend_object *date_object_new_period(zend_class_entry *class_type);
  136. static zend_object *date_object_clone_date(zend_object *this_ptr);
  137. static zend_object *date_object_clone_timezone(zend_object *this_ptr);
  138. static zend_object *date_object_clone_interval(zend_object *this_ptr);
  139. static zend_object *date_object_clone_period(zend_object *this_ptr);
  140. static int date_object_compare_date(zval *d1, zval *d2);
  141. static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n);
  142. static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose);
  143. static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n);
  144. static HashTable *date_object_get_properties_interval(zend_object *object);
  145. static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n);
  146. static HashTable *date_object_get_properties_period(zend_object *object);
  147. static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose);
  148. static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n);
  149. static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp);
  150. static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv);
  151. static int date_interval_compare_objects(zval *o1, zval *o2);
  152. static zval *date_interval_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
  153. static zval *date_interval_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot);
  154. static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot);
  155. static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv);
  156. static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot);
  157. static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot);
  158. static int date_object_compare_timezone(zval *tz1, zval *tz2);
  159. /* {{{ Module struct */
  160. zend_module_entry date_module_entry = {
  161. STANDARD_MODULE_HEADER_EX,
  162. NULL,
  163. NULL,
  164. "date", /* extension name */
  165. ext_functions, /* function list */
  166. PHP_MINIT(date), /* process startup */
  167. PHP_MSHUTDOWN(date), /* process shutdown */
  168. PHP_RINIT(date), /* request startup */
  169. PHP_RSHUTDOWN(date), /* request shutdown */
  170. PHP_MINFO(date), /* extension info */
  171. PHP_DATE_VERSION, /* extension version */
  172. PHP_MODULE_GLOBALS(date), /* globals descriptor */
  173. PHP_GINIT(date), /* globals ctor */
  174. NULL, /* globals dtor */
  175. ZEND_MODULE_POST_ZEND_DEACTIVATE_N(date), /* post deactivate */
  176. STANDARD_MODULE_PROPERTIES_EX
  177. };
  178. /* }}} */
  179. /* {{{ PHP_GINIT_FUNCTION */
  180. static PHP_GINIT_FUNCTION(date)
  181. {
  182. date_globals->default_timezone = NULL;
  183. date_globals->timezone = NULL;
  184. date_globals->tzcache = NULL;
  185. date_globals->timezone_valid = 0;
  186. }
  187. /* }}} */
  188. static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
  189. {
  190. timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
  191. timelib_tzinfo_dtor(tzi);
  192. } /* }}} */
  193. /* {{{ PHP_RINIT_FUNCTION */
  194. PHP_RINIT_FUNCTION(date)
  195. {
  196. if (DATEG(timezone)) {
  197. efree(DATEG(timezone));
  198. }
  199. DATEG(timezone) = NULL;
  200. DATEG(tzcache) = NULL;
  201. DATEG(last_errors) = NULL;
  202. return SUCCESS;
  203. }
  204. /* }}} */
  205. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  206. PHP_RSHUTDOWN_FUNCTION(date)
  207. {
  208. if (DATEG(timezone)) {
  209. efree(DATEG(timezone));
  210. }
  211. DATEG(timezone) = NULL;
  212. return SUCCESS;
  213. }
  214. /* }}} */
  215. ZEND_MODULE_POST_ZEND_DEACTIVATE_D(date)
  216. {
  217. if (DATEG(tzcache)) {
  218. zend_hash_destroy(DATEG(tzcache));
  219. FREE_HASHTABLE(DATEG(tzcache));
  220. DATEG(tzcache) = NULL;
  221. }
  222. if (DATEG(last_errors)) {
  223. timelib_error_container_dtor(DATEG(last_errors));
  224. DATEG(last_errors) = NULL;
  225. }
  226. return SUCCESS;
  227. }
  228. #define DATE_TIMEZONEDB php_date_global_timezone_db ? php_date_global_timezone_db : timelib_builtin_db()
  229. /*
  230. * RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
  231. * date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
  232. * day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
  233. * date = 1*2DIGIT month 2DIGIT ; day month year e.g. 20 Jun 82
  234. * month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
  235. * time = hour zone ; ANSI and Military
  236. * hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
  237. * zone = "UT" / "GMT" / "EST" / "EDT" / "CST" / "CDT" / "MST" / "MDT" / "PST" / "PDT" / 1ALPHA / ( ("+" / "-") 4DIGIT )
  238. */
  239. #define DATE_FORMAT_RFC822 "D, d M y H:i:s O"
  240. /*
  241. * RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
  242. * Format must be acceptable both to the ARPANET and to the getdate routine.
  243. * One format that is acceptable to both is Weekday, DD-Mon-YY HH:MM:SS TIMEZONE
  244. * TIMEZONE can be any timezone name (3 or more letters)
  245. */
  246. #define DATE_FORMAT_RFC850 "l, d-M-y H:i:s T"
  247. /*
  248. * RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
  249. * Its format must be acceptable both in RFC-822 and to the getdate(3)
  250. * Wdy, DD Mon YY HH:MM:SS TIMEZONE
  251. * There is no hope of having a complete list of timezones. Universal
  252. * Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
  253. * CDT, EST, EDT) and the +/-hhmm offset specified in RFC-822 should be supported.
  254. */
  255. #define DATE_FORMAT_RFC1036 "D, d M y H:i:s O"
  256. /*
  257. * RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
  258. * RFC-822 Date and Time Specification: RFC-822 Section 5
  259. * The syntax for the date is hereby changed to: date = 1*2DIGIT month 2*4DIGIT
  260. */
  261. #define DATE_FORMAT_RFC1123 "D, d M Y H:i:s O"
  262. /*
  263. * RFC7231, Section 7.1.1: http://tools.ietf.org/html/rfc7231
  264. */
  265. #define DATE_FORMAT_RFC7231 "D, d M Y H:i:s \\G\\M\\T"
  266. /*
  267. * RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
  268. * FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
  269. * CFWS = *([FWS] comment) (([FWS] comment) / FWS)
  270. *
  271. * date-time = [ day-of-week "," ] date FWS time [CFWS]
  272. * day-of-week = ([FWS] day-name)
  273. * day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
  274. * date = day month year
  275. * year = 4*DIGIT
  276. * month = (FWS month-name FWS)
  277. * month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
  278. * day = ([FWS] 1*2DIGIT)
  279. * time = time-of-day FWS zone
  280. * time-of-day = hour ":" minute [ ":" second ]
  281. * hour = 2DIGIT
  282. * minute = 2DIGIT
  283. * second = 2DIGIT
  284. * zone = (( "+" / "-" ) 4DIGIT)
  285. */
  286. #define DATE_FORMAT_RFC2822 "D, d M Y H:i:s O"
  287. /*
  288. * RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
  289. * date-fullyear = 4DIGIT
  290. * date-month = 2DIGIT ; 01-12
  291. * date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
  292. *
  293. * time-hour = 2DIGIT ; 00-23
  294. * time-minute = 2DIGIT ; 00-59
  295. * time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
  296. *
  297. * time-secfrac = "." 1*DIGIT
  298. * time-numoffset = ("+" / "-") time-hour ":" time-minute
  299. * time-offset = "Z" / time-numoffset
  300. *
  301. * partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
  302. * full-date = date-fullyear "-" date-month "-" date-mday
  303. * full-time = partial-time time-offset
  304. *
  305. * date-time = full-date "T" full-time
  306. */
  307. #define DATE_FORMAT_RFC3339 "Y-m-d\\TH:i:sP"
  308. #define DATE_FORMAT_ISO8601 "Y-m-d\\TH:i:sO"
  309. /*
  310. * RFC3339, Appendix A: http://www.ietf.org/rfc/rfc3339.txt
  311. * ISO 8601 also requires (in section 5.3.1.3) that a decimal fraction
  312. * be proceeded by a "0" if less than unity. Annex B.2 of ISO 8601
  313. * gives examples where the decimal fractions are not preceded by a "0".
  314. * This grammar assumes section 5.3.1.3 is correct and that Annex B.2 is
  315. * in error.
  316. */
  317. #define DATE_FORMAT_RFC3339_EXTENDED "Y-m-d\\TH:i:s.vP"
  318. /*
  319. * This comes from various sources that like to contradict. I'm going with the
  320. * format here because of:
  321. * http://msdn.microsoft.com/en-us/library/windows/desktop/aa384321%28v=vs.85%29.aspx
  322. * and http://curl.haxx.se/rfc/cookie_spec.html
  323. */
  324. #define DATE_FORMAT_COOKIE "l, d-M-Y H:i:s T"
  325. #define SUNFUNCS_RET_TIMESTAMP 0
  326. #define SUNFUNCS_RET_STRING 1
  327. #define SUNFUNCS_RET_DOUBLE 2
  328. /* {{{ PHP_MINIT_FUNCTION */
  329. PHP_MINIT_FUNCTION(date)
  330. {
  331. REGISTER_INI_ENTRIES();
  332. date_register_classes();
  333. /*
  334. * RFC4287, Section 3.3: http://www.ietf.org/rfc/rfc4287.txt
  335. * A Date construct is an element whose content MUST conform to the
  336. * "date-time" production in [RFC3339]. In addition, an uppercase "T"
  337. * character MUST be used to separate date and time, and an uppercase
  338. * "Z" character MUST be present in the absence of a numeric time zone offset.
  339. */
  340. REGISTER_STRING_CONSTANT("DATE_ATOM", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
  341. /*
  342. * Preliminary specification: http://wp.netscape.com/newsref/std/cookie_spec.html
  343. * "This is based on RFC 822, RFC 850, RFC 1036, and RFC 1123,
  344. * with the variations that the only legal time zone is GMT
  345. * and the separators between the elements of the date must be dashes."
  346. */
  347. REGISTER_STRING_CONSTANT("DATE_COOKIE", DATE_FORMAT_COOKIE, CONST_CS | CONST_PERSISTENT);
  348. REGISTER_STRING_CONSTANT("DATE_ISO8601", DATE_FORMAT_ISO8601, CONST_CS | CONST_PERSISTENT);
  349. REGISTER_STRING_CONSTANT("DATE_RFC822", DATE_FORMAT_RFC822, CONST_CS | CONST_PERSISTENT);
  350. REGISTER_STRING_CONSTANT("DATE_RFC850", DATE_FORMAT_RFC850, CONST_CS | CONST_PERSISTENT);
  351. REGISTER_STRING_CONSTANT("DATE_RFC1036", DATE_FORMAT_RFC1036, CONST_CS | CONST_PERSISTENT);
  352. REGISTER_STRING_CONSTANT("DATE_RFC1123", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
  353. REGISTER_STRING_CONSTANT("DATE_RFC7231", DATE_FORMAT_RFC7231, CONST_CS | CONST_PERSISTENT);
  354. REGISTER_STRING_CONSTANT("DATE_RFC2822", DATE_FORMAT_RFC2822, CONST_CS | CONST_PERSISTENT);
  355. REGISTER_STRING_CONSTANT("DATE_RFC3339", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
  356. REGISTER_STRING_CONSTANT("DATE_RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED, CONST_CS | CONST_PERSISTENT);
  357. /*
  358. * RSS 2.0 Specification: http://blogs.law.harvard.edu/tech/rss
  359. * "All date-times in RSS conform to the Date and Time Specification of RFC 822,
  360. * with the exception that the year may be expressed with two characters or four characters (four preferred)"
  361. */
  362. REGISTER_STRING_CONSTANT("DATE_RSS", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
  363. REGISTER_STRING_CONSTANT("DATE_W3C", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
  364. REGISTER_LONG_CONSTANT("SUNFUNCS_RET_TIMESTAMP", SUNFUNCS_RET_TIMESTAMP, CONST_CS | CONST_PERSISTENT);
  365. REGISTER_LONG_CONSTANT("SUNFUNCS_RET_STRING", SUNFUNCS_RET_STRING, CONST_CS | CONST_PERSISTENT);
  366. REGISTER_LONG_CONSTANT("SUNFUNCS_RET_DOUBLE", SUNFUNCS_RET_DOUBLE, CONST_CS | CONST_PERSISTENT);
  367. php_date_global_timezone_db = NULL;
  368. php_date_global_timezone_db_enabled = 0;
  369. DATEG(last_errors) = NULL;
  370. return SUCCESS;
  371. }
  372. /* }}} */
  373. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  374. PHP_MSHUTDOWN_FUNCTION(date)
  375. {
  376. UNREGISTER_INI_ENTRIES();
  377. if (DATEG(last_errors)) {
  378. timelib_error_container_dtor(DATEG(last_errors));
  379. }
  380. #ifndef ZTS
  381. DATEG(default_timezone) = NULL;
  382. #endif
  383. return SUCCESS;
  384. }
  385. /* }}} */
  386. /* {{{ PHP_MINFO_FUNCTION */
  387. PHP_MINFO_FUNCTION(date)
  388. {
  389. const timelib_tzdb *tzdb = DATE_TIMEZONEDB;
  390. php_info_print_table_start();
  391. php_info_print_table_row(2, "date/time support", "enabled");
  392. php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
  393. php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
  394. php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
  395. php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
  396. php_info_print_table_end();
  397. DISPLAY_INI_ENTRIES();
  398. }
  399. /* }}} */
  400. /* {{{ Timezone Cache functions */
  401. static timelib_tzinfo *php_date_parse_tzfile(const char *formal_tzname, const timelib_tzdb *tzdb)
  402. {
  403. timelib_tzinfo *tzi;
  404. int dummy_error_code;
  405. if(!DATEG(tzcache)) {
  406. ALLOC_HASHTABLE(DATEG(tzcache));
  407. zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
  408. }
  409. if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
  410. return tzi;
  411. }
  412. tzi = timelib_parse_tzfile(formal_tzname, tzdb, &dummy_error_code);
  413. if (tzi) {
  414. zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
  415. }
  416. return tzi;
  417. }
  418. timelib_tzinfo *php_date_parse_tzfile_wrapper(const char *formal_tzname, const timelib_tzdb *tzdb, int *dummy_error_code)
  419. {
  420. return php_date_parse_tzfile(formal_tzname, tzdb);
  421. }
  422. /* }}} */
  423. /* Callback to check the date.timezone only when changed increases performance */
  424. /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
  425. static PHP_INI_MH(OnUpdate_date_timezone)
  426. {
  427. if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
  428. return FAILURE;
  429. }
  430. DATEG(timezone_valid) = 0;
  431. if (stage == PHP_INI_STAGE_RUNTIME) {
  432. if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
  433. if (DATEG(default_timezone) && *DATEG(default_timezone)) {
  434. php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
  435. }
  436. } else {
  437. DATEG(timezone_valid) = 1;
  438. }
  439. }
  440. return SUCCESS;
  441. }
  442. /* }}} */
  443. /* {{{ Helper functions */
  444. static char* guess_timezone(const timelib_tzdb *tzdb)
  445. {
  446. /* Checking configure timezone */
  447. if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
  448. return DATEG(timezone);
  449. }
  450. /* Check config setting for default timezone */
  451. if (!DATEG(default_timezone)) {
  452. /* Special case: ext/date wasn't initialized yet */
  453. zval *ztz;
  454. if (NULL != (ztz = cfg_get_entry("date.timezone", sizeof("date.timezone")))
  455. && Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
  456. return Z_STRVAL_P(ztz);
  457. }
  458. } else if (*DATEG(default_timezone)) {
  459. if (DATEG(timezone_valid) == 1) {
  460. return DATEG(default_timezone);
  461. }
  462. if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
  463. php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
  464. return "UTC";
  465. }
  466. DATEG(timezone_valid) = 1;
  467. return DATEG(default_timezone);
  468. }
  469. /* Fallback to UTC */
  470. return "UTC";
  471. }
  472. PHPAPI timelib_tzinfo *get_timezone_info(void)
  473. {
  474. char *tz;
  475. timelib_tzinfo *tzi;
  476. tz = guess_timezone(DATE_TIMEZONEDB);
  477. tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
  478. if (! tzi) {
  479. zend_throw_error(NULL, "Timezone database is corrupt. Please file a bug report as this should never happen");
  480. }
  481. return tzi;
  482. }
  483. /* }}} */
  484. /* {{{ date() and gmdate() data */
  485. #include "zend_smart_str.h"
  486. static const char * const mon_full_names[] = {
  487. "January", "February", "March", "April",
  488. "May", "June", "July", "August",
  489. "September", "October", "November", "December"
  490. };
  491. static const char * const mon_short_names[] = {
  492. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  493. };
  494. static const char * const day_full_names[] = {
  495. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  496. };
  497. static const char * const day_short_names[] = {
  498. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  499. };
  500. static char *english_suffix(timelib_sll number)
  501. {
  502. if (number >= 10 && number <= 19) {
  503. return "th";
  504. } else {
  505. switch (number % 10) {
  506. case 1: return "st";
  507. case 2: return "nd";
  508. case 3: return "rd";
  509. }
  510. }
  511. return "th";
  512. }
  513. /* }}} */
  514. /* {{{ day of week helpers */
  515. static const char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
  516. {
  517. timelib_sll day_of_week = timelib_day_of_week(y, m, d);
  518. if (day_of_week < 0) {
  519. return "Unknown";
  520. }
  521. return day_full_names[day_of_week];
  522. }
  523. static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
  524. {
  525. timelib_sll day_of_week = timelib_day_of_week(y, m, d);
  526. if (day_of_week < 0) {
  527. return "Unknown";
  528. }
  529. return day_short_names[day_of_week];
  530. }
  531. /* }}} */
  532. /* {{{ date_format - (gm)date helper */
  533. static zend_string *date_format(const char *format, size_t format_len, timelib_time *t, int localtime)
  534. {
  535. smart_str string = {0};
  536. size_t i;
  537. int length = 0;
  538. char buffer[97];
  539. timelib_time_offset *offset = NULL;
  540. timelib_sll isoweek, isoyear;
  541. int rfc_colon;
  542. int weekYearSet = 0;
  543. if (!format_len) {
  544. return ZSTR_EMPTY_ALLOC();
  545. }
  546. if (localtime) {
  547. if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
  548. offset = timelib_time_offset_ctor();
  549. offset->offset = (t->z + (t->dst * 3600));
  550. offset->leap_secs = 0;
  551. offset->is_dst = t->dst;
  552. offset->abbr = timelib_strdup(t->tz_abbr);
  553. } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
  554. offset = timelib_time_offset_ctor();
  555. offset->offset = (t->z);
  556. offset->leap_secs = 0;
  557. offset->is_dst = 0;
  558. offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
  559. snprintf(offset->abbr, 9, "GMT%c%02d%02d",
  560. (offset->offset < 0) ? '-' : '+',
  561. abs(offset->offset / 3600),
  562. abs((offset->offset % 3600) / 60));
  563. } else {
  564. offset = timelib_get_time_zone_info(t->sse, t->tz_info);
  565. }
  566. }
  567. for (i = 0; i < format_len; i++) {
  568. rfc_colon = 0;
  569. switch (format[i]) {
  570. /* day */
  571. case 'd': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
  572. case 'D': length = slprintf(buffer, sizeof(buffer), "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
  573. case 'j': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
  574. case 'l': length = slprintf(buffer, sizeof(buffer), "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
  575. case 'S': length = slprintf(buffer, sizeof(buffer), "%s", english_suffix(t->d)); break;
  576. case 'w': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
  577. case 'N': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
  578. case 'z': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
  579. /* week */
  580. case 'W':
  581. if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
  582. length = slprintf(buffer, sizeof(buffer), "%02d", (int) isoweek); break; /* iso weeknr */
  583. case 'o':
  584. if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
  585. length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) isoyear); break; /* iso year */
  586. /* month */
  587. case 'F': length = slprintf(buffer, sizeof(buffer), "%s", mon_full_names[t->m - 1]); break;
  588. case 'm': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
  589. case 'M': length = slprintf(buffer, sizeof(buffer), "%s", mon_short_names[t->m - 1]); break;
  590. case 'n': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
  591. case 't': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_days_in_month(t->y, t->m)); break;
  592. /* year */
  593. case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
  594. case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) (t->y % 100)); break;
  595. case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
  596. /* time */
  597. case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;
  598. case 'A': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "PM" : "AM"); break;
  599. case 'B': {
  600. int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
  601. if (retval < 0) {
  602. retval += 864000;
  603. }
  604. /* Make sure to do this on a positive int to avoid rounding errors */
  605. retval = (retval / 864) % 1000;
  606. length = slprintf(buffer, sizeof(buffer), "%03d", retval);
  607. break;
  608. }
  609. case 'g': length = slprintf(buffer, sizeof(buffer), "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
  610. case 'G': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
  611. case 'h': length = slprintf(buffer, sizeof(buffer), "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
  612. case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
  613. case 'i': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
  614. case 's': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->s); break;
  615. case 'u': length = slprintf(buffer, sizeof(buffer), "%06d", (int) floor(t->us)); break;
  616. case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;
  617. /* timezone */
  618. case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
  619. case 'p':
  620. if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0) {
  621. length = slprintf(buffer, sizeof(buffer), "%s", "Z");
  622. break;
  623. }
  624. ZEND_FALLTHROUGH;
  625. case 'P': rfc_colon = 1; ZEND_FALLTHROUGH;
  626. case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
  627. localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
  628. localtime ? abs(offset->offset / 3600) : 0,
  629. rfc_colon ? ":" : "",
  630. localtime ? abs((offset->offset % 3600) / 60) : 0
  631. );
  632. break;
  633. case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
  634. case 'e': if (!localtime) {
  635. length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
  636. } else {
  637. switch (t->zone_type) {
  638. case TIMELIB_ZONETYPE_ID:
  639. length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
  640. break;
  641. case TIMELIB_ZONETYPE_ABBR:
  642. length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
  643. break;
  644. case TIMELIB_ZONETYPE_OFFSET:
  645. length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
  646. ((offset->offset < 0) ? '-' : '+'),
  647. abs(offset->offset / 3600),
  648. abs((offset->offset % 3600) / 60)
  649. );
  650. break;
  651. }
  652. }
  653. break;
  654. case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
  655. /* full date/time */
  656. case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
  657. (zend_long) t->y, (int) t->m, (int) t->d,
  658. (int) t->h, (int) t->i, (int) t->s,
  659. localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
  660. localtime ? abs(offset->offset / 3600) : 0,
  661. localtime ? abs((offset->offset % 3600) / 60) : 0
  662. );
  663. break;
  664. case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
  665. php_date_short_day_name(t->y, t->m, t->d),
  666. (int) t->d, mon_short_names[t->m - 1],
  667. (zend_long) t->y, (int) t->h, (int) t->i, (int) t->s,
  668. localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
  669. localtime ? abs(offset->offset / 3600) : 0,
  670. localtime ? abs((offset->offset % 3600) / 60) : 0
  671. );
  672. break;
  673. case 'U': length = slprintf(buffer, sizeof(buffer), "%lld", (timelib_sll) t->sse); break;
  674. case '\\': if (i < format_len) i++; ZEND_FALLTHROUGH;
  675. default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
  676. }
  677. smart_str_appendl(&string, buffer, length);
  678. }
  679. smart_str_0(&string);
  680. if (localtime) {
  681. timelib_time_offset_dtor(offset);
  682. }
  683. return string.s;
  684. }
  685. static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
  686. {
  687. zend_string *format;
  688. zend_long ts;
  689. bool ts_is_null = 1;
  690. ZEND_PARSE_PARAMETERS_START(1, 2)
  691. Z_PARAM_STR(format)
  692. Z_PARAM_OPTIONAL
  693. Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
  694. ZEND_PARSE_PARAMETERS_END();
  695. if (ts_is_null) {
  696. ts = php_time();
  697. }
  698. RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
  699. }
  700. /* }}} */
  701. PHPAPI zend_string *php_format_date(const char *format, size_t format_len, time_t ts, int localtime) /* {{{ */
  702. {
  703. timelib_time *t;
  704. timelib_tzinfo *tzi;
  705. zend_string *string;
  706. t = timelib_time_ctor();
  707. if (localtime) {
  708. tzi = get_timezone_info();
  709. t->tz_info = tzi;
  710. t->zone_type = TIMELIB_ZONETYPE_ID;
  711. timelib_unixtime2local(t, ts);
  712. } else {
  713. tzi = NULL;
  714. timelib_unixtime2gmt(t, ts);
  715. }
  716. string = date_format(format, format_len, t, localtime);
  717. timelib_time_dtor(t);
  718. return string;
  719. }
  720. /* }}} */
  721. /* {{{ php_idate */
  722. PHPAPI int php_idate(char format, time_t ts, int localtime)
  723. {
  724. timelib_time *t;
  725. timelib_tzinfo *tzi;
  726. int retval = -1;
  727. timelib_time_offset *offset = NULL;
  728. timelib_sll isoweek, isoyear;
  729. t = timelib_time_ctor();
  730. if (!localtime) {
  731. tzi = get_timezone_info();
  732. t->tz_info = tzi;
  733. t->zone_type = TIMELIB_ZONETYPE_ID;
  734. timelib_unixtime2local(t, ts);
  735. } else {
  736. tzi = NULL;
  737. timelib_unixtime2gmt(t, ts);
  738. }
  739. if (!localtime) {
  740. if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
  741. offset = timelib_time_offset_ctor();
  742. offset->offset = (t->z + (t->dst * 3600));
  743. offset->leap_secs = 0;
  744. offset->is_dst = t->dst;
  745. offset->abbr = timelib_strdup(t->tz_abbr);
  746. } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
  747. offset = timelib_time_offset_ctor();
  748. offset->offset = (t->z + (t->dst * 3600));
  749. offset->leap_secs = 0;
  750. offset->is_dst = t->dst;
  751. offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
  752. snprintf(offset->abbr, 9, "GMT%c%02d%02d",
  753. (offset->offset < 0) ? '-' : '+',
  754. abs(offset->offset / 3600),
  755. abs((offset->offset % 3600) / 60));
  756. } else {
  757. offset = timelib_get_time_zone_info(t->sse, t->tz_info);
  758. }
  759. }
  760. timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
  761. switch (format) {
  762. /* day */
  763. case 'd': case 'j': retval = (int) t->d; break;
  764. case 'w': retval = (int) timelib_day_of_week(t->y, t->m, t->d); break;
  765. case 'z': retval = (int) timelib_day_of_year(t->y, t->m, t->d); break;
  766. /* week */
  767. case 'W': retval = (int) isoweek; break; /* iso weeknr */
  768. /* month */
  769. case 'm': case 'n': retval = (int) t->m; break;
  770. case 't': retval = (int) timelib_days_in_month(t->y, t->m); break;
  771. /* year */
  772. case 'L': retval = (int) timelib_is_leap((int) t->y); break;
  773. case 'y': retval = (int) (t->y % 100); break;
  774. case 'Y': retval = (int) t->y; break;
  775. /* Swatch Beat a.k.a. Internet Time */
  776. case 'B':
  777. retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
  778. if (retval < 0) {
  779. retval += 864000;
  780. }
  781. /* Make sure to do this on a positive int to avoid rounding errors */
  782. retval = (retval / 864) % 1000;
  783. break;
  784. /* time */
  785. case 'g': case 'h': retval = (int) ((t->h % 12) ? (int) t->h % 12 : 12); break;
  786. case 'H': case 'G': retval = (int) t->h; break;
  787. case 'i': retval = (int) t->i; break;
  788. case 's': retval = (int) t->s; break;
  789. /* timezone */
  790. case 'I': retval = (int) (!localtime ? offset->is_dst : 0); break;
  791. case 'Z': retval = (int) (!localtime ? offset->offset : 0); break;
  792. case 'U': retval = (int) t->sse; break;
  793. }
  794. if (!localtime) {
  795. timelib_time_offset_dtor(offset);
  796. }
  797. timelib_time_dtor(t);
  798. return retval;
  799. }
  800. /* }}} */
  801. /* {{{ Format a local date/time */
  802. PHP_FUNCTION(date)
  803. {
  804. php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  805. }
  806. /* }}} */
  807. /* {{{ Format a GMT date/time */
  808. PHP_FUNCTION(gmdate)
  809. {
  810. php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  811. }
  812. /* }}} */
  813. /* {{{ Format a local time/date as integer */
  814. PHP_FUNCTION(idate)
  815. {
  816. zend_string *format;
  817. zend_long ts;
  818. bool ts_is_null = 1;
  819. int ret;
  820. ZEND_PARSE_PARAMETERS_START(1, 2)
  821. Z_PARAM_STR(format)
  822. Z_PARAM_OPTIONAL
  823. Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
  824. ZEND_PARSE_PARAMETERS_END();
  825. if (ZSTR_LEN(format) != 1) {
  826. php_error_docref(NULL, E_WARNING, "idate format is one char");
  827. RETURN_FALSE;
  828. }
  829. if (ts_is_null) {
  830. ts = php_time();
  831. }
  832. ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
  833. if (ret == -1) {
  834. php_error_docref(NULL, E_WARNING, "Unrecognized date format token");
  835. RETURN_FALSE;
  836. }
  837. RETURN_LONG(ret);
  838. }
  839. /* }}} */
  840. /* {{{ php_date_set_tzdb - NOT THREADSAFE */
  841. PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb)
  842. {
  843. const timelib_tzdb *builtin = timelib_builtin_db();
  844. if (php_version_compare(tzdb->version, builtin->version) > 0) {
  845. php_date_global_timezone_db = tzdb;
  846. php_date_global_timezone_db_enabled = 1;
  847. }
  848. }
  849. /* }}} */
  850. /* {{{ php_parse_date: Backwards compatibility function */
  851. PHPAPI zend_long php_parse_date(const char *string, zend_long *now)
  852. {
  853. timelib_time *parsed_time;
  854. timelib_error_container *error = NULL;
  855. int error2;
  856. zend_long retval;
  857. parsed_time = timelib_strtotime(string, strlen(string), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  858. if (error->error_count) {
  859. timelib_time_dtor(parsed_time);
  860. timelib_error_container_dtor(error);
  861. return -1;
  862. }
  863. timelib_error_container_dtor(error);
  864. timelib_update_ts(parsed_time, NULL);
  865. retval = timelib_date_to_int(parsed_time, &error2);
  866. timelib_time_dtor(parsed_time);
  867. if (error2) {
  868. return -1;
  869. }
  870. return retval;
  871. }
  872. /* }}} */
  873. /* {{{ Convert string representation of date and time to a timestamp */
  874. PHP_FUNCTION(strtotime)
  875. {
  876. zend_string *times;
  877. int parse_error, epoch_does_not_fit_in_zend_long;
  878. timelib_error_container *error;
  879. zend_long preset_ts, ts;
  880. bool preset_ts_is_null = 1;
  881. timelib_time *t, *now;
  882. timelib_tzinfo *tzi;
  883. ZEND_PARSE_PARAMETERS_START(1, 2)
  884. Z_PARAM_STR(times)
  885. Z_PARAM_OPTIONAL
  886. Z_PARAM_LONG_OR_NULL(preset_ts, preset_ts_is_null)
  887. ZEND_PARSE_PARAMETERS_END();
  888. /* timelib_strtotime() expects the string to not be empty */
  889. if (ZSTR_LEN(times) == 0) {
  890. RETURN_FALSE;
  891. }
  892. tzi = get_timezone_info();
  893. if (!tzi) {
  894. return;
  895. }
  896. now = timelib_time_ctor();
  897. now->tz_info = tzi;
  898. now->zone_type = TIMELIB_ZONETYPE_ID;
  899. timelib_unixtime2local(now,
  900. !preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) php_time());
  901. t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
  902. DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  903. parse_error = error->error_count;
  904. timelib_error_container_dtor(error);
  905. if (parse_error) {
  906. timelib_time_dtor(now);
  907. timelib_time_dtor(t);
  908. RETURN_FALSE;
  909. }
  910. timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
  911. timelib_update_ts(t, tzi);
  912. ts = timelib_date_to_int(t, &epoch_does_not_fit_in_zend_long);
  913. timelib_time_dtor(now);
  914. timelib_time_dtor(t);
  915. if (epoch_does_not_fit_in_zend_long) {
  916. php_error_docref(NULL, E_WARNING, "Epoch doesn't fit in a PHP integer");
  917. RETURN_FALSE;
  918. }
  919. RETURN_LONG(ts);
  920. }
  921. /* }}} */
  922. /* {{{ php_mktime - (gm)mktime helper */
  923. PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
  924. {
  925. zend_long hou, min, sec, mon, day, yea;
  926. bool min_is_null = 1, sec_is_null = 1, mon_is_null = 1, day_is_null = 1, yea_is_null = 1;
  927. timelib_time *now;
  928. timelib_tzinfo *tzi = NULL;
  929. zend_long ts, adjust_seconds = 0;
  930. int epoch_does_not_fit_in_zend_long;
  931. ZEND_PARSE_PARAMETERS_START(1, 6)
  932. Z_PARAM_LONG(hou)
  933. Z_PARAM_OPTIONAL
  934. Z_PARAM_LONG_OR_NULL(min, min_is_null)
  935. Z_PARAM_LONG_OR_NULL(sec, sec_is_null)
  936. Z_PARAM_LONG_OR_NULL(mon, mon_is_null)
  937. Z_PARAM_LONG_OR_NULL(day, day_is_null)
  938. Z_PARAM_LONG_OR_NULL(yea, yea_is_null)
  939. ZEND_PARSE_PARAMETERS_END();
  940. /* Initialize structure with current time */
  941. now = timelib_time_ctor();
  942. if (gmt) {
  943. timelib_unixtime2gmt(now, (timelib_sll) php_time());
  944. } else {
  945. tzi = get_timezone_info();
  946. if (!tzi) {
  947. return;
  948. }
  949. now->tz_info = tzi;
  950. now->zone_type = TIMELIB_ZONETYPE_ID;
  951. timelib_unixtime2local(now, (timelib_sll) php_time());
  952. }
  953. now->h = hou;
  954. if (!min_is_null) {
  955. now->i = min;
  956. }
  957. if (!sec_is_null) {
  958. now->s = sec;
  959. }
  960. if (!mon_is_null) {
  961. now->m = mon;
  962. }
  963. if (!day_is_null) {
  964. now->d = day;
  965. }
  966. if (!yea_is_null) {
  967. if (yea >= 0 && yea < 70) {
  968. yea += 2000;
  969. } else if (yea >= 70 && yea <= 100) {
  970. yea += 1900;
  971. }
  972. now->y = yea;
  973. }
  974. /* Update the timestamp */
  975. if (gmt) {
  976. timelib_update_ts(now, NULL);
  977. } else {
  978. timelib_update_ts(now, tzi);
  979. }
  980. /* Clean up and return */
  981. ts = timelib_date_to_int(now, &epoch_does_not_fit_in_zend_long);
  982. if (epoch_does_not_fit_in_zend_long) {
  983. timelib_time_dtor(now);
  984. php_error_docref(NULL, E_WARNING, "Epoch doesn't fit in a PHP integer");
  985. RETURN_FALSE;
  986. }
  987. ts += adjust_seconds;
  988. timelib_time_dtor(now);
  989. RETURN_LONG(ts);
  990. }
  991. /* }}} */
  992. /* {{{ Get UNIX timestamp for a date */
  993. PHP_FUNCTION(mktime)
  994. {
  995. php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  996. }
  997. /* }}} */
  998. /* {{{ Get UNIX timestamp for a GMT date */
  999. PHP_FUNCTION(gmmktime)
  1000. {
  1001. php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1002. }
  1003. /* }}} */
  1004. /* {{{ Returns true(1) if it is a valid date in gregorian calendar */
  1005. PHP_FUNCTION(checkdate)
  1006. {
  1007. zend_long m, d, y;
  1008. ZEND_PARSE_PARAMETERS_START(3, 3)
  1009. Z_PARAM_LONG(m)
  1010. Z_PARAM_LONG(d)
  1011. Z_PARAM_LONG(y)
  1012. ZEND_PARSE_PARAMETERS_END();
  1013. if (y < 1 || y > 32767 || !timelib_valid_date(y, m, d)) {
  1014. RETURN_FALSE;
  1015. }
  1016. RETURN_TRUE; /* True : This month, day, year arguments are valid */
  1017. }
  1018. /* }}} */
  1019. /* {{{ php_strftime - (gm)strftime helper */
  1020. PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
  1021. {
  1022. zend_string *format;
  1023. zend_long timestamp;
  1024. bool timestamp_is_null = 1;
  1025. struct tm ta;
  1026. int max_reallocs = 5;
  1027. size_t buf_len = 256, real_len;
  1028. timelib_time *ts;
  1029. timelib_tzinfo *tzi;
  1030. timelib_time_offset *offset = NULL;
  1031. zend_string *buf;
  1032. ZEND_PARSE_PARAMETERS_START(1, 2)
  1033. Z_PARAM_STR(format)
  1034. Z_PARAM_OPTIONAL
  1035. Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
  1036. ZEND_PARSE_PARAMETERS_END();
  1037. if (ZSTR_LEN(format) == 0) {
  1038. RETURN_FALSE;
  1039. }
  1040. if (timestamp_is_null) {
  1041. timestamp = (zend_long) php_time();
  1042. }
  1043. ts = timelib_time_ctor();
  1044. if (gmt) {
  1045. tzi = NULL;
  1046. timelib_unixtime2gmt(ts, (timelib_sll) timestamp);
  1047. } else {
  1048. tzi = get_timezone_info();
  1049. if (!tzi) {
  1050. return;
  1051. }
  1052. ts->tz_info = tzi;
  1053. ts->zone_type = TIMELIB_ZONETYPE_ID;
  1054. timelib_unixtime2local(ts, (timelib_sll) timestamp);
  1055. }
  1056. ta.tm_sec = ts->s;
  1057. ta.tm_min = ts->i;
  1058. ta.tm_hour = ts->h;
  1059. ta.tm_mday = ts->d;
  1060. ta.tm_mon = ts->m - 1;
  1061. ta.tm_year = ts->y - 1900;
  1062. ta.tm_wday = timelib_day_of_week(ts->y, ts->m, ts->d);
  1063. ta.tm_yday = timelib_day_of_year(ts->y, ts->m, ts->d);
  1064. if (gmt) {
  1065. ta.tm_isdst = 0;
  1066. #if HAVE_STRUCT_TM_TM_GMTOFF
  1067. ta.tm_gmtoff = 0;
  1068. #endif
  1069. #if HAVE_STRUCT_TM_TM_ZONE
  1070. ta.tm_zone = "GMT";
  1071. #endif
  1072. } else {
  1073. offset = timelib_get_time_zone_info(timestamp, tzi);
  1074. ta.tm_isdst = offset->is_dst;
  1075. #if HAVE_STRUCT_TM_TM_GMTOFF
  1076. ta.tm_gmtoff = offset->offset;
  1077. #endif
  1078. #if HAVE_STRUCT_TM_TM_ZONE
  1079. ta.tm_zone = offset->abbr;
  1080. #endif
  1081. }
  1082. /* VS2012 crt has a bug where strftime crash with %z and %Z format when the
  1083. initial buffer is too small. See
  1084. http://connect.microsoft.com/VisualStudio/feedback/details/759720/vs2012-strftime-crash-with-z-formatting-code */
  1085. buf = zend_string_alloc(buf_len, 0);
  1086. while ((real_len = strftime(ZSTR_VAL(buf), buf_len, ZSTR_VAL(format), &ta)) == buf_len || real_len == 0) {
  1087. buf_len *= 2;
  1088. buf = zend_string_extend(buf, buf_len, 0);
  1089. if (!--max_reallocs) {
  1090. break;
  1091. }
  1092. }
  1093. #ifdef PHP_WIN32
  1094. /* VS2012 strftime() returns number of characters, not bytes.
  1095. See VC++11 bug id 766205. */
  1096. if (real_len > 0) {
  1097. real_len = strlen(buf->val);
  1098. }
  1099. #endif
  1100. timelib_time_dtor(ts);
  1101. if (!gmt) {
  1102. timelib_time_offset_dtor(offset);
  1103. }
  1104. if (real_len && real_len != buf_len) {
  1105. buf = zend_string_truncate(buf, real_len, 0);
  1106. RETURN_NEW_STR(buf);
  1107. }
  1108. zend_string_efree(buf);
  1109. RETURN_FALSE;
  1110. }
  1111. /* }}} */
  1112. /* {{{ Format a local time/date according to locale settings */
  1113. PHP_FUNCTION(strftime)
  1114. {
  1115. php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1116. }
  1117. /* }}} */
  1118. /* {{{ Format a GMT/UCT time/date according to locale settings */
  1119. PHP_FUNCTION(gmstrftime)
  1120. {
  1121. php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1122. }
  1123. /* }}} */
  1124. /* {{{ Return current UNIX timestamp */
  1125. PHP_FUNCTION(time)
  1126. {
  1127. ZEND_PARSE_PARAMETERS_NONE();
  1128. RETURN_LONG((zend_long)php_time());
  1129. }
  1130. /* }}} */
  1131. /* {{{ Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array */
  1132. PHP_FUNCTION(localtime)
  1133. {
  1134. zend_long timestamp;
  1135. bool timestamp_is_null = 1;
  1136. bool associative = 0;
  1137. timelib_tzinfo *tzi;
  1138. timelib_time *ts;
  1139. ZEND_PARSE_PARAMETERS_START(0, 2)
  1140. Z_PARAM_OPTIONAL
  1141. Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
  1142. Z_PARAM_BOOL(associative)
  1143. ZEND_PARSE_PARAMETERS_END();
  1144. if (timestamp_is_null) {
  1145. timestamp = (zend_long) php_time();
  1146. }
  1147. tzi = get_timezone_info();
  1148. if (!tzi) {
  1149. RETURN_THROWS();
  1150. }
  1151. ts = timelib_time_ctor();
  1152. ts->tz_info = tzi;
  1153. ts->zone_type = TIMELIB_ZONETYPE_ID;
  1154. timelib_unixtime2local(ts, (timelib_sll) timestamp);
  1155. array_init(return_value);
  1156. if (associative) {
  1157. add_assoc_long(return_value, "tm_sec", ts->s);
  1158. add_assoc_long(return_value, "tm_min", ts->i);
  1159. add_assoc_long(return_value, "tm_hour", ts->h);
  1160. add_assoc_long(return_value, "tm_mday", ts->d);
  1161. add_assoc_long(return_value, "tm_mon", ts->m - 1);
  1162. add_assoc_long(return_value, "tm_year", ts->y - 1900);
  1163. add_assoc_long(return_value, "tm_wday", timelib_day_of_week(ts->y, ts->m, ts->d));
  1164. add_assoc_long(return_value, "tm_yday", timelib_day_of_year(ts->y, ts->m, ts->d));
  1165. add_assoc_long(return_value, "tm_isdst", ts->dst);
  1166. } else {
  1167. add_next_index_long(return_value, ts->s);
  1168. add_next_index_long(return_value, ts->i);
  1169. add_next_index_long(return_value, ts->h);
  1170. add_next_index_long(return_value, ts->d);
  1171. add_next_index_long(return_value, ts->m - 1);
  1172. add_next_index_long(return_value, ts->y- 1900);
  1173. add_next_index_long(return_value, timelib_day_of_week(ts->y, ts->m, ts->d));
  1174. add_next_index_long(return_value, timelib_day_of_year(ts->y, ts->m, ts->d));
  1175. add_next_index_long(return_value, ts->dst);
  1176. }
  1177. timelib_time_dtor(ts);
  1178. }
  1179. /* }}} */
  1180. /* {{{ Get date/time information */
  1181. PHP_FUNCTION(getdate)
  1182. {
  1183. zend_long timestamp;
  1184. bool timestamp_is_null = 1;
  1185. timelib_tzinfo *tzi;
  1186. timelib_time *ts;
  1187. ZEND_PARSE_PARAMETERS_START(0, 1)
  1188. Z_PARAM_OPTIONAL
  1189. Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
  1190. ZEND_PARSE_PARAMETERS_END();
  1191. if (timestamp_is_null) {
  1192. timestamp = (zend_long) php_time();
  1193. }
  1194. tzi = get_timezone_info();
  1195. if (!tzi) {
  1196. RETURN_THROWS();
  1197. }
  1198. ts = timelib_time_ctor();
  1199. ts->tz_info = tzi;
  1200. ts->zone_type = TIMELIB_ZONETYPE_ID;
  1201. timelib_unixtime2local(ts, (timelib_sll) timestamp);
  1202. array_init(return_value);
  1203. add_assoc_long(return_value, "seconds", ts->s);
  1204. add_assoc_long(return_value, "minutes", ts->i);
  1205. add_assoc_long(return_value, "hours", ts->h);
  1206. add_assoc_long(return_value, "mday", ts->d);
  1207. add_assoc_long(return_value, "wday", timelib_day_of_week(ts->y, ts->m, ts->d));
  1208. add_assoc_long(return_value, "mon", ts->m);
  1209. add_assoc_long(return_value, "year", ts->y);
  1210. add_assoc_long(return_value, "yday", timelib_day_of_year(ts->y, ts->m, ts->d));
  1211. add_assoc_string(return_value, "weekday", php_date_full_day_name(ts->y, ts->m, ts->d));
  1212. add_assoc_string(return_value, "month", mon_full_names[ts->m - 1]);
  1213. add_index_long(return_value, 0, timestamp);
  1214. timelib_time_dtor(ts);
  1215. }
  1216. /* }}} */
  1217. #define PHP_DATE_TIMEZONE_GROUP_AFRICA 0x0001
  1218. #define PHP_DATE_TIMEZONE_GROUP_AMERICA 0x0002
  1219. #define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0004
  1220. #define PHP_DATE_TIMEZONE_GROUP_ARCTIC 0x0008
  1221. #define PHP_DATE_TIMEZONE_GROUP_ASIA 0x0010
  1222. #define PHP_DATE_TIMEZONE_GROUP_ATLANTIC 0x0020
  1223. #define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA 0x0040
  1224. #define PHP_DATE_TIMEZONE_GROUP_EUROPE 0x0080
  1225. #define PHP_DATE_TIMEZONE_GROUP_INDIAN 0x0100
  1226. #define PHP_DATE_TIMEZONE_GROUP_PACIFIC 0x0200
  1227. #define PHP_DATE_TIMEZONE_GROUP_UTC 0x0400
  1228. #define PHP_DATE_TIMEZONE_GROUP_ALL 0x07FF
  1229. #define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC 0x0FFF
  1230. #define PHP_DATE_TIMEZONE_PER_COUNTRY 0x1000
  1231. #define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
  1232. /* define an overloaded iterator structure */
  1233. typedef struct {
  1234. zend_object_iterator intern;
  1235. zval current;
  1236. php_period_obj *object;
  1237. int current_index;
  1238. } date_period_it;
  1239. /* {{{ date_period_it_invalidate_current */
  1240. static void date_period_it_invalidate_current(zend_object_iterator *iter)
  1241. {
  1242. date_period_it *iterator = (date_period_it *)iter;
  1243. if (Z_TYPE(iterator->current) != IS_UNDEF) {
  1244. zval_ptr_dtor(&iterator->current);
  1245. ZVAL_UNDEF(&iterator->current);
  1246. }
  1247. }
  1248. /* }}} */
  1249. /* {{{ date_period_it_dtor */
  1250. static void date_period_it_dtor(zend_object_iterator *iter)
  1251. {
  1252. date_period_it *iterator = (date_period_it *)iter;
  1253. date_period_it_invalidate_current(iter);
  1254. zval_ptr_dtor(&iterator->intern.data);
  1255. }
  1256. /* }}} */
  1257. /* {{{ date_period_it_has_more */
  1258. static int date_period_it_has_more(zend_object_iterator *iter)
  1259. {
  1260. date_period_it *iterator = (date_period_it *)iter;
  1261. php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
  1262. if (object->end) {
  1263. return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
  1264. } else {
  1265. return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
  1266. }
  1267. }
  1268. /* }}} */
  1269. static zend_class_entry *get_base_date_class(zend_class_entry *start_ce)
  1270. {
  1271. zend_class_entry *tmp = start_ce;
  1272. while (tmp != date_ce_date && tmp != date_ce_immutable && tmp->parent) {
  1273. tmp = tmp->parent;
  1274. }
  1275. return tmp;
  1276. }
  1277. /* {{{ date_period_it_current_data */
  1278. static zval *date_period_it_current_data(zend_object_iterator *iter)
  1279. {
  1280. date_period_it *iterator = (date_period_it *)iter;
  1281. php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
  1282. timelib_time *it_time = object->current;
  1283. php_date_obj *newdateobj;
  1284. /* Create new object */
  1285. php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current);
  1286. newdateobj = Z_PHPDATE_P(&iterator->current);
  1287. newdateobj->time = timelib_time_ctor();
  1288. *newdateobj->time = *it_time;
  1289. if (it_time->tz_abbr) {
  1290. newdateobj->time->tz_abbr = timelib_strdup(it_time->tz_abbr);
  1291. }
  1292. if (it_time->tz_info) {
  1293. newdateobj->time->tz_info = it_time->tz_info;
  1294. }
  1295. return &iterator->current;
  1296. }
  1297. /* }}} */
  1298. /* {{{ date_period_it_current_key */
  1299. static void date_period_it_current_key(zend_object_iterator *iter, zval *key)
  1300. {
  1301. date_period_it *iterator = (date_period_it *)iter;
  1302. ZVAL_LONG(key, iterator->current_index);
  1303. }
  1304. /* }}} */
  1305. static void date_period_advance(timelib_time *it_time, timelib_rel_time *interval)
  1306. {
  1307. it_time->have_relative = 1;
  1308. it_time->relative = *interval;
  1309. it_time->sse_uptodate = 0;
  1310. timelib_update_ts(it_time, NULL);
  1311. timelib_update_from_sse(it_time);
  1312. }
  1313. /* {{{ date_period_it_move_forward */
  1314. static void date_period_it_move_forward(zend_object_iterator *iter)
  1315. {
  1316. date_period_it *iterator = (date_period_it *)iter;
  1317. php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
  1318. timelib_time *it_time = object->current;
  1319. date_period_advance(it_time, object->interval);
  1320. iterator->current_index++;
  1321. date_period_it_invalidate_current(iter);
  1322. }
  1323. /* }}} */
  1324. /* {{{ date_period_it_rewind */
  1325. static void date_period_it_rewind(zend_object_iterator *iter)
  1326. {
  1327. date_period_it *iterator = (date_period_it *)iter;
  1328. iterator->current_index = 0;
  1329. if (iterator->object->current) {
  1330. timelib_time_dtor(iterator->object->current);
  1331. }
  1332. if (!iterator->object->start) {
  1333. zend_throw_error(NULL, "DatePeriod has not been initialized correctly");
  1334. return;
  1335. }
  1336. iterator->object->current = timelib_time_clone(iterator->object->start);
  1337. if (!iterator->object->include_start_date) {
  1338. date_period_advance(iterator->object->current, iterator->object->interval);
  1339. }
  1340. date_period_it_invalidate_current(iter);
  1341. }
  1342. /* }}} */
  1343. /* iterator handler table */
  1344. static const zend_object_iterator_funcs date_period_it_funcs = {
  1345. date_period_it_dtor,
  1346. date_period_it_has_more,
  1347. date_period_it_current_data,
  1348. date_period_it_current_key,
  1349. date_period_it_move_forward,
  1350. date_period_it_rewind,
  1351. date_period_it_invalidate_current,
  1352. NULL, /* get_gc */
  1353. };
  1354. zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  1355. {
  1356. date_period_it *iterator;
  1357. if (by_ref) {
  1358. zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
  1359. return NULL;
  1360. }
  1361. iterator = emalloc(sizeof(date_period_it));
  1362. zend_iterator_init((zend_object_iterator*)iterator);
  1363. ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
  1364. iterator->intern.funcs = &date_period_it_funcs;
  1365. iterator->object = Z_PHPPERIOD_P(object);
  1366. ZVAL_UNDEF(&iterator->current);
  1367. return (zend_object_iterator*)iterator;
  1368. } /* }}} */
  1369. static int implement_date_interface_handler(zend_class_entry *interface, zend_class_entry *implementor) /* {{{ */
  1370. {
  1371. if (implementor->type == ZEND_USER_CLASS &&
  1372. !instanceof_function(implementor, date_ce_date) &&
  1373. !instanceof_function(implementor, date_ce_immutable)
  1374. ) {
  1375. zend_error(E_ERROR, "DateTimeInterface can't be implemented by user classes");
  1376. }
  1377. return SUCCESS;
  1378. } /* }}} */
  1379. static int date_interval_has_property(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
  1380. {
  1381. php_interval_obj *obj;
  1382. zval rv;
  1383. zval *prop;
  1384. int retval = 0;
  1385. obj = php_interval_obj_from_obj(object);
  1386. if (!obj->initialized) {
  1387. retval = zend_std_has_property(object, name, type, cache_slot);
  1388. return retval;
  1389. }
  1390. prop = date_interval_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
  1391. if (prop != &EG(uninitialized_zval)) {
  1392. if (type == 2) {
  1393. retval = 1;
  1394. } else if (type == 1) {
  1395. retval = zend_is_true(prop);
  1396. } else if (type == 0) {
  1397. retval = (Z_TYPE_P(prop) != IS_NULL);
  1398. }
  1399. } else {
  1400. retval = zend_std_has_property(object, name, type, cache_slot);
  1401. }
  1402. return retval;
  1403. }
  1404. /* }}} */
  1405. static void date_register_classes(void) /* {{{ */
  1406. {
  1407. date_ce_interface = register_class_DateTimeInterface();
  1408. date_ce_interface->interface_gets_implemented = implement_date_interface_handler;
  1409. #define REGISTER_DATE_INTERFACE_CONST_STRING(const_name, value) \
  1410. zend_declare_class_constant_stringl(date_ce_interface, const_name, sizeof(const_name)-1, value, sizeof(value)-1);
  1411. REGISTER_DATE_INTERFACE_CONST_STRING("ATOM", DATE_FORMAT_RFC3339);
  1412. REGISTER_DATE_INTERFACE_CONST_STRING("COOKIE", DATE_FORMAT_COOKIE);
  1413. REGISTER_DATE_INTERFACE_CONST_STRING("ISO8601", DATE_FORMAT_ISO8601);
  1414. REGISTER_DATE_INTERFACE_CONST_STRING("RFC822", DATE_FORMAT_RFC822);
  1415. REGISTER_DATE_INTERFACE_CONST_STRING("RFC850", DATE_FORMAT_RFC850);
  1416. REGISTER_DATE_INTERFACE_CONST_STRING("RFC1036", DATE_FORMAT_RFC1036);
  1417. REGISTER_DATE_INTERFACE_CONST_STRING("RFC1123", DATE_FORMAT_RFC1123);
  1418. REGISTER_DATE_INTERFACE_CONST_STRING("RFC7231", DATE_FORMAT_RFC7231);
  1419. REGISTER_DATE_INTERFACE_CONST_STRING("RFC2822", DATE_FORMAT_RFC2822);
  1420. REGISTER_DATE_INTERFACE_CONST_STRING("RFC3339", DATE_FORMAT_RFC3339);
  1421. REGISTER_DATE_INTERFACE_CONST_STRING("RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED);
  1422. REGISTER_DATE_INTERFACE_CONST_STRING("RSS", DATE_FORMAT_RFC1123);
  1423. REGISTER_DATE_INTERFACE_CONST_STRING("W3C", DATE_FORMAT_RFC3339);
  1424. date_ce_date = register_class_DateTime(date_ce_interface);
  1425. date_ce_date->create_object = date_object_new_date;
  1426. memcpy(&date_object_handlers_date, &std_object_handlers, sizeof(zend_object_handlers));
  1427. date_object_handlers_date.offset = XtOffsetOf(php_date_obj, std);
  1428. date_object_handlers_date.free_obj = date_object_free_storage_date;
  1429. date_object_handlers_date.clone_obj = date_object_clone_date;
  1430. date_object_handlers_date.compare = date_object_compare_date;
  1431. date_object_handlers_date.get_properties_for = date_object_get_properties_for;
  1432. date_object_handlers_date.get_gc = date_object_get_gc;
  1433. date_ce_immutable = register_class_DateTimeImmutable(date_ce_interface);
  1434. date_ce_immutable->create_object = date_object_new_date;
  1435. memcpy(&date_object_handlers_immutable, &std_object_handlers, sizeof(zend_object_handlers));
  1436. date_object_handlers_immutable.clone_obj = date_object_clone_date;
  1437. date_object_handlers_immutable.compare = date_object_compare_date;
  1438. date_object_handlers_immutable.get_properties_for = date_object_get_properties_for;
  1439. date_object_handlers_immutable.get_gc = date_object_get_gc;
  1440. date_ce_timezone = register_class_DateTimeZone();
  1441. date_ce_timezone->create_object = date_object_new_timezone;
  1442. memcpy(&date_object_handlers_timezone, &std_object_handlers, sizeof(zend_object_handlers));
  1443. date_object_handlers_timezone.offset = XtOffsetOf(php_timezone_obj, std);
  1444. date_object_handlers_timezone.free_obj = date_object_free_storage_timezone;
  1445. date_object_handlers_timezone.clone_obj = date_object_clone_timezone;
  1446. date_object_handlers_timezone.get_properties_for = date_object_get_properties_for_timezone;
  1447. date_object_handlers_timezone.get_gc = date_object_get_gc_timezone;
  1448. date_object_handlers_timezone.get_debug_info = date_object_get_debug_info_timezone;
  1449. date_object_handlers_timezone.compare = date_object_compare_timezone;
  1450. #define REGISTER_TIMEZONE_CLASS_CONST_STRING(const_name, value) \
  1451. zend_declare_class_constant_long(date_ce_timezone, const_name, sizeof(const_name)-1, value);
  1452. REGISTER_TIMEZONE_CLASS_CONST_STRING("AFRICA", PHP_DATE_TIMEZONE_GROUP_AFRICA);
  1453. REGISTER_TIMEZONE_CLASS_CONST_STRING("AMERICA", PHP_DATE_TIMEZONE_GROUP_AMERICA);
  1454. REGISTER_TIMEZONE_CLASS_CONST_STRING("ANTARCTICA", PHP_DATE_TIMEZONE_GROUP_ANTARCTICA);
  1455. REGISTER_TIMEZONE_CLASS_CONST_STRING("ARCTIC", PHP_DATE_TIMEZONE_GROUP_ARCTIC);
  1456. REGISTER_TIMEZONE_CLASS_CONST_STRING("ASIA", PHP_DATE_TIMEZONE_GROUP_ASIA);
  1457. REGISTER_TIMEZONE_CLASS_CONST_STRING("ATLANTIC", PHP_DATE_TIMEZONE_GROUP_ATLANTIC);
  1458. REGISTER_TIMEZONE_CLASS_CONST_STRING("AUSTRALIA", PHP_DATE_TIMEZONE_GROUP_AUSTRALIA);
  1459. REGISTER_TIMEZONE_CLASS_CONST_STRING("EUROPE", PHP_DATE_TIMEZONE_GROUP_EUROPE);
  1460. REGISTER_TIMEZONE_CLASS_CONST_STRING("INDIAN", PHP_DATE_TIMEZONE_GROUP_INDIAN);
  1461. REGISTER_TIMEZONE_CLASS_CONST_STRING("PACIFIC", PHP_DATE_TIMEZONE_GROUP_PACIFIC);
  1462. REGISTER_TIMEZONE_CLASS_CONST_STRING("UTC", PHP_DATE_TIMEZONE_GROUP_UTC);
  1463. REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL", PHP_DATE_TIMEZONE_GROUP_ALL);
  1464. REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL_WITH_BC", PHP_DATE_TIMEZONE_GROUP_ALL_W_BC);
  1465. REGISTER_TIMEZONE_CLASS_CONST_STRING("PER_COUNTRY", PHP_DATE_TIMEZONE_PER_COUNTRY);
  1466. date_ce_interval = register_class_DateInterval();
  1467. date_ce_interval->create_object = date_object_new_interval;
  1468. memcpy(&date_object_handlers_interval, &std_object_handlers, sizeof(zend_object_handlers));
  1469. date_object_handlers_interval.offset = XtOffsetOf(php_interval_obj, std);
  1470. date_object_handlers_interval.free_obj = date_object_free_storage_interval;
  1471. date_object_handlers_interval.clone_obj = date_object_clone_interval;
  1472. date_object_handlers_interval.has_property = date_interval_has_property;
  1473. date_object_handlers_interval.read_property = date_interval_read_property;
  1474. date_object_handlers_interval.write_property = date_interval_write_property;
  1475. date_object_handlers_interval.get_properties = date_object_get_properties_interval;
  1476. date_object_handlers_interval.get_property_ptr_ptr = date_interval_get_property_ptr_ptr;
  1477. date_object_handlers_interval.get_gc = date_object_get_gc_interval;
  1478. date_object_handlers_interval.compare = date_interval_compare_objects;
  1479. date_ce_period = register_class_DatePeriod(zend_ce_aggregate);
  1480. date_ce_period->create_object = date_object_new_period;
  1481. date_ce_period->get_iterator = date_object_period_get_iterator;
  1482. memcpy(&date_object_handlers_period, &std_object_handlers, sizeof(zend_object_handlers));
  1483. date_object_handlers_period.offset = XtOffsetOf(php_period_obj, std);
  1484. date_object_handlers_period.free_obj = date_object_free_storage_period;
  1485. date_object_handlers_period.clone_obj = date_object_clone_period;
  1486. date_object_handlers_period.get_properties = date_object_get_properties_period;
  1487. date_object_handlers_period.get_property_ptr_ptr = date_period_get_property_ptr_ptr;
  1488. date_object_handlers_period.get_gc = date_object_get_gc_period;
  1489. date_object_handlers_period.read_property = date_period_read_property;
  1490. date_object_handlers_period.write_property = date_period_write_property;
  1491. #define REGISTER_PERIOD_CLASS_CONST_STRING(const_name, value) \
  1492. zend_declare_class_constant_long(date_ce_period, const_name, sizeof(const_name)-1, value);
  1493. REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE);
  1494. } /* }}} */
  1495. static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */
  1496. {
  1497. php_date_obj *intern = zend_object_alloc(sizeof(php_date_obj), class_type);
  1498. zend_object_std_init(&intern->std, class_type);
  1499. object_properties_init(&intern->std, class_type);
  1500. intern->std.handlers = &date_object_handlers_date;
  1501. return &intern->std;
  1502. } /* }}} */
  1503. static zend_object *date_object_clone_date(zend_object *this_ptr) /* {{{ */
  1504. {
  1505. php_date_obj *old_obj = php_date_obj_from_obj(this_ptr);
  1506. php_date_obj *new_obj = php_date_obj_from_obj(date_object_new_date(old_obj->std.ce));
  1507. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1508. if (!old_obj->time) {
  1509. return &new_obj->std;
  1510. }
  1511. /* this should probably moved to a new `timelib_time *timelime_time_clone(timelib_time *)` */
  1512. new_obj->time = timelib_time_ctor();
  1513. *new_obj->time = *old_obj->time;
  1514. if (old_obj->time->tz_abbr) {
  1515. new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr);
  1516. }
  1517. if (old_obj->time->tz_info) {
  1518. new_obj->time->tz_info = old_obj->time->tz_info;
  1519. }
  1520. return &new_obj->std;
  1521. } /* }}} */
  1522. static void date_clone_immutable(zval *object, zval *new_object) /* {{{ */
  1523. {
  1524. ZVAL_OBJ(new_object, date_object_clone_date(Z_OBJ_P(object)));
  1525. } /* }}} */
  1526. static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
  1527. {
  1528. php_date_obj *o1;
  1529. php_date_obj *o2;
  1530. ZEND_COMPARE_OBJECTS_FALLBACK(d1, d2);
  1531. o1 = Z_PHPDATE_P(d1);
  1532. o2 = Z_PHPDATE_P(d2);
  1533. if (!o1->time || !o2->time) {
  1534. php_error_docref(NULL, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
  1535. return ZEND_UNCOMPARABLE;
  1536. }
  1537. if (!o1->time->sse_uptodate) {
  1538. timelib_update_ts(o1->time, o1->time->tz_info);
  1539. }
  1540. if (!o2->time->sse_uptodate) {
  1541. timelib_update_ts(o2->time, o2->time->tz_info);
  1542. }
  1543. return timelib_time_compare(o1->time, o2->time);
  1544. } /* }}} */
  1545. static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n) /* {{{ */
  1546. {
  1547. *table = NULL;
  1548. *n = 0;
  1549. return zend_std_get_properties(object);
  1550. } /* }}} */
  1551. static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n) /* {{{ */
  1552. {
  1553. *table = NULL;
  1554. *n = 0;
  1555. return zend_std_get_properties(object);
  1556. } /* }}} */
  1557. static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose) /* {{{ */
  1558. {
  1559. HashTable *props;
  1560. zval zv;
  1561. php_date_obj *dateobj;
  1562. switch (purpose) {
  1563. case ZEND_PROP_PURPOSE_DEBUG:
  1564. case ZEND_PROP_PURPOSE_SERIALIZE:
  1565. case ZEND_PROP_PURPOSE_VAR_EXPORT:
  1566. case ZEND_PROP_PURPOSE_JSON:
  1567. case ZEND_PROP_PURPOSE_ARRAY_CAST:
  1568. break;
  1569. default:
  1570. return zend_std_get_properties_for(object, purpose);
  1571. }
  1572. dateobj = php_date_obj_from_obj(object);
  1573. props = zend_array_dup(zend_std_get_properties(object));
  1574. if (!dateobj->time) {
  1575. return props;
  1576. }
  1577. /* first we add the date and time in ISO format */
  1578. ZVAL_STR(&zv, date_format("Y-m-d H:i:s.u", sizeof("Y-m-d H:i:s.u")-1, dateobj->time, 1));
  1579. zend_hash_str_update(props, "date", sizeof("date")-1, &zv);
  1580. /* then we add the timezone name (or similar) */
  1581. if (dateobj->time->is_localtime) {
  1582. ZVAL_LONG(&zv, dateobj->time->zone_type);
  1583. zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
  1584. switch (dateobj->time->zone_type) {
  1585. case TIMELIB_ZONETYPE_ID:
  1586. ZVAL_STRING(&zv, dateobj->time->tz_info->name);
  1587. break;
  1588. case TIMELIB_ZONETYPE_OFFSET: {
  1589. zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
  1590. int utc_offset = dateobj->time->z;
  1591. ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
  1592. utc_offset < 0 ? '-' : '+',
  1593. abs(utc_offset / 3600),
  1594. abs(((utc_offset % 3600) / 60)));
  1595. ZVAL_NEW_STR(&zv, tmpstr);
  1596. }
  1597. break;
  1598. case TIMELIB_ZONETYPE_ABBR:
  1599. ZVAL_STRING(&zv, dateobj->time->tz_abbr);
  1600. break;
  1601. }
  1602. zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
  1603. }
  1604. return props;
  1605. } /* }}} */
  1606. static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */
  1607. {
  1608. php_timezone_obj *intern = zend_object_alloc(sizeof(php_timezone_obj), class_type);
  1609. zend_object_std_init(&intern->std, class_type);
  1610. object_properties_init(&intern->std, class_type);
  1611. intern->std.handlers = &date_object_handlers_timezone;
  1612. return &intern->std;
  1613. } /* }}} */
  1614. static zend_object *date_object_clone_timezone(zend_object *this_ptr) /* {{{ */
  1615. {
  1616. php_timezone_obj *old_obj = php_timezone_obj_from_obj(this_ptr);
  1617. php_timezone_obj *new_obj = php_timezone_obj_from_obj(date_object_new_timezone(old_obj->std.ce));
  1618. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1619. if (!old_obj->initialized) {
  1620. return &new_obj->std;
  1621. }
  1622. new_obj->type = old_obj->type;
  1623. new_obj->initialized = 1;
  1624. switch (new_obj->type) {
  1625. case TIMELIB_ZONETYPE_ID:
  1626. new_obj->tzi.tz = old_obj->tzi.tz;
  1627. break;
  1628. case TIMELIB_ZONETYPE_OFFSET:
  1629. new_obj->tzi.utc_offset = old_obj->tzi.utc_offset;
  1630. break;
  1631. case TIMELIB_ZONETYPE_ABBR:
  1632. new_obj->tzi.z.utc_offset = old_obj->tzi.z.utc_offset;
  1633. new_obj->tzi.z.dst = old_obj->tzi.z.dst;
  1634. new_obj->tzi.z.abbr = timelib_strdup(old_obj->tzi.z.abbr);
  1635. break;
  1636. }
  1637. return &new_obj->std;
  1638. } /* }}} */
  1639. static int date_object_compare_timezone(zval *tz1, zval *tz2) /* {{{ */
  1640. {
  1641. php_timezone_obj *o1, *o2;
  1642. ZEND_COMPARE_OBJECTS_FALLBACK(tz1, tz2);
  1643. o1 = Z_PHPTIMEZONE_P(tz1);
  1644. o2 = Z_PHPTIMEZONE_P(tz2);
  1645. if (!o1->initialized || !o2->initialized) {
  1646. zend_throw_error(NULL, "Trying to compare uninitialized DateTimeZone objects");
  1647. return 1;
  1648. }
  1649. if (o1->type != o2->type) {
  1650. php_error_docref(NULL, E_WARNING, "Trying to compare different kinds of DateTimeZone objects");
  1651. return ZEND_UNCOMPARABLE;
  1652. }
  1653. switch (o1->type) {
  1654. case TIMELIB_ZONETYPE_OFFSET:
  1655. return o1->tzi.utc_offset == o2->tzi.utc_offset ? 0 : 1;
  1656. case TIMELIB_ZONETYPE_ABBR:
  1657. return strcmp(o1->tzi.z.abbr, o2->tzi.z.abbr) ? 1 : 0;
  1658. case TIMELIB_ZONETYPE_ID:
  1659. return strcmp(o1->tzi.tz->name, o2->tzi.tz->name) ? 1 : 0;
  1660. EMPTY_SWITCH_DEFAULT_CASE();
  1661. }
  1662. } /* }}} */
  1663. static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
  1664. {
  1665. switch (tzobj->type) {
  1666. case TIMELIB_ZONETYPE_ID:
  1667. ZVAL_STRING(zv, tzobj->tzi.tz->name);
  1668. break;
  1669. case TIMELIB_ZONETYPE_OFFSET: {
  1670. zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
  1671. timelib_sll utc_offset = tzobj->tzi.utc_offset;
  1672. ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
  1673. utc_offset < 0 ? '-' : '+',
  1674. abs((int)(utc_offset / 3600)),
  1675. abs((int)(utc_offset % 3600) / 60));
  1676. ZVAL_NEW_STR(zv, tmpstr);
  1677. }
  1678. break;
  1679. case TIMELIB_ZONETYPE_ABBR:
  1680. ZVAL_STRING(zv, tzobj->tzi.z.abbr);
  1681. break;
  1682. }
  1683. }
  1684. static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose) /* {{{ */
  1685. {
  1686. HashTable *props;
  1687. zval zv;
  1688. php_timezone_obj *tzobj;
  1689. switch (purpose) {
  1690. case ZEND_PROP_PURPOSE_DEBUG:
  1691. case ZEND_PROP_PURPOSE_SERIALIZE:
  1692. case ZEND_PROP_PURPOSE_VAR_EXPORT:
  1693. case ZEND_PROP_PURPOSE_JSON:
  1694. case ZEND_PROP_PURPOSE_ARRAY_CAST:
  1695. break;
  1696. default:
  1697. return zend_std_get_properties_for(object, purpose);
  1698. }
  1699. tzobj = php_timezone_obj_from_obj(object);
  1700. props = zend_array_dup(zend_std_get_properties(object));
  1701. if (!tzobj->initialized) {
  1702. return props;
  1703. }
  1704. ZVAL_LONG(&zv, tzobj->type);
  1705. zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
  1706. php_timezone_to_string(tzobj, &zv);
  1707. zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
  1708. return props;
  1709. } /* }}} */
  1710. static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp) /* {{{ */
  1711. {
  1712. HashTable *ht, *props;
  1713. zval zv;
  1714. php_timezone_obj *tzobj;
  1715. tzobj = php_timezone_obj_from_obj(object);
  1716. props = zend_std_get_properties(object);
  1717. *is_temp = 1;
  1718. ht = zend_array_dup(props);
  1719. ZVAL_LONG(&zv, tzobj->type);
  1720. zend_hash_str_update(ht, "timezone_type", sizeof("timezone_type")-1, &zv);
  1721. php_timezone_to_string(tzobj, &zv);
  1722. zend_hash_str_update(ht, "timezone", sizeof("timezone")-1, &zv);
  1723. return ht;
  1724. } /* }}} */
  1725. static zend_object *date_object_new_interval(zend_class_entry *class_type) /* {{{ */
  1726. {
  1727. php_interval_obj *intern = zend_object_alloc(sizeof(php_interval_obj), class_type);
  1728. zend_object_std_init(&intern->std, class_type);
  1729. object_properties_init(&intern->std, class_type);
  1730. intern->std.handlers = &date_object_handlers_interval;
  1731. return &intern->std;
  1732. } /* }}} */
  1733. static zend_object *date_object_clone_interval(zend_object *this_ptr) /* {{{ */
  1734. {
  1735. php_interval_obj *old_obj = php_interval_obj_from_obj(this_ptr);
  1736. php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval(old_obj->std.ce));
  1737. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1738. new_obj->initialized = old_obj->initialized;
  1739. if (old_obj->diff) {
  1740. new_obj->diff = timelib_rel_time_clone(old_obj->diff);
  1741. }
  1742. return &new_obj->std;
  1743. } /* }}} */
  1744. static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n) /* {{{ */
  1745. {
  1746. *table = NULL;
  1747. *n = 0;
  1748. return zend_std_get_properties(object);
  1749. } /* }}} */
  1750. static HashTable *date_object_get_properties_interval(zend_object *object) /* {{{ */
  1751. {
  1752. HashTable *props;
  1753. zval zv;
  1754. php_interval_obj *intervalobj;
  1755. intervalobj = php_interval_obj_from_obj(object);
  1756. props = zend_std_get_properties(object);
  1757. if (!intervalobj->initialized) {
  1758. return props;
  1759. }
  1760. #define PHP_DATE_INTERVAL_ADD_PROPERTY(n,f) \
  1761. ZVAL_LONG(&zv, (zend_long)intervalobj->diff->f); \
  1762. zend_hash_str_update(props, n, sizeof(n)-1, &zv);
  1763. PHP_DATE_INTERVAL_ADD_PROPERTY("y", y);
  1764. PHP_DATE_INTERVAL_ADD_PROPERTY("m", m);
  1765. PHP_DATE_INTERVAL_ADD_PROPERTY("d", d);
  1766. PHP_DATE_INTERVAL_ADD_PROPERTY("h", h);
  1767. PHP_DATE_INTERVAL_ADD_PROPERTY("i", i);
  1768. PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
  1769. ZVAL_DOUBLE(&zv, (double)intervalobj->diff->us / 1000000.0);
  1770. zend_hash_str_update(props, "f", sizeof("f") - 1, &zv);
  1771. PHP_DATE_INTERVAL_ADD_PROPERTY("weekday", weekday);
  1772. PHP_DATE_INTERVAL_ADD_PROPERTY("weekday_behavior", weekday_behavior);
  1773. PHP_DATE_INTERVAL_ADD_PROPERTY("first_last_day_of", first_last_day_of);
  1774. PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
  1775. if (intervalobj->diff->days != -99999) {
  1776. PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
  1777. } else {
  1778. ZVAL_FALSE(&zv);
  1779. zend_hash_str_update(props, "days", sizeof("days")-1, &zv);
  1780. }
  1781. PHP_DATE_INTERVAL_ADD_PROPERTY("special_type", special.type);
  1782. PHP_DATE_INTERVAL_ADD_PROPERTY("special_amount", special.amount);
  1783. PHP_DATE_INTERVAL_ADD_PROPERTY("have_weekday_relative", have_weekday_relative);
  1784. PHP_DATE_INTERVAL_ADD_PROPERTY("have_special_relative", have_special_relative);
  1785. return props;
  1786. } /* }}} */
  1787. static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */
  1788. {
  1789. php_period_obj *intern = zend_object_alloc(sizeof(php_period_obj), class_type);
  1790. zend_object_std_init(&intern->std, class_type);
  1791. object_properties_init(&intern->std, class_type);
  1792. intern->std.handlers = &date_object_handlers_period;
  1793. return &intern->std;
  1794. } /* }}} */
  1795. static zend_object *date_object_clone_period(zend_object *this_ptr) /* {{{ */
  1796. {
  1797. php_period_obj *old_obj = php_period_obj_from_obj(this_ptr);
  1798. php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period(old_obj->std.ce));
  1799. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1800. new_obj->initialized = old_obj->initialized;
  1801. new_obj->recurrences = old_obj->recurrences;
  1802. new_obj->include_start_date = old_obj->include_start_date;
  1803. new_obj->start_ce = old_obj->start_ce;
  1804. if (old_obj->start) {
  1805. new_obj->start = timelib_time_clone(old_obj->start);
  1806. }
  1807. if (old_obj->current) {
  1808. new_obj->current = timelib_time_clone(old_obj->current);
  1809. }
  1810. if (old_obj->end) {
  1811. new_obj->end = timelib_time_clone(old_obj->end);
  1812. }
  1813. if (old_obj->interval) {
  1814. new_obj->interval = timelib_rel_time_clone(old_obj->interval);
  1815. }
  1816. return &new_obj->std;
  1817. } /* }}} */
  1818. static void date_object_free_storage_date(zend_object *object) /* {{{ */
  1819. {
  1820. php_date_obj *intern = php_date_obj_from_obj(object);
  1821. if (intern->time) {
  1822. timelib_time_dtor(intern->time);
  1823. }
  1824. zend_object_std_dtor(&intern->std);
  1825. } /* }}} */
  1826. static void date_object_free_storage_timezone(zend_object *object) /* {{{ */
  1827. {
  1828. php_timezone_obj *intern = php_timezone_obj_from_obj(object);
  1829. if (intern->type == TIMELIB_ZONETYPE_ABBR) {
  1830. timelib_free(intern->tzi.z.abbr);
  1831. }
  1832. zend_object_std_dtor(&intern->std);
  1833. } /* }}} */
  1834. static void date_object_free_storage_interval(zend_object *object) /* {{{ */
  1835. {
  1836. php_interval_obj *intern = php_interval_obj_from_obj(object);
  1837. timelib_rel_time_dtor(intern->diff);
  1838. zend_object_std_dtor(&intern->std);
  1839. } /* }}} */
  1840. static void date_object_free_storage_period(zend_object *object) /* {{{ */
  1841. {
  1842. php_period_obj *intern = php_period_obj_from_obj(object);
  1843. if (intern->start) {
  1844. timelib_time_dtor(intern->start);
  1845. }
  1846. if (intern->current) {
  1847. timelib_time_dtor(intern->current);
  1848. }
  1849. if (intern->end) {
  1850. timelib_time_dtor(intern->end);
  1851. }
  1852. timelib_rel_time_dtor(intern->interval);
  1853. zend_object_std_dtor(&intern->std);
  1854. } /* }}} */
  1855. /* Advanced Interface */
  1856. PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
  1857. {
  1858. object_init_ex(object, pce);
  1859. return object;
  1860. } /* }}} */
  1861. /* Helper function used to store the latest found warnings and errors while
  1862. * parsing, from either strtotime or parse_from_format. */
  1863. static void update_errors_warnings(timelib_error_container *last_errors) /* {{{ */
  1864. {
  1865. if (DATEG(last_errors)) {
  1866. timelib_error_container_dtor(DATEG(last_errors));
  1867. DATEG(last_errors) = NULL;
  1868. }
  1869. DATEG(last_errors) = last_errors;
  1870. } /* }}} */
  1871. static void php_date_set_time_fraction(timelib_time *time, int microseconds)
  1872. {
  1873. time->us = microseconds;
  1874. }
  1875. static void php_date_get_current_time_with_fraction(time_t *sec, suseconds_t *usec)
  1876. {
  1877. #if HAVE_GETTIMEOFDAY
  1878. struct timeval tp = {0}; /* For setting microseconds */
  1879. gettimeofday(&tp, NULL);
  1880. *sec = tp.tv_sec;
  1881. *usec = tp.tv_usec;
  1882. #else
  1883. *sec = time(NULL);
  1884. *usec = 0;
  1885. #endif
  1886. }
  1887. PHPAPI int php_date_initialize(php_date_obj *dateobj, const char *time_str, size_t time_str_len, const char *format, zval *timezone_object, int flags) /* {{{ */
  1888. {
  1889. timelib_time *now;
  1890. timelib_tzinfo *tzi = NULL;
  1891. timelib_error_container *err = NULL;
  1892. int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
  1893. char *new_abbr = NULL;
  1894. timelib_sll new_offset = 0;
  1895. time_t sec;
  1896. suseconds_t usec;
  1897. int options = 0;
  1898. if (dateobj->time) {
  1899. timelib_time_dtor(dateobj->time);
  1900. }
  1901. if (format) {
  1902. if (time_str_len == 0) {
  1903. time_str = "";
  1904. }
  1905. dateobj->time = timelib_parse_from_format(format, time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  1906. } else {
  1907. if (time_str_len == 0) {
  1908. time_str = "now";
  1909. time_str_len = sizeof("now") - 1;
  1910. }
  1911. dateobj->time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  1912. }
  1913. /* update last errors and warnings */
  1914. update_errors_warnings(err);
  1915. /* If called from a constructor throw an exception */
  1916. if ((flags & PHP_DATE_INIT_CTOR) && err && err->error_count) {
  1917. /* spit out the first library error message, at least */
  1918. zend_throw_exception_ex(NULL, 0, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
  1919. err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
  1920. }
  1921. if (err && err->error_count) {
  1922. timelib_time_dtor(dateobj->time);
  1923. dateobj->time = 0;
  1924. return 0;
  1925. }
  1926. if (timezone_object) {
  1927. php_timezone_obj *tzobj;
  1928. tzobj = Z_PHPTIMEZONE_P(timezone_object);
  1929. switch (tzobj->type) {
  1930. case TIMELIB_ZONETYPE_ID:
  1931. tzi = tzobj->tzi.tz;
  1932. break;
  1933. case TIMELIB_ZONETYPE_OFFSET:
  1934. new_offset = tzobj->tzi.utc_offset;
  1935. break;
  1936. case TIMELIB_ZONETYPE_ABBR:
  1937. new_offset = tzobj->tzi.z.utc_offset;
  1938. new_dst = tzobj->tzi.z.dst;
  1939. new_abbr = timelib_strdup(tzobj->tzi.z.abbr);
  1940. break;
  1941. }
  1942. type = tzobj->type;
  1943. } else if (dateobj->time->tz_info) {
  1944. tzi = dateobj->time->tz_info;
  1945. } else {
  1946. tzi = get_timezone_info();
  1947. if (!tzi) {
  1948. return 0;
  1949. }
  1950. }
  1951. now = timelib_time_ctor();
  1952. now->zone_type = type;
  1953. switch (type) {
  1954. case TIMELIB_ZONETYPE_ID:
  1955. now->tz_info = tzi;
  1956. break;
  1957. case TIMELIB_ZONETYPE_OFFSET:
  1958. now->z = new_offset;
  1959. break;
  1960. case TIMELIB_ZONETYPE_ABBR:
  1961. now->z = new_offset;
  1962. now->dst = new_dst;
  1963. now->tz_abbr = new_abbr;
  1964. break;
  1965. }
  1966. php_date_get_current_time_with_fraction(&sec, &usec);
  1967. timelib_unixtime2local(now, (timelib_sll) sec);
  1968. php_date_set_time_fraction(now, usec);
  1969. if (!format
  1970. && time_str_len == sizeof("now") - 1
  1971. && memcmp(time_str, "now", sizeof("now") - 1) == 0) {
  1972. timelib_time_dtor(dateobj->time);
  1973. dateobj->time = now;
  1974. return 1;
  1975. }
  1976. options = TIMELIB_NO_CLONE;
  1977. if (flags & PHP_DATE_INIT_FORMAT) {
  1978. options |= TIMELIB_OVERRIDE_TIME;
  1979. }
  1980. timelib_fill_holes(dateobj->time, now, options);
  1981. timelib_update_ts(dateobj->time, tzi);
  1982. timelib_update_from_sse(dateobj->time);
  1983. dateobj->time->have_relative = 0;
  1984. timelib_time_dtor(now);
  1985. return 1;
  1986. } /* }}} */
  1987. /* {{{ Returns new DateTime object */
  1988. PHP_FUNCTION(date_create)
  1989. {
  1990. zval *timezone_object = NULL;
  1991. char *time_str = NULL;
  1992. size_t time_str_len = 0;
  1993. ZEND_PARSE_PARAMETERS_START(0, 2)
  1994. Z_PARAM_OPTIONAL
  1995. Z_PARAM_STRING(time_str, time_str_len)
  1996. Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
  1997. ZEND_PARSE_PARAMETERS_END();
  1998. php_date_instantiate(date_ce_date, return_value);
  1999. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
  2000. zval_ptr_dtor(return_value);
  2001. RETURN_FALSE;
  2002. }
  2003. }
  2004. /* }}} */
  2005. /* {{{ Returns new DateTimeImmutable object */
  2006. PHP_FUNCTION(date_create_immutable)
  2007. {
  2008. zval *timezone_object = NULL;
  2009. char *time_str = NULL;
  2010. size_t time_str_len = 0;
  2011. ZEND_PARSE_PARAMETERS_START(0, 2)
  2012. Z_PARAM_OPTIONAL
  2013. Z_PARAM_STRING(time_str, time_str_len)
  2014. Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
  2015. ZEND_PARSE_PARAMETERS_END();
  2016. php_date_instantiate(date_ce_immutable, return_value);
  2017. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
  2018. zval_ptr_dtor(return_value);
  2019. RETURN_FALSE;
  2020. }
  2021. }
  2022. /* }}} */
  2023. /* {{{ Returns new DateTime object formatted according to the specified format */
  2024. PHP_FUNCTION(date_create_from_format)
  2025. {
  2026. zval *timezone_object = NULL;
  2027. char *time_str = NULL, *format_str = NULL;
  2028. size_t time_str_len = 0, format_str_len = 0;
  2029. ZEND_PARSE_PARAMETERS_START(2, 3)
  2030. Z_PARAM_STRING(format_str, format_str_len)
  2031. Z_PARAM_PATH(time_str, time_str_len)
  2032. Z_PARAM_OPTIONAL
  2033. Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
  2034. ZEND_PARSE_PARAMETERS_END();
  2035. php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
  2036. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, PHP_DATE_INIT_FORMAT)) {
  2037. zval_ptr_dtor(return_value);
  2038. RETURN_FALSE;
  2039. }
  2040. }
  2041. /* }}} */
  2042. /* {{{ Returns new DateTime object formatted according to the specified format */
  2043. PHP_FUNCTION(date_create_immutable_from_format)
  2044. {
  2045. zval *timezone_object = NULL;
  2046. char *time_str = NULL, *format_str = NULL;
  2047. size_t time_str_len = 0, format_str_len = 0;
  2048. ZEND_PARSE_PARAMETERS_START(2, 3)
  2049. Z_PARAM_STRING(format_str, format_str_len)
  2050. Z_PARAM_PATH(time_str, time_str_len)
  2051. Z_PARAM_OPTIONAL
  2052. Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
  2053. ZEND_PARSE_PARAMETERS_END();
  2054. php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
  2055. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, PHP_DATE_INIT_FORMAT)) {
  2056. zval_ptr_dtor(return_value);
  2057. RETURN_FALSE;
  2058. }
  2059. }
  2060. /* }}} */
  2061. /* {{{ Creates new DateTime object */
  2062. PHP_METHOD(DateTime, __construct)
  2063. {
  2064. zval *timezone_object = NULL;
  2065. char *time_str = NULL;
  2066. size_t time_str_len = 0;
  2067. ZEND_PARSE_PARAMETERS_START(0, 2)
  2068. Z_PARAM_OPTIONAL
  2069. Z_PARAM_STRING(time_str, time_str_len)
  2070. Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
  2071. ZEND_PARSE_PARAMETERS_END();
  2072. php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
  2073. }
  2074. /* }}} */
  2075. /* {{{ Creates new DateTimeImmutable object */
  2076. PHP_METHOD(DateTimeImmutable, __construct)
  2077. {
  2078. zval *timezone_object = NULL;
  2079. char *time_str = NULL;
  2080. size_t time_str_len = 0;
  2081. ZEND_PARSE_PARAMETERS_START(0, 2)
  2082. Z_PARAM_OPTIONAL
  2083. Z_PARAM_STRING(time_str, time_str_len)
  2084. Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
  2085. ZEND_PARSE_PARAMETERS_END();
  2086. php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
  2087. }
  2088. /* }}} */
  2089. /* {{{ Creates new DateTime object from an existing immutable DateTimeImmutable object. */
  2090. PHP_METHOD(DateTime, createFromImmutable)
  2091. {
  2092. zval *datetimeimmutable_object = NULL;
  2093. php_date_obj *new_obj = NULL;
  2094. php_date_obj *old_obj = NULL;
  2095. ZEND_PARSE_PARAMETERS_START(1, 1)
  2096. Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
  2097. ZEND_PARSE_PARAMETERS_END();
  2098. old_obj = Z_PHPDATE_P(datetimeimmutable_object);
  2099. DATE_CHECK_INITIALIZED(old_obj->time, DateTimeImmutable);
  2100. php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
  2101. new_obj = Z_PHPDATE_P(return_value);
  2102. new_obj->time = timelib_time_clone(old_obj->time);
  2103. }
  2104. /* }}} */
  2105. /* {{{ Creates new DateTime object from an existing DateTimeInterface object. */
  2106. PHP_METHOD(DateTime, createFromInterface)
  2107. {
  2108. zval *datetimeinterface_object = NULL;
  2109. php_date_obj *new_obj = NULL;
  2110. php_date_obj *old_obj = NULL;
  2111. ZEND_PARSE_PARAMETERS_START(1, 1)
  2112. Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
  2113. ZEND_PARSE_PARAMETERS_END();
  2114. old_obj = Z_PHPDATE_P(datetimeinterface_object);
  2115. DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);
  2116. php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, return_value);
  2117. new_obj = Z_PHPDATE_P(return_value);
  2118. new_obj->time = timelib_time_clone(old_obj->time);
  2119. }
  2120. /* }}} */
  2121. /* {{{ Creates new DateTimeImmutable object from an existing mutable DateTime object. */
  2122. PHP_METHOD(DateTimeImmutable, createFromMutable)
  2123. {
  2124. zval *datetime_object = NULL;
  2125. php_date_obj *new_obj = NULL;
  2126. php_date_obj *old_obj = NULL;
  2127. ZEND_PARSE_PARAMETERS_START(1, 1)
  2128. Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_date)
  2129. ZEND_PARSE_PARAMETERS_END();
  2130. old_obj = Z_PHPDATE_P(datetime_object);
  2131. DATE_CHECK_INITIALIZED(old_obj->time, DateTime);
  2132. php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
  2133. new_obj = Z_PHPDATE_P(return_value);
  2134. new_obj->time = timelib_time_clone(old_obj->time);
  2135. }
  2136. /* }}} */
  2137. /* {{{ Creates new DateTimeImmutable object from an existing DateTimeInterface object. */
  2138. PHP_METHOD(DateTimeImmutable, createFromInterface)
  2139. {
  2140. zval *datetimeinterface_object = NULL;
  2141. php_date_obj *new_obj = NULL;
  2142. php_date_obj *old_obj = NULL;
  2143. ZEND_PARSE_PARAMETERS_START(1, 1)
  2144. Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
  2145. ZEND_PARSE_PARAMETERS_END();
  2146. old_obj = Z_PHPDATE_P(datetimeinterface_object);
  2147. DATE_CHECK_INITIALIZED(old_obj->time, DateTimeInterface);
  2148. php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, return_value);
  2149. new_obj = Z_PHPDATE_P(return_value);
  2150. new_obj->time = timelib_time_clone(old_obj->time);
  2151. }
  2152. /* }}} */
  2153. static bool php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht)
  2154. {
  2155. zval *z_date;
  2156. zval *z_timezone_type;
  2157. zval *z_timezone;
  2158. zval tmp_obj;
  2159. timelib_tzinfo *tzi;
  2160. z_date = zend_hash_str_find(myht, "date", sizeof("date")-1);
  2161. if (!z_date || Z_TYPE_P(z_date) != IS_STRING) {
  2162. return false;
  2163. }
  2164. z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type")-1);
  2165. if (!z_timezone_type || Z_TYPE_P(z_timezone_type) != IS_LONG) {
  2166. return false;
  2167. }
  2168. z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone")-1);
  2169. if (!z_timezone || Z_TYPE_P(z_timezone) != IS_STRING) {
  2170. return false;
  2171. }
  2172. switch (Z_LVAL_P(z_timezone_type)) {
  2173. case TIMELIB_ZONETYPE_OFFSET:
  2174. case TIMELIB_ZONETYPE_ABBR: {
  2175. zend_string *tmp = zend_string_concat3(
  2176. Z_STRVAL_P(z_date), Z_STRLEN_P(z_date), " ", 1,
  2177. Z_STRVAL_P(z_timezone), Z_STRLEN_P(z_timezone));
  2178. int ret = php_date_initialize(*dateobj, ZSTR_VAL(tmp), ZSTR_LEN(tmp), NULL, NULL, 0);
  2179. zend_string_release(tmp);
  2180. return 1 == ret;
  2181. }
  2182. case TIMELIB_ZONETYPE_ID: {
  2183. int ret;
  2184. php_timezone_obj *tzobj;
  2185. tzi = php_date_parse_tzfile(Z_STRVAL_P(z_timezone), DATE_TIMEZONEDB);
  2186. if (tzi == NULL) {
  2187. return false;
  2188. }
  2189. tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, &tmp_obj));
  2190. tzobj->type = TIMELIB_ZONETYPE_ID;
  2191. tzobj->tzi.tz = tzi;
  2192. tzobj->initialized = 1;
  2193. ret = php_date_initialize(*dateobj, Z_STRVAL_P(z_date), Z_STRLEN_P(z_date), NULL, &tmp_obj, 0);
  2194. zval_ptr_dtor(&tmp_obj);
  2195. return 1 == ret;
  2196. }
  2197. }
  2198. return false;
  2199. } /* }}} */
  2200. /* {{{ */
  2201. PHP_METHOD(DateTime, __set_state)
  2202. {
  2203. php_date_obj *dateobj;
  2204. zval *array;
  2205. HashTable *myht;
  2206. ZEND_PARSE_PARAMETERS_START(1, 1)
  2207. Z_PARAM_ARRAY(array)
  2208. ZEND_PARSE_PARAMETERS_END();
  2209. myht = Z_ARRVAL_P(array);
  2210. php_date_instantiate(date_ce_date, return_value);
  2211. dateobj = Z_PHPDATE_P(return_value);
  2212. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2213. zend_throw_error(NULL, "Invalid serialization data for DateTime object");
  2214. }
  2215. }
  2216. /* }}} */
  2217. /* {{{ */
  2218. PHP_METHOD(DateTimeImmutable, __set_state)
  2219. {
  2220. php_date_obj *dateobj;
  2221. zval *array;
  2222. HashTable *myht;
  2223. ZEND_PARSE_PARAMETERS_START(1, 1)
  2224. Z_PARAM_ARRAY(array)
  2225. ZEND_PARSE_PARAMETERS_END();
  2226. myht = Z_ARRVAL_P(array);
  2227. php_date_instantiate(date_ce_immutable, return_value);
  2228. dateobj = Z_PHPDATE_P(return_value);
  2229. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2230. zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
  2231. }
  2232. }
  2233. /* }}} */
  2234. /* {{{ */
  2235. PHP_METHOD(DateTime, __wakeup)
  2236. {
  2237. zval *object = ZEND_THIS;
  2238. php_date_obj *dateobj;
  2239. HashTable *myht;
  2240. ZEND_PARSE_PARAMETERS_NONE();
  2241. dateobj = Z_PHPDATE_P(object);
  2242. myht = Z_OBJPROP_P(object);
  2243. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2244. zend_throw_error(NULL, "Invalid serialization data for DateTime object");
  2245. }
  2246. }
  2247. /* }}} */
  2248. /* {{{ */
  2249. PHP_METHOD(DateTimeImmutable, __wakeup)
  2250. {
  2251. zval *object = ZEND_THIS;
  2252. php_date_obj *dateobj;
  2253. HashTable *myht;
  2254. ZEND_PARSE_PARAMETERS_NONE();
  2255. dateobj = Z_PHPDATE_P(object);
  2256. myht = Z_OBJPROP_P(object);
  2257. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2258. zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
  2259. }
  2260. }
  2261. /* }}} */
  2262. /* Helper function used to add an associative array of warnings and errors to a zval */
  2263. static void zval_from_error_container(zval *z, timelib_error_container *error) /* {{{ */
  2264. {
  2265. int i;
  2266. zval element;
  2267. add_assoc_long(z, "warning_count", error->warning_count);
  2268. array_init(&element);
  2269. for (i = 0; i < error->warning_count; i++) {
  2270. add_index_string(&element, error->warning_messages[i].position, error->warning_messages[i].message);
  2271. }
  2272. add_assoc_zval(z, "warnings", &element);
  2273. add_assoc_long(z, "error_count", error->error_count);
  2274. array_init(&element);
  2275. for (i = 0; i < error->error_count; i++) {
  2276. add_index_string(&element, error->error_messages[i].position, error->error_messages[i].message);
  2277. }
  2278. add_assoc_zval(z, "errors", &element);
  2279. } /* }}} */
  2280. /* {{{ Returns the warnings and errors found while parsing a date/time string. */
  2281. PHP_FUNCTION(date_get_last_errors)
  2282. {
  2283. ZEND_PARSE_PARAMETERS_NONE();
  2284. if (DATEG(last_errors)) {
  2285. array_init(return_value);
  2286. zval_from_error_container(return_value, DATEG(last_errors));
  2287. } else {
  2288. RETURN_FALSE;
  2289. }
  2290. }
  2291. /* }}} */
  2292. void php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAMETERS, timelib_time *parsed_time, timelib_error_container *error) /* {{{ */
  2293. {
  2294. zval element;
  2295. array_init(return_value);
  2296. #define PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(name, elem) \
  2297. if (parsed_time->elem == -99999) { \
  2298. add_assoc_bool(return_value, #name, 0); \
  2299. } else { \
  2300. add_assoc_long(return_value, #name, parsed_time->elem); \
  2301. }
  2302. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(year, y);
  2303. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(month, m);
  2304. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(day, d);
  2305. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(hour, h);
  2306. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(minute, i);
  2307. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(second, s);
  2308. if (parsed_time->us == -99999) {
  2309. add_assoc_bool(return_value, "fraction", 0);
  2310. } else {
  2311. add_assoc_double(return_value, "fraction", (double)parsed_time->us / 1000000.0);
  2312. }
  2313. zval_from_error_container(return_value, error);
  2314. timelib_error_container_dtor(error);
  2315. add_assoc_bool(return_value, "is_localtime", parsed_time->is_localtime);
  2316. if (parsed_time->is_localtime) {
  2317. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone_type, zone_type);
  2318. switch (parsed_time->zone_type) {
  2319. case TIMELIB_ZONETYPE_OFFSET:
  2320. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
  2321. add_assoc_bool(return_value, "is_dst", parsed_time->dst);
  2322. break;
  2323. case TIMELIB_ZONETYPE_ID:
  2324. if (parsed_time->tz_abbr) {
  2325. add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
  2326. }
  2327. if (parsed_time->tz_info) {
  2328. add_assoc_string(return_value, "tz_id", parsed_time->tz_info->name);
  2329. }
  2330. break;
  2331. case TIMELIB_ZONETYPE_ABBR:
  2332. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
  2333. add_assoc_bool(return_value, "is_dst", parsed_time->dst);
  2334. add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
  2335. break;
  2336. }
  2337. }
  2338. if (parsed_time->have_relative) {
  2339. array_init(&element);
  2340. add_assoc_long(&element, "year", parsed_time->relative.y);
  2341. add_assoc_long(&element, "month", parsed_time->relative.m);
  2342. add_assoc_long(&element, "day", parsed_time->relative.d);
  2343. add_assoc_long(&element, "hour", parsed_time->relative.h);
  2344. add_assoc_long(&element, "minute", parsed_time->relative.i);
  2345. add_assoc_long(&element, "second", parsed_time->relative.s);
  2346. if (parsed_time->relative.have_weekday_relative) {
  2347. add_assoc_long(&element, "weekday", parsed_time->relative.weekday);
  2348. }
  2349. if (parsed_time->relative.have_special_relative && (parsed_time->relative.special.type == TIMELIB_SPECIAL_WEEKDAY)) {
  2350. add_assoc_long(&element, "weekdays", parsed_time->relative.special.amount);
  2351. }
  2352. if (parsed_time->relative.first_last_day_of) {
  2353. add_assoc_bool(&element, parsed_time->relative.first_last_day_of == TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH ? "first_day_of_month" : "last_day_of_month", 1);
  2354. }
  2355. add_assoc_zval(return_value, "relative", &element);
  2356. }
  2357. timelib_time_dtor(parsed_time);
  2358. } /* }}} */
  2359. /* {{{ Returns associative array with detailed info about given date */
  2360. PHP_FUNCTION(date_parse)
  2361. {
  2362. zend_string *date;
  2363. timelib_error_container *error;
  2364. timelib_time *parsed_time;
  2365. ZEND_PARSE_PARAMETERS_START(1, 1)
  2366. Z_PARAM_STR(date)
  2367. ZEND_PARSE_PARAMETERS_END();
  2368. parsed_time = timelib_strtotime(ZSTR_VAL(date), ZSTR_LEN(date), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2369. php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
  2370. }
  2371. /* }}} */
  2372. /* {{{ Returns associative array with detailed info about given date */
  2373. PHP_FUNCTION(date_parse_from_format)
  2374. {
  2375. zend_string *date, *format;
  2376. timelib_error_container *error;
  2377. timelib_time *parsed_time;
  2378. ZEND_PARSE_PARAMETERS_START(2, 2)
  2379. Z_PARAM_STR(format)
  2380. Z_PARAM_PATH_STR(date)
  2381. ZEND_PARSE_PARAMETERS_END();
  2382. parsed_time = timelib_parse_from_format(ZSTR_VAL(format), ZSTR_VAL(date), ZSTR_LEN(date), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2383. php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
  2384. }
  2385. /* }}} */
  2386. /* {{{ Returns date formatted according to given format */
  2387. PHP_FUNCTION(date_format)
  2388. {
  2389. zval *object;
  2390. php_date_obj *dateobj;
  2391. char *format;
  2392. size_t format_len;
  2393. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interface, &format, &format_len) == FAILURE) {
  2394. RETURN_THROWS();
  2395. }
  2396. dateobj = Z_PHPDATE_P(object);
  2397. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2398. RETURN_STR(date_format(format, format_len, dateobj->time, dateobj->time->is_localtime));
  2399. }
  2400. /* }}} */
  2401. static int php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{ */
  2402. {
  2403. php_date_obj *dateobj;
  2404. timelib_time *tmp_time;
  2405. timelib_error_container *err = NULL;
  2406. dateobj = Z_PHPDATE_P(object);
  2407. if (!(dateobj->time)) {
  2408. zend_throw_error(NULL, "The DateTime object has not been correctly initialized by its constructor");
  2409. return 0;
  2410. }
  2411. tmp_time = timelib_strtotime(modify, modify_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2412. /* update last errors and warnings */
  2413. update_errors_warnings(err);
  2414. if (err && err->error_count) {
  2415. /* spit out the first library error message, at least */
  2416. php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
  2417. err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
  2418. timelib_time_dtor(tmp_time);
  2419. return 0;
  2420. }
  2421. memcpy(&dateobj->time->relative, &tmp_time->relative, sizeof(timelib_rel_time));
  2422. dateobj->time->have_relative = tmp_time->have_relative;
  2423. dateobj->time->sse_uptodate = 0;
  2424. if (tmp_time->y != -99999) {
  2425. dateobj->time->y = tmp_time->y;
  2426. }
  2427. if (tmp_time->m != -99999) {
  2428. dateobj->time->m = tmp_time->m;
  2429. }
  2430. if (tmp_time->d != -99999) {
  2431. dateobj->time->d = tmp_time->d;
  2432. }
  2433. if (tmp_time->h != -99999) {
  2434. dateobj->time->h = tmp_time->h;
  2435. if (tmp_time->i != -99999) {
  2436. dateobj->time->i = tmp_time->i;
  2437. if (tmp_time->s != -99999) {
  2438. dateobj->time->s = tmp_time->s;
  2439. } else {
  2440. dateobj->time->s = 0;
  2441. }
  2442. } else {
  2443. dateobj->time->i = 0;
  2444. dateobj->time->s = 0;
  2445. }
  2446. }
  2447. if (tmp_time->us != -99999) {
  2448. dateobj->time->us = tmp_time->us;
  2449. }
  2450. timelib_time_dtor(tmp_time);
  2451. timelib_update_ts(dateobj->time, NULL);
  2452. timelib_update_from_sse(dateobj->time);
  2453. dateobj->time->have_relative = 0;
  2454. memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
  2455. return 1;
  2456. } /* }}} */
  2457. /* {{{ Alters the timestamp. */
  2458. PHP_FUNCTION(date_modify)
  2459. {
  2460. zval *object;
  2461. char *modify;
  2462. size_t modify_len;
  2463. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_date, &modify, &modify_len) == FAILURE) {
  2464. RETURN_THROWS();
  2465. }
  2466. if (!php_date_modify(object, modify, modify_len)) {
  2467. RETURN_FALSE;
  2468. }
  2469. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2470. }
  2471. /* }}} */
  2472. /* {{{ */
  2473. PHP_METHOD(DateTimeImmutable, modify)
  2474. {
  2475. zval *object, new_object;
  2476. char *modify;
  2477. size_t modify_len;
  2478. object = ZEND_THIS;
  2479. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &modify, &modify_len) == FAILURE) {
  2480. RETURN_THROWS();
  2481. }
  2482. date_clone_immutable(object, &new_object);
  2483. if (!php_date_modify(&new_object, modify, modify_len)) {
  2484. zval_ptr_dtor(&new_object);
  2485. RETURN_FALSE;
  2486. }
  2487. RETURN_OBJ(Z_OBJ(new_object));
  2488. }
  2489. /* }}} */
  2490. static void php_date_add(zval *object, zval *interval, zval *return_value) /* {{{ */
  2491. {
  2492. php_date_obj *dateobj;
  2493. php_interval_obj *intobj;
  2494. timelib_time *new_time;
  2495. dateobj = Z_PHPDATE_P(object);
  2496. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2497. intobj = Z_PHPINTERVAL_P(interval);
  2498. DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);
  2499. if (intobj->civil_or_wall == PHP_DATE_WALL) {
  2500. new_time = timelib_add_wall(dateobj->time, intobj->diff);
  2501. } else {
  2502. new_time = timelib_add(dateobj->time, intobj->diff);
  2503. }
  2504. timelib_time_dtor(dateobj->time);
  2505. dateobj->time = new_time;
  2506. } /* }}} */
  2507. /* {{{ Adds an interval to the current date in object. */
  2508. PHP_FUNCTION(date_add)
  2509. {
  2510. zval *object, *interval;
  2511. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
  2512. RETURN_THROWS();
  2513. }
  2514. php_date_add(object, interval, return_value);
  2515. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2516. }
  2517. /* }}} */
  2518. /* {{{ */
  2519. PHP_METHOD(DateTimeImmutable, add)
  2520. {
  2521. zval *object, *interval, new_object;
  2522. object = ZEND_THIS;
  2523. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &interval, date_ce_interval) == FAILURE) {
  2524. RETURN_THROWS();
  2525. }
  2526. date_clone_immutable(object, &new_object);
  2527. php_date_add(&new_object, interval, return_value);
  2528. RETURN_OBJ(Z_OBJ(new_object));
  2529. }
  2530. /* }}} */
  2531. static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
  2532. {
  2533. php_date_obj *dateobj;
  2534. php_interval_obj *intobj;
  2535. timelib_time *new_time;
  2536. dateobj = Z_PHPDATE_P(object);
  2537. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2538. intobj = Z_PHPINTERVAL_P(interval);
  2539. DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);
  2540. if (intobj->diff->have_special_relative) {
  2541. php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
  2542. return;
  2543. }
  2544. if (intobj->civil_or_wall == PHP_DATE_WALL) {
  2545. new_time = timelib_sub_wall(dateobj->time, intobj->diff);
  2546. } else {
  2547. new_time = timelib_sub(dateobj->time, intobj->diff);
  2548. }
  2549. timelib_time_dtor(dateobj->time);
  2550. dateobj->time = new_time;
  2551. } /* }}} */
  2552. /* {{{ Subtracts an interval to the current date in object. */
  2553. PHP_FUNCTION(date_sub)
  2554. {
  2555. zval *object, *interval;
  2556. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
  2557. RETURN_THROWS();
  2558. }
  2559. php_date_sub(object, interval, return_value);
  2560. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2561. }
  2562. /* }}} */
  2563. /* {{{ */
  2564. PHP_METHOD(DateTimeImmutable, sub)
  2565. {
  2566. zval *object, *interval, new_object;
  2567. object = ZEND_THIS;
  2568. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &interval, date_ce_interval) == FAILURE) {
  2569. RETURN_THROWS();
  2570. }
  2571. date_clone_immutable(object, &new_object);
  2572. php_date_sub(&new_object, interval, return_value);
  2573. RETURN_OBJ(Z_OBJ(new_object));
  2574. }
  2575. /* }}} */
  2576. static void set_timezone_from_timelib_time(php_timezone_obj *tzobj, timelib_time *t)
  2577. {
  2578. tzobj->initialized = 1;
  2579. tzobj->type = t->zone_type;
  2580. switch (t->zone_type) {
  2581. case TIMELIB_ZONETYPE_ID:
  2582. tzobj->tzi.tz = t->tz_info;
  2583. break;
  2584. case TIMELIB_ZONETYPE_OFFSET:
  2585. tzobj->tzi.utc_offset = t->z;
  2586. break;
  2587. case TIMELIB_ZONETYPE_ABBR:
  2588. tzobj->tzi.z.utc_offset = t->z;
  2589. tzobj->tzi.z.dst = t->dst;
  2590. tzobj->tzi.z.abbr = timelib_strdup(t->tz_abbr);
  2591. break;
  2592. }
  2593. }
  2594. /* {{{ Return new DateTimeZone object relative to give DateTime */
  2595. PHP_FUNCTION(date_timezone_get)
  2596. {
  2597. zval *object;
  2598. php_date_obj *dateobj;
  2599. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
  2600. RETURN_THROWS();
  2601. }
  2602. dateobj = Z_PHPDATE_P(object);
  2603. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2604. if (dateobj->time->is_localtime) {
  2605. php_timezone_obj *tzobj;
  2606. php_date_instantiate(date_ce_timezone, return_value);
  2607. tzobj = Z_PHPTIMEZONE_P(return_value);
  2608. set_timezone_from_timelib_time(tzobj, dateobj->time);
  2609. } else {
  2610. RETURN_FALSE;
  2611. }
  2612. }
  2613. /* }}} */
  2614. static void php_date_timezone_set(zval *object, zval *timezone_object, zval *return_value) /* {{{ */
  2615. {
  2616. php_date_obj *dateobj;
  2617. php_timezone_obj *tzobj;
  2618. dateobj = Z_PHPDATE_P(object);
  2619. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2620. tzobj = Z_PHPTIMEZONE_P(timezone_object);
  2621. switch (tzobj->type) {
  2622. case TIMELIB_ZONETYPE_OFFSET:
  2623. timelib_set_timezone_from_offset(dateobj->time, tzobj->tzi.utc_offset);
  2624. break;
  2625. case TIMELIB_ZONETYPE_ABBR:
  2626. timelib_set_timezone_from_abbr(dateobj->time, tzobj->tzi.z);
  2627. break;
  2628. case TIMELIB_ZONETYPE_ID:
  2629. timelib_set_timezone(dateobj->time, tzobj->tzi.tz);
  2630. break;
  2631. }
  2632. timelib_unixtime2local(dateobj->time, dateobj->time->sse);
  2633. } /* }}} */
  2634. /* {{{ Sets the timezone for the DateTime object. */
  2635. PHP_FUNCTION(date_timezone_set)
  2636. {
  2637. zval *object;
  2638. zval *timezone_object;
  2639. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &timezone_object, date_ce_timezone) == FAILURE) {
  2640. RETURN_THROWS();
  2641. }
  2642. php_date_timezone_set(object, timezone_object, return_value);
  2643. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2644. }
  2645. /* }}} */
  2646. /* {{{ */
  2647. PHP_METHOD(DateTimeImmutable, setTimezone)
  2648. {
  2649. zval *object, new_object;
  2650. zval *timezone_object;
  2651. object = ZEND_THIS;
  2652. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &timezone_object, date_ce_timezone) == FAILURE) {
  2653. RETURN_THROWS();
  2654. }
  2655. date_clone_immutable(object, &new_object);
  2656. php_date_timezone_set(&new_object, timezone_object, return_value);
  2657. RETURN_OBJ(Z_OBJ(new_object));
  2658. }
  2659. /* }}} */
  2660. /* {{{ Returns the DST offset. */
  2661. PHP_FUNCTION(date_offset_get)
  2662. {
  2663. zval *object;
  2664. php_date_obj *dateobj;
  2665. timelib_time_offset *offset;
  2666. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
  2667. RETURN_THROWS();
  2668. }
  2669. dateobj = Z_PHPDATE_P(object);
  2670. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2671. if (dateobj->time->is_localtime) {
  2672. switch (dateobj->time->zone_type) {
  2673. case TIMELIB_ZONETYPE_ID:
  2674. offset = timelib_get_time_zone_info(dateobj->time->sse, dateobj->time->tz_info);
  2675. RETVAL_LONG(offset->offset);
  2676. timelib_time_offset_dtor(offset);
  2677. break;
  2678. case TIMELIB_ZONETYPE_OFFSET:
  2679. RETVAL_LONG(dateobj->time->z);
  2680. break;
  2681. case TIMELIB_ZONETYPE_ABBR:
  2682. RETVAL_LONG((dateobj->time->z + (3600 * dateobj->time->dst)));
  2683. break;
  2684. }
  2685. return;
  2686. } else {
  2687. RETURN_LONG(0);
  2688. }
  2689. }
  2690. /* }}} */
  2691. static void php_date_time_set(zval *object, zend_long h, zend_long i, zend_long s, zend_long ms, zval *return_value) /* {{{ */
  2692. {
  2693. php_date_obj *dateobj;
  2694. dateobj = Z_PHPDATE_P(object);
  2695. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2696. dateobj->time->h = h;
  2697. dateobj->time->i = i;
  2698. dateobj->time->s = s;
  2699. dateobj->time->us = ms;
  2700. timelib_update_ts(dateobj->time, NULL);
  2701. timelib_update_from_sse(dateobj->time);
  2702. } /* }}} */
  2703. /* {{{ Sets the time. */
  2704. PHP_FUNCTION(date_time_set)
  2705. {
  2706. zval *object;
  2707. zend_long h, i, s = 0, ms = 0;
  2708. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|ll", &object, date_ce_date, &h, &i, &s, &ms) == FAILURE) {
  2709. RETURN_THROWS();
  2710. }
  2711. php_date_time_set(object, h, i, s, ms, return_value);
  2712. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2713. }
  2714. /* }}} */
  2715. /* {{{ */
  2716. PHP_METHOD(DateTimeImmutable, setTime)
  2717. {
  2718. zval *object, new_object;
  2719. zend_long h, i, s = 0, ms = 0;
  2720. object = ZEND_THIS;
  2721. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|ll", &h, &i, &s, &ms) == FAILURE) {
  2722. RETURN_THROWS();
  2723. }
  2724. date_clone_immutable(object, &new_object);
  2725. php_date_time_set(&new_object, h, i, s, ms, return_value);
  2726. RETURN_OBJ(Z_OBJ(new_object));
  2727. }
  2728. /* }}} */
  2729. static void php_date_date_set(zval *object, zend_long y, zend_long m, zend_long d, zval *return_value) /* {{{ */
  2730. {
  2731. php_date_obj *dateobj;
  2732. dateobj = Z_PHPDATE_P(object);
  2733. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2734. dateobj->time->y = y;
  2735. dateobj->time->m = m;
  2736. dateobj->time->d = d;
  2737. timelib_update_ts(dateobj->time, NULL);
  2738. } /* }}} */
  2739. /* {{{ Sets the date. */
  2740. PHP_FUNCTION(date_date_set)
  2741. {
  2742. zval *object;
  2743. zend_long y, m, d;
  2744. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olll", &object, date_ce_date, &y, &m, &d) == FAILURE) {
  2745. RETURN_THROWS();
  2746. }
  2747. php_date_date_set(object, y, m, d, return_value);
  2748. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2749. }
  2750. /* }}} */
  2751. /* {{{ */
  2752. PHP_METHOD(DateTimeImmutable, setDate)
  2753. {
  2754. zval *object, new_object;
  2755. zend_long y, m, d;
  2756. object = ZEND_THIS;
  2757. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &y, &m, &d) == FAILURE) {
  2758. RETURN_THROWS();
  2759. }
  2760. date_clone_immutable(object, &new_object);
  2761. php_date_date_set(&new_object, y, m, d, return_value);
  2762. RETURN_OBJ(Z_OBJ(new_object));
  2763. }
  2764. /* }}} */
  2765. static void php_date_isodate_set(zval *object, zend_long y, zend_long w, zend_long d, zval *return_value) /* {{{ */
  2766. {
  2767. php_date_obj *dateobj;
  2768. dateobj = Z_PHPDATE_P(object);
  2769. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2770. dateobj->time->y = y;
  2771. dateobj->time->m = 1;
  2772. dateobj->time->d = 1;
  2773. memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
  2774. dateobj->time->relative.d = timelib_daynr_from_weeknr(y, w, d);
  2775. dateobj->time->have_relative = 1;
  2776. timelib_update_ts(dateobj->time, NULL);
  2777. } /* }}} */
  2778. /* {{{ Sets the ISO date. */
  2779. PHP_FUNCTION(date_isodate_set)
  2780. {
  2781. zval *object;
  2782. zend_long y, w, d = 1;
  2783. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|l", &object, date_ce_date, &y, &w, &d) == FAILURE) {
  2784. RETURN_THROWS();
  2785. }
  2786. php_date_isodate_set(object, y, w, d, return_value);
  2787. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2788. }
  2789. /* }}} */
  2790. /* {{{ */
  2791. PHP_METHOD(DateTimeImmutable, setISODate)
  2792. {
  2793. zval *object, new_object;
  2794. zend_long y, w, d = 1;
  2795. object = ZEND_THIS;
  2796. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|l", &y, &w, &d) == FAILURE) {
  2797. RETURN_THROWS();
  2798. }
  2799. date_clone_immutable(object, &new_object);
  2800. php_date_isodate_set(&new_object, y, w, d, return_value);
  2801. RETURN_OBJ(Z_OBJ(new_object));
  2802. }
  2803. /* }}} */
  2804. static void php_date_timestamp_set(zval *object, zend_long timestamp, zval *return_value) /* {{{ */
  2805. {
  2806. php_date_obj *dateobj;
  2807. dateobj = Z_PHPDATE_P(object);
  2808. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2809. timelib_unixtime2local(dateobj->time, (timelib_sll)timestamp);
  2810. timelib_update_ts(dateobj->time, NULL);
  2811. php_date_set_time_fraction(dateobj->time, 0);
  2812. } /* }}} */
  2813. /* {{{ Sets the date and time based on an Unix timestamp. */
  2814. PHP_FUNCTION(date_timestamp_set)
  2815. {
  2816. zval *object;
  2817. zend_long timestamp;
  2818. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &object, date_ce_date, &timestamp) == FAILURE) {
  2819. RETURN_THROWS();
  2820. }
  2821. php_date_timestamp_set(object, timestamp, return_value);
  2822. RETURN_OBJ_COPY(Z_OBJ_P(object));
  2823. }
  2824. /* }}} */
  2825. /* {{{ */
  2826. PHP_METHOD(DateTimeImmutable, setTimestamp)
  2827. {
  2828. zval *object, new_object;
  2829. zend_long timestamp;
  2830. object = ZEND_THIS;
  2831. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &timestamp) == FAILURE) {
  2832. RETURN_THROWS();
  2833. }
  2834. date_clone_immutable(object, &new_object);
  2835. php_date_timestamp_set(&new_object, timestamp, return_value);
  2836. RETURN_OBJ(Z_OBJ(new_object));
  2837. }
  2838. /* }}} */
  2839. /* {{{ Gets the Unix timestamp. */
  2840. PHP_FUNCTION(date_timestamp_get)
  2841. {
  2842. zval *object;
  2843. php_date_obj *dateobj;
  2844. zend_long timestamp;
  2845. int epoch_does_not_fit_in_zend_long;
  2846. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
  2847. RETURN_THROWS();
  2848. }
  2849. dateobj = Z_PHPDATE_P(object);
  2850. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2851. if (!dateobj->time->sse_uptodate) {
  2852. timelib_update_ts(dateobj->time, NULL);
  2853. }
  2854. timestamp = timelib_date_to_int(dateobj->time, &epoch_does_not_fit_in_zend_long);
  2855. if (epoch_does_not_fit_in_zend_long) {
  2856. zend_value_error("Epoch doesn't fit in a PHP integer");
  2857. RETURN_THROWS();
  2858. }
  2859. RETURN_LONG(timestamp);
  2860. }
  2861. /* }}} */
  2862. /* {{{ Returns the difference between two DateTime objects. */
  2863. PHP_FUNCTION(date_diff)
  2864. {
  2865. zval *object1, *object2;
  2866. php_date_obj *dateobj1, *dateobj2;
  2867. php_interval_obj *interval;
  2868. bool absolute = 0;
  2869. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO|b", &object1, date_ce_interface, &object2, date_ce_interface, &absolute) == FAILURE) {
  2870. RETURN_THROWS();
  2871. }
  2872. dateobj1 = Z_PHPDATE_P(object1);
  2873. dateobj2 = Z_PHPDATE_P(object2);
  2874. DATE_CHECK_INITIALIZED(dateobj1->time, DateTimeInterface);
  2875. DATE_CHECK_INITIALIZED(dateobj2->time, DateTimeInterface);
  2876. php_date_instantiate(date_ce_interval, return_value);
  2877. interval = Z_PHPINTERVAL_P(return_value);
  2878. interval->diff = timelib_diff(dateobj1->time, dateobj2->time);
  2879. if (absolute) {
  2880. interval->diff->invert = 0;
  2881. }
  2882. interval->initialized = 1;
  2883. interval->civil_or_wall = PHP_DATE_CIVIL;
  2884. }
  2885. /* }}} */
  2886. static int timezone_initialize(php_timezone_obj *tzobj, const char *tz, size_t tz_len) /* {{{ */
  2887. {
  2888. timelib_time *dummy_t = ecalloc(1, sizeof(timelib_time));
  2889. int dst, not_found;
  2890. const char *orig_tz = tz;
  2891. if (strlen(tz) != tz_len) {
  2892. php_error_docref(NULL, E_WARNING, "Timezone must not contain null bytes");
  2893. efree(dummy_t);
  2894. return FAILURE;
  2895. }
  2896. dummy_t->z = timelib_parse_zone(&tz, &dst, dummy_t, &not_found, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2897. dummy_t->dst = dst;
  2898. if (!not_found && (*tz != '\0')) {
  2899. php_error_docref(NULL, E_WARNING, "Unknown or bad timezone (%s)", orig_tz);
  2900. timelib_free(dummy_t->tz_abbr);
  2901. efree(dummy_t);
  2902. return FAILURE;
  2903. }
  2904. if (not_found) {
  2905. php_error_docref(NULL, E_WARNING, "Unknown or bad timezone (%s)", orig_tz);
  2906. efree(dummy_t);
  2907. return FAILURE;
  2908. } else {
  2909. set_timezone_from_timelib_time(tzobj, dummy_t);
  2910. timelib_free(dummy_t->tz_abbr);
  2911. efree(dummy_t);
  2912. return SUCCESS;
  2913. }
  2914. } /* }}} */
  2915. /* {{{ Returns new DateTimeZone object */
  2916. PHP_FUNCTION(timezone_open)
  2917. {
  2918. zend_string *tz;
  2919. php_timezone_obj *tzobj;
  2920. ZEND_PARSE_PARAMETERS_START(1, 1)
  2921. Z_PARAM_PATH_STR(tz) /* To prevent null bytes */
  2922. ZEND_PARSE_PARAMETERS_END();
  2923. tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, return_value));
  2924. if (SUCCESS != timezone_initialize(tzobj, ZSTR_VAL(tz), ZSTR_LEN(tz))) {
  2925. zval_ptr_dtor(return_value);
  2926. RETURN_FALSE;
  2927. }
  2928. }
  2929. /* }}} */
  2930. /* {{{ Creates new DateTimeZone object. */
  2931. PHP_METHOD(DateTimeZone, __construct)
  2932. {
  2933. zend_string *tz;
  2934. php_timezone_obj *tzobj;
  2935. zend_error_handling error_handling;
  2936. ZEND_PARSE_PARAMETERS_START(1, 1)
  2937. Z_PARAM_PATH_STR(tz) /* To prevent null bytes */
  2938. ZEND_PARSE_PARAMETERS_END();
  2939. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  2940. tzobj = Z_PHPTIMEZONE_P(ZEND_THIS);
  2941. timezone_initialize(tzobj, ZSTR_VAL(tz), ZSTR_LEN(tz));
  2942. zend_restore_error_handling(&error_handling);
  2943. }
  2944. /* }}} */
  2945. static int php_date_timezone_initialize_from_hash(zval **return_value, php_timezone_obj **tzobj, HashTable *myht) /* {{{ */
  2946. {
  2947. zval *z_timezone_type;
  2948. if ((z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type") - 1)) != NULL) {
  2949. zval *z_timezone;
  2950. if ((z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone") - 1)) != NULL) {
  2951. if (Z_TYPE_P(z_timezone_type) != IS_LONG) {
  2952. return FAILURE;
  2953. }
  2954. if (Z_TYPE_P(z_timezone) != IS_STRING) {
  2955. return FAILURE;
  2956. }
  2957. if (SUCCESS == timezone_initialize(*tzobj, Z_STRVAL_P(z_timezone), Z_STRLEN_P(z_timezone))) {
  2958. return SUCCESS;
  2959. }
  2960. }
  2961. }
  2962. return FAILURE;
  2963. } /* }}} */
  2964. /* {{{ */
  2965. PHP_METHOD(DateTimeZone, __set_state)
  2966. {
  2967. php_timezone_obj *tzobj;
  2968. zval *array;
  2969. HashTable *myht;
  2970. ZEND_PARSE_PARAMETERS_START(1, 1)
  2971. Z_PARAM_ARRAY(array)
  2972. ZEND_PARSE_PARAMETERS_END();
  2973. myht = Z_ARRVAL_P(array);
  2974. php_date_instantiate(date_ce_timezone, return_value);
  2975. tzobj = Z_PHPTIMEZONE_P(return_value);
  2976. if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
  2977. zend_throw_error(NULL, "Timezone initialization failed");
  2978. zval_ptr_dtor(return_value);
  2979. }
  2980. }
  2981. /* }}} */
  2982. /* {{{ */
  2983. PHP_METHOD(DateTimeZone, __wakeup)
  2984. {
  2985. zval *object = ZEND_THIS;
  2986. php_timezone_obj *tzobj;
  2987. HashTable *myht;
  2988. ZEND_PARSE_PARAMETERS_NONE();
  2989. tzobj = Z_PHPTIMEZONE_P(object);
  2990. myht = Z_OBJPROP_P(object);
  2991. if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
  2992. zend_throw_error(NULL, "Timezone initialization failed");
  2993. }
  2994. }
  2995. /* }}} */
  2996. /* {{{ Returns the name of the timezone. */
  2997. PHP_FUNCTION(timezone_name_get)
  2998. {
  2999. zval *object;
  3000. php_timezone_obj *tzobj;
  3001. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
  3002. RETURN_THROWS();
  3003. }
  3004. tzobj = Z_PHPTIMEZONE_P(object);
  3005. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3006. php_timezone_to_string(tzobj, return_value);
  3007. }
  3008. /* }}} */
  3009. /* {{{ Returns the timezone name from abbreviation */
  3010. PHP_FUNCTION(timezone_name_from_abbr)
  3011. {
  3012. zend_string *abbr;
  3013. const char *tzid;
  3014. zend_long gmtoffset = -1;
  3015. zend_long isdst = -1;
  3016. ZEND_PARSE_PARAMETERS_START(1, 3)
  3017. Z_PARAM_STR(abbr)
  3018. Z_PARAM_OPTIONAL
  3019. Z_PARAM_LONG(gmtoffset)
  3020. Z_PARAM_LONG(isdst)
  3021. ZEND_PARSE_PARAMETERS_END();
  3022. tzid = timelib_timezone_id_from_abbr(ZSTR_VAL(abbr), gmtoffset, isdst);
  3023. if (tzid) {
  3024. RETURN_STRING(tzid);
  3025. } else {
  3026. RETURN_FALSE;
  3027. }
  3028. }
  3029. /* }}} */
  3030. /* {{{ Returns the timezone offset. */
  3031. PHP_FUNCTION(timezone_offset_get)
  3032. {
  3033. zval *object, *dateobject;
  3034. php_timezone_obj *tzobj;
  3035. php_date_obj *dateobj;
  3036. timelib_time_offset *offset;
  3037. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_timezone, &dateobject, date_ce_interface) == FAILURE) {
  3038. RETURN_THROWS();
  3039. }
  3040. tzobj = Z_PHPTIMEZONE_P(object);
  3041. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3042. dateobj = Z_PHPDATE_P(dateobject);
  3043. DATE_CHECK_INITIALIZED(dateobj->time, DateTimeInterface);
  3044. switch (tzobj->type) {
  3045. case TIMELIB_ZONETYPE_ID:
  3046. offset = timelib_get_time_zone_info(dateobj->time->sse, tzobj->tzi.tz);
  3047. RETVAL_LONG(offset->offset);
  3048. timelib_time_offset_dtor(offset);
  3049. break;
  3050. case TIMELIB_ZONETYPE_OFFSET:
  3051. RETURN_LONG(tzobj->tzi.utc_offset);
  3052. break;
  3053. case TIMELIB_ZONETYPE_ABBR:
  3054. RETURN_LONG(tzobj->tzi.z.utc_offset + (tzobj->tzi.z.dst * 3600));
  3055. break;
  3056. }
  3057. }
  3058. /* }}} */
  3059. /* {{{ Returns numerically indexed array containing associative array for all transitions in the specified range for the timezone. */
  3060. PHP_FUNCTION(timezone_transitions_get)
  3061. {
  3062. zval *object, element;
  3063. php_timezone_obj *tzobj;
  3064. unsigned int begin = 0, found;
  3065. zend_long timestamp_begin = ZEND_LONG_MIN, timestamp_end = INT32_MAX;
  3066. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ll", &object, date_ce_timezone, &timestamp_begin, &timestamp_end) == FAILURE) {
  3067. RETURN_THROWS();
  3068. }
  3069. tzobj = Z_PHPTIMEZONE_P(object);
  3070. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3071. if (tzobj->type != TIMELIB_ZONETYPE_ID) {
  3072. RETURN_FALSE;
  3073. }
  3074. #define add_nominal() \
  3075. array_init(&element); \
  3076. add_assoc_long(&element, "ts", timestamp_begin); \
  3077. add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, timestamp_begin, 0)); \
  3078. add_assoc_long(&element, "offset", tzobj->tzi.tz->type[0].offset); \
  3079. add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[0].isdst); \
  3080. add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[0].abbr_idx]); \
  3081. add_next_index_zval(return_value, &element);
  3082. #define add(i,ts) \
  3083. array_init(&element); \
  3084. add_assoc_long(&element, "ts", ts); \
  3085. add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, ts, 0)); \
  3086. add_assoc_long(&element, "offset", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].offset); \
  3087. add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].isdst); \
  3088. add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].abbr_idx]); \
  3089. add_next_index_zval(return_value, &element);
  3090. #define add_by_index(i,ts) \
  3091. array_init(&element); \
  3092. add_assoc_long(&element, "ts", ts); \
  3093. add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, ts, 0)); \
  3094. add_assoc_long(&element, "offset", tzobj->tzi.tz->type[i].offset); \
  3095. add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[i].isdst); \
  3096. add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[i].abbr_idx]); \
  3097. add_next_index_zval(return_value, &element);
  3098. #define add_from_tto(to,ts) \
  3099. array_init(&element); \
  3100. add_assoc_long(&element, "ts", ts); \
  3101. add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, ts, 0)); \
  3102. add_assoc_long(&element, "offset", (to)->offset); \
  3103. add_assoc_bool(&element, "isdst", (to)->is_dst); \
  3104. add_assoc_string(&element, "abbr", (to)->abbr); \
  3105. add_next_index_zval(return_value, &element);
  3106. #define add_last() add(tzobj->tzi.tz->bit64.timecnt - 1, timestamp_begin)
  3107. array_init(return_value);
  3108. if (timestamp_begin == ZEND_LONG_MIN) {
  3109. add_nominal();
  3110. begin = 0;
  3111. found = 1;
  3112. } else {
  3113. begin = 0;
  3114. found = 0;
  3115. if (tzobj->tzi.tz->bit64.timecnt > 0) {
  3116. do {
  3117. if (tzobj->tzi.tz->trans[begin] > timestamp_begin) {
  3118. if (begin > 0) {
  3119. add(begin - 1, timestamp_begin);
  3120. } else {
  3121. add_nominal();
  3122. }
  3123. found = 1;
  3124. break;
  3125. }
  3126. begin++;
  3127. } while (begin < tzobj->tzi.tz->bit64.timecnt);
  3128. }
  3129. }
  3130. if (!found) {
  3131. if (tzobj->tzi.tz->bit64.timecnt > 0) {
  3132. if (tzobj->tzi.tz->posix_info && tzobj->tzi.tz->posix_info->dst_end) {
  3133. timelib_time_offset *tto = timelib_get_time_zone_info(timestamp_begin, tzobj->tzi.tz);
  3134. add_from_tto(tto, timestamp_begin);
  3135. timelib_time_offset_dtor(tto);
  3136. } else {
  3137. add_last();
  3138. }
  3139. } else {
  3140. add_nominal();
  3141. }
  3142. } else {
  3143. unsigned int i;
  3144. for (i = begin; i < tzobj->tzi.tz->bit64.timecnt; ++i) {
  3145. if (tzobj->tzi.tz->trans[i] < timestamp_end) {
  3146. add(i, tzobj->tzi.tz->trans[i]);
  3147. } else {
  3148. return;
  3149. }
  3150. }
  3151. }
  3152. if (tzobj->tzi.tz->posix_info && tzobj->tzi.tz->posix_info->dst_end) {
  3153. int i, j;
  3154. timelib_sll start_y, end_y, dummy_m, dummy_d;
  3155. timelib_sll last_transition_ts = tzobj->tzi.tz->trans[tzobj->tzi.tz->bit64.timecnt - 1];
  3156. /* Find out year for last transition */
  3157. timelib_unixtime2date(last_transition_ts, &start_y, &dummy_m, &dummy_d);
  3158. /* Find out year for final boundary timestamp */
  3159. timelib_unixtime2date(timestamp_end, &end_y, &dummy_m, &dummy_d);
  3160. for (i = start_y; i <= end_y; i++) {
  3161. timelib_posix_transitions transitions = { 0 };
  3162. timelib_get_transitions_for_year(tzobj->tzi.tz, i, &transitions);
  3163. for (j = 0; j < transitions.count; j++) {
  3164. if (transitions.times[j] <= last_transition_ts) {
  3165. continue;
  3166. }
  3167. if (transitions.times[j] < timestamp_begin) {
  3168. continue;
  3169. }
  3170. if (transitions.times[j] > timestamp_end) {
  3171. return;
  3172. }
  3173. add_by_index(transitions.types[j], transitions.times[j]);
  3174. }
  3175. }
  3176. }
  3177. }
  3178. /* }}} */
  3179. /* {{{ Returns location information for a timezone, including country code, latitude/longitude and comments */
  3180. PHP_FUNCTION(timezone_location_get)
  3181. {
  3182. zval *object;
  3183. php_timezone_obj *tzobj;
  3184. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
  3185. RETURN_THROWS();
  3186. }
  3187. tzobj = Z_PHPTIMEZONE_P(object);
  3188. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3189. if (tzobj->type != TIMELIB_ZONETYPE_ID) {
  3190. RETURN_FALSE;
  3191. }
  3192. array_init(return_value);
  3193. add_assoc_string(return_value, "country_code", tzobj->tzi.tz->location.country_code);
  3194. add_assoc_double(return_value, "latitude", tzobj->tzi.tz->location.latitude);
  3195. add_assoc_double(return_value, "longitude", tzobj->tzi.tz->location.longitude);
  3196. add_assoc_string(return_value, "comments", tzobj->tzi.tz->location.comments);
  3197. }
  3198. /* }}} */
  3199. static bool date_interval_initialize(timelib_rel_time **rt, /*const*/ char *format, size_t format_length) /* {{{ */
  3200. {
  3201. timelib_time *b = NULL, *e = NULL;
  3202. timelib_rel_time *p = NULL;
  3203. int r = 0;
  3204. bool retval = false;
  3205. timelib_error_container *errors;
  3206. timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
  3207. if (errors->error_count > 0) {
  3208. zend_throw_exception_ex(NULL, 0, "Unknown or bad format (%s)", format);
  3209. retval = false;
  3210. if (p) {
  3211. timelib_rel_time_dtor(p);
  3212. }
  3213. } else {
  3214. if(p) {
  3215. *rt = p;
  3216. retval = true;
  3217. } else {
  3218. if(b && e) {
  3219. timelib_update_ts(b, NULL);
  3220. timelib_update_ts(e, NULL);
  3221. *rt = timelib_diff(b, e);
  3222. retval = true;
  3223. } else {
  3224. zend_throw_exception_ex(NULL, 0, "Failed to parse interval (%s)", format);
  3225. retval = false;
  3226. }
  3227. }
  3228. }
  3229. timelib_error_container_dtor(errors);
  3230. timelib_free(b);
  3231. timelib_free(e);
  3232. return retval;
  3233. } /* }}} */
  3234. static int date_interval_compare_objects(zval *o1, zval *o2) {
  3235. ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
  3236. /* There is no well defined way to compare intervals like P1M and P30D, which may compare
  3237. * smaller, equal or greater depending on the point in time at which the interval starts. As
  3238. * such, we treat DateInterval objects are non-comparable and emit a warning. */
  3239. zend_error(E_WARNING, "Cannot compare DateInterval objects");
  3240. return ZEND_UNCOMPARABLE;
  3241. }
  3242. /* {{{ date_interval_read_property */
  3243. static zval *date_interval_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
  3244. {
  3245. php_interval_obj *obj;
  3246. zval *retval;
  3247. timelib_sll value = -1;
  3248. double fvalue = -1;
  3249. obj = php_interval_obj_from_obj(object);
  3250. if (!obj->initialized) {
  3251. retval = zend_std_read_property(object, name, type, cache_slot, rv);
  3252. return retval;
  3253. }
  3254. #define GET_VALUE_FROM_STRUCT(n,m) \
  3255. if (zend_string_equals_literal(name, m)) { \
  3256. value = obj->diff->n; \
  3257. break; \
  3258. }
  3259. do {
  3260. GET_VALUE_FROM_STRUCT(y, "y");
  3261. GET_VALUE_FROM_STRUCT(m, "m");
  3262. GET_VALUE_FROM_STRUCT(d, "d");
  3263. GET_VALUE_FROM_STRUCT(h, "h");
  3264. GET_VALUE_FROM_STRUCT(i, "i");
  3265. GET_VALUE_FROM_STRUCT(s, "s");
  3266. if (zend_string_equals_literal(name, "f")) {
  3267. fvalue = obj->diff->us / 1000000.0;
  3268. break;
  3269. }
  3270. GET_VALUE_FROM_STRUCT(invert, "invert");
  3271. GET_VALUE_FROM_STRUCT(days, "days");
  3272. /* didn't find any */
  3273. retval = zend_std_read_property(object, name, type, cache_slot, rv);
  3274. return retval;
  3275. } while(0);
  3276. retval = rv;
  3277. if (fvalue != -1) {
  3278. ZVAL_DOUBLE(retval, fvalue);
  3279. } else if (value != -99999) {
  3280. ZVAL_LONG(retval, value);
  3281. } else {
  3282. ZVAL_FALSE(retval);
  3283. }
  3284. return retval;
  3285. }
  3286. /* }}} */
  3287. /* {{{ date_interval_write_property */
  3288. static zval *date_interval_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  3289. {
  3290. php_interval_obj *obj;
  3291. obj = php_interval_obj_from_obj(object);
  3292. if (!obj->initialized) {
  3293. return zend_std_write_property(object, name, value, cache_slot);
  3294. }
  3295. #define SET_VALUE_FROM_STRUCT(n,m) \
  3296. if (zend_string_equals_literal(name, m)) { \
  3297. obj->diff->n = zval_get_long(value); \
  3298. break; \
  3299. }
  3300. do {
  3301. SET_VALUE_FROM_STRUCT(y, "y");
  3302. SET_VALUE_FROM_STRUCT(m, "m");
  3303. SET_VALUE_FROM_STRUCT(d, "d");
  3304. SET_VALUE_FROM_STRUCT(h, "h");
  3305. SET_VALUE_FROM_STRUCT(i, "i");
  3306. SET_VALUE_FROM_STRUCT(s, "s");
  3307. if (zend_string_equals_literal(name, "f")) {
  3308. obj->diff->us = zend_dval_to_lval(zval_get_double(value) * 1000000.0);
  3309. break;
  3310. }
  3311. SET_VALUE_FROM_STRUCT(invert, "invert");
  3312. /* didn't find any */
  3313. value = zend_std_write_property(object, name, value, cache_slot);
  3314. } while(0);
  3315. return value;
  3316. }
  3317. /* }}} */
  3318. /* {{{ date_interval_get_property_ptr_ptr */
  3319. static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
  3320. {
  3321. zval *ret;
  3322. if (
  3323. zend_string_equals_literal(name, "y") ||
  3324. zend_string_equals_literal(name, "m") ||
  3325. zend_string_equals_literal(name, "d") ||
  3326. zend_string_equals_literal(name, "h") ||
  3327. zend_string_equals_literal(name, "i") ||
  3328. zend_string_equals_literal(name, "s") ||
  3329. zend_string_equals_literal(name, "f") ||
  3330. zend_string_equals_literal(name, "days") ||
  3331. zend_string_equals_literal(name, "invert") ) {
  3332. /* Fallback to read_property. */
  3333. ret = NULL;
  3334. } else {
  3335. ret = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
  3336. }
  3337. return ret;
  3338. }
  3339. /* }}} */
  3340. /* {{{ Creates new DateInterval object. */
  3341. PHP_METHOD(DateInterval, __construct)
  3342. {
  3343. zend_string *interval_string = NULL;
  3344. timelib_rel_time *reltime;
  3345. ZEND_PARSE_PARAMETERS_START(1, 1)
  3346. Z_PARAM_STR(interval_string)
  3347. ZEND_PARSE_PARAMETERS_END();
  3348. if (!date_interval_initialize(&reltime, ZSTR_VAL(interval_string), ZSTR_LEN(interval_string))) {
  3349. RETURN_THROWS();
  3350. }
  3351. php_interval_obj *diobj = Z_PHPINTERVAL_P(ZEND_THIS);
  3352. diobj->diff = reltime;
  3353. diobj->initialized = 1;
  3354. diobj->civil_or_wall = PHP_DATE_WALL;
  3355. }
  3356. /* }}} */
  3357. static int php_date_interval_initialize_from_hash(zval **return_value, php_interval_obj **intobj, HashTable *myht) /* {{{ */
  3358. {
  3359. (*intobj)->diff = timelib_rel_time_ctor();
  3360. #define PHP_DATE_INTERVAL_READ_PROPERTY(element, member, itype, def) \
  3361. do { \
  3362. zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
  3363. if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
  3364. (*intobj)->diff->member = (itype)zval_get_long(z_arg); \
  3365. } else { \
  3366. (*intobj)->diff->member = (itype)def; \
  3367. } \
  3368. } while (0);
  3369. #define PHP_DATE_INTERVAL_READ_PROPERTY_I64(element, member) \
  3370. do { \
  3371. zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
  3372. if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
  3373. zend_string *tmp_str; \
  3374. zend_string *str = zval_get_tmp_string(z_arg, &tmp_str); \
  3375. DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \
  3376. zend_tmp_string_release(tmp_str); \
  3377. } else { \
  3378. (*intobj)->diff->member = -1LL; \
  3379. } \
  3380. } while (0);
  3381. #define PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(member) \
  3382. do { \
  3383. zval *z_arg = zend_hash_str_find(myht, "days", sizeof("days") - 1); \
  3384. if (z_arg && Z_TYPE_P(z_arg) == IS_FALSE) { \
  3385. (*intobj)->diff->member = -99999; \
  3386. } else if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
  3387. zend_string *str = zval_get_string(z_arg); \
  3388. DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \
  3389. zend_string_release(str); \
  3390. } else { \
  3391. (*intobj)->diff->member = -1LL; \
  3392. } \
  3393. } while (0);
  3394. #define PHP_DATE_INTERVAL_READ_PROPERTY_DOUBLE(element, member, def) \
  3395. do { \
  3396. zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
  3397. if (z_arg) { \
  3398. (*intobj)->diff->member = (double)zval_get_double(z_arg); \
  3399. } else { \
  3400. (*intobj)->diff->member = (double)def; \
  3401. } \
  3402. } while (0);
  3403. PHP_DATE_INTERVAL_READ_PROPERTY("y", y, timelib_sll, -1)
  3404. PHP_DATE_INTERVAL_READ_PROPERTY("m", m, timelib_sll, -1)
  3405. PHP_DATE_INTERVAL_READ_PROPERTY("d", d, timelib_sll, -1)
  3406. PHP_DATE_INTERVAL_READ_PROPERTY("h", h, timelib_sll, -1)
  3407. PHP_DATE_INTERVAL_READ_PROPERTY("i", i, timelib_sll, -1)
  3408. PHP_DATE_INTERVAL_READ_PROPERTY("s", s, timelib_sll, -1)
  3409. {
  3410. zval *z_arg = zend_hash_str_find(myht, "f", sizeof("f") - 1);
  3411. if (z_arg) {
  3412. (*intobj)->diff->us = zend_dval_to_lval(zval_get_double(z_arg) * 1000000.0);
  3413. }
  3414. }
  3415. PHP_DATE_INTERVAL_READ_PROPERTY("weekday", weekday, int, -1)
  3416. PHP_DATE_INTERVAL_READ_PROPERTY("weekday_behavior", weekday_behavior, int, -1)
  3417. PHP_DATE_INTERVAL_READ_PROPERTY("first_last_day_of", first_last_day_of, int, -1)
  3418. PHP_DATE_INTERVAL_READ_PROPERTY("invert", invert, int, 0);
  3419. PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(days);
  3420. PHP_DATE_INTERVAL_READ_PROPERTY("special_type", special.type, unsigned int, 0);
  3421. PHP_DATE_INTERVAL_READ_PROPERTY_I64("special_amount", special.amount);
  3422. PHP_DATE_INTERVAL_READ_PROPERTY("have_weekday_relative", have_weekday_relative, unsigned int, 0);
  3423. PHP_DATE_INTERVAL_READ_PROPERTY("have_special_relative", have_special_relative, unsigned int, 0);
  3424. {
  3425. zval *z_arg = zend_hash_str_find(myht, "civil_or_wall", sizeof("civil_or_wall") - 1);
  3426. (*intobj)->civil_or_wall = PHP_DATE_CIVIL;
  3427. if (z_arg) {
  3428. zend_long val = zval_get_long(z_arg);
  3429. (*intobj)->civil_or_wall = val;
  3430. }
  3431. }
  3432. (*intobj)->initialized = 1;
  3433. return 0;
  3434. } /* }}} */
  3435. /* {{{ */
  3436. PHP_METHOD(DateInterval, __set_state)
  3437. {
  3438. php_interval_obj *intobj;
  3439. zval *array;
  3440. HashTable *myht;
  3441. ZEND_PARSE_PARAMETERS_START(1, 1)
  3442. Z_PARAM_ARRAY(array)
  3443. ZEND_PARSE_PARAMETERS_END();
  3444. myht = Z_ARRVAL_P(array);
  3445. php_date_instantiate(date_ce_interval, return_value);
  3446. intobj = Z_PHPINTERVAL_P(return_value);
  3447. php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
  3448. }
  3449. /* }}} */
  3450. /* {{{ */
  3451. PHP_METHOD(DateInterval, __wakeup)
  3452. {
  3453. zval *object = ZEND_THIS;
  3454. php_interval_obj *intobj;
  3455. HashTable *myht;
  3456. ZEND_PARSE_PARAMETERS_NONE();
  3457. intobj = Z_PHPINTERVAL_P(object);
  3458. myht = Z_OBJPROP_P(object);
  3459. php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
  3460. }
  3461. /* }}} */
  3462. /* {{{ Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string */
  3463. PHP_FUNCTION(date_interval_create_from_date_string)
  3464. {
  3465. zend_string *time_str = NULL;
  3466. timelib_time *time;
  3467. timelib_error_container *err = NULL;
  3468. php_interval_obj *diobj;
  3469. ZEND_PARSE_PARAMETERS_START(1, 1)
  3470. Z_PARAM_STR(time_str)
  3471. ZEND_PARSE_PARAMETERS_END();
  3472. time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  3473. if (err->error_count > 0) {
  3474. php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
  3475. err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
  3476. RETVAL_FALSE;
  3477. goto cleanup;
  3478. }
  3479. php_date_instantiate(date_ce_interval, return_value);
  3480. diobj = Z_PHPINTERVAL_P(return_value);
  3481. diobj->diff = timelib_rel_time_clone(&time->relative);
  3482. diobj->initialized = 1;
  3483. diobj->civil_or_wall = PHP_DATE_CIVIL;
  3484. cleanup:
  3485. timelib_time_dtor(time);
  3486. timelib_error_container_dtor(err);
  3487. }
  3488. /* }}} */
  3489. /* {{{ date_interval_format - */
  3490. static zend_string *date_interval_format(char *format, size_t format_len, timelib_rel_time *t)
  3491. {
  3492. smart_str string = {0};
  3493. size_t i;
  3494. int length, have_format_spec = 0;
  3495. char buffer[33];
  3496. if (!format_len) {
  3497. return ZSTR_EMPTY_ALLOC();
  3498. }
  3499. for (i = 0; i < format_len; i++) {
  3500. if (have_format_spec) {
  3501. switch (format[i]) {
  3502. case 'Y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->y); break;
  3503. case 'y': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->y); break;
  3504. case 'M': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
  3505. case 'm': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
  3506. case 'D': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
  3507. case 'd': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
  3508. case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
  3509. case 'h': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
  3510. case 'I': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
  3511. case 'i': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->i); break;
  3512. case 'S': length = slprintf(buffer, sizeof(buffer), "%02" ZEND_LONG_FMT_SPEC, (zend_long) t->s); break;
  3513. case 's': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->s); break;
  3514. case 'F': length = slprintf(buffer, sizeof(buffer), "%06" ZEND_LONG_FMT_SPEC, (zend_long) t->us); break;
  3515. case 'f': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->us); break;
  3516. case 'a': {
  3517. if ((int) t->days != -99999) {
  3518. length = slprintf(buffer, sizeof(buffer), "%d", (int) t->days);
  3519. } else {
  3520. length = slprintf(buffer, sizeof(buffer), "(unknown)");
  3521. }
  3522. } break;
  3523. case 'r': length = slprintf(buffer, sizeof(buffer), "%s", t->invert ? "-" : ""); break;
  3524. case 'R': length = slprintf(buffer, sizeof(buffer), "%c", t->invert ? '-' : '+'); break;
  3525. case '%': length = slprintf(buffer, sizeof(buffer), "%%"); break;
  3526. default: buffer[0] = '%'; buffer[1] = format[i]; buffer[2] = '\0'; length = 2; break;
  3527. }
  3528. smart_str_appendl(&string, buffer, length);
  3529. have_format_spec = 0;
  3530. } else {
  3531. if (format[i] == '%') {
  3532. have_format_spec = 1;
  3533. } else {
  3534. smart_str_appendc(&string, format[i]);
  3535. }
  3536. }
  3537. }
  3538. smart_str_0(&string);
  3539. if (string.s == NULL) {
  3540. return ZSTR_EMPTY_ALLOC();
  3541. }
  3542. return string.s;
  3543. }
  3544. /* }}} */
  3545. /* {{{ Formats the interval. */
  3546. PHP_FUNCTION(date_interval_format)
  3547. {
  3548. zval *object;
  3549. php_interval_obj *diobj;
  3550. char *format;
  3551. size_t format_len;
  3552. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interval, &format, &format_len) == FAILURE) {
  3553. RETURN_THROWS();
  3554. }
  3555. diobj = Z_PHPINTERVAL_P(object);
  3556. DATE_CHECK_INITIALIZED(diobj->initialized, DateInterval);
  3557. RETURN_STR(date_interval_format(format, format_len, diobj->diff));
  3558. }
  3559. /* }}} */
  3560. static bool date_period_initialize(timelib_time **st, timelib_time **et, timelib_rel_time **d, zend_long *recurrences, /*const*/ char *format, size_t format_length) /* {{{ */
  3561. {
  3562. timelib_time *b = NULL, *e = NULL;
  3563. timelib_rel_time *p = NULL;
  3564. int r = 0;
  3565. timelib_error_container *errors;
  3566. bool retval = false;
  3567. timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
  3568. if (errors->error_count > 0) {
  3569. retval = false;
  3570. zend_throw_exception_ex(NULL, 0, "Unknown or bad format (%s)", format);
  3571. if (b) {
  3572. timelib_time_dtor(b);
  3573. }
  3574. if (e) {
  3575. timelib_time_dtor(e);
  3576. }
  3577. if (p) {
  3578. timelib_rel_time_dtor(p);
  3579. }
  3580. } else {
  3581. *st = b;
  3582. *et = e;
  3583. *d = p;
  3584. *recurrences = r;
  3585. retval = true;
  3586. }
  3587. timelib_error_container_dtor(errors);
  3588. return retval;
  3589. } /* }}} */
  3590. /* {{{ Creates new DatePeriod object. */
  3591. PHP_METHOD(DatePeriod, __construct)
  3592. {
  3593. php_period_obj *dpobj;
  3594. php_date_obj *dateobj;
  3595. zval *start, *end = NULL, *interval;
  3596. zend_long recurrences = 0, options = 0;
  3597. char *isostr = NULL;
  3598. size_t isostr_len = 0;
  3599. timelib_time *clone;
  3600. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOl|l", &start, date_ce_interface, &interval, date_ce_interval, &recurrences, &options) == FAILURE) {
  3601. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOO|l", &start, date_ce_interface, &interval, date_ce_interval, &end, date_ce_interface, &options) == FAILURE) {
  3602. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "s|l", &isostr, &isostr_len, &options) == FAILURE) {
  3603. zend_type_error("DatePeriod::__construct() accepts (DateTimeInterface, DateInterval, int [, int]), or (DateTimeInterface, DateInterval, DateTime [, int]), or (string [, int]) as arguments");
  3604. RETURN_THROWS();
  3605. }
  3606. }
  3607. }
  3608. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3609. dpobj->current = NULL;
  3610. if (isostr) {
  3611. if (!date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), &recurrences, isostr, isostr_len)) {
  3612. RETURN_THROWS();
  3613. }
  3614. if (dpobj->start == NULL) {
  3615. zend_string *func = get_active_function_or_method_name();
  3616. zend_throw_exception_ex(NULL, 0, "%s(): ISO interval must contain a start date, \"%s\" given", ZSTR_VAL(func), isostr);
  3617. zend_string_release(func);
  3618. RETURN_THROWS();
  3619. }
  3620. if (dpobj->interval == NULL) {
  3621. zend_string *func = get_active_function_or_method_name();
  3622. zend_throw_exception_ex(NULL, 0, "%s(): ISO interval must contain an interval, \"%s\" given", ZSTR_VAL(func), isostr);
  3623. zend_string_release(func);
  3624. RETURN_THROWS();
  3625. }
  3626. if (dpobj->end == NULL && recurrences == 0) {
  3627. zend_string *func = get_active_function_or_method_name();
  3628. zend_throw_exception_ex(NULL, 0, "%s(): ISO interval must contain an end date or a recurrence count, \"%s\" given", ZSTR_VAL(func), isostr);
  3629. zend_string_release(func);
  3630. RETURN_THROWS();
  3631. }
  3632. if (dpobj->start) {
  3633. timelib_update_ts(dpobj->start, NULL);
  3634. }
  3635. if (dpobj->end) {
  3636. timelib_update_ts(dpobj->end, NULL);
  3637. }
  3638. dpobj->start_ce = date_ce_date;
  3639. } else {
  3640. /* init */
  3641. php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
  3642. /* start date */
  3643. dateobj = Z_PHPDATE_P(start);
  3644. clone = timelib_time_ctor();
  3645. memcpy(clone, dateobj->time, sizeof(timelib_time));
  3646. if (dateobj->time->tz_abbr) {
  3647. clone->tz_abbr = timelib_strdup(dateobj->time->tz_abbr);
  3648. }
  3649. if (dateobj->time->tz_info) {
  3650. clone->tz_info = dateobj->time->tz_info;
  3651. }
  3652. dpobj->start = clone;
  3653. dpobj->start_ce = Z_OBJCE_P(start);
  3654. /* interval */
  3655. dpobj->interval = timelib_rel_time_clone(intobj->diff);
  3656. /* end date */
  3657. if (end) {
  3658. dateobj = Z_PHPDATE_P(end);
  3659. clone = timelib_time_clone(dateobj->time);
  3660. dpobj->end = clone;
  3661. }
  3662. }
  3663. if (dpobj->end == NULL && recurrences < 1) {
  3664. zend_string *func = get_active_function_or_method_name();
  3665. zend_throw_exception_ex(NULL, 0, "%s(): Recurrence count must be greater than 0", ZSTR_VAL(func));
  3666. zend_string_release(func);
  3667. RETURN_THROWS();
  3668. }
  3669. /* options */
  3670. dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
  3671. /* recurrrences */
  3672. dpobj->recurrences = recurrences + dpobj->include_start_date;
  3673. dpobj->initialized = 1;
  3674. }
  3675. /* }}} */
  3676. /* {{{ Get start date. */
  3677. PHP_METHOD(DatePeriod, getStartDate)
  3678. {
  3679. php_period_obj *dpobj;
  3680. php_date_obj *dateobj;
  3681. ZEND_PARSE_PARAMETERS_NONE();
  3682. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3683. DATE_CHECK_INITIALIZED(dpobj->start, DatePeriod);
  3684. php_date_instantiate(dpobj->start_ce, return_value);
  3685. dateobj = Z_PHPDATE_P(return_value);
  3686. dateobj->time = timelib_time_ctor();
  3687. *dateobj->time = *dpobj->start;
  3688. if (dpobj->start->tz_abbr) {
  3689. dateobj->time->tz_abbr = timelib_strdup(dpobj->start->tz_abbr);
  3690. }
  3691. if (dpobj->start->tz_info) {
  3692. dateobj->time->tz_info = dpobj->start->tz_info;
  3693. }
  3694. }
  3695. /* }}} */
  3696. /* {{{ Get end date. */
  3697. PHP_METHOD(DatePeriod, getEndDate)
  3698. {
  3699. php_period_obj *dpobj;
  3700. php_date_obj *dateobj;
  3701. ZEND_PARSE_PARAMETERS_NONE();
  3702. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3703. if (!dpobj->end) {
  3704. return;
  3705. }
  3706. php_date_instantiate(dpobj->start_ce, return_value);
  3707. dateobj = Z_PHPDATE_P(return_value);
  3708. dateobj->time = timelib_time_ctor();
  3709. *dateobj->time = *dpobj->end;
  3710. if (dpobj->end->tz_abbr) {
  3711. dateobj->time->tz_abbr = timelib_strdup(dpobj->end->tz_abbr);
  3712. }
  3713. if (dpobj->end->tz_info) {
  3714. dateobj->time->tz_info = dpobj->end->tz_info;
  3715. }
  3716. }
  3717. /* }}} */
  3718. /* {{{ Get date interval. */
  3719. PHP_METHOD(DatePeriod, getDateInterval)
  3720. {
  3721. php_period_obj *dpobj;
  3722. php_interval_obj *diobj;
  3723. ZEND_PARSE_PARAMETERS_NONE();
  3724. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3725. DATE_CHECK_INITIALIZED(dpobj->interval, DatePeriod);
  3726. php_date_instantiate(date_ce_interval, return_value);
  3727. diobj = Z_PHPINTERVAL_P(return_value);
  3728. diobj->diff = timelib_rel_time_clone(dpobj->interval);
  3729. diobj->initialized = 1;
  3730. }
  3731. /* }}} */
  3732. /* {{{ Get recurrences. */
  3733. PHP_METHOD(DatePeriod, getRecurrences)
  3734. {
  3735. php_period_obj *dpobj;
  3736. ZEND_PARSE_PARAMETERS_NONE();
  3737. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3738. if (0 == dpobj->recurrences - dpobj->include_start_date) {
  3739. return;
  3740. }
  3741. RETURN_LONG(dpobj->recurrences - dpobj->include_start_date);
  3742. }
  3743. /* }}} */
  3744. PHP_METHOD(DatePeriod, getIterator)
  3745. {
  3746. ZEND_PARSE_PARAMETERS_NONE();
  3747. zend_create_internal_iterator_zval(return_value, ZEND_THIS);
  3748. }
  3749. static int check_id_allowed(char *id, zend_long what) /* {{{ */
  3750. {
  3751. if ((what & PHP_DATE_TIMEZONE_GROUP_AFRICA) && strncasecmp(id, "Africa/", 7) == 0) return 1;
  3752. if ((what & PHP_DATE_TIMEZONE_GROUP_AMERICA) && strncasecmp(id, "America/", 8) == 0) return 1;
  3753. if ((what & PHP_DATE_TIMEZONE_GROUP_ANTARCTICA) && strncasecmp(id, "Antarctica/", 11) == 0) return 1;
  3754. if ((what & PHP_DATE_TIMEZONE_GROUP_ARCTIC) && strncasecmp(id, "Arctic/", 7) == 0) return 1;
  3755. if ((what & PHP_DATE_TIMEZONE_GROUP_ASIA) && strncasecmp(id, "Asia/", 5) == 0) return 1;
  3756. if ((what & PHP_DATE_TIMEZONE_GROUP_ATLANTIC) && strncasecmp(id, "Atlantic/", 9) == 0) return 1;
  3757. if ((what & PHP_DATE_TIMEZONE_GROUP_AUSTRALIA) && strncasecmp(id, "Australia/", 10) == 0) return 1;
  3758. if ((what & PHP_DATE_TIMEZONE_GROUP_EUROPE) && strncasecmp(id, "Europe/", 7) == 0) return 1;
  3759. if ((what & PHP_DATE_TIMEZONE_GROUP_INDIAN) && strncasecmp(id, "Indian/", 7) == 0) return 1;
  3760. if ((what & PHP_DATE_TIMEZONE_GROUP_PACIFIC) && strncasecmp(id, "Pacific/", 8) == 0) return 1;
  3761. if ((what & PHP_DATE_TIMEZONE_GROUP_UTC) && strncasecmp(id, "UTC", 3) == 0) return 1;
  3762. return 0;
  3763. } /* }}} */
  3764. /* {{{ Returns numerically index array with all timezone identifiers. */
  3765. PHP_FUNCTION(timezone_identifiers_list)
  3766. {
  3767. const timelib_tzdb *tzdb;
  3768. const timelib_tzdb_index_entry *table;
  3769. int i, item_count;
  3770. zend_long what = PHP_DATE_TIMEZONE_GROUP_ALL;
  3771. char *option = NULL;
  3772. size_t option_len = 0;
  3773. ZEND_PARSE_PARAMETERS_START(0, 2)
  3774. Z_PARAM_OPTIONAL
  3775. Z_PARAM_LONG(what)
  3776. Z_PARAM_STRING_OR_NULL(option, option_len)
  3777. ZEND_PARSE_PARAMETERS_END();
  3778. /* Extra validation */
  3779. if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
  3780. zend_argument_value_error(2, "must be a two-letter ISO 3166-1 compatible country code "
  3781. "when argument #1 ($timezoneGroup) is DateTimeZone::PER_COUNTRY");
  3782. RETURN_THROWS();
  3783. }
  3784. tzdb = DATE_TIMEZONEDB;
  3785. table = timelib_timezone_identifiers_list((timelib_tzdb*) tzdb, &item_count);
  3786. array_init(return_value);
  3787. for (i = 0; i < item_count; ++i) {
  3788. if (what == PHP_DATE_TIMEZONE_PER_COUNTRY) {
  3789. if (tzdb->data[table[i].pos + 5] == option[0] && tzdb->data[table[i].pos + 6] == option[1]) {
  3790. add_next_index_string(return_value, table[i].id);
  3791. }
  3792. } else if (what == PHP_DATE_TIMEZONE_GROUP_ALL_W_BC || (check_id_allowed(table[i].id, what) && (tzdb->data[table[i].pos + 4] == '\1'))) {
  3793. add_next_index_string(return_value, table[i].id);
  3794. }
  3795. };
  3796. }
  3797. /* }}} */
  3798. /* {{{ Returns the Olson database version number. */
  3799. PHP_FUNCTION(timezone_version_get)
  3800. {
  3801. const timelib_tzdb *tzdb;
  3802. ZEND_PARSE_PARAMETERS_NONE();
  3803. tzdb = DATE_TIMEZONEDB;
  3804. RETURN_STRING(tzdb->version);
  3805. }
  3806. /* }}} */
  3807. /* {{{ Returns associative array containing dst, offset and the timezone name */
  3808. PHP_FUNCTION(timezone_abbreviations_list)
  3809. {
  3810. const timelib_tz_lookup_table *table, *entry;
  3811. zval element, *abbr_array_p, abbr_array;
  3812. ZEND_PARSE_PARAMETERS_NONE();
  3813. table = timelib_timezone_abbreviations_list();
  3814. array_init(return_value);
  3815. entry = table;
  3816. do {
  3817. array_init(&element);
  3818. add_assoc_bool_ex(&element, "dst", sizeof("dst") -1, entry->type);
  3819. add_assoc_long_ex(&element, "offset", sizeof("offset") - 1, entry->gmtoffset);
  3820. if (entry->full_tz_name) {
  3821. add_assoc_string_ex(&element, "timezone_id", sizeof("timezone_id") - 1, entry->full_tz_name);
  3822. } else {
  3823. add_assoc_null_ex(&element, "timezone_id", sizeof("timezone_id") - 1);
  3824. }
  3825. abbr_array_p = zend_hash_str_find(Z_ARRVAL_P(return_value), entry->name, strlen(entry->name));
  3826. if (!abbr_array_p) {
  3827. array_init(&abbr_array);
  3828. add_assoc_zval(return_value, entry->name, &abbr_array);
  3829. } else {
  3830. ZVAL_COPY_VALUE(&abbr_array, abbr_array_p);
  3831. }
  3832. add_next_index_zval(&abbr_array, &element);
  3833. entry++;
  3834. } while (entry->name);
  3835. }
  3836. /* }}} */
  3837. /* {{{ Sets the default timezone used by all date/time functions in a script */
  3838. PHP_FUNCTION(date_default_timezone_set)
  3839. {
  3840. char *zone;
  3841. size_t zone_len;
  3842. ZEND_PARSE_PARAMETERS_START(1, 1)
  3843. Z_PARAM_STRING(zone, zone_len)
  3844. ZEND_PARSE_PARAMETERS_END();
  3845. if (!timelib_timezone_id_is_valid(zone, DATE_TIMEZONEDB)) {
  3846. php_error_docref(NULL, E_NOTICE, "Timezone ID '%s' is invalid", zone);
  3847. RETURN_FALSE;
  3848. }
  3849. if (DATEG(timezone)) {
  3850. efree(DATEG(timezone));
  3851. DATEG(timezone) = NULL;
  3852. }
  3853. DATEG(timezone) = estrndup(zone, zone_len);
  3854. RETURN_TRUE;
  3855. }
  3856. /* }}} */
  3857. /* {{{ Gets the default timezone used by all date/time functions in a script */
  3858. PHP_FUNCTION(date_default_timezone_get)
  3859. {
  3860. timelib_tzinfo *default_tz;
  3861. ZEND_PARSE_PARAMETERS_NONE();
  3862. default_tz = get_timezone_info();
  3863. if (!default_tz) {
  3864. RETURN_THROWS();
  3865. }
  3866. RETVAL_STRING(default_tz->name);
  3867. }
  3868. /* }}} */
  3869. /* {{{ php_do_date_sunrise_sunset
  3870. * Common for date_sunrise() and date_sunset() functions
  3871. */
  3872. static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_sunset)
  3873. {
  3874. double latitude, longitude, zenith, gmt_offset, altitude;
  3875. bool latitude_is_null = 1, longitude_is_null = 1, zenith_is_null = 1, gmt_offset_is_null = 1;
  3876. double h_rise, h_set, N;
  3877. timelib_sll rise, set, transit;
  3878. zend_long time, retformat = SUNFUNCS_RET_STRING;
  3879. int rs;
  3880. timelib_time *t;
  3881. timelib_tzinfo *tzi;
  3882. zend_string *retstr;
  3883. ZEND_PARSE_PARAMETERS_START(1, 6)
  3884. Z_PARAM_LONG(time)
  3885. Z_PARAM_OPTIONAL
  3886. Z_PARAM_LONG(retformat)
  3887. Z_PARAM_DOUBLE_OR_NULL(latitude, latitude_is_null)
  3888. Z_PARAM_DOUBLE_OR_NULL(longitude, longitude_is_null)
  3889. Z_PARAM_DOUBLE_OR_NULL(zenith, zenith_is_null)
  3890. Z_PARAM_DOUBLE_OR_NULL(gmt_offset, gmt_offset_is_null)
  3891. ZEND_PARSE_PARAMETERS_END();
  3892. if (latitude_is_null) {
  3893. latitude = INI_FLT("date.default_latitude");
  3894. }
  3895. if (longitude_is_null) {
  3896. longitude = INI_FLT("date.default_longitude");
  3897. }
  3898. if (zenith_is_null) {
  3899. if (calc_sunset) {
  3900. zenith = INI_FLT("date.sunset_zenith");
  3901. } else {
  3902. zenith = INI_FLT("date.sunrise_zenith");
  3903. }
  3904. }
  3905. if (retformat != SUNFUNCS_RET_TIMESTAMP &&
  3906. retformat != SUNFUNCS_RET_STRING &&
  3907. retformat != SUNFUNCS_RET_DOUBLE)
  3908. {
  3909. zend_argument_value_error(2, "must be one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING, or SUNFUNCS_RET_DOUBLE");
  3910. RETURN_THROWS();
  3911. }
  3912. altitude = 90 - zenith;
  3913. /* Initialize time struct */
  3914. tzi = get_timezone_info();
  3915. if (!tzi) {
  3916. RETURN_THROWS();
  3917. }
  3918. t = timelib_time_ctor();
  3919. t->tz_info = tzi;
  3920. t->zone_type = TIMELIB_ZONETYPE_ID;
  3921. if (gmt_offset_is_null) {
  3922. gmt_offset = timelib_get_current_offset(t) / 3600;
  3923. }
  3924. timelib_unixtime2local(t, time);
  3925. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, altitude, 1, &h_rise, &h_set, &rise, &set, &transit);
  3926. timelib_time_dtor(t);
  3927. if (rs != 0) {
  3928. RETURN_FALSE;
  3929. }
  3930. if (retformat == SUNFUNCS_RET_TIMESTAMP) {
  3931. RETURN_LONG(calc_sunset ? set : rise);
  3932. }
  3933. N = (calc_sunset ? h_set : h_rise) + gmt_offset;
  3934. if (N > 24 || N < 0) {
  3935. N -= floor(N / 24) * 24;
  3936. }
  3937. switch (retformat) {
  3938. case SUNFUNCS_RET_STRING:
  3939. retstr = strpprintf(0, "%02d:%02d", (int) N, (int) (60 * (N - (int) N)));
  3940. RETURN_NEW_STR(retstr);
  3941. break;
  3942. case SUNFUNCS_RET_DOUBLE:
  3943. RETURN_DOUBLE(N);
  3944. break;
  3945. }
  3946. }
  3947. /* }}} */
  3948. /* {{{ Returns time of sunrise for a given day and location */
  3949. PHP_FUNCTION(date_sunrise)
  3950. {
  3951. php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3952. }
  3953. /* }}} */
  3954. /* {{{ Returns time of sunset for a given day and location */
  3955. PHP_FUNCTION(date_sunset)
  3956. {
  3957. php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  3958. }
  3959. /* }}} */
  3960. /* {{{ Returns an array with information about sun set/rise and twilight begin/end */
  3961. PHP_FUNCTION(date_sun_info)
  3962. {
  3963. zend_long time;
  3964. double latitude, longitude;
  3965. timelib_time *t, *t2;
  3966. timelib_tzinfo *tzi;
  3967. int rs;
  3968. timelib_sll rise, set, transit;
  3969. int dummy;
  3970. double ddummy;
  3971. ZEND_PARSE_PARAMETERS_START(3, 3)
  3972. Z_PARAM_LONG(time)
  3973. Z_PARAM_DOUBLE(latitude)
  3974. Z_PARAM_DOUBLE(longitude)
  3975. ZEND_PARSE_PARAMETERS_END();
  3976. /* Initialize time struct */
  3977. tzi = get_timezone_info();
  3978. if (!tzi) {
  3979. RETURN_THROWS();
  3980. }
  3981. t = timelib_time_ctor();
  3982. t->tz_info = tzi;
  3983. t->zone_type = TIMELIB_ZONETYPE_ID;
  3984. timelib_unixtime2local(t, time);
  3985. /* Setup */
  3986. t2 = timelib_time_ctor();
  3987. array_init(return_value);
  3988. /* Get sun up/down and transit */
  3989. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -50.0/60, 1, &ddummy, &ddummy, &rise, &set, &transit);
  3990. switch (rs) {
  3991. case -1: /* always below */
  3992. add_assoc_bool(return_value, "sunrise", 0);
  3993. add_assoc_bool(return_value, "sunset", 0);
  3994. break;
  3995. case 1: /* always above */
  3996. add_assoc_bool(return_value, "sunrise", 1);
  3997. add_assoc_bool(return_value, "sunset", 1);
  3998. break;
  3999. default:
  4000. t2->sse = rise;
  4001. add_assoc_long(return_value, "sunrise", timelib_date_to_int(t2, &dummy));
  4002. t2->sse = set;
  4003. add_assoc_long(return_value, "sunset", timelib_date_to_int(t2, &dummy));
  4004. }
  4005. t2->sse = transit;
  4006. add_assoc_long(return_value, "transit", timelib_date_to_int(t2, &dummy));
  4007. /* Get civil twilight */
  4008. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -6.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
  4009. switch (rs) {
  4010. case -1: /* always below */
  4011. add_assoc_bool(return_value, "civil_twilight_begin", 0);
  4012. add_assoc_bool(return_value, "civil_twilight_end", 0);
  4013. break;
  4014. case 1: /* always above */
  4015. add_assoc_bool(return_value, "civil_twilight_begin", 1);
  4016. add_assoc_bool(return_value, "civil_twilight_end", 1);
  4017. break;
  4018. default:
  4019. t2->sse = rise;
  4020. add_assoc_long(return_value, "civil_twilight_begin", timelib_date_to_int(t2, &dummy));
  4021. t2->sse = set;
  4022. add_assoc_long(return_value, "civil_twilight_end", timelib_date_to_int(t2, &dummy));
  4023. }
  4024. /* Get nautical twilight */
  4025. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -12.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
  4026. switch (rs) {
  4027. case -1: /* always below */
  4028. add_assoc_bool(return_value, "nautical_twilight_begin", 0);
  4029. add_assoc_bool(return_value, "nautical_twilight_end", 0);
  4030. break;
  4031. case 1: /* always above */
  4032. add_assoc_bool(return_value, "nautical_twilight_begin", 1);
  4033. add_assoc_bool(return_value, "nautical_twilight_end", 1);
  4034. break;
  4035. default:
  4036. t2->sse = rise;
  4037. add_assoc_long(return_value, "nautical_twilight_begin", timelib_date_to_int(t2, &dummy));
  4038. t2->sse = set;
  4039. add_assoc_long(return_value, "nautical_twilight_end", timelib_date_to_int(t2, &dummy));
  4040. }
  4041. /* Get astronomical twilight */
  4042. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -18.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
  4043. switch (rs) {
  4044. case -1: /* always below */
  4045. add_assoc_bool(return_value, "astronomical_twilight_begin", 0);
  4046. add_assoc_bool(return_value, "astronomical_twilight_end", 0);
  4047. break;
  4048. case 1: /* always above */
  4049. add_assoc_bool(return_value, "astronomical_twilight_begin", 1);
  4050. add_assoc_bool(return_value, "astronomical_twilight_end", 1);
  4051. break;
  4052. default:
  4053. t2->sse = rise;
  4054. add_assoc_long(return_value, "astronomical_twilight_begin", timelib_date_to_int(t2, &dummy));
  4055. t2->sse = set;
  4056. add_assoc_long(return_value, "astronomical_twilight_end", timelib_date_to_int(t2, &dummy));
  4057. }
  4058. timelib_time_dtor(t);
  4059. timelib_time_dtor(t2);
  4060. }
  4061. /* }}} */
  4062. static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n) /* {{{ */
  4063. {
  4064. *table = NULL;
  4065. *n = 0;
  4066. return zend_std_get_properties(object);
  4067. } /* }}} */
  4068. static HashTable *date_object_get_properties_period(zend_object *object) /* {{{ */
  4069. {
  4070. HashTable *props;
  4071. zval zv;
  4072. php_period_obj *period_obj;
  4073. period_obj = php_period_obj_from_obj(object);
  4074. props = zend_std_get_properties(object);
  4075. if (!period_obj->start) {
  4076. return props;
  4077. }
  4078. if (period_obj->start) {
  4079. php_date_obj *date_obj;
  4080. object_init_ex(&zv, period_obj->start_ce);
  4081. date_obj = Z_PHPDATE_P(&zv);
  4082. date_obj->time = timelib_time_clone(period_obj->start);
  4083. } else {
  4084. ZVAL_NULL(&zv);
  4085. }
  4086. zend_hash_str_update(props, "start", sizeof("start")-1, &zv);
  4087. if (period_obj->current) {
  4088. php_date_obj *date_obj;
  4089. object_init_ex(&zv, period_obj->start_ce);
  4090. date_obj = Z_PHPDATE_P(&zv);
  4091. date_obj->time = timelib_time_clone(period_obj->current);
  4092. } else {
  4093. ZVAL_NULL(&zv);
  4094. }
  4095. zend_hash_str_update(props, "current", sizeof("current")-1, &zv);
  4096. if (period_obj->end) {
  4097. php_date_obj *date_obj;
  4098. object_init_ex(&zv, period_obj->start_ce);
  4099. date_obj = Z_PHPDATE_P(&zv);
  4100. date_obj->time = timelib_time_clone(period_obj->end);
  4101. } else {
  4102. ZVAL_NULL(&zv);
  4103. }
  4104. zend_hash_str_update(props, "end", sizeof("end")-1, &zv);
  4105. if (period_obj->interval) {
  4106. php_interval_obj *interval_obj;
  4107. object_init_ex(&zv, date_ce_interval);
  4108. interval_obj = Z_PHPINTERVAL_P(&zv);
  4109. interval_obj->diff = timelib_rel_time_clone(period_obj->interval);
  4110. interval_obj->initialized = 1;
  4111. } else {
  4112. ZVAL_NULL(&zv);
  4113. }
  4114. zend_hash_str_update(props, "interval", sizeof("interval")-1, &zv);
  4115. /* converted to larger type (int->long); must check when unserializing */
  4116. ZVAL_LONG(&zv, (zend_long) period_obj->recurrences);
  4117. zend_hash_str_update(props, "recurrences", sizeof("recurrences")-1, &zv);
  4118. ZVAL_BOOL(&zv, period_obj->include_start_date);
  4119. zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);
  4120. return props;
  4121. } /* }}} */
  4122. static int php_date_period_initialize_from_hash(php_period_obj *period_obj, HashTable *myht) /* {{{ */
  4123. {
  4124. zval *ht_entry;
  4125. /* this function does no rollback on error */
  4126. ht_entry = zend_hash_str_find(myht, "start", sizeof("start")-1);
  4127. if (ht_entry) {
  4128. if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
  4129. php_date_obj *date_obj;
  4130. date_obj = Z_PHPDATE_P(ht_entry);
  4131. period_obj->start = timelib_time_clone(date_obj->time);
  4132. period_obj->start_ce = Z_OBJCE_P(ht_entry);
  4133. } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
  4134. return 0;
  4135. }
  4136. } else {
  4137. return 0;
  4138. }
  4139. ht_entry = zend_hash_str_find(myht, "end", sizeof("end")-1);
  4140. if (ht_entry) {
  4141. if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
  4142. php_date_obj *date_obj;
  4143. date_obj = Z_PHPDATE_P(ht_entry);
  4144. period_obj->end = timelib_time_clone(date_obj->time);
  4145. } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
  4146. return 0;
  4147. }
  4148. } else {
  4149. return 0;
  4150. }
  4151. ht_entry = zend_hash_str_find(myht, "current", sizeof("current")-1);
  4152. if (ht_entry) {
  4153. if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
  4154. php_date_obj *date_obj;
  4155. date_obj = Z_PHPDATE_P(ht_entry);
  4156. period_obj->current = timelib_time_clone(date_obj->time);
  4157. } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
  4158. return 0;
  4159. }
  4160. } else {
  4161. return 0;
  4162. }
  4163. ht_entry = zend_hash_str_find(myht, "interval", sizeof("interval")-1);
  4164. if (ht_entry) {
  4165. if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_interval) {
  4166. php_interval_obj *interval_obj;
  4167. interval_obj = Z_PHPINTERVAL_P(ht_entry);
  4168. period_obj->interval = timelib_rel_time_clone(interval_obj->diff);
  4169. } else { /* interval is required */
  4170. return 0;
  4171. }
  4172. } else {
  4173. return 0;
  4174. }
  4175. ht_entry = zend_hash_str_find(myht, "recurrences", sizeof("recurrences")-1);
  4176. if (ht_entry &&
  4177. Z_TYPE_P(ht_entry) == IS_LONG && Z_LVAL_P(ht_entry) >= 0 && Z_LVAL_P(ht_entry) <= INT_MAX) {
  4178. period_obj->recurrences = Z_LVAL_P(ht_entry);
  4179. } else {
  4180. return 0;
  4181. }
  4182. ht_entry = zend_hash_str_find(myht, "include_start_date", sizeof("include_start_date")-1);
  4183. if (ht_entry &&
  4184. (Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
  4185. period_obj->include_start_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
  4186. } else {
  4187. return 0;
  4188. }
  4189. period_obj->initialized = 1;
  4190. return 1;
  4191. } /* }}} */
  4192. /* {{{ */
  4193. PHP_METHOD(DatePeriod, __set_state)
  4194. {
  4195. php_period_obj *period_obj;
  4196. zval *array;
  4197. HashTable *myht;
  4198. ZEND_PARSE_PARAMETERS_START(1, 1)
  4199. Z_PARAM_ARRAY(array)
  4200. ZEND_PARSE_PARAMETERS_END();
  4201. myht = Z_ARRVAL_P(array);
  4202. object_init_ex(return_value, date_ce_period);
  4203. period_obj = Z_PHPPERIOD_P(return_value);
  4204. if (!php_date_period_initialize_from_hash(period_obj, myht)) {
  4205. zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
  4206. }
  4207. }
  4208. /* }}} */
  4209. /* {{{ */
  4210. PHP_METHOD(DatePeriod, __wakeup)
  4211. {
  4212. zval *object = ZEND_THIS;
  4213. php_period_obj *period_obj;
  4214. HashTable *myht;
  4215. ZEND_PARSE_PARAMETERS_NONE();
  4216. period_obj = Z_PHPPERIOD_P(object);
  4217. myht = Z_OBJPROP_P(object);
  4218. if (!php_date_period_initialize_from_hash(period_obj, myht)) {
  4219. zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
  4220. }
  4221. }
  4222. /* }}} */
  4223. /* {{{ date_period_is_magic_property
  4224. * Common for date_period_read_property() and date_period_write_property() functions
  4225. */
  4226. static int date_period_is_magic_property(zend_string *name)
  4227. {
  4228. if (zend_string_equals_literal(name, "recurrences")
  4229. || zend_string_equals_literal(name, "include_start_date")
  4230. || zend_string_equals_literal(name, "start")
  4231. || zend_string_equals_literal(name, "current")
  4232. || zend_string_equals_literal(name, "end")
  4233. || zend_string_equals_literal(name, "interval")
  4234. ) {
  4235. return 1;
  4236. }
  4237. return 0;
  4238. }
  4239. /* }}} */
  4240. /* {{{ date_period_read_property */
  4241. static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
  4242. {
  4243. if (type != BP_VAR_IS && type != BP_VAR_R) {
  4244. if (date_period_is_magic_property(name)) {
  4245. zend_throw_error(NULL, "Retrieval of DatePeriod->%s for modification is unsupported", ZSTR_VAL(name));
  4246. return &EG(uninitialized_zval);
  4247. }
  4248. }
  4249. object->handlers->get_properties(object); /* build properties hash table */
  4250. return zend_std_read_property(object, name, type, cache_slot, rv);
  4251. }
  4252. /* }}} */
  4253. /* {{{ date_period_write_property */
  4254. static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  4255. {
  4256. if (date_period_is_magic_property(name)) {
  4257. zend_throw_error(NULL, "Writing to DatePeriod->%s is unsupported", ZSTR_VAL(name));
  4258. return value;
  4259. }
  4260. return zend_std_write_property(object, name, value, cache_slot);
  4261. }
  4262. /* }}} */
  4263. /* {{{ date_period_get_property_ptr_ptr */
  4264. static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
  4265. {
  4266. if (date_period_is_magic_property(name)) {
  4267. zend_throw_error(NULL, "Retrieval of DatePeriod->%s for modification is unsupported", ZSTR_VAL(name));
  4268. return &EG(error_zval);
  4269. }
  4270. return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
  4271. }
  4272. /* }}} */