This file is indexed.

/usr/share/doc/php-doctrine-orm/html/cookbook/working-with-datetime.html is in doctrine-orm-doc 2.4.6-1+deb8u1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Working with DateTime Instances &mdash; Doctrine 2 ORM 2 documentation</title>
    <link rel="stylesheet" href="../_static/bootstrap/css/bootstrap.min.css" type="text/css" />
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/layout.css" type="text/css" />
    <link rel="stylesheet" href="../_static/configurationblock.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script src="../_static/bootstrap/js/bootstrap.min.js"></script>

    <script type="text/javascript">
    <!--
        $(document).ready(function() {
            $("#versions").change(function() {
                var docsUrl = $(this).val();
                window.location.href = docsUrl;
            });
        });
    -->
    </script>
    <link rel="shortcut icon" href="../_static/doctrine.ico"/>
    <link rel="top" title="Doctrine 2 ORM 2 documentation" href="../index.html" />
    <link rel="next" title="Mysql Enums" href="mysql-enums.html" />
    <link rel="prev" title="Validation of Entities" href="validation-of-entities.html" /> 
  </head>
  <body>
    <div id="wrapper">
      <div id="header">
        <h1 id="h1title"></h1>
        <div id="logo">
          <a href="http://www.doctrine-project.org/">Doctrine - PHP Database Libraries</a>
        </div>
      </div>
      <div id="nav" class="cls">
        <div class="tl cls">
          <ul>
            <li><a target="_top" href="/">home</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/about">about</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects.html">projects</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects/orm">orm</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects/dbal">dbal</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/blog">blog</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/jira">development</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/contribute">contribute</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/community">community</a></li>
          </ul>
        </div>
      </div>
      <div id="content" class="cls">
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mysql-enums.html" title="Mysql Enums"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="validation-of-entities.html" title="Validation of Entities"
             accesskey="P">previous</a> |</li>
        <li><a href="/">Doctrine Homepage</a> &raquo;</li>
        <li><a href="../index.html">Doctrine 2 ORM 2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

        <div class="document">
            <div class="documentwrapper">
                <div class="bodywrapper">

              <div class="body" >
                
  <div class="section" id="working-with-datetime-instances">
