php_date.c 167 KB

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