Date.php 196 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Date
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Include needed Date classes
  23. */
  24. #require_once 'Zend/Date/DateObject.php';
  25. #require_once 'Zend/Locale.php';
  26. #require_once 'Zend/Locale/Format.php';
  27. #require_once 'Zend/Locale/Math.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Date
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Date extends Zend_Date_DateObject
  35. {
  36. private $_locale = null;
  37. // Fractional second variables
  38. private $_fractional = 0;
  39. private $_precision = 3;
  40. private static $_options = array(
  41. 'format_type' => 'iso', // format for date strings 'iso' or 'php'
  42. 'fix_dst' => true, // fix dst on summer/winter time change
  43. 'extend_month' => false, // false - addMonth like SQL, true like excel
  44. 'cache' => null, // cache to set
  45. 'timesync' => null // timesync server to set
  46. );
  47. // Class wide Date Constants
  48. const DAY = 'dd';
  49. const DAY_SHORT = 'd';
  50. const DAY_SUFFIX = 'SS';
  51. const DAY_OF_YEAR = 'D';
  52. const WEEKDAY = 'EEEE';
  53. const WEEKDAY_SHORT = 'EEE';
  54. const WEEKDAY_NARROW = 'E';
  55. const WEEKDAY_NAME = 'EE';
  56. const WEEKDAY_8601 = 'eee';
  57. const WEEKDAY_DIGIT = 'e';
  58. const WEEK = 'ww';
  59. const MONTH = 'MM';
  60. const MONTH_SHORT = 'M';
  61. const MONTH_DAYS = 'ddd';
  62. const MONTH_NAME = 'MMMM';
  63. const MONTH_NAME_SHORT = 'MMM';
  64. const MONTH_NAME_NARROW = 'MMMMM';
  65. const YEAR = 'y';
  66. const YEAR_SHORT = 'yy';
  67. const YEAR_8601 = 'Y';
  68. const YEAR_SHORT_8601 = 'YY';
  69. const LEAPYEAR = 'l';
  70. const MERIDIEM = 'a';
  71. const SWATCH = 'B';
  72. const HOUR = 'HH';
  73. const HOUR_SHORT = 'H';
  74. const HOUR_AM = 'hh';
  75. const HOUR_SHORT_AM = 'h';
  76. const MINUTE = 'mm';
  77. const MINUTE_SHORT = 'm';
  78. const SECOND = 'ss';
  79. const SECOND_SHORT = 's';
  80. const MILLISECOND = 'S';
  81. const TIMEZONE_NAME = 'zzzz';
  82. const DAYLIGHT = 'I';
  83. const GMT_DIFF = 'Z';
  84. const GMT_DIFF_SEP = 'ZZZZ';
  85. const TIMEZONE = 'z';
  86. const TIMEZONE_SECS = 'X';
  87. const ISO_8601 = 'c';
  88. const RFC_2822 = 'r';
  89. const TIMESTAMP = 'U';
  90. const ERA = 'G';
  91. const ERA_NAME = 'GGGG';
  92. const ERA_NARROW = 'GGGGG';
  93. const DATES = 'F';
  94. const DATE_FULL = 'FFFFF';
  95. const DATE_LONG = 'FFFF';
  96. const DATE_MEDIUM = 'FFF';
  97. const DATE_SHORT = 'FF';
  98. const TIMES = 'WW';
  99. const TIME_FULL = 'TTTTT';
  100. const TIME_LONG = 'TTTT';
  101. const TIME_MEDIUM = 'TTT';
  102. const TIME_SHORT = 'TT';
  103. const DATETIME = 'K';
  104. const DATETIME_FULL = 'KKKKK';
  105. const DATETIME_LONG = 'KKKK';
  106. const DATETIME_MEDIUM = 'KKK';
  107. const DATETIME_SHORT = 'KK';
  108. const ATOM = 'OOO';
  109. const COOKIE = 'CCC';
  110. const RFC_822 = 'R';
  111. const RFC_850 = 'RR';
  112. const RFC_1036 = 'RRR';
  113. const RFC_1123 = 'RRRR';
  114. const RFC_3339 = 'RRRRR';
  115. const RSS = 'SSS';
  116. const W3C = 'WWW';
  117. /**
  118. * Minimum allowed year value
  119. */
  120. const YEAR_MIN_VALUE = -10000;
  121. /**
  122. * Maximum allowed year value
  123. */
  124. const YEAR_MAX_VALUE = 10000;
  125. /**
  126. * Generates the standard date object, could be a unix timestamp, localized date,
  127. * string, integer, array and so on. Also parts of dates or time are supported
  128. * Always set the default timezone: http://php.net/date_default_timezone_set
  129. * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
  130. * For detailed instructions please look in the docu.
  131. *
  132. * @param string|integer|Zend_Date|array $date OPTIONAL Date value or value of date part to set
  133. * ,depending on $part. If null the actual time is set
  134. * @param string $part OPTIONAL Defines the input format of $date
  135. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  136. * @return Zend_Date
  137. * @throws Zend_Date_Exception
  138. */
  139. public function __construct($date = null, $part = null, $locale = null)
  140. {
  141. if (is_object($date) and !($date instanceof Zend_TimeSync_Protocol) and
  142. !($date instanceof Zend_Date)) {
  143. if ($locale instanceof Zend_Locale) {
  144. $locale = $date;
  145. $date = null;
  146. $part = null;
  147. } else {
  148. $date = (string) $date;
  149. }
  150. }
  151. if (($date !== null) and !is_array($date) and !($date instanceof Zend_TimeSync_Protocol) and
  152. !($date instanceof Zend_Date) and !defined($date) and Zend_Locale::isLocale($date, true, false)) {
  153. $locale = $date;
  154. $date = null;
  155. $part = null;
  156. } else if (($part !== null) and !defined($part) and Zend_Locale::isLocale($part, true, false)) {
  157. $locale = $part;
  158. $part = null;
  159. }
  160. $this->setLocale($locale);
  161. if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
  162. $part = $date;
  163. $date = null;
  164. }
  165. if ($date === null) {
  166. if ($part === null) {
  167. $date = time();
  168. } else if ($part !== self::TIMESTAMP) {
  169. $date = self::now($locale);
  170. $date = $date->get($part);
  171. }
  172. }
  173. if ($date instanceof Zend_TimeSync_Protocol) {
  174. $date = $date->getInfo();
  175. $date = $this->_getTime($date['offset']);
  176. $part = null;
  177. } else if (parent::$_defaultOffset != 0) {
  178. $date = $this->_getTime(parent::$_defaultOffset);
  179. }
  180. // set the timezone and offset for $this
  181. $zone = @date_default_timezone_get();
  182. $this->setTimezone($zone);
  183. // try to get timezone from date-string
  184. if (!is_int($date)) {
  185. $zone = $this->getTimezoneFromString($date);
  186. $this->setTimezone($zone);
  187. }
  188. // set datepart
  189. if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
  190. // switch off dst handling for value setting
  191. $this->setUnixTimestamp($this->getGmtOffset());
  192. $this->set($date, $part, $this->_locale);
  193. // DST fix
  194. if (is_array($date) === true) {
  195. if (!isset($date['hour'])) {
  196. $date['hour'] = 0;
  197. }
  198. $hour = $this->toString('H', 'iso', true);
  199. $hour = $date['hour'] - $hour;
  200. switch ($hour) {
  201. case 1 :
  202. case -23 :
  203. $this->addTimestamp(3600);
  204. break;
  205. case -1 :
  206. case 23 :
  207. $this->subTimestamp(3600);
  208. break;
  209. case 2 :
  210. case -22 :
  211. $this->addTimestamp(7200);
  212. break;
  213. case -2 :
  214. case 22 :
  215. $this->subTimestamp(7200);
  216. break;
  217. }
  218. }
  219. } else {
  220. $this->setUnixTimestamp($date);
  221. }
  222. }
  223. /**
  224. * Sets class wide options, if no option was given, the actual set options will be returned
  225. *
  226. * @param array $options Options to set
  227. * @throws Zend_Date_Exception
  228. * @return Options array if no option was given
  229. */
  230. public static function setOptions(array $options = array())
  231. {
  232. if (empty($options)) {
  233. return self::$_options;
  234. }
  235. foreach ($options as $name => $value) {
  236. $name = strtolower($name);
  237. if (array_key_exists($name, self::$_options)) {
  238. switch($name) {
  239. case 'format_type' :
  240. if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
  241. #require_once 'Zend/Date/Exception.php';
  242. throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", 0, null, $value);
  243. }
  244. break;
  245. case 'fix_dst' :
  246. if (!is_bool($value)) {
  247. #require_once 'Zend/Date/Exception.php';
  248. throw new Zend_Date_Exception("'fix_dst' has to be boolean", 0, null, $value);
  249. }
  250. break;
  251. case 'extend_month' :
  252. if (!is_bool($value)) {
  253. #require_once 'Zend/Date/Exception.php';
  254. throw new Zend_Date_Exception("'extend_month' has to be boolean", 0, null, $value);
  255. }
  256. break;
  257. case 'cache' :
  258. if ($value === null) {
  259. parent::$_cache = null;
  260. } else {
  261. if (!$value instanceof Zend_Cache_Core) {
  262. #require_once 'Zend/Date/Exception.php';
  263. throw new Zend_Date_Exception("Instance of Zend_Cache expected");
  264. }
  265. parent::$_cache = $value;
  266. parent::$_cacheTags = Zend_Date_DateObject::_getTagSupportForCache();
  267. Zend_Locale_Data::setCache($value);
  268. }
  269. break;
  270. case 'timesync' :
  271. if ($value === null) {
  272. parent::$_defaultOffset = 0;
  273. } else {
  274. if (!$value instanceof Zend_TimeSync_Protocol) {
  275. #require_once 'Zend/Date/Exception.php';
  276. throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
  277. }
  278. $date = $value->getInfo();
  279. parent::$_defaultOffset = $date['offset'];
  280. }
  281. break;
  282. }
  283. self::$_options[$name] = $value;
  284. }
  285. else {
  286. #require_once 'Zend/Date/Exception.php';
  287. throw new Zend_Date_Exception("Unknown option: $name = $value");
  288. }
  289. }
  290. }
  291. /**
  292. * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
  293. * If the timestamp is too large for integers, then the return value will be a string.
  294. * This function does not return the timestamp as an object.
  295. * Use clone() or copyPart() instead.
  296. *
  297. * @return integer|string UNIX timestamp
  298. */
  299. public function getTimestamp()
  300. {
  301. return $this->getUnixTimestamp();
  302. }
  303. /**
  304. * Returns the calculated timestamp
  305. * HINT: timestamps are always GMT
  306. *
  307. * @param string $calc Type of calculation to make
  308. * @param string|integer|array|Zend_Date $stamp Timestamp to calculate, when null the actual timestamp is calculated
  309. * @return Zend_Date|integer
  310. * @throws Zend_Date_Exception
  311. */
  312. private function _timestamp($calc, $stamp)
  313. {
  314. if ($stamp instanceof Zend_Date) {
  315. // extract timestamp from object
  316. $stamp = $stamp->getTimestamp();
  317. }
  318. if (is_array($stamp)) {
  319. if (isset($stamp['timestamp']) === true) {
  320. $stamp = $stamp['timestamp'];
  321. } else {
  322. #require_once 'Zend/Date/Exception.php';
  323. throw new Zend_Date_Exception('no timestamp given in array');
  324. }
  325. }
  326. if ($calc === 'set') {
  327. $return = $this->setUnixTimestamp($stamp);
  328. } else {
  329. $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
  330. }
  331. if ($calc != 'cmp') {
  332. return $this;
  333. }
  334. return $return;
  335. }
  336. /**
  337. * Sets a new timestamp
  338. *
  339. * @param integer|string|array|Zend_Date $timestamp Timestamp to set
  340. * @return Zend_Date Provides a fluent interface
  341. * @throws Zend_Date_Exception
  342. */
  343. public function setTimestamp($timestamp)
  344. {
  345. return $this->_timestamp('set', $timestamp);
  346. }
  347. /**
  348. * Adds a timestamp
  349. *
  350. * @param integer|string|array|Zend_Date $timestamp Timestamp to add
  351. * @return Zend_Date Provides a fluent interface
  352. * @throws Zend_Date_Exception
  353. */
  354. public function addTimestamp($timestamp)
  355. {
  356. return $this->_timestamp('add', $timestamp);
  357. }
  358. /**
  359. * Subtracts a timestamp
  360. *
  361. * @param integer|string|array|Zend_Date $timestamp Timestamp to sub
  362. * @return Zend_Date Provides a fluent interface
  363. * @throws Zend_Date_Exception
  364. */
  365. public function subTimestamp($timestamp)
  366. {
  367. return $this->_timestamp('sub', $timestamp);
  368. }
  369. /**
  370. * Compares two timestamps, returning the difference as integer
  371. *
  372. * @param integer|string|array|Zend_Date $timestamp Timestamp to compare
  373. * @return integer 0 = equal, 1 = later, -1 = earlier
  374. * @throws Zend_Date_Exception
  375. */
  376. public function compareTimestamp($timestamp)
  377. {
  378. return $this->_timestamp('cmp', $timestamp);
  379. }
  380. /**
  381. * Returns a string representation of the object
  382. * Supported format tokens are:
  383. * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
  384. * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
  385. * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
  386. *
  387. * Additionally format tokens but non ISO conform are:
  388. * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
  389. * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
  390. * r - RFC2822 format, U - unix timestamp
  391. *
  392. * Not supported ISO tokens are
  393. * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
  394. * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
  395. * v - wall zone
  396. *
  397. * @param string $format OPTIONAL Rule for formatting output. If null the default date format is used
  398. * @param string $type OPTIONAL Type for the format string which overrides the standard setting
  399. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  400. * @return string
  401. */
  402. public function toString($format = null, $type = null, $locale = null)
  403. {
  404. if (is_object($format)) {
  405. if ($format instanceof Zend_Locale) {
  406. $locale = $format;
  407. $format = null;
  408. } else {
  409. $format = (string) $format;
  410. }
  411. }
  412. if (is_object($type)) {
  413. if ($type instanceof Zend_Locale) {
  414. $locale = $type;
  415. $type = null;
  416. } else {
  417. $type = (string) $type;
  418. }
  419. }
  420. if (($format !== null) && !defined($format)
  421. && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
  422. && Zend_Locale::isLocale($format, null, false)) {
  423. $locale = $format;
  424. $format = null;
  425. }
  426. if (($type !== null) and ($type != 'php') and ($type != 'iso') and
  427. Zend_Locale::isLocale($type, null, false)) {
  428. $locale = $type;
  429. $type = null;
  430. }
  431. if ($locale === null) {
  432. $locale = $this->getLocale();
  433. }
  434. if ($format === null) {
  435. $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
  436. } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
  437. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  438. }
  439. return $this->date($this->_toToken($format, $locale), $this->getUnixTimestamp(), false);
  440. }
  441. /**
  442. * Returns a string representation of the date which is equal with the timestamp
  443. *
  444. * @return string
  445. */
  446. public function __toString()
  447. {
  448. return $this->toString(null, $this->_locale);
  449. }
  450. /**
  451. * Returns a integer representation of the object
  452. * But returns false when the given part is no value f.e. Month-Name
  453. *
  454. * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
  455. * @return integer|false
  456. */
  457. public function toValue($part = null)
  458. {
  459. $result = $this->get($part);
  460. if (is_numeric($result)) {
  461. return intval("$result");
  462. } else {
  463. return false;
  464. }
  465. }
  466. /**
  467. * Returns an array representation of the object
  468. *
  469. * @return array
  470. */
  471. public function toArray()
  472. {
  473. return array('day' => $this->toString(self::DAY_SHORT, 'iso'),
  474. 'month' => $this->toString(self::MONTH_SHORT, 'iso'),
  475. 'year' => $this->toString(self::YEAR, 'iso'),
  476. 'hour' => $this->toString(self::HOUR_SHORT, 'iso'),
  477. 'minute' => $this->toString(self::MINUTE_SHORT, 'iso'),
  478. 'second' => $this->toString(self::SECOND_SHORT, 'iso'),
  479. 'timezone' => $this->toString(self::TIMEZONE, 'iso'),
  480. 'timestamp' => $this->toString(self::TIMESTAMP, 'iso'),
  481. 'weekday' => $this->toString(self::WEEKDAY_8601, 'iso'),
  482. 'dayofyear' => $this->toString(self::DAY_OF_YEAR, 'iso'),
  483. 'week' => $this->toString(self::WEEK, 'iso'),
  484. 'gmtsecs' => $this->toString(self::TIMEZONE_SECS, 'iso'));
  485. }
  486. /**
  487. * Returns a representation of a date or datepart
  488. * This could be for example a localized monthname, the time without date,
  489. * the era or only the fractional seconds. There are about 50 different supported date parts.
  490. * For a complete list of supported datepart values look into the docu
  491. *
  492. * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
  493. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  494. * @return string date or datepart
  495. */
  496. public function get($part = null, $locale = null)
  497. {
  498. if ($locale === null) {
  499. $locale = $this->getLocale();
  500. }
  501. if (($part !== null) && !defined($part)
  502. && ($part != 'ee') && ($part != 'ss') && ($part != 'GG') && ($part != 'MM') && ($part != 'EE') && ($part != 'TT')
  503. && Zend_Locale::isLocale($part, null, false)) {
  504. $locale = $part;
  505. $part = null;
  506. }
  507. if ($part === null) {
  508. $part = self::TIMESTAMP;
  509. } else if (self::$_options['format_type'] == 'php') {
  510. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  511. }
  512. return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
  513. }
  514. /**
  515. * Internal method to apply tokens
  516. *
  517. * @param string $part
  518. * @param string $locale
  519. * @return string
  520. */
  521. private function _toToken($part, $locale) {
  522. // get format tokens
  523. $comment = false;
  524. $format = '';
  525. $orig = '';
  526. for ($i = 0; isset($part[$i]); ++$i) {
  527. if ($part[$i] == "'") {
  528. $comment = $comment ? false : true;
  529. if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
  530. $comment = $comment ? false : true;
  531. $format .= "\\'";
  532. ++$i;
  533. }
  534. $orig = '';
  535. continue;
  536. }
  537. if ($comment) {
  538. $format .= '\\' . $part[$i];
  539. $orig = '';
  540. } else {
  541. $orig .= $part[$i];
  542. if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
  543. $format .= $this->_parseIsoToDate($orig, $locale);
  544. $orig = '';
  545. }
  546. }
  547. }
  548. return $format;
  549. }
  550. /**
  551. * Internal parsing method
  552. *
  553. * @param string $token
  554. * @param string $locale
  555. * @return string
  556. */
  557. private function _parseIsoToDate($token, $locale) {
  558. switch($token) {
  559. case self::DAY :
  560. return 'd';
  561. break;
  562. case self::WEEKDAY_SHORT :
  563. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  564. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  565. return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
  566. break;
  567. case self::DAY_SHORT :
  568. return 'j';
  569. break;
  570. case self::WEEKDAY :
  571. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  572. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
  573. break;
  574. case self::WEEKDAY_8601 :
  575. return 'N';
  576. break;
  577. case 'ee' :
  578. return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  579. break;
  580. case self::DAY_SUFFIX :
  581. return 'S';
  582. break;
  583. case self::WEEKDAY_DIGIT :
  584. return 'w';
  585. break;
  586. case self::DAY_OF_YEAR :
  587. return 'z';
  588. break;
  589. case 'DDD' :
  590. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
  591. break;
  592. case 'DD' :
  593. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  594. break;
  595. case self::WEEKDAY_NARROW :
  596. case 'EEEEE' :
  597. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  598. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  599. return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
  600. break;
  601. case self::WEEKDAY_NAME :
  602. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  603. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
  604. break;
  605. case 'w' :
  606. $week = $this->date('W', $this->getUnixTimestamp(), false);
  607. return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
  608. break;
  609. case self::WEEK :
  610. return 'W';
  611. break;
  612. case self::MONTH_NAME :
  613. $month = $this->date('n', $this->getUnixTimestamp(), false);
  614. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
  615. break;
  616. case self::MONTH :
  617. return 'm';
  618. break;
  619. case self::MONTH_NAME_SHORT :
  620. $month = $this->date('n', $this->getUnixTimestamp(), false);
  621. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
  622. break;
  623. case self::MONTH_SHORT :
  624. return 'n';
  625. break;
  626. case self::MONTH_DAYS :
  627. return 't';
  628. break;
  629. case self::MONTH_NAME_NARROW :
  630. $month = $this->date('n', $this->getUnixTimestamp(), false);
  631. $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  632. return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
  633. break;
  634. case self::LEAPYEAR :
  635. return 'L';
  636. break;
  637. case self::YEAR_8601 :
  638. return 'o';
  639. break;
  640. case self::YEAR :
  641. return 'Y';
  642. break;
  643. case self::YEAR_SHORT :
  644. return 'y';
  645. break;
  646. case self::YEAR_SHORT_8601 :
  647. return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
  648. break;
  649. case self::MERIDIEM :
  650. $am = $this->date('a', $this->getUnixTimestamp(), false);
  651. if ($am == 'am') {
  652. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'am'));
  653. }
  654. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'pm'));
  655. break;
  656. case self::SWATCH :
  657. return 'B';
  658. break;
  659. case self::HOUR_SHORT_AM :
  660. return 'g';
  661. break;
  662. case self::HOUR_SHORT :
  663. return 'G';
  664. break;
  665. case self::HOUR_AM :
  666. return 'h';
  667. break;
  668. case self::HOUR :
  669. return 'H';
  670. break;
  671. case self::MINUTE :
  672. return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  673. break;
  674. case self::SECOND :
  675. return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  676. break;
  677. case self::MINUTE_SHORT :
  678. return 'i';
  679. break;
  680. case self::SECOND_SHORT :
  681. return 's';
  682. break;
  683. case self::MILLISECOND :
  684. return $this->_toComment($this->getMilliSecond());
  685. break;
  686. case self::TIMEZONE_NAME :
  687. case 'vvvv' :
  688. return 'e';
  689. break;
  690. case self::DAYLIGHT :
  691. return 'I';
  692. break;
  693. case self::GMT_DIFF :
  694. case 'ZZ' :
  695. case 'ZZZ' :
  696. return 'O';
  697. break;
  698. case self::GMT_DIFF_SEP :
  699. return 'P';
  700. break;
  701. case self::TIMEZONE :
  702. case 'v' :
  703. case 'zz' :
  704. case 'zzz' :
  705. return 'T';
  706. break;
  707. case self::TIMEZONE_SECS :
  708. return 'Z';
  709. break;
  710. case self::ISO_8601 :
  711. return 'c';
  712. break;
  713. case self::RFC_2822 :
  714. return 'r';
  715. break;
  716. case self::TIMESTAMP :
  717. return 'U';
  718. break;
  719. case self::ERA :
  720. case 'GG' :
  721. case 'GGG' :
  722. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  723. if ($year < 0) {
  724. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
  725. }
  726. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
  727. break;
  728. case self::ERA_NARROW :
  729. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  730. if ($year < 0) {
  731. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
  732. }
  733. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
  734. break;
  735. case self::ERA_NAME :
  736. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  737. if ($year < 0) {
  738. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0')));
  739. }
  740. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1')));
  741. break;
  742. case self::DATES :
  743. return $this->_toToken(Zend_Locale_Format::getDateFormat($locale), $locale);
  744. break;
  745. case self::DATE_FULL :
  746. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')), $locale);
  747. break;
  748. case self::DATE_LONG :
  749. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')), $locale);
  750. break;
  751. case self::DATE_MEDIUM :
  752. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
  753. break;
  754. case self::DATE_SHORT :
  755. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')), $locale);
  756. break;
  757. case self::TIMES :
  758. return $this->_toToken(Zend_Locale_Format::getTimeFormat($locale), $locale);
  759. break;
  760. case self::TIME_FULL :
  761. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'full'), $locale);
  762. break;
  763. case self::TIME_LONG :
  764. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'long'), $locale);
  765. break;
  766. case self::TIME_MEDIUM :
  767. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'medium'), $locale);
  768. break;
  769. case self::TIME_SHORT :
  770. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'short'), $locale);
  771. break;
  772. case self::DATETIME :
  773. return $this->_toToken(Zend_Locale_Format::getDateTimeFormat($locale), $locale);
  774. break;
  775. case self::DATETIME_FULL :
  776. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
  777. break;
  778. case self::DATETIME_LONG :
  779. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
  780. break;
  781. case self::DATETIME_MEDIUM :
  782. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
  783. break;
  784. case self::DATETIME_SHORT :
  785. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
  786. break;
  787. case self::ATOM :
  788. return 'Y\-m\-d\TH\:i\:sP';
  789. break;
  790. case self::COOKIE :
  791. return 'l\, d\-M\-y H\:i\:s e';
  792. break;
  793. case self::RFC_822 :
  794. return 'D\, d M y H\:i\:s O';
  795. break;
  796. case self::RFC_850 :
  797. return 'l\, d\-M\-y H\:i\:s e';
  798. break;
  799. case self::RFC_1036 :
  800. return 'D\, d M y H\:i\:s O';
  801. break;
  802. case self::RFC_1123 :
  803. return 'D\, d M Y H\:i\:s O';
  804. break;
  805. case self::RFC_3339 :
  806. return 'Y\-m\-d\TH\:i\:sP';
  807. break;
  808. case self::RSS :
  809. return 'D\, d M Y H\:i\:s O';
  810. break;
  811. case self::W3C :
  812. return 'Y\-m\-d\TH\:i\:sP';
  813. break;
  814. }
  815. if ($token == '') {
  816. return '';
  817. }
  818. switch ($token[0]) {
  819. case 'y' :
  820. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  821. return 'Y';
  822. }
  823. $length = iconv_strlen($token, 'UTF-8');
  824. return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  825. break;
  826. case 'Y' :
  827. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  828. return 'o';
  829. }
  830. $length = iconv_strlen($token, 'UTF-8');
  831. return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  832. break;
  833. case 'A' :
  834. $length = iconv_strlen($token, 'UTF-8');
  835. $result = substr($this->getMilliSecond(), 0, 3);
  836. $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
  837. $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
  838. $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
  839. return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
  840. break;
  841. }
  842. return $this->_toComment($token);
  843. }
  844. /**
  845. * Private function to make a comment of a token
  846. *
  847. * @param string $token
  848. * @return string
  849. */
  850. private function _toComment($token)
  851. {
  852. $token = str_split($token);
  853. $result = '';
  854. foreach ($token as $tok) {
  855. $result .= '\\' . $tok;
  856. }
  857. return $result;
  858. }
  859. /**
  860. * Return digit from standard names (english)
  861. * Faster implementation than locale aware searching
  862. *
  863. * @param string $name
  864. * @return integer Number of this month
  865. * @throws Zend_Date_Exception
  866. */
  867. private function _getDigitFromName($name)
  868. {
  869. switch($name) {
  870. case "Jan":
  871. return 1;
  872. case "Feb":
  873. return 2;
  874. case "Mar":
  875. return 3;
  876. case "Apr":
  877. return 4;
  878. case "May":
  879. return 5;
  880. case "Jun":
  881. return 6;
  882. case "Jul":
  883. return 7;
  884. case "Aug":
  885. return 8;
  886. case "Sep":
  887. return 9;
  888. case "Oct":
  889. return 10;
  890. case "Nov":
  891. return 11;
  892. case "Dec":
  893. return 12;
  894. default:
  895. #require_once 'Zend/Date/Exception.php';
  896. throw new Zend_Date_Exception('Month ($name) is not a known month');
  897. }
  898. }
  899. /**
  900. * Counts the exact year number
  901. * < 70 - 2000 added, >70 < 100 - 1900, others just returned
  902. *
  903. * @param integer $value year number
  904. * @return integer Number of year
  905. */
  906. public static function getFullYear($value)
  907. {
  908. if ($value >= 0) {
  909. if ($value < 70) {
  910. $value += 2000;
  911. } else if ($value < 100) {
  912. $value += 1900;
  913. }
  914. }
  915. return $value;
  916. }
  917. /**
  918. * Sets the given date as new date or a given datepart as new datepart returning the new datepart
  919. * This could be for example a localized dayname, the date without time,
  920. * the month or only the seconds. There are about 50 different supported date parts.
  921. * For a complete list of supported datepart values look into the docu
  922. *
  923. * @param string|integer|array|Zend_Date $date Date or datepart to set
  924. * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
  925. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  926. * @return Zend_Date Provides a fluent interface
  927. * @throws Zend_Date_Exception
  928. */
  929. public function set($date, $part = null, $locale = null)
  930. {
  931. if (self::$_options['format_type'] == 'php') {
  932. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  933. }
  934. $zone = $this->getTimezoneFromString($date);
  935. $this->setTimezone($zone);
  936. $this->_calculate('set', $date, $part, $locale);
  937. return $this;
  938. }
  939. /**
  940. * Adds a date or datepart to the existing date, by extracting $part from $date,
  941. * and modifying this object by adding that part. The $part is then extracted from
  942. * this object and returned as an integer or numeric string (for large values, or $part's
  943. * corresponding to pre-defined formatted date strings).
  944. * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
  945. * There are about 50 different supported date parts.
  946. * For a complete list of supported datepart values look into the docu.
  947. *
  948. * @param string|integer|array|Zend_Date $date Date or datepart to add
  949. * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
  950. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  951. * @return Zend_Date Provides a fluent interface
  952. * @throws Zend_Date_Exception
  953. */
  954. public function add($date, $part = self::TIMESTAMP, $locale = null)
  955. {
  956. if (self::$_options['format_type'] == 'php') {
  957. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  958. }
  959. $this->_calculate('add', $date, $part, $locale);
  960. return $this;
  961. }
  962. /**
  963. * Subtracts a date from another date.
  964. * This could be for example a RFC2822 date, the time,
  965. * the year or only the timestamp. There are about 50 different supported date parts.
  966. * For a complete list of supported datepart values look into the docu
  967. * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
  968. *
  969. * @param string|integer|array|Zend_Date $date Date or datepart to subtract
  970. * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
  971. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  972. * @return Zend_Date Provides a fluent interface
  973. * @throws Zend_Date_Exception
  974. */
  975. public function sub($date, $part = self::TIMESTAMP, $locale = null)
  976. {
  977. if (self::$_options['format_type'] == 'php') {
  978. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  979. }
  980. $this->_calculate('sub', $date, $part, $locale);
  981. return $this;
  982. }
  983. /**
  984. * Compares a date or datepart with the existing one.
  985. * Returns -1 if earlier, 0 if equal and 1 if later.
  986. *
  987. * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
  988. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
  989. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  990. * @return integer 0 = equal, 1 = later, -1 = earlier
  991. * @throws Zend_Date_Exception
  992. */
  993. public function compare($date, $part = self::TIMESTAMP, $locale = null)
  994. {
  995. if (self::$_options['format_type'] == 'php') {
  996. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  997. }
  998. $compare = $this->_calculate('cmp', $date, $part, $locale);
  999. if ($compare > 0) {
  1000. return 1;
  1001. } else if ($compare < 0) {
  1002. return -1;
  1003. }
  1004. return 0;
  1005. }
  1006. /**
  1007. * Returns a new instance of Zend_Date with the selected part copied.
  1008. * To make an exact copy, use PHP's clone keyword.
  1009. * For a complete list of supported date part values look into the docu.
  1010. * If a date part is copied, all other date parts are set to standard values.
  1011. * For example: If only YEAR is copied, the returned date object is equal to
  1012. * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
  1013. * If only HOUR is copied, the returned date object is equal to
  1014. * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
  1015. *
  1016. * @param string $part Part of the date to compare, if null the timestamp is subtracted
  1017. * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
  1018. * @return Zend_Date New clone with requested part
  1019. */
  1020. public function copyPart($part, $locale = null)
  1021. {
  1022. $clone = clone $this; // copy all instance variables
  1023. $clone->setUnixTimestamp(0); // except the timestamp
  1024. if ($locale != null) {
  1025. $clone->setLocale($locale); // set an other locale if selected
  1026. }
  1027. $clone->set($this, $part);
  1028. return $clone;
  1029. }
  1030. /**
  1031. * Internal function, returns the offset of a given timezone
  1032. *
  1033. * @param string $zone
  1034. * @return integer
  1035. */
  1036. public function getTimezoneFromString($zone)
  1037. {
  1038. if (is_array($zone)) {
  1039. return $this->getTimezone();
  1040. }
  1041. if ($zone instanceof Zend_Date) {
  1042. return $zone->getTimezone();
  1043. }
  1044. $match = array();
  1045. preg_match('/\dZ$/', $zone, $match);
  1046. if (!empty($match)) {
  1047. return "Etc/UTC";
  1048. }
  1049. preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
  1050. if (!empty($match) and ($match[count($match) - 1] <= 14) and ($match[count($match) - 1] >= -12)) {
  1051. $zone = "Etc/GMT";
  1052. $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
  1053. $zone .= (int) abs($match[count($match) - 1]);
  1054. return $zone;
  1055. }
  1056. preg_match('/([[:alpha:]\/_]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
  1057. try {
  1058. if (!empty($match) and (!is_int($match[count($match) - 1]))) {
  1059. $oldzone = $this->getTimezone();
  1060. $this->setTimezone($match[count($match) - 1]);
  1061. $result = $this->getTimezone();
  1062. $this->setTimezone($oldzone);
  1063. if ($result !== $oldzone) {
  1064. return $match[count($match) - 1];
  1065. }
  1066. }
  1067. } catch (Exception $e) {
  1068. // fall through
  1069. }
  1070. return $this->getTimezone();
  1071. }
  1072. /**
  1073. * Calculates the date or object
  1074. *
  1075. * @param string $calc Calculation to make
  1076. * @param string|integer $date Date for calculation
  1077. * @param string|integer $comp Second date for calculation
  1078. * @param boolean|integer $dst Use dst correction if option is set
  1079. * @return integer|string|Zend_Date new timestamp or Zend_Date depending on calculation
  1080. */
  1081. private function _assign($calc, $date, $comp = 0, $dst = false)
  1082. {
  1083. switch ($calc) {
  1084. case 'set' :
  1085. if (!empty($comp)) {
  1086. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
  1087. }
  1088. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1089. $value = $this->getUnixTimestamp();
  1090. break;
  1091. case 'add' :
  1092. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1093. $value = $this->getUnixTimestamp();
  1094. break;
  1095. case 'sub' :
  1096. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
  1097. $value = $this->getUnixTimestamp();
  1098. break;
  1099. default :
  1100. // cmp - compare
  1101. return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
  1102. break;
  1103. }
  1104. // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
  1105. if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
  1106. $hour = $this->toString(self::HOUR, 'iso');
  1107. if ($hour != $dst) {
  1108. if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
  1109. $value += 3600;
  1110. } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
  1111. $value -= 3600;
  1112. }
  1113. $this->setUnixTimestamp($value);
  1114. }
  1115. }
  1116. return $this->getUnixTimestamp();
  1117. }
  1118. /**
  1119. * Calculates the date or object
  1120. *
  1121. * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
  1122. * @param string|integer|array|Zend_Date $date Date or datepart to calculate with
  1123. * @param string $part Part of the date to calculate, if null the timestamp is used
  1124. * @param string|Zend_Locale $locale Locale for parsing input
  1125. * @return integer|string|Zend_Date new timestamp
  1126. * @throws Zend_Date_Exception
  1127. */
  1128. private function _calculate($calc, $date, $part, $locale)
  1129. {
  1130. if ($date === null) {
  1131. #require_once 'Zend/Date/Exception.php';
  1132. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  1133. }
  1134. if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
  1135. $locale = $part;
  1136. $part = null;
  1137. }
  1138. if ($locale === null) {
  1139. $locale = $this->getLocale();
  1140. }
  1141. $locale = (string) $locale;
  1142. // Create date parts
  1143. $year = $this->toString(self::YEAR, 'iso');
  1144. $month = $this->toString(self::MONTH_SHORT, 'iso');
  1145. $day = $this->toString(self::DAY_SHORT, 'iso');
  1146. $hour = $this->toString(self::HOUR_SHORT, 'iso');
  1147. $minute = $this->toString(self::MINUTE_SHORT, 'iso');
  1148. $second = $this->toString(self::SECOND_SHORT, 'iso');
  1149. // If object extract value
  1150. if ($date instanceof Zend_Date) {
  1151. $date = $date->toString($part, 'iso', $locale);
  1152. }
  1153. if (is_array($date) === true) {
  1154. if (empty($part) === false) {
  1155. switch($part) {
  1156. // Fall through
  1157. case self::DAY:
  1158. case self::DAY_SHORT:
  1159. if (isset($date['day']) === true) {
  1160. $date = $date['day'];
  1161. }
  1162. break;
  1163. // Fall through
  1164. case self::WEEKDAY_SHORT:
  1165. case self::WEEKDAY:
  1166. case self::WEEKDAY_8601:
  1167. case self::WEEKDAY_DIGIT:
  1168. case self::WEEKDAY_NARROW:
  1169. case self::WEEKDAY_NAME:
  1170. if (isset($date['weekday']) === true) {
  1171. $date = $date['weekday'];
  1172. $part = self::WEEKDAY_DIGIT;
  1173. }
  1174. break;
  1175. case self::DAY_OF_YEAR:
  1176. if (isset($date['day_of_year']) === true) {
  1177. $date = $date['day_of_year'];
  1178. }
  1179. break;
  1180. // Fall through
  1181. case self::MONTH:
  1182. case self::MONTH_SHORT:
  1183. case self::MONTH_NAME:
  1184. case self::MONTH_NAME_SHORT:
  1185. case self::MONTH_NAME_NARROW:
  1186. if (isset($date['month']) === true) {
  1187. $date = $date['month'];
  1188. }
  1189. break;
  1190. // Fall through
  1191. case self::YEAR:
  1192. case self::YEAR_SHORT:
  1193. case self::YEAR_8601:
  1194. case self::YEAR_SHORT_8601:
  1195. if (isset($date['year']) === true) {
  1196. $date = $date['year'];
  1197. }
  1198. break;
  1199. // Fall through
  1200. case self::HOUR:
  1201. case self::HOUR_AM:
  1202. case self::HOUR_SHORT:
  1203. case self::HOUR_SHORT_AM:
  1204. if (isset($date['hour']) === true) {
  1205. $date = $date['hour'];
  1206. }
  1207. break;
  1208. // Fall through
  1209. case self::MINUTE:
  1210. case self::MINUTE_SHORT:
  1211. if (isset($date['minute']) === true) {
  1212. $date = $date['minute'];
  1213. }
  1214. break;
  1215. // Fall through
  1216. case self::SECOND:
  1217. case self::SECOND_SHORT:
  1218. if (isset($date['second']) === true) {
  1219. $date = $date['second'];
  1220. }
  1221. break;
  1222. // Fall through
  1223. case self::TIMEZONE:
  1224. case self::TIMEZONE_NAME:
  1225. if (isset($date['timezone']) === true) {
  1226. $date = $date['timezone'];
  1227. }
  1228. break;
  1229. case self::TIMESTAMP:
  1230. if (isset($date['timestamp']) === true) {
  1231. $date = $date['timestamp'];
  1232. }
  1233. break;
  1234. case self::WEEK:
  1235. if (isset($date['week']) === true) {
  1236. $date = $date['week'];
  1237. }
  1238. break;
  1239. case self::TIMEZONE_SECS:
  1240. if (isset($date['gmtsecs']) === true) {
  1241. $date = $date['gmtsecs'];
  1242. }
  1243. break;
  1244. default:
  1245. #require_once 'Zend/Date/Exception.php';
  1246. throw new Zend_Date_Exception("datepart for part ($part) not found in array");
  1247. break;
  1248. }
  1249. } else {
  1250. $hours = 0;
  1251. if (isset($date['hour']) === true) {
  1252. $hours = $date['hour'];
  1253. }
  1254. $minutes = 0;
  1255. if (isset($date['minute']) === true) {
  1256. $minutes = $date['minute'];
  1257. }
  1258. $seconds = 0;
  1259. if (isset($date['second']) === true) {
  1260. $seconds = $date['second'];
  1261. }
  1262. $months = 0;
  1263. if (isset($date['month']) === true) {
  1264. $months = $date['month'];
  1265. }
  1266. $days = 0;
  1267. if (isset($date['day']) === true) {
  1268. $days = $date['day'];
  1269. }
  1270. $years = 0;
  1271. if (isset($date['year']) === true) {
  1272. $years = $date['year'];
  1273. }
  1274. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
  1275. $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
  1276. }
  1277. }
  1278. // $date as object, part of foreign date as own date
  1279. switch($part) {
  1280. // day formats
  1281. case self::DAY:
  1282. if (is_numeric($date)) {
  1283. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1284. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1285. }
  1286. #require_once 'Zend/Date/Exception.php';
  1287. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
  1288. break;
  1289. case self::WEEKDAY_SHORT:
  1290. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1291. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1292. $cnt = 0;
  1293. foreach ($daylist as $key => $value) {
  1294. if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) {
  1295. $found = $cnt;
  1296. break;
  1297. }
  1298. ++$cnt;
  1299. }
  1300. // Weekday found
  1301. if ($cnt < 7) {
  1302. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1303. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1304. }
  1305. // Weekday not found
  1306. #require_once 'Zend/Date/Exception.php';
  1307. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1308. break;
  1309. case self::DAY_SHORT:
  1310. if (is_numeric($date)) {
  1311. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1312. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1313. }
  1314. #require_once 'Zend/Date/Exception.php';
  1315. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
  1316. break;
  1317. case self::WEEKDAY:
  1318. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1319. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1320. $cnt = 0;
  1321. foreach ($daylist as $key => $value) {
  1322. if (strtoupper($value) == strtoupper($date)) {
  1323. $found = $cnt;
  1324. break;
  1325. }
  1326. ++$cnt;
  1327. }
  1328. // Weekday found
  1329. if ($cnt < 7) {
  1330. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1331. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1332. }
  1333. // Weekday not found
  1334. #require_once 'Zend/Date/Exception.php';
  1335. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1336. break;
  1337. case self::WEEKDAY_8601:
  1338. $weekday = (int) $this->toString(self::WEEKDAY_8601, 'iso', $locale);
  1339. if ((intval($date) > 0) and (intval($date) < 8)) {
  1340. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1341. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1342. }
  1343. // Weekday not found
  1344. #require_once 'Zend/Date/Exception.php';
  1345. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1346. break;
  1347. case self::DAY_SUFFIX:
  1348. #require_once 'Zend/Date/Exception.php';
  1349. throw new Zend_Date_Exception('day suffix not supported', 0, null, $date);
  1350. break;
  1351. case self::WEEKDAY_DIGIT:
  1352. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1353. if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
  1354. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1355. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1356. }
  1357. // Weekday not found
  1358. #require_once 'Zend/Date/Exception.php';
  1359. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1360. break;
  1361. case self::DAY_OF_YEAR:
  1362. if (is_numeric($date)) {
  1363. if (($calc == 'add') || ($calc == 'sub')) {
  1364. $year = 1970;
  1365. ++$date;
  1366. ++$day;
  1367. }
  1368. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, $date, $year, true),
  1369. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1370. }
  1371. #require_once 'Zend/Date/Exception.php';
  1372. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
  1373. break;
  1374. case self::WEEKDAY_NARROW:
  1375. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1376. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1377. $cnt = 0;
  1378. foreach ($daylist as $key => $value) {
  1379. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) {
  1380. $found = $cnt;
  1381. break;
  1382. }
  1383. ++$cnt;
  1384. }
  1385. // Weekday found
  1386. if ($cnt < 7) {
  1387. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1388. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1389. }
  1390. // Weekday not found
  1391. #require_once 'Zend/Date/Exception.php';
  1392. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1393. break;
  1394. case self::WEEKDAY_NAME:
  1395. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1396. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1397. $cnt = 0;
  1398. foreach ($daylist as $key => $value) {
  1399. if (strtoupper($value) == strtoupper($date)) {
  1400. $found = $cnt;
  1401. break;
  1402. }
  1403. ++$cnt;
  1404. }
  1405. // Weekday found
  1406. if ($cnt < 7) {
  1407. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1408. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1409. }
  1410. // Weekday not found
  1411. #require_once 'Zend/Date/Exception.php';
  1412. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1413. break;
  1414. // week formats
  1415. case self::WEEK:
  1416. if (is_numeric($date)) {
  1417. $week = (int) $this->toString(self::WEEK, 'iso', $locale);
  1418. return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
  1419. parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
  1420. }
  1421. #require_once 'Zend/Date/Exception.php';
  1422. throw new Zend_Date_Exception("invalid date ($date) operand, week expected", 0, null, $date);
  1423. break;
  1424. // month formats
  1425. case self::MONTH_NAME:
  1426. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  1427. $cnt = 0;
  1428. foreach ($monthlist as $key => $value) {
  1429. if (strtoupper($value) == strtoupper($date)) {
  1430. $found = $key;
  1431. break;
  1432. }
  1433. ++$cnt;
  1434. }
  1435. $date = array_search($date, $monthlist);
  1436. // Monthname found
  1437. if ($cnt < 12) {
  1438. $fixday = 0;
  1439. if ($calc == 'add') {
  1440. $date += $found;
  1441. $calc = 'set';
  1442. if (self::$_options['extend_month'] == false) {
  1443. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1444. if ($parts['mday'] != $day) {
  1445. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1446. }
  1447. }
  1448. } else if ($calc == 'sub') {
  1449. $date = $month - $found;
  1450. $calc = 'set';
  1451. if (self::$_options['extend_month'] == false) {
  1452. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1453. if ($parts['mday'] != $day) {
  1454. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1455. }
  1456. }
  1457. }
  1458. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1459. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1460. }
  1461. // Monthname not found
  1462. #require_once 'Zend/Date/Exception.php';
  1463. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1464. break;
  1465. case self::MONTH:
  1466. if (is_numeric($date)) {
  1467. $fixday = 0;
  1468. if ($calc == 'add') {
  1469. $date += $month;
  1470. $calc = 'set';
  1471. if (self::$_options['extend_month'] == false) {
  1472. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1473. if ($parts['mday'] != $day) {
  1474. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1475. }
  1476. }
  1477. } else if ($calc == 'sub') {
  1478. $date = $month - $date;
  1479. $calc = 'set';
  1480. if (self::$_options['extend_month'] == false) {
  1481. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1482. if ($parts['mday'] != $day) {
  1483. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1484. }
  1485. }
  1486. }
  1487. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1488. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1489. }
  1490. #require_once 'Zend/Date/Exception.php';
  1491. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1492. break;
  1493. case self::MONTH_NAME_SHORT:
  1494. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  1495. $cnt = 0;
  1496. foreach ($monthlist as $key => $value) {
  1497. if (strtoupper($value) == strtoupper($date)) {
  1498. $found = $key;
  1499. break;
  1500. }
  1501. ++$cnt;
  1502. }
  1503. $date = array_search($date, $monthlist);
  1504. // Monthname found
  1505. if ($cnt < 12) {
  1506. $fixday = 0;
  1507. if ($calc == 'add') {
  1508. $date += $found;
  1509. $calc = 'set';
  1510. if (self::$_options['extend_month'] === false) {
  1511. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1512. if ($parts['mday'] != $day) {
  1513. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1514. }
  1515. }
  1516. } else if ($calc == 'sub') {
  1517. $date = $month - $found;
  1518. $calc = 'set';
  1519. if (self::$_options['extend_month'] === false) {
  1520. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1521. if ($parts['mday'] != $day) {
  1522. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1523. }
  1524. }
  1525. }
  1526. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1527. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1528. }
  1529. // Monthname not found
  1530. #require_once 'Zend/Date/Exception.php';
  1531. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1532. break;
  1533. case self::MONTH_SHORT:
  1534. if (is_numeric($date) === true) {
  1535. $fixday = 0;
  1536. if ($calc === 'add') {
  1537. $date += $month;
  1538. $calc = 'set';
  1539. if (self::$_options['extend_month'] === false) {
  1540. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1541. if ($parts['mday'] != $day) {
  1542. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1543. }
  1544. }
  1545. } else if ($calc === 'sub') {
  1546. $date = $month - $date;
  1547. $calc = 'set';
  1548. if (self::$_options['extend_month'] === false) {
  1549. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1550. if ($parts['mday'] != $day) {
  1551. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1552. }
  1553. }
  1554. }
  1555. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1556. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1557. }
  1558. #require_once 'Zend/Date/Exception.php';
  1559. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1560. break;
  1561. case self::MONTH_DAYS:
  1562. #require_once 'Zend/Date/Exception.php';
  1563. throw new Zend_Date_Exception('month days not supported', 0, null, $date);
  1564. break;
  1565. case self::MONTH_NAME_NARROW:
  1566. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
  1567. $cnt = 0;
  1568. foreach ($monthlist as $key => $value) {
  1569. if (strtoupper($value) === strtoupper($date)) {
  1570. $found = $key;
  1571. break;
  1572. }
  1573. ++$cnt;
  1574. }
  1575. $date = array_search($date, $monthlist);
  1576. // Monthname found
  1577. if ($cnt < 12) {
  1578. $fixday = 0;
  1579. if ($calc === 'add') {
  1580. $date += $found;
  1581. $calc = 'set';
  1582. if (self::$_options['extend_month'] === false) {
  1583. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1584. if ($parts['mday'] != $day) {
  1585. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1586. }
  1587. }
  1588. } else if ($calc === 'sub') {
  1589. $date = $month - $found;
  1590. $calc = 'set';
  1591. if (self::$_options['extend_month'] === false) {
  1592. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1593. if ($parts['mday'] != $day) {
  1594. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1595. }
  1596. }
  1597. }
  1598. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1599. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1600. }
  1601. // Monthname not found
  1602. #require_once 'Zend/Date/Exception.php';
  1603. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1604. break;
  1605. // year formats
  1606. case self::LEAPYEAR:
  1607. #require_once 'Zend/Date/Exception.php';
  1608. throw new Zend_Date_Exception('leap year not supported', 0, null, $date);
  1609. break;
  1610. case self::YEAR_8601:
  1611. if (is_numeric($date)) {
  1612. if ($calc === 'add') {
  1613. $date += $year;
  1614. $calc = 'set';
  1615. } else if ($calc === 'sub') {
  1616. $date = $year - $date;
  1617. $calc = 'set';
  1618. }
  1619. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1620. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1621. }
  1622. #require_once 'Zend/Date/Exception.php';
  1623. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1624. break;
  1625. case self::YEAR:
  1626. if (is_numeric($date)) {
  1627. if ($calc === 'add') {
  1628. $date += $year;
  1629. $calc = 'set';
  1630. } else if ($calc === 'sub') {
  1631. $date = $year - $date;
  1632. $calc = 'set';
  1633. }
  1634. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1635. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1636. }
  1637. #require_once 'Zend/Date/Exception.php';
  1638. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1639. break;
  1640. case self::YEAR_SHORT:
  1641. if (is_numeric($date)) {
  1642. $date = intval($date);
  1643. if (($calc == 'set') || ($calc == 'cmp')) {
  1644. $date = self::getFullYear($date);
  1645. }
  1646. if ($calc === 'add') {
  1647. $date += $year;
  1648. $calc = 'set';
  1649. } else if ($calc === 'sub') {
  1650. $date = $year - $date;
  1651. $calc = 'set';
  1652. }
  1653. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1654. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1655. }
  1656. #require_once 'Zend/Date/Exception.php';
  1657. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1658. break;
  1659. case self::YEAR_SHORT_8601:
  1660. if (is_numeric($date)) {
  1661. $date = intval($date);
  1662. if (($calc === 'set') || ($calc === 'cmp')) {
  1663. $date = self::getFullYear($date);
  1664. }
  1665. if ($calc === 'add') {
  1666. $date += $year;
  1667. $calc = 'set';
  1668. } else if ($calc === 'sub') {
  1669. $date = $year - $date;
  1670. $calc = 'set';
  1671. }
  1672. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1673. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1674. }
  1675. #require_once 'Zend/Date/Exception.php';
  1676. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1677. break;
  1678. // time formats
  1679. case self::MERIDIEM:
  1680. #require_once 'Zend/Date/Exception.php';
  1681. throw new Zend_Date_Exception('meridiem not supported', 0, null, $date);
  1682. break;
  1683. case self::SWATCH:
  1684. if (is_numeric($date)) {
  1685. $rest = intval($date);
  1686. $hours = floor($rest * 24 / 1000);
  1687. $rest = $rest - ($hours * 1000 / 24);
  1688. $minutes = floor($rest * 1440 / 1000);
  1689. $rest = $rest - ($minutes * 1000 / 1440);
  1690. $seconds = floor($rest * 86400 / 1000);
  1691. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
  1692. $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
  1693. }
  1694. #require_once 'Zend/Date/Exception.php';
  1695. throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", 0, null, $date);
  1696. break;
  1697. case self::HOUR_SHORT_AM:
  1698. if (is_numeric($date)) {
  1699. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1700. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1701. }
  1702. #require_once 'Zend/Date/Exception.php';
  1703. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1704. break;
  1705. case self::HOUR_SHORT:
  1706. if (is_numeric($date)) {
  1707. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1708. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1709. }
  1710. #require_once 'Zend/Date/Exception.php';
  1711. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1712. break;
  1713. case self::HOUR_AM:
  1714. if (is_numeric($date)) {
  1715. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1716. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1717. }
  1718. #require_once 'Zend/Date/Exception.php';
  1719. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1720. break;
  1721. case self::HOUR:
  1722. if (is_numeric($date)) {
  1723. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1724. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1725. }
  1726. #require_once 'Zend/Date/Exception.php';
  1727. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1728. break;
  1729. case self::MINUTE:
  1730. if (is_numeric($date)) {
  1731. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1732. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1733. }
  1734. #require_once 'Zend/Date/Exception.php';
  1735. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
  1736. break;
  1737. case self::SECOND:
  1738. if (is_numeric($date)) {
  1739. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1740. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1741. }
  1742. #require_once 'Zend/Date/Exception.php';
  1743. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
  1744. break;
  1745. case self::MILLISECOND:
  1746. if (is_numeric($date)) {
  1747. switch($calc) {
  1748. case 'set' :
  1749. return $this->setMillisecond($date);
  1750. break;
  1751. case 'add' :
  1752. return $this->addMillisecond($date);
  1753. break;
  1754. case 'sub' :
  1755. return $this->subMillisecond($date);
  1756. break;
  1757. }
  1758. return $this->compareMillisecond($date);
  1759. }
  1760. #require_once 'Zend/Date/Exception.php';
  1761. throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", 0, null, $date);
  1762. break;
  1763. case self::MINUTE_SHORT:
  1764. if (is_numeric($date)) {
  1765. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1766. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1767. }
  1768. #require_once 'Zend/Date/Exception.php';
  1769. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
  1770. break;
  1771. case self::SECOND_SHORT:
  1772. if (is_numeric($date)) {
  1773. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1774. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1775. }
  1776. #require_once 'Zend/Date/Exception.php';
  1777. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
  1778. break;
  1779. // timezone formats
  1780. // break intentionally omitted
  1781. case self::TIMEZONE_NAME:
  1782. case self::TIMEZONE:
  1783. case self::TIMEZONE_SECS:
  1784. #require_once 'Zend/Date/Exception.php';
  1785. throw new Zend_Date_Exception('timezone not supported', 0, null, $date);
  1786. break;
  1787. case self::DAYLIGHT:
  1788. #require_once 'Zend/Date/Exception.php';
  1789. throw new Zend_Date_Exception('daylight not supported', 0, null, $date);
  1790. break;
  1791. case self::GMT_DIFF:
  1792. case self::GMT_DIFF_SEP:
  1793. #require_once 'Zend/Date/Exception.php';
  1794. throw new Zend_Date_Exception('gmtdiff not supported', 0, null, $date);
  1795. break;
  1796. // date strings
  1797. case self::ISO_8601:
  1798. // (-)YYYY-MM-dd
  1799. preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
  1800. // (-)YY-MM-dd
  1801. if (empty($datematch)) {
  1802. preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
  1803. }
  1804. // (-)YYYYMMdd
  1805. if (empty($datematch)) {
  1806. preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
  1807. }
  1808. // (-)YYMMdd
  1809. if (empty($datematch)) {
  1810. preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
  1811. }
  1812. $tmpdate = $date;
  1813. if (!empty($datematch)) {
  1814. $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8');
  1815. $tmpdate = iconv_substr($date,
  1816. $dateMatchCharCount,
  1817. iconv_strlen($date, 'UTF-8') - $dateMatchCharCount,
  1818. 'UTF-8');
  1819. }
  1820. // (T)hh:mm:ss
  1821. preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
  1822. // (T)hhmmss
  1823. if (empty($timematch)) {
  1824. preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
  1825. }
  1826. // (T)hh:mm
  1827. if (empty($timematch)) {
  1828. preg_match('/[T,\s]{0,1}(\d{2}):(\d{2})/', $tmpdate, $timematch);
  1829. }
  1830. // (T)hhmm
  1831. if (empty($timematch)) {
  1832. preg_match('/[T,\s]{0,1}(\d{2})(\d{2})/', $tmpdate, $timematch);
  1833. }
  1834. if (empty($datematch) and empty($timematch)) {
  1835. #require_once 'Zend/Date/Exception.php';
  1836. throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", 0, null, $date);
  1837. }
  1838. if (!empty($timematch)) {
  1839. $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
  1840. $tmpdate = iconv_substr($tmpdate,
  1841. $timeMatchCharCount,
  1842. iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
  1843. 'UTF-8');
  1844. }
  1845. if (empty($datematch)) {
  1846. $datematch[1] = 1970;
  1847. $datematch[2] = 1;
  1848. $datematch[3] = 1;
  1849. } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
  1850. $datematch[1] = self::getFullYear($datematch[1]);
  1851. }
  1852. if (empty($timematch)) {
  1853. $timematch[1] = 0;
  1854. $timematch[2] = 0;
  1855. $timematch[3] = 0;
  1856. }
  1857. if (!isset($timematch[3])) {
  1858. $timematch[3] = 0;
  1859. }
  1860. if (($calc == 'set') || ($calc == 'cmp')) {
  1861. --$datematch[2];
  1862. --$month;
  1863. --$datematch[3];
  1864. --$day;
  1865. $datematch[1] -= 1970;
  1866. $year -= 1970;
  1867. }
  1868. return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
  1869. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1870. break;
  1871. case self::RFC_2822:
  1872. $result = preg_match('/^\w{3},\s(\d{1,2})\s(\w{3})\s(\d{4})\s'
  1873. . '(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]'
  1874. . '{1}\d{4}|\w{1,20})$/', $date, $match);
  1875. if (!$result) {
  1876. #require_once 'Zend/Date/Exception.php';
  1877. throw new Zend_Date_Exception("no RFC 2822 format ($date)", 0, null, $date);
  1878. }
  1879. $months = $this->_getDigitFromName($match[2]);
  1880. if (($calc == 'set') || ($calc == 'cmp')) {
  1881. --$months;
  1882. --$month;
  1883. --$match[1];
  1884. --$day;
  1885. $match[3] -= 1970;
  1886. $year -= 1970;
  1887. }
  1888. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  1889. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1890. break;
  1891. case self::TIMESTAMP:
  1892. if (is_numeric($date)) {
  1893. return $this->_assign($calc, $date, $this->getUnixTimestamp());
  1894. }
  1895. #require_once 'Zend/Date/Exception.php';
  1896. throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", 0, null, $date);
  1897. break;
  1898. // additional formats
  1899. // break intentionally omitted
  1900. case self::ERA:
  1901. case self::ERA_NAME:
  1902. #require_once 'Zend/Date/Exception.php';
  1903. throw new Zend_Date_Exception('era not supported', 0, null, $date);
  1904. break;
  1905. case self::DATES:
  1906. try {
  1907. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1908. if (($calc == 'set') || ($calc == 'cmp')) {
  1909. --$parsed['month'];
  1910. --$month;
  1911. --$parsed['day'];
  1912. --$day;
  1913. $parsed['year'] -= 1970;
  1914. $year -= 1970;
  1915. }
  1916. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1917. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1918. } catch (Zend_Locale_Exception $e) {
  1919. #require_once 'Zend/Date/Exception.php';
  1920. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1921. }
  1922. break;
  1923. case self::DATE_FULL:
  1924. try {
  1925. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  1926. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1927. if (($calc == 'set') || ($calc == 'cmp')) {
  1928. --$parsed['month'];
  1929. --$month;
  1930. --$parsed['day'];
  1931. --$day;
  1932. $parsed['year'] -= 1970;
  1933. $year -= 1970;
  1934. }
  1935. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1936. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1937. } catch (Zend_Locale_Exception $e) {
  1938. #require_once 'Zend/Date/Exception.php';
  1939. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1940. }
  1941. break;
  1942. case self::DATE_LONG:
  1943. try {
  1944. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  1945. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1946. if (($calc == 'set') || ($calc == 'cmp')){
  1947. --$parsed['month'];
  1948. --$month;
  1949. --$parsed['day'];
  1950. --$day;
  1951. $parsed['year'] -= 1970;
  1952. $year -= 1970;
  1953. }
  1954. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1955. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1956. } catch (Zend_Locale_Exception $e) {
  1957. #require_once 'Zend/Date/Exception.php';
  1958. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1959. }
  1960. break;
  1961. case self::DATE_MEDIUM:
  1962. try {
  1963. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  1964. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1965. if (($calc == 'set') || ($calc == 'cmp')) {
  1966. --$parsed['month'];
  1967. --$month;
  1968. --$parsed['day'];
  1969. --$day;
  1970. $parsed['year'] -= 1970;
  1971. $year -= 1970;
  1972. }
  1973. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1974. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1975. } catch (Zend_Locale_Exception $e) {
  1976. #require_once 'Zend/Date/Exception.php';
  1977. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1978. }
  1979. break;
  1980. case self::DATE_SHORT:
  1981. try {
  1982. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  1983. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1984. $parsed['year'] = self::getFullYear($parsed['year']);
  1985. if (($calc == 'set') || ($calc == 'cmp')) {
  1986. --$parsed['month'];
  1987. --$month;
  1988. --$parsed['day'];
  1989. --$day;
  1990. $parsed['year'] -= 1970;
  1991. $year -= 1970;
  1992. }
  1993. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1994. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1995. } catch (Zend_Locale_Exception $e) {
  1996. #require_once 'Zend/Date/Exception.php';
  1997. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1998. }
  1999. break;
  2000. case self::TIMES:
  2001. try {
  2002. if ($calc != 'set') {
  2003. $month = 1;
  2004. $day = 1;
  2005. $year = 1970;
  2006. }
  2007. $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  2008. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2009. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2010. } catch (Zend_Locale_Exception $e) {
  2011. #require_once 'Zend/Date/Exception.php';
  2012. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2013. }
  2014. break;
  2015. case self::TIME_FULL:
  2016. try {
  2017. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  2018. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2019. if ($calc != 'set') {
  2020. $month = 1;
  2021. $day = 1;
  2022. $year = 1970;
  2023. }
  2024. if (!isset($parsed['second'])) {
  2025. $parsed['second'] = 0;
  2026. }
  2027. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2028. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2029. } catch (Zend_Locale_Exception $e) {
  2030. #require_once 'Zend/Date/Exception.php';
  2031. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2032. }
  2033. break;
  2034. case self::TIME_LONG:
  2035. try {
  2036. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  2037. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2038. if ($calc != 'set') {
  2039. $month = 1;
  2040. $day = 1;
  2041. $year = 1970;
  2042. }
  2043. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2044. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2045. } catch (Zend_Locale_Exception $e) {
  2046. #require_once 'Zend/Date/Exception.php';
  2047. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2048. }
  2049. break;
  2050. case self::TIME_MEDIUM:
  2051. try {
  2052. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  2053. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2054. if ($calc != 'set') {
  2055. $month = 1;
  2056. $day = 1;
  2057. $year = 1970;
  2058. }
  2059. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2060. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2061. } catch (Zend_Locale_Exception $e) {
  2062. #require_once 'Zend/Date/Exception.php';
  2063. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2064. }
  2065. break;
  2066. case self::TIME_SHORT:
  2067. try {
  2068. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  2069. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2070. if ($calc != 'set') {
  2071. $month = 1;
  2072. $day = 1;
  2073. $year = 1970;
  2074. }
  2075. if (!isset($parsed['second'])) {
  2076. $parsed['second'] = 0;
  2077. }
  2078. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2079. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2080. } catch (Zend_Locale_Exception $e) {
  2081. #require_once 'Zend/Date/Exception.php';
  2082. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2083. }
  2084. break;
  2085. case self::DATETIME:
  2086. try {
  2087. $parsed = Zend_Locale_Format::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  2088. if (($calc == 'set') || ($calc == 'cmp')) {
  2089. --$parsed['month'];
  2090. --$month;
  2091. --$parsed['day'];
  2092. --$day;
  2093. $parsed['year'] -= 1970;
  2094. $year -= 1970;
  2095. }
  2096. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2097. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2098. } catch (Zend_Locale_Exception $e) {
  2099. #require_once 'Zend/Date/Exception.php';
  2100. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2101. }
  2102. break;
  2103. case self::DATETIME_FULL:
  2104. try {
  2105. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
  2106. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2107. if (($calc == 'set') || ($calc == 'cmp')) {
  2108. --$parsed['month'];
  2109. --$month;
  2110. --$parsed['day'];
  2111. --$day;
  2112. $parsed['year'] -= 1970;
  2113. $year -= 1970;
  2114. }
  2115. if (!isset($parsed['second'])) {
  2116. $parsed['second'] = 0;
  2117. }
  2118. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2119. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2120. } catch (Zend_Locale_Exception $e) {
  2121. #require_once 'Zend/Date/Exception.php';
  2122. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2123. }
  2124. break;
  2125. case self::DATETIME_LONG:
  2126. try {
  2127. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
  2128. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2129. if (($calc == 'set') || ($calc == 'cmp')){
  2130. --$parsed['month'];
  2131. --$month;
  2132. --$parsed['day'];
  2133. --$day;
  2134. $parsed['year'] -= 1970;
  2135. $year -= 1970;
  2136. }
  2137. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2138. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2139. } catch (Zend_Locale_Exception $e) {
  2140. #require_once 'Zend/Date/Exception.php';
  2141. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2142. }
  2143. break;
  2144. case self::DATETIME_MEDIUM:
  2145. try {
  2146. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
  2147. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2148. if (($calc == 'set') || ($calc == 'cmp')) {
  2149. --$parsed['month'];
  2150. --$month;
  2151. --$parsed['day'];
  2152. --$day;
  2153. $parsed['year'] -= 1970;
  2154. $year -= 1970;
  2155. }
  2156. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2157. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2158. } catch (Zend_Locale_Exception $e) {
  2159. #require_once 'Zend/Date/Exception.php';
  2160. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2161. }
  2162. break;
  2163. case self::DATETIME_SHORT:
  2164. try {
  2165. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
  2166. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2167. $parsed['year'] = self::getFullYear($parsed['year']);
  2168. if (($calc == 'set') || ($calc == 'cmp')) {
  2169. --$parsed['month'];
  2170. --$month;
  2171. --$parsed['day'];
  2172. --$day;
  2173. $parsed['year'] -= 1970;
  2174. $year -= 1970;
  2175. }
  2176. if (!isset($parsed['second'])) {
  2177. $parsed['second'] = 0;
  2178. }
  2179. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2180. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2181. } catch (Zend_Locale_Exception $e) {
  2182. #require_once 'Zend/Date/Exception.php';
  2183. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2184. }
  2185. break;
  2186. // ATOM and RFC_3339 are identical
  2187. case self::ATOM:
  2188. case self::RFC_3339:
  2189. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\d{0,4}([+-]{1}\d{2}:\d{2}|Z)$/', $date, $match);
  2190. if (!$result) {
  2191. #require_once 'Zend/Date/Exception.php';
  2192. throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", 0, null, $date);
  2193. }
  2194. if (($calc == 'set') || ($calc == 'cmp')) {
  2195. --$match[2];
  2196. --$month;
  2197. --$match[3];
  2198. --$day;
  2199. $match[1] -= 1970;
  2200. $year -= 1970;
  2201. }
  2202. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2203. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2204. break;
  2205. case self::COOKIE:
  2206. $result = preg_match("/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,20}$/", $date, $match);
  2207. if (!$result) {
  2208. #require_once 'Zend/Date/Exception.php';
  2209. throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", 0, null, $date);
  2210. }
  2211. $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1;
  2212. $match[0] = iconv_substr($match[0],
  2213. $matchStartPos,
  2214. iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
  2215. 'UTF-8');
  2216. $months = $this->_getDigitFromName($match[2]);
  2217. $match[3] = self::getFullYear($match[3]);
  2218. if (($calc == 'set') || ($calc == 'cmp')) {
  2219. --$months;
  2220. --$month;
  2221. --$match[1];
  2222. --$day;
  2223. $match[3] -= 1970;
  2224. $year -= 1970;
  2225. }
  2226. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2227. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2228. break;
  2229. case self::RFC_822:
  2230. case self::RFC_1036:
  2231. // new RFC 822 format, identical to RFC 1036 standard
  2232. $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
  2233. if (!$result) {
  2234. #require_once 'Zend/Date/Exception.php';
  2235. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", 0, null, $date);
  2236. }
  2237. $months = $this->_getDigitFromName($match[2]);
  2238. $match[3] = self::getFullYear($match[3]);
  2239. if (($calc == 'set') || ($calc == 'cmp')) {
  2240. --$months;
  2241. --$month;
  2242. --$match[1];
  2243. --$day;
  2244. $match[3] -= 1970;
  2245. $year -= 1970;
  2246. }
  2247. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  2248. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  2249. break;
  2250. case self::RFC_850:
  2251. $result = preg_match('/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,21}$/', $date, $match);
  2252. if (!$result) {
  2253. #require_once 'Zend/Date/Exception.php';
  2254. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", 0, null, $date);
  2255. }
  2256. $months = $this->_getDigitFromName($match[2]);
  2257. $match[3] = self::getFullYear($match[3]);
  2258. if (($calc == 'set') || ($calc == 'cmp')) {
  2259. --$months;
  2260. --$month;
  2261. --$match[1];
  2262. --$day;
  2263. $match[3] -= 1970;
  2264. $year -= 1970;
  2265. }
  2266. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2267. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2268. break;
  2269. case self::RFC_1123:
  2270. $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2,4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
  2271. if (!$result) {
  2272. #require_once 'Zend/Date/Exception.php';
  2273. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", 0, null, $date);
  2274. }
  2275. $months = $this->_getDigitFromName($match[2]);
  2276. if (($calc == 'set') || ($calc == 'cmp')) {
  2277. --$months;
  2278. --$month;
  2279. --$match[1];
  2280. --$day;
  2281. $match[3] -= 1970;
  2282. $year -= 1970;
  2283. }
  2284. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2285. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2286. break;
  2287. case self::RSS:
  2288. $result = preg_match('/^\w{3},\s(\d{2})\s(\w{3})\s(\d{2,4})\s(\d{1,2}):(\d{2}):(\d{2})\s.{1,21}$/', $date, $match);
  2289. if (!$result) {
  2290. #require_once 'Zend/Date/Exception.php';
  2291. throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", 0, null, $date);
  2292. }
  2293. $months = $this->_getDigitFromName($match[2]);
  2294. $match[3] = self::getFullYear($match[3]);
  2295. if (($calc == 'set') || ($calc == 'cmp')) {
  2296. --$months;
  2297. --$month;
  2298. --$match[1];
  2299. --$day;
  2300. $match[3] -= 1970;
  2301. $year -= 1970;
  2302. }
  2303. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2304. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2305. break;
  2306. case self::W3C:
  2307. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
  2308. if (!$result) {
  2309. #require_once 'Zend/Date/Exception.php';
  2310. throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", 0, null, $date);
  2311. }
  2312. if (($calc == 'set') || ($calc == 'cmp')) {
  2313. --$match[2];
  2314. --$month;
  2315. --$match[3];
  2316. --$day;
  2317. $match[1] -= 1970;
  2318. $year -= 1970;
  2319. }
  2320. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2321. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2322. break;
  2323. default:
  2324. if (!is_numeric($date) || !empty($part)) {
  2325. try {
  2326. if (empty($part)) {
  2327. $part = Zend_Locale_Format::getDateFormat($locale) . " ";
  2328. $part .= Zend_Locale_Format::getTimeFormat($locale);
  2329. }
  2330. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
  2331. if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
  2332. $parsed['year'] = self::getFullYear($parsed['year']);
  2333. }
  2334. if (($calc == 'set') || ($calc == 'cmp')) {
  2335. if (isset($parsed['month'])) {
  2336. --$parsed['month'];
  2337. } else {
  2338. $parsed['month'] = 0;
  2339. }
  2340. if (isset($parsed['day'])) {
  2341. --$parsed['day'];
  2342. } else {
  2343. $parsed['day'] = 0;
  2344. }
  2345. if (!isset($parsed['year'])) {
  2346. $parsed['year'] = 1970;
  2347. }
  2348. }
  2349. return $this->_assign($calc, $this->mktime(
  2350. isset($parsed['hour']) ? $parsed['hour'] : 0,
  2351. isset($parsed['minute']) ? $parsed['minute'] : 0,
  2352. isset($parsed['second']) ? $parsed['second'] : 0,
  2353. isset($parsed['month']) ? (1 + $parsed['month']) : 1,
  2354. isset($parsed['day']) ? (1 + $parsed['day']) : 1,
  2355. $parsed['year'],
  2356. false), $this->getUnixTimestamp(), false);
  2357. } catch (Zend_Locale_Exception $e) {
  2358. if (!is_numeric($date)) {
  2359. #require_once 'Zend/Date/Exception.php';
  2360. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2361. }
  2362. }
  2363. }
  2364. return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
  2365. break;
  2366. }
  2367. }
  2368. /**
  2369. * Returns true when both date objects or date parts are equal.
  2370. * For example:
  2371. * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
  2372. *
  2373. * @param string|integer|array|Zend_Date $date Date or datepart to equal with
  2374. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2375. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2376. * @return boolean
  2377. * @throws Zend_Date_Exception
  2378. */
  2379. public function equals($date, $part = self::TIMESTAMP, $locale = null)
  2380. {
  2381. $result = $this->compare($date, $part, $locale);
  2382. if ($result == 0) {
  2383. return true;
  2384. }
  2385. return false;
  2386. }
  2387. /**
  2388. * Returns if the given date or datepart is earlier
  2389. * For example:
  2390. * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
  2391. *
  2392. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2393. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2394. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2395. * @return boolean
  2396. * @throws Zend_Date_Exception
  2397. */
  2398. public function isEarlier($date, $part = null, $locale = null)
  2399. {
  2400. $result = $this->compare($date, $part, $locale);
  2401. if ($result == -1) {
  2402. return true;
  2403. }
  2404. return false;
  2405. }
  2406. /**
  2407. * Returns if the given date or datepart is later
  2408. * For example:
  2409. * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
  2410. * Returns if the given date is later
  2411. *
  2412. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2413. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2414. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2415. * @return boolean
  2416. * @throws Zend_Date_Exception
  2417. */
  2418. public function isLater($date, $part = null, $locale = null)
  2419. {
  2420. $result = $this->compare($date, $part, $locale);
  2421. if ($result == 1) {
  2422. return true;
  2423. }
  2424. return false;
  2425. }
  2426. /**
  2427. * Returns only the time of the date as new Zend_Date object
  2428. * For example:
  2429. * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
  2430. *
  2431. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2432. * @return Zend_Date
  2433. */
  2434. public function getTime($locale = null)
  2435. {
  2436. if (self::$_options['format_type'] == 'php') {
  2437. $format = 'H:i:s';
  2438. } else {
  2439. $format = self::TIME_MEDIUM;
  2440. }
  2441. return $this->copyPart($format, $locale);
  2442. }
  2443. /**
  2444. * Returns the calculated time
  2445. *
  2446. * @param string $calc Calculation to make
  2447. * @param string|integer|array|Zend_Date $time Time to calculate with, if null the actual time is taken
  2448. * @param string $format Timeformat for parsing input
  2449. * @param string|Zend_Locale $locale Locale for parsing input
  2450. * @return integer|Zend_Date new time
  2451. * @throws Zend_Date_Exception
  2452. */
  2453. private function _time($calc, $time, $format, $locale)
  2454. {
  2455. if ($time === null) {
  2456. #require_once 'Zend/Date/Exception.php';
  2457. throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
  2458. }
  2459. if ($time instanceof Zend_Date) {
  2460. // extract time from object
  2461. $time = $time->toString('HH:mm:ss', 'iso');
  2462. } else {
  2463. if (is_array($time)) {
  2464. if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
  2465. (isset($time['second']) === true)) {
  2466. $parsed = $time;
  2467. } else {
  2468. #require_once 'Zend/Date/Exception.php';
  2469. throw new Zend_Date_Exception("no hour, minute or second given in array");
  2470. }
  2471. } else {
  2472. if (self::$_options['format_type'] == 'php') {
  2473. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2474. }
  2475. try {
  2476. if ($locale === null) {
  2477. $locale = $this->getLocale();
  2478. }
  2479. $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2480. } catch (Zend_Locale_Exception $e) {
  2481. #require_once 'Zend/Date/Exception.php';
  2482. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  2483. }
  2484. }
  2485. if (!array_key_exists('hour', $parsed)) {
  2486. $parsed['hour'] = 0;
  2487. }
  2488. if (!array_key_exists('minute', $parsed)) {
  2489. $parsed['minute'] = 0;
  2490. }
  2491. if (!array_key_exists('second', $parsed)) {
  2492. $parsed['second'] = 0;
  2493. }
  2494. $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
  2495. $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
  2496. $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
  2497. }
  2498. $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
  2499. if ($calc != 'cmp') {
  2500. return $this;
  2501. }
  2502. return $return;
  2503. }
  2504. /**
  2505. * Sets a new time for the date object. Format defines how to parse the time string.
  2506. * Also a complete date can be given, but only the time is used for setting.
  2507. * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
  2508. * Returned is the new date object and the existing date is left as it was before
  2509. *
  2510. * @param string|integer|array|Zend_Date $time Time to set
  2511. * @param string $format OPTIONAL Timeformat for parsing input
  2512. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2513. * @return Zend_Date Provides a fluent interface
  2514. * @throws Zend_Date_Exception
  2515. */
  2516. public function setTime($time, $format = null, $locale = null)
  2517. {
  2518. return $this->_time('set', $time, $format, $locale);
  2519. }
  2520. /**
  2521. * Adds a time to the existing date. Format defines how to parse the time string.
  2522. * If only parts are given the other parts are set to 0.
  2523. * If no format is given, the standardformat of this locale is used.
  2524. * For example: HH:mm:ss -> 10 -> +10 hours
  2525. *
  2526. * @param string|integer|array|Zend_Date $time Time to add
  2527. * @param string $format OPTIONAL Timeformat for parsing input
  2528. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2529. * @return Zend_Date Provides a fluent interface
  2530. * @throws Zend_Date_Exception
  2531. */
  2532. public function addTime($time, $format = null, $locale = null)
  2533. {
  2534. return $this->_time('add', $time, $format, $locale);
  2535. }
  2536. /**
  2537. * Subtracts a time from the existing date. Format defines how to parse the time string.
  2538. * If only parts are given the other parts are set to 0.
  2539. * If no format is given, the standardformat of this locale is used.
  2540. * For example: HH:mm:ss -> 10 -> -10 hours
  2541. *
  2542. * @param string|integer|array|Zend_Date $time Time to sub
  2543. * @param string $format OPTIONAL Timeformat for parsing input
  2544. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2545. * @return Zend_Date Provides a fluent inteface
  2546. * @throws Zend_Date_Exception
  2547. */
  2548. public function subTime($time, $format = null, $locale = null)
  2549. {
  2550. return $this->_time('sub', $time, $format, $locale);
  2551. }
  2552. /**
  2553. * Compares the time from the existing date. Format defines how to parse the time string.
  2554. * If only parts are given the other parts are set to default.
  2555. * If no format us given, the standardformat of this locale is used.
  2556. * For example: HH:mm:ss -> 10 -> 10 hours
  2557. *
  2558. * @param string|integer|array|Zend_Date $time Time to compare
  2559. * @param string $format OPTIONAL Timeformat for parsing input
  2560. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2561. * @return integer 0 = equal, 1 = later, -1 = earlier
  2562. * @throws Zend_Date_Exception
  2563. */
  2564. public function compareTime($time, $format = null, $locale = null)
  2565. {
  2566. return $this->_time('cmp', $time, $format, $locale);
  2567. }
  2568. /**
  2569. * Returns a clone of $this, with the time part set to 00:00:00.
  2570. *
  2571. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2572. * @return Zend_Date
  2573. */
  2574. public function getDate($locale = null)
  2575. {
  2576. $orig = self::$_options['format_type'];
  2577. if (self::$_options['format_type'] == 'php') {
  2578. self::$_options['format_type'] = 'iso';
  2579. }
  2580. $date = $this->copyPart(self::DATE_MEDIUM, $locale);
  2581. $date->addTimestamp($this->getGmtOffset());
  2582. self::$_options['format_type'] = $orig;
  2583. return $date;
  2584. }
  2585. /**
  2586. * Returns the calculated date
  2587. *
  2588. * @param string $calc Calculation to make
  2589. * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
  2590. * @param string $format Date format for parsing
  2591. * @param string|Zend_Locale $locale Locale for parsing input
  2592. * @return integer|Zend_Date new date
  2593. * @throws Zend_Date_Exception
  2594. */
  2595. private function _date($calc, $date, $format, $locale)
  2596. {
  2597. if ($date === null) {
  2598. #require_once 'Zend/Date/Exception.php';
  2599. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  2600. }
  2601. if ($date instanceof Zend_Date) {
  2602. // extract date from object
  2603. $date = $date->toString('d.M.y', 'iso');
  2604. } else {
  2605. if (is_array($date)) {
  2606. if ((isset($date['year']) === true) or (isset($date['month']) === true) or
  2607. (isset($date['day']) === true)) {
  2608. $parsed = $date;
  2609. } else {
  2610. #require_once 'Zend/Date/Exception.php';
  2611. throw new Zend_Date_Exception("no day,month or year given in array");
  2612. }
  2613. } else {
  2614. if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  2615. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2616. }
  2617. try {
  2618. if ($locale === null) {
  2619. $locale = $this->getLocale();
  2620. }
  2621. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2622. if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
  2623. $parsed['year'] = self::getFullYear($parsed['year']);
  2624. }
  2625. } catch (Zend_Locale_Exception $e) {
  2626. #require_once 'Zend/Date/Exception.php';
  2627. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  2628. }
  2629. }
  2630. if (!array_key_exists('day', $parsed)) {
  2631. $parsed['day'] = 1;
  2632. }
  2633. if (!array_key_exists('month', $parsed)) {
  2634. $parsed['month'] = 1;
  2635. }
  2636. if (!array_key_exists('year', $parsed)) {
  2637. $parsed['year'] = 0;
  2638. }
  2639. $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
  2640. }
  2641. $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
  2642. if ($calc != 'cmp') {
  2643. return $this;
  2644. }
  2645. return $return;
  2646. }
  2647. /**
  2648. * Sets a new date for the date object. Format defines how to parse the date string.
  2649. * Also a complete date with time can be given, but only the date is used for setting.
  2650. * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
  2651. * Returned is the new date object and the existing time is left as it was before
  2652. *
  2653. * @param string|integer|array|Zend_Date $date Date to set
  2654. * @param string $format OPTIONAL Date format for parsing
  2655. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2656. * @return Zend_Date Provides a fluent interface
  2657. * @throws Zend_Date_Exception
  2658. */
  2659. public function setDate($date, $format = null, $locale = null)
  2660. {
  2661. return $this->_date('set', $date, $format, $locale);
  2662. }
  2663. /**
  2664. * Adds a date to the existing date object. Format defines how to parse the date string.
  2665. * If only parts are given the other parts are set to 0.
  2666. * If no format is given, the standardformat of this locale is used.
  2667. * For example: MM.dd.YYYY -> 10 -> +10 months
  2668. *
  2669. * @param string|integer|array|Zend_Date $date Date to add
  2670. * @param string $format OPTIONAL Date format for parsing input
  2671. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2672. * @return Zend_Date Provides a fluent interface
  2673. * @throws Zend_Date_Exception
  2674. */
  2675. public function addDate($date, $format = null, $locale = null)
  2676. {
  2677. return $this->_date('add', $date, $format, $locale);
  2678. }
  2679. /**
  2680. * Subtracts a date from the existing date object. Format defines how to parse the date string.
  2681. * If only parts are given the other parts are set to 0.
  2682. * If no format is given, the standardformat of this locale is used.
  2683. * For example: MM.dd.YYYY -> 10 -> -10 months
  2684. * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
  2685. *
  2686. * @param string|integer|array|Zend_Date $date Date to sub
  2687. * @param string $format OPTIONAL Date format for parsing input
  2688. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2689. * @return Zend_Date Provides a fluent interface
  2690. * @throws Zend_Date_Exception
  2691. */
  2692. public function subDate($date, $format = null, $locale = null)
  2693. {
  2694. return $this->_date('sub', $date, $format, $locale);
  2695. }
  2696. /**
  2697. * Compares the date from the existing date object, ignoring the time.
  2698. * Format defines how to parse the date string.
  2699. * If only parts are given the other parts are set to 0.
  2700. * If no format is given, the standardformat of this locale is used.
  2701. * For example: 10.01.2000 => 10.02.1999 -> false
  2702. *
  2703. * @param string|integer|array|Zend_Date $date Date to compare
  2704. * @param string $format OPTIONAL Date format for parsing input
  2705. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2706. * @return integer 0 = equal, 1 = later, -1 = earlier
  2707. * @throws Zend_Date_Exception
  2708. */
  2709. public function compareDate($date, $format = null, $locale = null)
  2710. {
  2711. return $this->_date('cmp', $date, $format, $locale);
  2712. }
  2713. /**
  2714. * Returns the full ISO 8601 date from the date object.
  2715. * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
  2716. * (ISO 8601 defines several formats) use toString() instead.
  2717. * This function does not return the ISO date as object. Use copy() instead.
  2718. *
  2719. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2720. * @return string
  2721. */
  2722. public function getIso($locale = null)
  2723. {
  2724. return $this->toString(self::ISO_8601, 'iso', $locale);
  2725. }
  2726. /**
  2727. * Sets a new date for the date object. Not given parts are set to default.
  2728. * Only supported ISO 8601 formats are accepted.
  2729. * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
  2730. * Returned is the new date object
  2731. *
  2732. * @param string|integer|Zend_Date $date ISO Date to set
  2733. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2734. * @return Zend_Date Provides a fluent interface
  2735. * @throws Zend_Date_Exception
  2736. */
  2737. public function setIso($date, $locale = null)
  2738. {
  2739. return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
  2740. }
  2741. /**
  2742. * Adds a ISO date to the date object. Not given parts are set to default.
  2743. * Only supported ISO 8601 formats are accepted.
  2744. * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
  2745. * Returned is the new date object
  2746. *
  2747. * @param string|integer|Zend_Date $date ISO Date to add
  2748. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2749. * @return Zend_Date Provides a fluent interface
  2750. * @throws Zend_Date_Exception
  2751. */
  2752. public function addIso($date, $locale = null)
  2753. {
  2754. return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
  2755. }
  2756. /**
  2757. * Subtracts a ISO date from the date object. Not given parts are set to default.
  2758. * Only supported ISO 8601 formats are accepted.
  2759. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2760. * Returned is the new date object
  2761. *
  2762. * @param string|integer|Zend_Date $date ISO Date to sub
  2763. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2764. * @return Zend_Date Provides a fluent interface
  2765. * @throws Zend_Date_Exception
  2766. */
  2767. public function subIso($date, $locale = null)
  2768. {
  2769. return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
  2770. }
  2771. /**
  2772. * Compares a ISO date with the date object. Not given parts are set to default.
  2773. * Only supported ISO 8601 formats are accepted.
  2774. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2775. * Returns if equal, earlier or later
  2776. *
  2777. * @param string|integer|Zend_Date $date ISO Date to sub
  2778. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2779. * @return integer 0 = equal, 1 = later, -1 = earlier
  2780. * @throws Zend_Date_Exception
  2781. */
  2782. public function compareIso($date, $locale = null)
  2783. {
  2784. return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
  2785. }
  2786. /**
  2787. * Returns a RFC 822 compilant datestring from the date object.
  2788. * This function does not return the RFC date as object. Use copy() instead.
  2789. *
  2790. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2791. * @return string
  2792. */
  2793. public function getArpa($locale = null)
  2794. {
  2795. if (self::$_options['format_type'] == 'php') {
  2796. $format = 'D\, d M y H\:i\:s O';
  2797. } else {
  2798. $format = self::RFC_822;
  2799. }
  2800. return $this->toString($format, 'iso', $locale);
  2801. }
  2802. /**
  2803. * Sets a RFC 822 date as new date for the date object.
  2804. * Only RFC 822 compilant date strings are accepted.
  2805. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2806. * Returned is the new date object
  2807. *
  2808. * @param string|integer|Zend_Date $date RFC 822 to set
  2809. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2810. * @return Zend_Date Provides a fluent interface
  2811. * @throws Zend_Date_Exception
  2812. */
  2813. public function setArpa($date, $locale = null)
  2814. {
  2815. return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
  2816. }
  2817. /**
  2818. * Adds a RFC 822 date to the date object.
  2819. * ARPA messages are used in emails or HTTP Headers.
  2820. * Only RFC 822 compilant date strings are accepted.
  2821. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2822. * Returned is the new date object
  2823. *
  2824. * @param string|integer|Zend_Date $date RFC 822 Date to add
  2825. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2826. * @return Zend_Date Provides a fluent interface
  2827. * @throws Zend_Date_Exception
  2828. */
  2829. public function addArpa($date, $locale = null)
  2830. {
  2831. return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
  2832. }
  2833. /**
  2834. * Subtracts a RFC 822 date from the date object.
  2835. * ARPA messages are used in emails or HTTP Headers.
  2836. * Only RFC 822 compilant date strings are accepted.
  2837. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2838. * Returned is the new date object
  2839. *
  2840. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2841. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2842. * @return Zend_Date Provides a fluent interface
  2843. * @throws Zend_Date_Exception
  2844. */
  2845. public function subArpa($date, $locale = null)
  2846. {
  2847. return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
  2848. }
  2849. /**
  2850. * Compares a RFC 822 compilant date with the date object.
  2851. * ARPA messages are used in emails or HTTP Headers.
  2852. * Only RFC 822 compilant date strings are accepted.
  2853. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2854. * Returns if equal, earlier or later
  2855. *
  2856. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2857. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2858. * @return integer 0 = equal, 1 = later, -1 = earlier
  2859. * @throws Zend_Date_Exception
  2860. */
  2861. public function compareArpa($date, $locale = null)
  2862. {
  2863. return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
  2864. }
  2865. /**
  2866. * Check if location is supported
  2867. *
  2868. * @param array $location locations array
  2869. * @throws Zend_Date_Exception
  2870. * @return float $horizon float
  2871. */
  2872. private function _checkLocation($location)
  2873. {
  2874. if (!isset($location['longitude']) or !isset($location['latitude'])) {
  2875. #require_once 'Zend/Date/Exception.php';
  2876. throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', 0, null, $location);
  2877. }
  2878. if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
  2879. #require_once 'Zend/Date/Exception.php';
  2880. throw new Zend_Date_Exception('Longitude must be between -180 and 180', 0, null, $location);
  2881. }
  2882. if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
  2883. #require_once 'Zend/Date/Exception.php';
  2884. throw new Zend_Date_Exception('Latitude must be between -90 and 90', 0, null, $location);
  2885. }
  2886. if (!isset($location['horizon'])){
  2887. $location['horizon'] = 'effective';
  2888. }
  2889. switch ($location['horizon']) {
  2890. case 'civil' :
  2891. return -0.104528;
  2892. break;
  2893. case 'nautic' :
  2894. return -0.207912;
  2895. break;
  2896. case 'astronomic' :
  2897. return -0.309017;
  2898. break;
  2899. default :
  2900. return -0.0145439;
  2901. break;
  2902. }
  2903. }
  2904. /**
  2905. * Returns the time of sunrise for this date and a given location as new date object
  2906. * For a list of cities and correct locations use the class Zend_Date_Cities
  2907. *
  2908. * @param array $location location of sunrise
  2909. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2910. * ['longitude'] -> longitude of location
  2911. * ['latitude'] -> latitude of location
  2912. * @return Zend_Date
  2913. * @throws Zend_Date_Exception
  2914. */
  2915. public function getSunrise($location)
  2916. {
  2917. $horizon = $this->_checkLocation($location);
  2918. $result = clone $this;
  2919. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2920. return $result;
  2921. }
  2922. /**
  2923. * Returns the time of sunset for this date and a given location as new date object
  2924. * For a list of cities and correct locations use the class Zend_Date_Cities
  2925. *
  2926. * @param array $location location of sunset
  2927. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2928. * ['longitude'] -> longitude of location
  2929. * ['latitude'] -> latitude of location
  2930. * @return Zend_Date
  2931. * @throws Zend_Date_Exception
  2932. */
  2933. public function getSunset($location)
  2934. {
  2935. $horizon = $this->_checkLocation($location);
  2936. $result = clone $this;
  2937. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2938. return $result;
  2939. }
  2940. /**
  2941. * Returns an array with the sunset and sunrise dates for all horizon types
  2942. * For a list of cities and correct locations use the class Zend_Date_Cities
  2943. *
  2944. * @param array $location location of suninfo
  2945. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2946. * ['longitude'] -> longitude of location
  2947. * ['latitude'] -> latitude of location
  2948. * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
  2949. * @throws Zend_Date_Exception
  2950. */
  2951. public function getSunInfo($location)
  2952. {
  2953. $suninfo = array();
  2954. for ($i = 0; $i < 4; ++$i) {
  2955. switch ($i) {
  2956. case 0 :
  2957. $location['horizon'] = 'effective';
  2958. break;
  2959. case 1 :
  2960. $location['horizon'] = 'civil';
  2961. break;
  2962. case 2 :
  2963. $location['horizon'] = 'nautic';
  2964. break;
  2965. case 3 :
  2966. $location['horizon'] = 'astronomic';
  2967. break;
  2968. }
  2969. $horizon = $this->_checkLocation($location);
  2970. $result = clone $this;
  2971. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2972. $suninfo['sunrise'][$location['horizon']] = $result;
  2973. $result = clone $this;
  2974. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2975. $suninfo['sunset'][$location['horizon']] = $result;
  2976. }
  2977. return $suninfo;
  2978. }
  2979. /**
  2980. * Check a given year for leap year.
  2981. *
  2982. * @param integer|array|Zend_Date $year Year to check
  2983. * @throws Zend_Date_Exception
  2984. * @return boolean
  2985. */
  2986. public static function checkLeapYear($year)
  2987. {
  2988. if ($year instanceof Zend_Date) {
  2989. $year = (int) $year->toString(self::YEAR, 'iso');
  2990. }
  2991. if (is_array($year)) {
  2992. if (isset($year['year']) === true) {
  2993. $year = $year['year'];
  2994. } else {
  2995. #require_once 'Zend/Date/Exception.php';
  2996. throw new Zend_Date_Exception("no year given in array");
  2997. }
  2998. }
  2999. if (!is_numeric($year)) {
  3000. #require_once 'Zend/Date/Exception.php';
  3001. throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", 0, null, $year);
  3002. }
  3003. return (bool) parent::isYearLeapYear($year);
  3004. }
  3005. /**
  3006. * Returns true, if the year is a leap year.
  3007. *
  3008. * @return boolean
  3009. */
  3010. public function isLeapYear()
  3011. {
  3012. return self::checkLeapYear($this);
  3013. }
  3014. /**
  3015. * Returns if the set date is todays date
  3016. *
  3017. * @return boolean
  3018. */
  3019. public function isToday()
  3020. {
  3021. $today = $this->date('Ymd', $this->_getTime());
  3022. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3023. return ($today == $day);
  3024. }
  3025. /**
  3026. * Returns if the set date is yesterdays date
  3027. *
  3028. * @return boolean
  3029. */
  3030. public function isYesterday()
  3031. {
  3032. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  3033. // adjusts for leap days and DST changes that are timezone specific
  3034. $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
  3035. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3036. return $day == $yesterday;
  3037. }
  3038. /**
  3039. * Returns if the set date is tomorrows date
  3040. *
  3041. * @return boolean
  3042. */
  3043. public function isTomorrow()
  3044. {
  3045. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  3046. // adjusts for leap days and DST changes that are timezone specific
  3047. $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
  3048. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3049. return $day == $tomorrow;
  3050. }
  3051. /**
  3052. * Returns the actual date as new date object
  3053. *
  3054. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3055. * @return Zend_Date
  3056. */
  3057. public static function now($locale = null)
  3058. {
  3059. return new Zend_Date(time(), self::TIMESTAMP, $locale);
  3060. }
  3061. /**
  3062. * Calculate date details
  3063. *
  3064. * @param string $calc Calculation to make
  3065. * @param string|integer|array|Zend_Date $date Date or Part to calculate
  3066. * @param string $type Datepart for Calculation
  3067. * @param string|Zend_Locale $locale Locale for parsing input
  3068. * @return integer|string new date
  3069. * @throws Zend_Date_Exception
  3070. */
  3071. private function _calcdetail($calc, $date, $type, $locale)
  3072. {
  3073. $old = false;
  3074. if (self::$_options['format_type'] == 'php') {
  3075. self::$_options['format_type'] = 'iso';
  3076. $old = true;
  3077. }
  3078. switch($calc) {
  3079. case 'set' :
  3080. $return = $this->set($date, $type, $locale);
  3081. break;
  3082. case 'add' :
  3083. $return = $this->add($date, $type, $locale);
  3084. break;
  3085. case 'sub' :
  3086. $return = $this->sub($date, $type, $locale);
  3087. break;
  3088. default :
  3089. $return = $this->compare($date, $type, $locale);
  3090. break;
  3091. }
  3092. if ($old) {
  3093. self::$_options['format_type'] = 'php';
  3094. }
  3095. return $return;
  3096. }
  3097. /**
  3098. * Internal calculation, returns the requested date type
  3099. *
  3100. * @param string $calc Calculation to make
  3101. * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
  3102. * @param string $type
  3103. * @param string $parameter
  3104. * @param string|Zend_Locale $locale Locale for parsing input
  3105. * @throws Zend_Date_Exception
  3106. * @return integer|Zend_Date new date
  3107. */
  3108. private function _calcvalue($calc, $value, $type, $parameter, $locale)
  3109. {
  3110. if ($value === null) {
  3111. #require_once 'Zend/Date/Exception.php';
  3112. throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
  3113. }
  3114. if ($locale === null) {
  3115. $locale = $this->getLocale();
  3116. }
  3117. if ($value instanceof Zend_Date) {
  3118. // extract value from object
  3119. $value = $value->toString($parameter, 'iso', $locale);
  3120. } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
  3121. #require_once 'Zend/Date/Exception.php';
  3122. throw new Zend_Date_Exception("invalid $type ($value) operand", 0, null, $value);
  3123. }
  3124. $return = $this->_calcdetail($calc, $value, $parameter, $locale);
  3125. if ($calc != 'cmp') {
  3126. return $this;
  3127. }
  3128. return $return;
  3129. }
  3130. /**
  3131. * Returns only the year from the date object as new object.
  3132. * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
  3133. *
  3134. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3135. * @return Zend_Date
  3136. */
  3137. public function getYear($locale = null)
  3138. {
  3139. if (self::$_options['format_type'] == 'php') {
  3140. $format = 'Y';
  3141. } else {
  3142. $format = self::YEAR;
  3143. }
  3144. return $this->copyPart($format, $locale);
  3145. }
  3146. /**
  3147. * Sets a new year
  3148. * If the year is between 0 and 69, 2000 will be set (2000-2069)
  3149. * If the year if between 70 and 99, 1999 will be set (1970-1999)
  3150. * 3 or 4 digit years are set as expected. If you need to set year 0-99
  3151. * use set() instead.
  3152. * Returned is the new date object
  3153. *
  3154. * @param string|integer|array|Zend_Date $year Year to set
  3155. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3156. * @return Zend_Date Provides a fluent interface
  3157. * @throws Zend_Date_Exception
  3158. */
  3159. public function setYear($year, $locale = null)
  3160. {
  3161. return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
  3162. }
  3163. /**
  3164. * Adds the year to the existing date object
  3165. * If the year is between 0 and 69, 2000 will be added (2000-2069)
  3166. * If the year if between 70 and 99, 1999 will be added (1970-1999)
  3167. * 3 or 4 digit years are added as expected. If you need to add years from 0-99
  3168. * use add() instead.
  3169. * Returned is the new date object
  3170. *
  3171. * @param string|integer|array|Zend_Date $year Year to add
  3172. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3173. * @return Zend_Date Provides a fluent interface
  3174. * @throws Zend_Date_Exception
  3175. */
  3176. public function addYear($year, $locale = null)
  3177. {
  3178. return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
  3179. }
  3180. /**
  3181. * Subs the year from the existing date object
  3182. * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
  3183. * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
  3184. * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
  3185. * use sub() instead.
  3186. * Returned is the new date object
  3187. *
  3188. * @param string|integer|array|Zend_Date $year Year to sub
  3189. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3190. * @return Zend_Date Provides a fluent interface
  3191. * @throws Zend_Date_Exception
  3192. */
  3193. public function subYear($year, $locale = null)
  3194. {
  3195. return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
  3196. }
  3197. /**
  3198. * Compares the year with the existing date object, ignoring other date parts.
  3199. * For example: 10.03.2000 -> 15.02.2000 -> true
  3200. * Returns if equal, earlier or later
  3201. *
  3202. * @param string|integer|array|Zend_Date $year Year to compare
  3203. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3204. * @return integer 0 = equal, 1 = later, -1 = earlier
  3205. * @throws Zend_Date_Exception
  3206. */
  3207. public function compareYear($year, $locale = null)
  3208. {
  3209. return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
  3210. }
  3211. /**
  3212. * Returns only the month from the date object as new object.
  3213. * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
  3214. *
  3215. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3216. * @return Zend_Date
  3217. */
  3218. public function getMonth($locale = null)
  3219. {
  3220. if (self::$_options['format_type'] == 'php') {
  3221. $format = 'm';
  3222. } else {
  3223. $format = self::MONTH;
  3224. }
  3225. return $this->copyPart($format, $locale);
  3226. }
  3227. /**
  3228. * Returns the calculated month
  3229. *
  3230. * @param string $calc Calculation to make
  3231. * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
  3232. * @param string|Zend_Locale $locale Locale for parsing input
  3233. * @return integer|Zend_Date new time
  3234. * @throws Zend_Date_Exception
  3235. */
  3236. private function _month($calc, $month, $locale)
  3237. {
  3238. if ($month === null) {
  3239. #require_once 'Zend/Date/Exception.php';
  3240. throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
  3241. }
  3242. if ($locale === null) {
  3243. $locale = $this->getLocale();
  3244. }
  3245. if ($month instanceof Zend_Date) {
  3246. // extract month from object
  3247. $found = $month->toString(self::MONTH_SHORT, 'iso', $locale);
  3248. } else {
  3249. if (is_numeric($month)) {
  3250. $found = $month;
  3251. } else if (is_array($month)) {
  3252. if (isset($month['month']) === true) {
  3253. $month = $month['month'];
  3254. } else {
  3255. #require_once 'Zend/Date/Exception.php';
  3256. throw new Zend_Date_Exception("no month given in array");
  3257. }
  3258. } else {
  3259. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  3260. $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  3261. $monthlist = array_merge($monthlist, $monthlist2);
  3262. $found = 0;
  3263. $cnt = 0;
  3264. foreach ($monthlist as $key => $value) {
  3265. if (strtoupper($value) == strtoupper($month)) {
  3266. $found = ($key % 12) + 1;
  3267. break;
  3268. }
  3269. ++$cnt;
  3270. }
  3271. if ($found == 0) {
  3272. foreach ($monthlist2 as $key => $value) {
  3273. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
  3274. $found = $key + 1;
  3275. break;
  3276. }
  3277. ++$cnt;
  3278. }
  3279. }
  3280. if ($found == 0) {
  3281. #require_once 'Zend/Date/Exception.php';
  3282. throw new Zend_Date_Exception("unknown month name ($month)", 0, null, $month);
  3283. }
  3284. }
  3285. }
  3286. $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
  3287. if ($calc != 'cmp') {
  3288. return $this;
  3289. }
  3290. return $return;
  3291. }
  3292. /**
  3293. * Sets a new month
  3294. * The month can be a number or a string. Setting months lower then 0 and greater then 12
  3295. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3296. * If a localized monthname is given it will be parsed with the default locale or the optional
  3297. * set locale.
  3298. * Returned is the new date object
  3299. *
  3300. * @param string|integer|array|Zend_Date $month Month to set
  3301. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3302. * @return Zend_Date Provides a fluent interface
  3303. * @throws Zend_Date_Exception
  3304. */
  3305. public function setMonth($month, $locale = null)
  3306. {
  3307. return $this->_month('set', $month, $locale);
  3308. }
  3309. /**
  3310. * Adds months to the existing date object.
  3311. * The month can be a number or a string. Adding months lower then 0 and greater then 12
  3312. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3313. * If a localized monthname is given it will be parsed with the default locale or the optional
  3314. * set locale.
  3315. * Returned is the new date object
  3316. *
  3317. * @param string|integer|array|Zend_Date $month Month to add
  3318. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3319. * @return Zend_Date Provides a fluent interface
  3320. * @throws Zend_Date_Exception
  3321. */
  3322. public function addMonth($month, $locale = null)
  3323. {
  3324. return $this->_month('add', $month, $locale);
  3325. }
  3326. /**
  3327. * Subtracts months from the existing date object.
  3328. * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
  3329. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3330. * If a localized monthname is given it will be parsed with the default locale or the optional
  3331. * set locale.
  3332. * Returned is the new date object
  3333. *
  3334. * @param string|integer|array|Zend_Date $month Month to sub
  3335. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3336. * @return Zend_Date Provides a fluent interface
  3337. * @throws Zend_Date_Exception
  3338. */
  3339. public function subMonth($month, $locale = null)
  3340. {
  3341. return $this->_month('sub', $month, $locale);
  3342. }
  3343. /**
  3344. * Compares the month with the existing date object, ignoring other date parts.
  3345. * For example: 10.03.2000 -> 15.03.1950 -> true
  3346. * Returns if equal, earlier or later
  3347. *
  3348. * @param string|integer|array|Zend_Date $month Month to compare
  3349. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3350. * @return integer 0 = equal, 1 = later, -1 = earlier
  3351. * @throws Zend_Date_Exception
  3352. */
  3353. public function compareMonth($month, $locale = null)
  3354. {
  3355. return $this->_month('cmp', $month, $locale);
  3356. }
  3357. /**
  3358. * Returns the day as new date object
  3359. * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
  3360. *
  3361. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  3362. * @return Zend_Date
  3363. */
  3364. public function getDay($locale = null)
  3365. {
  3366. return $this->copyPart(self::DAY_SHORT, $locale);
  3367. }
  3368. /**
  3369. * Returns the calculated day
  3370. *
  3371. * @param string $calc Type of calculation to make
  3372. * @param Zend_Date $day Day to calculate, when null the actual day is calculated
  3373. * @param Zend_Locale $locale Locale for parsing input
  3374. * @throws Zend_Date_Exception
  3375. * @return Zend_Date|integer
  3376. */
  3377. private function _day($calc, $day, $locale)
  3378. {
  3379. if ($day === null) {
  3380. #require_once 'Zend/Date/Exception.php';
  3381. throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
  3382. }
  3383. if ($locale === null) {
  3384. $locale = $this->getLocale();
  3385. }
  3386. if ($day instanceof Zend_Date) {
  3387. $day = $day->toString(self::DAY_SHORT, 'iso', $locale);
  3388. }
  3389. if (is_numeric($day)) {
  3390. $type = self::DAY_SHORT;
  3391. } else if (is_array($day)) {
  3392. if (isset($day['day']) === true) {
  3393. $day = $day['day'];
  3394. $type = self::WEEKDAY;
  3395. } else {
  3396. #require_once 'Zend/Date/Exception.php';
  3397. throw new Zend_Date_Exception("no day given in array");
  3398. }
  3399. } else {
  3400. switch (iconv_strlen($day, 'UTF-8')) {
  3401. case 1 :
  3402. $type = self::WEEKDAY_NARROW;
  3403. break;
  3404. case 2:
  3405. $type = self::WEEKDAY_NAME;
  3406. break;
  3407. case 3:
  3408. $type = self::WEEKDAY_SHORT;
  3409. break;
  3410. default:
  3411. $type = self::WEEKDAY;
  3412. break;
  3413. }
  3414. }
  3415. $return = $this->_calcdetail($calc, $day, $type, $locale);
  3416. if ($calc != 'cmp') {
  3417. return $this;
  3418. }
  3419. return $return;
  3420. }
  3421. /**
  3422. * Sets a new day
  3423. * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
  3424. * will result in adding or subtracting the relevant month.
  3425. * If a localized dayname is given it will be parsed with the default locale or the optional
  3426. * set locale.
  3427. * Returned is the new date object
  3428. * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
  3429. *
  3430. * @param string|integer|array|Zend_Date $day Day to set
  3431. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3432. * @return Zend_Date Provides a fluent interface
  3433. * @throws Zend_Date_Exception
  3434. */
  3435. public function setDay($day, $locale = null)
  3436. {
  3437. return $this->_day('set', $day, $locale);
  3438. }
  3439. /**
  3440. * Adds days to the existing date object.
  3441. * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
  3442. * will result in adding or subtracting the relevant month.
  3443. * If a localized dayname is given it will be parsed with the default locale or the optional
  3444. * set locale.
  3445. *
  3446. * @param string|integer|array|Zend_Date $day Day to add
  3447. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3448. * @return Zend_Date Provides a fluent interface
  3449. * @throws Zend_Date_Exception
  3450. */
  3451. public function addDay($day, $locale = null)
  3452. {
  3453. return $this->_day('add', $day, $locale);
  3454. }
  3455. /**
  3456. * Subtracts days from the existing date object.
  3457. * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
  3458. * will result in adding or subtracting the relevant month.
  3459. * If a localized dayname is given it will be parsed with the default locale or the optional
  3460. * set locale.
  3461. *
  3462. * @param string|integer|array|Zend_Date $day Day to sub
  3463. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3464. * @return Zend_Date Provides a fluent interface
  3465. * @throws Zend_Date_Exception
  3466. */
  3467. public function subDay($day, $locale = null)
  3468. {
  3469. return $this->_day('sub', $day, $locale);
  3470. }
  3471. /**
  3472. * Compares the day with the existing date object, ignoring other date parts.
  3473. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3474. * Returns if equal, earlier or later
  3475. *
  3476. * @param string|integer|array|Zend_Date $day Day to compare
  3477. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3478. * @return integer 0 = equal, 1 = later, -1 = earlier
  3479. * @throws Zend_Date_Exception
  3480. */
  3481. public function compareDay($day, $locale = null)
  3482. {
  3483. return $this->_day('cmp', $day, $locale);
  3484. }
  3485. /**
  3486. * Returns the weekday as new date object
  3487. * Weekday is always from 1-7
  3488. * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
  3489. *
  3490. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  3491. * @return Zend_Date
  3492. */
  3493. public function getWeekday($locale = null)
  3494. {
  3495. if (self::$_options['format_type'] == 'php') {
  3496. $format = 'l';
  3497. } else {
  3498. $format = self::WEEKDAY;
  3499. }
  3500. return $this->copyPart($format, $locale);
  3501. }
  3502. /**
  3503. * Returns the calculated weekday
  3504. *
  3505. * @param string $calc Type of calculation to make
  3506. * @param Zend_Date $weekday Weekday to calculate, when null the actual weekday is calculated
  3507. * @param Zend_Locale $locale Locale for parsing input
  3508. * @return Zend_Date|integer
  3509. * @throws Zend_Date_Exception
  3510. */
  3511. private function _weekday($calc, $weekday, $locale)
  3512. {
  3513. if ($weekday === null) {
  3514. #require_once 'Zend/Date/Exception.php';
  3515. throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
  3516. }
  3517. if ($locale === null) {
  3518. $locale = $this->getLocale();
  3519. }
  3520. if ($weekday instanceof Zend_Date) {
  3521. $weekday = $weekday->toString(self::WEEKDAY_8601, 'iso', $locale);
  3522. }
  3523. if (is_numeric($weekday)) {
  3524. $type = self::WEEKDAY_8601;
  3525. } else if (is_array($weekday)) {
  3526. if (isset($weekday['weekday']) === true) {
  3527. $weekday = $weekday['weekday'];
  3528. $type = self::WEEKDAY;
  3529. } else {
  3530. #require_once 'Zend/Date/Exception.php';
  3531. throw new Zend_Date_Exception("no weekday given in array");
  3532. }
  3533. } else {
  3534. switch(iconv_strlen($weekday, 'UTF-8')) {
  3535. case 1:
  3536. $type = self::WEEKDAY_NARROW;
  3537. break;
  3538. case 2:
  3539. $type = self::WEEKDAY_NAME;
  3540. break;
  3541. case 3:
  3542. $type = self::WEEKDAY_SHORT;
  3543. break;
  3544. default:
  3545. $type = self::WEEKDAY;
  3546. break;
  3547. }
  3548. }
  3549. $return = $this->_calcdetail($calc, $weekday, $type, $locale);
  3550. if ($calc != 'cmp') {
  3551. return $this;
  3552. }
  3553. return $return;
  3554. }
  3555. /**
  3556. * Sets a new weekday
  3557. * The weekday can be a number or a string. If a localized weekday name is given,
  3558. * then it will be parsed as a date in $locale (defaults to the same locale as $this).
  3559. * Returned is the new date object.
  3560. * Example: setWeekday(3); will set the wednesday of this week as day.
  3561. *
  3562. * @param string|integer|array|Zend_Date $weekday Weekday to set
  3563. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3564. * @return Zend_Date Provides a fluent interface
  3565. * @throws Zend_Date_Exception
  3566. */
  3567. public function setWeekday($weekday, $locale = null)
  3568. {
  3569. return $this->_weekday('set', $weekday, $locale);
  3570. }
  3571. /**
  3572. * Adds weekdays to the existing date object.
  3573. * The weekday can be a number or a string.
  3574. * If a localized dayname is given it will be parsed with the default locale or the optional
  3575. * set locale.
  3576. * Returned is the new date object
  3577. * Example: addWeekday(3); will add the difference of days from the begining of the month until
  3578. * wednesday.
  3579. *
  3580. * @param string|integer|array|Zend_Date $weekday Weekday to add
  3581. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3582. * @return Zend_Date Provides a fluent interface
  3583. * @throws Zend_Date_Exception
  3584. */
  3585. public function addWeekday($weekday, $locale = null)
  3586. {
  3587. return $this->_weekday('add', $weekday, $locale);
  3588. }
  3589. /**
  3590. * Subtracts weekdays from the existing date object.
  3591. * The weekday can be a number or a string.
  3592. * If a localized dayname is given it will be parsed with the default locale or the optional
  3593. * set locale.
  3594. * Returned is the new date object
  3595. * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
  3596. * wednesday.
  3597. *
  3598. * @param string|integer|array|Zend_Date $weekday Weekday to sub
  3599. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3600. * @return Zend_Date Provides a fluent interface
  3601. * @throws Zend_Date_Exception
  3602. */
  3603. public function subWeekday($weekday, $locale = null)
  3604. {
  3605. return $this->_weekday('sub', $weekday, $locale);
  3606. }
  3607. /**
  3608. * Compares the weekday with the existing date object, ignoring other date parts.
  3609. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3610. * Returns if equal, earlier or later
  3611. *
  3612. * @param string|integer|array|Zend_Date $weekday Weekday to compare
  3613. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3614. * @return integer 0 = equal, 1 = later, -1 = earlier
  3615. * @throws Zend_Date_Exception
  3616. */
  3617. public function compareWeekday($weekday, $locale = null)
  3618. {
  3619. return $this->_weekday('cmp', $weekday, $locale);
  3620. }
  3621. /**
  3622. * Returns the day of year as new date object
  3623. * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
  3624. *
  3625. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3626. * @return Zend_Date
  3627. */
  3628. public function getDayOfYear($locale = null)
  3629. {
  3630. if (self::$_options['format_type'] == 'php') {
  3631. $format = 'D';
  3632. } else {
  3633. $format = self::DAY_OF_YEAR;
  3634. }
  3635. return $this->copyPart($format, $locale);
  3636. }
  3637. /**
  3638. * Sets a new day of year
  3639. * The day of year is always a number.
  3640. * Returned is the new date object
  3641. * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
  3642. *
  3643. * @param string|integer|array|Zend_Date $day Day of Year to set
  3644. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3645. * @return Zend_Date Provides a fluent interface
  3646. * @throws Zend_Date_Exception
  3647. */
  3648. public function setDayOfYear($day, $locale = null)
  3649. {
  3650. return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3651. }
  3652. /**
  3653. * Adds a day of year to the existing date object.
  3654. * The day of year is always a number.
  3655. * Returned is the new date object
  3656. * Example: addDayOfYear(10); will add 10 days to the existing date object.
  3657. *
  3658. * @param string|integer|array|Zend_Date $day Day of Year to add
  3659. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3660. * @return Zend_Date Provides a fluent interface
  3661. * @throws Zend_Date_Exception
  3662. */
  3663. public function addDayOfYear($day, $locale = null)
  3664. {
  3665. return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3666. }
  3667. /**
  3668. * Subtracts a day of year from the existing date object.
  3669. * The day of year is always a number.
  3670. * Returned is the new date object
  3671. * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
  3672. *
  3673. * @param string|integer|array|Zend_Date $day Day of Year to sub
  3674. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3675. * @return Zend_Date Provides a fluent interface
  3676. * @throws Zend_Date_Exception
  3677. */
  3678. public function subDayOfYear($day, $locale = null)
  3679. {
  3680. return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3681. }
  3682. /**
  3683. * Compares the day of year with the existing date object.
  3684. * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
  3685. * Returns if equal, earlier or later
  3686. *
  3687. * @param string|integer|array|Zend_Date $day Day of Year to compare
  3688. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3689. * @return integer 0 = equal, 1 = later, -1 = earlier
  3690. * @throws Zend_Date_Exception
  3691. */
  3692. public function compareDayOfYear($day, $locale = null)
  3693. {
  3694. return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3695. }
  3696. /**
  3697. * Returns the hour as new date object
  3698. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
  3699. *
  3700. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  3701. * @return Zend_Date
  3702. */
  3703. public function getHour($locale = null)
  3704. {
  3705. return $this->copyPart(self::HOUR, $locale);
  3706. }
  3707. /**
  3708. * Sets a new hour
  3709. * The hour is always a number.
  3710. * Returned is the new date object
  3711. * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
  3712. *
  3713. * @param string|integer|array|Zend_Date $hour Hour to set
  3714. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3715. * @return Zend_Date Provides a fluent interface
  3716. * @throws Zend_Date_Exception
  3717. */
  3718. public function setHour($hour, $locale = null)
  3719. {
  3720. return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
  3721. }
  3722. /**
  3723. * Adds hours to the existing date object.
  3724. * The hour is always a number.
  3725. * Returned is the new date object
  3726. * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
  3727. *
  3728. * @param string|integer|array|Zend_Date $hour Hour to add
  3729. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3730. * @return Zend_Date Provides a fluent interface
  3731. * @throws Zend_Date_Exception
  3732. */
  3733. public function addHour($hour, $locale = null)
  3734. {
  3735. return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
  3736. }
  3737. /**
  3738. * Subtracts hours from the existing date object.
  3739. * The hour is always a number.
  3740. * Returned is the new date object
  3741. * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
  3742. *
  3743. * @param string|integer|array|Zend_Date $hour Hour to sub
  3744. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3745. * @return Zend_Date Provides a fluent interface
  3746. * @throws Zend_Date_Exception
  3747. */
  3748. public function subHour($hour, $locale = null)
  3749. {
  3750. return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
  3751. }
  3752. /**
  3753. * Compares the hour with the existing date object.
  3754. * For example: 10:30:25 -> compareHour(10) -> 0
  3755. * Returns if equal, earlier or later
  3756. *
  3757. * @param string|integer|array|Zend_Date $hour Hour to compare
  3758. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3759. * @return integer 0 = equal, 1 = later, -1 = earlier
  3760. * @throws Zend_Date_Exception
  3761. */
  3762. public function compareHour($hour, $locale = null)
  3763. {
  3764. return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
  3765. }
  3766. /**
  3767. * Returns the minute as new date object
  3768. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
  3769. *
  3770. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3771. * @return Zend_Date
  3772. */
  3773. public function getMinute($locale = null)
  3774. {
  3775. if (self::$_options['format_type'] == 'php') {
  3776. $format = 'i';
  3777. } else {
  3778. $format = self::MINUTE;
  3779. }
  3780. return $this->copyPart($format, $locale);
  3781. }
  3782. /**
  3783. * Sets a new minute
  3784. * The minute is always a number.
  3785. * Returned is the new date object
  3786. * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
  3787. *
  3788. * @param string|integer|array|Zend_Date $minute Minute to set
  3789. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3790. * @return Zend_Date Provides a fluent interface
  3791. * @throws Zend_Date_Exception
  3792. */
  3793. public function setMinute($minute, $locale = null)
  3794. {
  3795. return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3796. }
  3797. /**
  3798. * Adds minutes to the existing date object.
  3799. * The minute is always a number.
  3800. * Returned is the new date object
  3801. * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
  3802. *
  3803. * @param string|integer|array|Zend_Date $minute Minute to add
  3804. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3805. * @return Zend_Date Provides a fluent interface
  3806. * @throws Zend_Date_Exception
  3807. */
  3808. public function addMinute($minute, $locale = null)
  3809. {
  3810. return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3811. }
  3812. /**
  3813. * Subtracts minutes from the existing date object.
  3814. * The minute is always a number.
  3815. * Returned is the new date object
  3816. * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
  3817. *
  3818. * @param string|integer|array|Zend_Date $minute Minute to sub
  3819. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3820. * @return Zend_Date Provides a fluent interface
  3821. * @throws Zend_Date_Exception
  3822. */
  3823. public function subMinute($minute, $locale = null)
  3824. {
  3825. return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3826. }
  3827. /**
  3828. * Compares the minute with the existing date object.
  3829. * For example: 10:30:25 -> compareMinute(30) -> 0
  3830. * Returns if equal, earlier or later
  3831. *
  3832. * @param string|integer|array|Zend_Date $minute Hour to compare
  3833. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3834. * @return integer 0 = equal, 1 = later, -1 = earlier
  3835. * @throws Zend_Date_Exception
  3836. */
  3837. public function compareMinute($minute, $locale = null)
  3838. {
  3839. return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3840. }
  3841. /**
  3842. * Returns the second as new date object
  3843. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
  3844. *
  3845. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3846. * @return Zend_Date
  3847. */
  3848. public function getSecond($locale = null)
  3849. {
  3850. if (self::$_options['format_type'] == 'php') {
  3851. $format = 's';
  3852. } else {
  3853. $format = self::SECOND;
  3854. }
  3855. return $this->copyPart($format, $locale);
  3856. }
  3857. /**
  3858. * Sets new seconds to the existing date object.
  3859. * The second is always a number.
  3860. * Returned is the new date object
  3861. * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
  3862. *
  3863. * @param string|integer|array|Zend_Date $second Second to set
  3864. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3865. * @return Zend_Date Provides a fluent interface
  3866. * @throws Zend_Date_Exception
  3867. */
  3868. public function setSecond($second, $locale = null)
  3869. {
  3870. return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
  3871. }
  3872. /**
  3873. * Adds seconds to the existing date object.
  3874. * The second is always a number.
  3875. * Returned is the new date object
  3876. * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
  3877. *
  3878. * @param string|integer|array|Zend_Date $second Second to add
  3879. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3880. * @return Zend_Date Provides a fluent interface
  3881. * @throws Zend_Date_Exception
  3882. */
  3883. public function addSecond($second, $locale = null)
  3884. {
  3885. return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
  3886. }
  3887. /**
  3888. * Subtracts seconds from the existing date object.
  3889. * The second is always a number.
  3890. * Returned is the new date object
  3891. * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
  3892. *
  3893. * @param string|integer|array|Zend_Date $second Second to sub
  3894. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3895. * @return Zend_Date Provides a fluent interface
  3896. * @throws Zend_Date_Exception
  3897. */
  3898. public function subSecond($second, $locale = null)
  3899. {
  3900. return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
  3901. }
  3902. /**
  3903. * Compares the second with the existing date object.
  3904. * For example: 10:30:25 -> compareSecond(25) -> 0
  3905. * Returns if equal, earlier or later
  3906. *
  3907. * @param string|integer|array|Zend_Date $second Second to compare
  3908. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3909. * @return integer 0 = equal, 1 = later, -1 = earlier
  3910. * @throws Zend_Date_Exception
  3911. */
  3912. public function compareSecond($second, $locale = null)
  3913. {
  3914. return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
  3915. }
  3916. /**
  3917. * Returns the precision for fractional seconds
  3918. *
  3919. * @return integer
  3920. */
  3921. public function getFractionalPrecision()
  3922. {
  3923. return $this->_precision;
  3924. }
  3925. /**
  3926. * Sets a new precision for fractional seconds
  3927. *
  3928. * @param integer $precision Precision for the fractional datepart 3 = milliseconds
  3929. * @throws Zend_Date_Exception
  3930. * @return Zend_Date Provides a fluent interface
  3931. */
  3932. public function setFractionalPrecision($precision)
  3933. {
  3934. if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
  3935. #require_once 'Zend/Date/Exception.php';
  3936. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3937. }
  3938. $this->_precision = (int) $precision;
  3939. if ($this->_precision < strlen($this->_fractional)) {
  3940. $this->_fractional = substr($this->_fractional, 0, $this->_precision);
  3941. } else {
  3942. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_RIGHT);
  3943. }
  3944. return $this;
  3945. }
  3946. /**
  3947. * Returns the milliseconds of the date object
  3948. *
  3949. * @return string
  3950. */
  3951. public function getMilliSecond()
  3952. {
  3953. return $this->_fractional;
  3954. }
  3955. /**
  3956. * Sets new milliseconds for the date object
  3957. * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
  3958. *
  3959. * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
  3960. * @param integer $precision (Optional) Fraction precision of the given milliseconds
  3961. * @throws Zend_Date_Exception
  3962. * @return Zend_Date Provides a fluent interface
  3963. */
  3964. public function setMilliSecond($milli = null, $precision = null)
  3965. {
  3966. if ($milli === null) {
  3967. list($milli, $time) = explode(" ", microtime());
  3968. $milli = intval($milli);
  3969. $precision = 6;
  3970. } else if (!is_numeric($milli)) {
  3971. #require_once 'Zend/Date/Exception.php';
  3972. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  3973. }
  3974. if ($precision === null) {
  3975. $precision = $this->_precision;
  3976. }
  3977. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3978. #require_once 'Zend/Date/Exception.php';
  3979. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3980. }
  3981. $this->_fractional = 0;
  3982. $this->addMilliSecond($milli, $precision);
  3983. return $this;
  3984. }
  3985. /**
  3986. * Adds milliseconds to the date object
  3987. *
  3988. * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
  3989. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3990. * @throws Zend_Date_Exception
  3991. * @return Zend_Date Provides a fluent interface
  3992. */
  3993. public function addMilliSecond($milli = null, $precision = null)
  3994. {
  3995. if ($milli === null) {
  3996. list($milli, $time) = explode(" ", microtime());
  3997. $milli = intval($milli);
  3998. } else if (!is_numeric($milli)) {
  3999. #require_once 'Zend/Date/Exception.php';
  4000. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  4001. }
  4002. if ($precision === null) {
  4003. // Use internal default precision
  4004. // Is not as logic as using the length of the input. But this would break tests and maybe other things
  4005. // as an input value of integer 10, which is used in tests, must be parsed as 10 milliseconds (real milliseconds, precision 3)
  4006. // but with auto-detect of precision, 100 milliseconds would be added.
  4007. $precision = $this->_precision;
  4008. }
  4009. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  4010. #require_once 'Zend/Date/Exception.php';
  4011. throw new Zend_Date_Exception(
  4012. "precision ($precision) must be a positive integer less than 10", 0, null, $precision
  4013. );
  4014. }
  4015. if ($this->_precision > $precision) {
  4016. $milli = $milli * pow(10, $this->_precision - $precision);
  4017. } elseif ($this->_precision < $precision) {
  4018. $milli = round($milli / pow(10, $precision - $this->_precision));
  4019. }
  4020. $this->_fractional += $milli;
  4021. // Add/sub milliseconds + add/sub seconds
  4022. $max = pow(10, $this->_precision);
  4023. // Milli includes seconds
  4024. if ($this->_fractional >= $max) {
  4025. while ($this->_fractional >= $max) {
  4026. $this->addSecond(1);
  4027. $this->_fractional -= $max;
  4028. }
  4029. }
  4030. if ($this->_fractional < 0) {
  4031. while ($this->_fractional < 0) {
  4032. $this->subSecond(1);
  4033. $this->_fractional += $max;
  4034. }
  4035. }
  4036. if ($this->_precision > strlen($this->_fractional)) {
  4037. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_LEFT);
  4038. }
  4039. return $this;
  4040. }
  4041. /**
  4042. * Subtracts a millisecond
  4043. *
  4044. * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
  4045. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  4046. * @return Zend_Date Provides a fluent interface
  4047. */
  4048. public function subMilliSecond($milli = null, $precision = null)
  4049. {
  4050. $this->addMilliSecond(0 - $milli, $precision);
  4051. return $this;
  4052. }
  4053. /**
  4054. * Compares only the millisecond part, returning the difference
  4055. *
  4056. * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
  4057. * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
  4058. * @throws Zend_Date_Exception On invalid input
  4059. * @return integer 0 = equal, 1 = later, -1 = earlier
  4060. */
  4061. public function compareMilliSecond($milli = null, $precision = null)
  4062. {
  4063. if ($milli === null) {
  4064. list($milli, $time) = explode(" ", microtime());
  4065. $milli = intval($milli);
  4066. } else if (is_numeric($milli) === false) {
  4067. #require_once 'Zend/Date/Exception.php';
  4068. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  4069. }
  4070. if ($precision === null) {
  4071. $precision = strlen($milli);
  4072. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  4073. #require_once 'Zend/Date/Exception.php';
  4074. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  4075. }
  4076. if ($precision === 0) {
  4077. #require_once 'Zend/Date/Exception.php';
  4078. throw new Zend_Date_Exception('precision is 0');
  4079. }
  4080. if ($precision != $this->_precision) {
  4081. if ($precision > $this->_precision) {
  4082. $diff = $precision - $this->_precision;
  4083. $milli = (int) ($milli / (10 * $diff));
  4084. } else {
  4085. $diff = $this->_precision - $precision;
  4086. $milli = (int) ($milli * (10 * $diff));
  4087. }
  4088. }
  4089. $comp = $this->_fractional - $milli;
  4090. if ($comp < 0) {
  4091. return -1;
  4092. } else if ($comp > 0) {
  4093. return 1;
  4094. }
  4095. return 0;
  4096. }
  4097. /**
  4098. * Returns the week as new date object using monday as begining of the week
  4099. * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
  4100. *
  4101. * @param Zend_Locale $locale OPTIONAL Locale for parsing input
  4102. * @return Zend_Date
  4103. */
  4104. public function getWeek($locale = null)
  4105. {
  4106. if (self::$_options['format_type'] == 'php') {
  4107. $format = 'W';
  4108. } else {
  4109. $format = self::WEEK;
  4110. }
  4111. return $this->copyPart($format, $locale);
  4112. }
  4113. /**
  4114. * Sets a new week. The week is always a number. The day of week is not changed.
  4115. * Returned is the new date object
  4116. * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
  4117. *
  4118. * @param string|integer|array|Zend_Date $week Week to set
  4119. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4120. * @return Zend_Date Provides a fluent interface
  4121. * @throws Zend_Date_Exception
  4122. */
  4123. public function setWeek($week, $locale = null)
  4124. {
  4125. return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
  4126. }
  4127. /**
  4128. * Adds a week. The week is always a number. The day of week is not changed.
  4129. * Returned is the new date object
  4130. * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
  4131. *
  4132. * @param string|integer|array|Zend_Date $week Week to add
  4133. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4134. * @return Zend_Date Provides a fluent interface
  4135. * @throws Zend_Date_Exception
  4136. */
  4137. public function addWeek($week, $locale = null)
  4138. {
  4139. return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
  4140. }
  4141. /**
  4142. * Subtracts a week. The week is always a number. The day of week is not changed.
  4143. * Returned is the new date object
  4144. * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
  4145. *
  4146. * @param string|integer|array|Zend_Date $week Week to sub
  4147. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4148. * @return Zend_Date Provides a fluent interface
  4149. * @throws Zend_Date_Exception
  4150. */
  4151. public function subWeek($week, $locale = null)
  4152. {
  4153. return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
  4154. }
  4155. /**
  4156. * Compares only the week part, returning the difference
  4157. * Returned is the new date object
  4158. * Returns if equal, earlier or later
  4159. * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
  4160. *
  4161. * @param string|integer|array|Zend_Date $week Week to compare
  4162. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4163. * @return integer 0 = equal, 1 = later, -1 = earlier
  4164. */
  4165. public function compareWeek($week, $locale = null)
  4166. {
  4167. return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
  4168. }
  4169. /**
  4170. * Sets a new standard locale for the date object.
  4171. * This locale will be used for all functions
  4172. * Returned is the really set locale.
  4173. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  4174. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  4175. *
  4176. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  4177. * @throws Zend_Date_Exception When the given locale does not exist
  4178. * @return Zend_Date Provides fluent interface
  4179. */
  4180. public function setLocale($locale = null)
  4181. {
  4182. try {
  4183. $this->_locale = Zend_Locale::findLocale($locale);
  4184. } catch (Zend_Locale_Exception $e) {
  4185. #require_once 'Zend/Date/Exception.php';
  4186. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  4187. }
  4188. return $this;
  4189. }
  4190. /**
  4191. * Returns the actual set locale
  4192. *
  4193. * @return string
  4194. */
  4195. public function getLocale()
  4196. {
  4197. return $this->_locale;
  4198. }
  4199. /**
  4200. * Checks if the given date is a real date or datepart.
  4201. * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  4202. * But the check will only be done for the expected dateparts which are given by format.
  4203. * If no format is given the standard dateformat for the actual locale is used.
  4204. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  4205. *
  4206. * @param string|array|Zend_Date $date Date to parse for correctness
  4207. * @param string $format (Optional) Format for parsing the date string
  4208. * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
  4209. * @return boolean True when all date parts are correct
  4210. */
  4211. public static function isDate($date, $format = null, $locale = null)
  4212. {
  4213. if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
  4214. !is_array($date)) {
  4215. return false;
  4216. }
  4217. if (($format !== null) && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
  4218. && (Zend_Locale::isLocale($format, null, false))) {
  4219. $locale = $format;
  4220. $format = null;
  4221. }
  4222. $locale = Zend_Locale::findLocale($locale);
  4223. if ($format === null) {
  4224. $format = Zend_Locale_Format::getDateFormat($locale);
  4225. } else if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  4226. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  4227. }
  4228. $format = self::_getLocalizedToken($format, $locale);
  4229. if (!is_array($date)) {
  4230. try {
  4231. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
  4232. 'date_format' => $format, 'format_type' => 'iso',
  4233. 'fix_date' => false));
  4234. } catch (Zend_Locale_Exception $e) {
  4235. // Date can not be parsed
  4236. return false;
  4237. }
  4238. } else {
  4239. $parsed = $date;
  4240. }
  4241. if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
  4242. (!isset($parsed['year']))) {
  4243. // Year expected but not found
  4244. return false;
  4245. }
  4246. if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
  4247. // Month expected but not found
  4248. return false;
  4249. }
  4250. if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
  4251. // Day expected but not found
  4252. return false;
  4253. }
  4254. if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
  4255. (!isset($parsed['hour']))) {
  4256. // Hour expected but not found
  4257. return false;
  4258. }
  4259. if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
  4260. // Minute expected but not found
  4261. return false;
  4262. }
  4263. if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
  4264. // Second expected but not found
  4265. return false;
  4266. }
  4267. // Set not given dateparts
  4268. if (isset($parsed['hour']) === false) {
  4269. $parsed['hour'] = 12;
  4270. }
  4271. if (isset($parsed['minute']) === false) {
  4272. $parsed['minute'] = 0;
  4273. }
  4274. if (isset($parsed['second']) === false) {
  4275. $parsed['second'] = 0;
  4276. }
  4277. if (isset($parsed['month']) === false) {
  4278. $parsed['month'] = 1;
  4279. }
  4280. if (isset($parsed['day']) === false) {
  4281. $parsed['day'] = 1;
  4282. }
  4283. if (isset($parsed['year']) === false) {
  4284. $parsed['year'] = 1970;
  4285. }
  4286. if (self::isYearLeapYear($parsed['year'])) {
  4287. $parsed['year'] = 1972;
  4288. } else {
  4289. $parsed['year'] = 1971;
  4290. }
  4291. $date = new self($parsed, null, $locale);
  4292. $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
  4293. $parsed['month'], $parsed['day'], $parsed['year']);
  4294. if ($parsed['year'] != $date->date('Y', $timestamp)) {
  4295. // Given year differs from parsed year
  4296. return false;
  4297. }
  4298. if ($parsed['month'] != $date->date('n', $timestamp)) {
  4299. // Given month differs from parsed month
  4300. return false;
  4301. }
  4302. if ($parsed['day'] != $date->date('j', $timestamp)) {
  4303. // Given day differs from parsed day
  4304. return false;
  4305. }
  4306. if ($parsed['hour'] != $date->date('G', $timestamp)) {
  4307. // Given hour differs from parsed hour
  4308. return false;
  4309. }
  4310. if ($parsed['minute'] != $date->date('i', $timestamp)) {
  4311. // Given minute differs from parsed minute
  4312. return false;
  4313. }
  4314. if ($parsed['second'] != $date->date('s', $timestamp)) {
  4315. // Given second differs from parsed second
  4316. return false;
  4317. }
  4318. return true;
  4319. }
  4320. /**
  4321. * Returns the ISO Token for all localized constants
  4322. *
  4323. * @param string $token Token to normalize
  4324. * @param string $locale Locale to search
  4325. * @return string
  4326. */
  4327. protected static function _getLocalizedToken($token, $locale)
  4328. {
  4329. switch($token) {
  4330. case self::ISO_8601 :
  4331. return "yyyy-MM-ddThh:mm:ss";
  4332. break;
  4333. case self::RFC_2822 :
  4334. return "EEE, dd MMM yyyy HH:mm:ss";
  4335. break;
  4336. case self::DATES :
  4337. return Zend_Locale_Data::getContent($locale, 'date');
  4338. break;
  4339. case self::DATE_FULL :
  4340. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  4341. break;
  4342. case self::DATE_LONG :
  4343. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  4344. break;
  4345. case self::DATE_MEDIUM :
  4346. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  4347. break;
  4348. case self::DATE_SHORT :
  4349. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  4350. break;
  4351. case self::TIMES :
  4352. return Zend_Locale_Data::getContent($locale, 'time');
  4353. break;
  4354. case self::TIME_FULL :
  4355. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  4356. break;
  4357. case self::TIME_LONG :
  4358. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  4359. break;
  4360. case self::TIME_MEDIUM :
  4361. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  4362. break;
  4363. case self::TIME_SHORT :
  4364. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  4365. break;
  4366. case self::DATETIME :
  4367. return Zend_Locale_Data::getContent($locale, 'datetime');
  4368. break;
  4369. case self::DATETIME_FULL :
  4370. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
  4371. break;
  4372. case self::DATETIME_LONG :
  4373. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
  4374. break;
  4375. case self::DATETIME_MEDIUM :
  4376. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
  4377. break;
  4378. case self::DATETIME_SHORT :
  4379. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
  4380. break;
  4381. case self::ATOM :
  4382. case self::RFC_3339 :
  4383. case self::W3C :
  4384. return "yyyy-MM-DD HH:mm:ss";
  4385. break;
  4386. case self::COOKIE :
  4387. case self::RFC_850 :
  4388. return "EEEE, dd-MM-yyyy HH:mm:ss";
  4389. break;
  4390. case self::RFC_822 :
  4391. case self::RFC_1036 :
  4392. case self::RFC_1123 :
  4393. case self::RSS :
  4394. return "EEE, dd MM yyyy HH:mm:ss";
  4395. break;
  4396. }
  4397. return $token;
  4398. }
  4399. /**
  4400. * Get unix timestamp.
  4401. * Added limitation: $year value must be between -10 000 and 10 000
  4402. * Parent method implementation causes 504 error if it gets too big(small) year value
  4403. *
  4404. * @see Zend_Date_DateObject::mktime
  4405. * @throws Zend_Date_Exception
  4406. * @param $hour
  4407. * @param $minute
  4408. * @param $second
  4409. * @param $month
  4410. * @param $day
  4411. * @param $year
  4412. * @param bool $gmt
  4413. * @return float|int
  4414. */
  4415. protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
  4416. {
  4417. $day = intval($day);
  4418. $month = intval($month);
  4419. $year = intval($year);
  4420. // correct months > 12 and months < 1
  4421. if ($month > 12) {
  4422. $overlap = floor($month / 12);
  4423. $year += $overlap;
  4424. $month -= $overlap * 12;
  4425. } else {
  4426. $overlap = ceil((1 - $month) / 12);
  4427. $year -= $overlap;
  4428. $month += $overlap * 12;
  4429. }
  4430. if ($year > self::YEAR_MAX_VALUE || $year < self::YEAR_MIN_VALUE) {
  4431. throw new Zend_Date_Exception('Invalid year, it must be between ' . self::YEAR_MIN_VALUE . ' and '
  4432. . self::YEAR_MAX_VALUE);
  4433. }
  4434. return parent::mktime($hour, $minute, $second, $month, $day, $year, $gmt);
  4435. }
  4436. }