php_date.c 163 KB

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