<h1>Working with DateTime Instances<a class="headerlink" href="#working-with-datetime-instances" title="Permalink to this headline"></a></h1>
<p>There are many nitty gritty details when working with PHPs DateTime instances. You have know their inner
workings pretty well not to make mistakes with date handling. This cookbook entry holds several
interesting pieces of information on how to work with PHP DateTime instances in Doctrine 2.</p>
<div class="section" id="datetime-changes-are-detected-by-reference">
<h2>DateTime changes are detected by Reference<a class="headerlink" href="#datetime-changes-are-detected-by-reference" title="Permalink to this headline"></a></h2>
<p>When calling <tt class="docutils literal"><span class="pre">EntityManager#flush()</span></tt> Doctrine computes the changesets of all the currently managed entities
and saves the differences to the database. In case of object properties (&#64;Column(type=&#8221;datetime&#8221;) or &#64;Column(type=&#8221;object&#8221;))
these comparisons are always made <strong>BY REFERENCE</strong>. That means the following change will <strong>NOT</strong> be saved into the database:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/** @Entity */</span>
<span class="k">class</span> <span class="nc">Article</span>
<span class="p">{</span>
    <span class="sd">/** @Column(type=&quot;datetime&quot;) */</span>
    <span class="k">private</span> <span class="nv">$updated</span><span class="p">;</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">setUpdated</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// will NOT be saved in the database</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">updated</span><span class="o">-&gt;</span><span class="na">modify</span><span class="p">(</span><span class="s2">&quot;now&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The way to go would be:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">class</span> <span class="nc">Article</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">setUpdated</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// WILL be saved in the database</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">updated</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\DateTime</span><span class="p">(</span><span class="s2">&quot;now&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="default-timezone-gotcha">
<h2>Default Timezone Gotcha<a class="headerlink" href="#default-timezone-gotcha" title="Permalink to this headline"></a></h2>
<p>By default Doctrine assumes that you are working with a default timezone. Each DateTime instance that
is created by Doctrine will be assigned the timezone that is currently the default, either through
the <tt class="docutils literal"><span class="pre">date.timezone</span></tt> ini setting or by calling <tt class="docutils literal"><span class="pre">date_default_timezone_set()</span></tt>.</p>
<p>This is very important to handle correctly if your application runs on different serves or is moved from one to another server
(with different timezone settings). You have to make sure that the timezone is the correct one
on all this systems.</p>
</div>
<div class="section" id="handling-different-timezones-with-the-datetime-type">
<h2>Handling different Timezones with the DateTime Type<a class="headerlink" href="#handling-different-timezones-with-the-datetime-type" title="Permalink to this headline"></a></h2>
<p>If you first come across the requirement to save different you are still optimistic to manage this mess,
however let me crush your expectations fast. There is not a single database out there (supported by Doctrine 2)
that supports timezones correctly. Correctly here means that you can cover all the use-cases that
can come up with timezones. If you don&#8217;t believe me you should read up on <a class="reference external" href="http://derickrethans.nl/storing-date-time-in-database.html">Storing DateTime
in Databases</a>.</p>
<p>The problem is simple. Not a single database vendor saves the timezone, only the differences to UTC.
However with frequent daylight saving and political timezone changes you can have a UTC offset that moves
in different offset directions depending on the real location.</p>
<p>The solution for this dilemma is simple. Don&#8217;t use timezones with DateTime and Doctrine 2. However there is a workaround
that even allows correct date-time handling with timezones:</p>
<ol class="arabic simple">
<li>Always convert any DateTime instance to UTC.</li>
<li>Only set Timezones for displaying purposes</li>
<li>Save the Timezone in the Entity for persistence.</li>
</ol>
<p>Say we have an application for an international postal company and employees insert events regarding postal-package
around the world, in their current timezones. To determine the exact time an event occurred means to save both
the UTC time at the time of the booking and the timezone the event happened in.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">namespace</span> <span class="nx">DoctrineExtensions\DBAL\Types</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Doctrine\DBAL\Platforms\AbstractPlatform</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\DBAL\Types\ConversionException</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">UTCDateTimeType</span> <span class="k">extends</span> <span class="nx">DateTimeType</span>
<span class="p">{</span>
    <span class="k">static</span> <span class="k">private</span> <span class="nv">$utc</span> <span class="o">=</span> <span class="k">null</span><span class="p">;</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">convertToDatabaseValue</span><span class="p">(</span><span class="nv">$value</span><span class="p">,</span> <span class="nx">AbstractPlatform</span> <span class="nv">$platform</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$value</span> <span class="o">===</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="k">null</span><span class="p">;</span>
        <span class="p">}</span>


        <span class="k">return</span> <span class="nv">$value</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="nv">$platform</span><span class="o">-&gt;</span><span class="na">getDateTimeFormatString</span><span class="p">(),</span>
            <span class="p">(</span><span class="nx">self</span><span class="o">::</span><span class="nv">$utc</span><span class="p">)</span> <span class="o">?</span> <span class="nx">self</span><span class="o">::</span><span class="nv">$utc</span> <span class="o">:</span> <span class="p">(</span><span class="nx">self</span><span class="o">::</span><span class="nv">$utc</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\DateTimeZone</span><span class="p">(</span><span class="s1">&#39;UTC&#39;</span><span class="p">))</span>
        <span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">convertToPHPValue</span><span class="p">(</span><span class="nv">$value</span><span class="p">,</span> <span class="nx">AbstractPlatform</span> <span class="nv">$platform</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$value</span> <span class="o">===</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="k">null</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="nv">$val</span> <span class="o">=</span> <span class="nx">\DateTime</span><span class="o">::</span><span class="na">createFromFormat</span><span class="p">(</span>
            <span class="nv">$platform</span><span class="o">-&gt;</span><span class="na">getDateTimeFormatString</span><span class="p">(),</span>
            <span class="nv">$value</span><span class="p">,</span>
            <span class="p">(</span><span class="nx">self</span><span class="o">::</span><span class="nv">$utc</span><span class="p">)</span> <span class="o">?</span> <span class="nx">self</span><span class="o">::</span><span class="nv">$utc</span> <span class="o">:</span> <span class="p">(</span><span class="nx">self</span><span class="o">::</span><span class="nv">$utc</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\DateTimeZone</span><span class="p">(</span><span class="s1">&#39;UTC&#39;</span><span class="p">))</span>
        <span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$val</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">throw</span> <span class="nx">ConversionException</span><span class="o">::</span><span class="na">conversionFailed</span><span class="p">(</span><span class="nv">$value</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getName</span><span class="p">());</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="nv">$val</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This database type makes sure that every DateTime instance is always saved in UTC, relative
to the current timezone that the passed DateTime instance has. To be able to transform these values
back into their real timezone you have to save the timezone in a separate field of the entity
requiring timezoned datetimes:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">namespace</span> <span class="nx">Shipping</span><span class="p">;</span>

<span class="sd">/**</span>
<span class="sd"> * @Entity</span>
<span class="sd"> */</span>
<span class="k">class</span> <span class="nc">Event</span>
<span class="p">{</span>
    <span class="sd">/** @Column(type=&quot;datetime&quot;) */</span>
    <span class="k">private</span> <span class="nv">$created</span><span class="p">;</span>

    <span class="sd">/** @Column(type=&quot;string&quot;) */</span>
    <span class="k">private</span> <span class="nv">$timezone</span><span class="p">;</span>

    <span class="sd">/**</span>
<span class="sd">     * @var bool</span>
<span class="sd">     */</span>
    <span class="k">private</span> <span class="nv">$localized</span> <span class="o">=</span> <span class="k">false</span><span class="p">;</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span><span class="nx">\DateTime</span> <span class="nv">$createDate</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">localized</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">created</span> <span class="o">=</span> <span class="nv">$createDate</span><span class="p">;</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">timezone</span> <span class="o">=</span> <span class="nv">$createDate</span><span class="o">-&gt;</span><span class="na">getTimeZone</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getName</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">getCreated</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">localized</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">created</span><span class="o">-&gt;</span><span class="na">setTimeZone</span><span class="p">(</span><span class="k">new</span> <span class="nx">\DateTimeZone</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">timezone</span><span class="p">));</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">created</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This snippet makes use of the previously discussed &#8220;changeset by reference only&#8221; property of
objects. That means a new DateTime will only be used during updating if the reference
changes between retrieval and flush operation. This means we can easily go and modify
the instance by setting the previous local timezone.</p>
</div>
</div>


              </div>
                </div>

            </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
             
            <div id="searchbox" style="">
              <h3>Search</h3>
                <form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="text" name="q" size="18">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>
            </div>
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference internal" href="#">Working with DateTime Instances</a><ul>
<li><a class="reference internal" href="#datetime-changes-are-detected-by-reference">DateTime changes are detected by Reference</a></li>
<li><a class="reference internal" href="#default-timezone-gotcha">Default Timezone Gotcha</a></li>
<li><a class="reference internal" href="#handling-different-timezones-with-the-datetime-type">Handling different Timezones with the DateTime Type</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="validation-of-entities.html"
                                  title="previous chapter">Validation of Entities</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="mysql-enums.html"
                                  title="next chapter">Mysql Enums</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/cookbook/working-with-datetime.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
        </div>
      </div>
          <div class="clearer"></div>
        </div>
          <div class="footer">
              &copy; Copyright 2010-12, Doctrine Project Team.
              Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
            <br/>
            <a target="_BLANK" href="http://www.servergrove.com"><img src="../_static/servergrove.jpg" /></a>      <br/><br/>
            <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
              <input type="hidden" name="cmd" value="_s-xclick" />
              <input type="hidden" name="hosted_button_id" value="BAE2E3XANQ77Y" />
              <input type="Submit" value="Donate via PayPal" />
            </form>
          </div>
      </div>

      <div id="bot-rcnr">
        <div class="tl"><!-- corner --></div>
      </div>
    </div>

  <a class="githublink" href="http://github.com/doctrine">Fork me on GitHub</a>
  </body>
</html>