<br><br><div class="gmail_quote">On Fri, Jul 13, 2012 at 10:51 AM, Camillo Bruni <span dir="ltr">&lt;<a href="mailto:camillobruni@gmail.com" target="_blank">camillobruni@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Cool thanks everyone for the references :D<br>
<br>
We will pu that into the sources<br>
<div class="im"><br>
On 2012-07-13, at 19:47, Nicolas Cellier wrote:<br>
<br>
&gt; The first numbers are quite easy to guess without reading any reference :<br>
<br>
</div>if you have a major in astronomy, sure :P<br>
<div class="im"><br>
&gt; - 400 years span 146097 days in gregorian calendar.<br>
&gt; - 100 years span 36524 days, except every 400 years.<br>
&gt; - 4 years span 1461 days, except every 100 years.<br>
&gt; - 1 year span 365 days, except every four years<br>
<br>
</div>well indeed, not that hard :P, but when you&#39;re in midst of a mind-boggling refactoring<br>
you don&#39;t waste time to understand these details :P<br></blockquote><div><br></div><div>:)</div><div><br></div><div>Here&#39;s an analogous method in VisualWorks.  They don&#39;t bother to document seconds per day (86400).</div>
<div><br></div><div><div>TimeZone&gt;&gt;timestampToSeconds: aTimestamp</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot; Convert a Timestamp in to seconds starting from the Smalltalk epoch in UTC.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>  Since we do not know leap-seconds in UTC, Answer the number of seconds since January 1, 1901.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>  Same as &#39;self asDate asSeconds + self asTime asSeconds&#39; &quot;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>| yearIndex absDays seconds |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>yearIndex := aTimestamp year - 1901.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>absDays := yearIndex * 365  &quot;elapsed years&quot;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>+ (yearIndex // 4)  &quot;ordinary leap years&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>+ ((yearIndex + 300) // 400)  &quot;leap centuries, first one is 2000, i.e. yearIndex = 99&quot;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>- (yearIndex // 100)  &quot;non-leap centuries&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>+ aTimestamp day - 1</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>+ (aTimestamp firstDayOf: aTimestamp month).</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>seconds := aTimestamp hour * 60 +</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>aTimestamp minute * 60 + </div><div><span class="Apple-tab-span" style="white-space:pre">                </span>aTimestamp second.</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>&quot;do it this way to minimize large arithmetic&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>^absDays * 86400 + seconds</div></div><div><br></div><div>But  I like &quot;Same as &#39;self asDate asSeconds + self asTime asSeconds&#39;&quot;  since</div>
<div><br></div><div>Date&gt;asSeconds</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;Answer the seconds between the time that 1901 began </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>and the same time in the receiver&#39;s day.&quot;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>^self asTimestamp asSeconds</div><div><br></div><div>and</div><div><br></div><div>Timestamp&gt;asSeconds</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;Answer the number of seconds since January 1, 1901.&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;Same as &#39;self asDate asSeconds + self asTime asSeconds&#39;&quot;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>^TimeZone default timestampToSeconds: self</div>
<div><br></div><div>So the documentation still leaves something to be desired :)</div><div><br></div><div>It is perhaps unfortunate that manifest constants in class variables cost more than literals because of the indirection through the association.  I did an inlining scheme in VisualWorks many years ago that depended on automatic recompilation when read-only bindings were changed (since the scope of these bindings is limited its quick to find affected methods).  It depended on the method including the binding in its literals even though the compiler inlined the value of the binding, and it depended on access to source (since the points where the variable is inlined are not marked in the bytecode).  I still think there&#39;s benefit here; even a JIT needs to know what bindings are read-only and a write-barrier to be able to inline the values of globals.  But it has a lot of impact for not very much performance (given that in performance-critical parts of the system people have already inlined by hand, as in the above).  So I suppose it will remain a fun experiment.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">&gt; For months and days, the tricks are less trivial...<br>
&gt;<br>
&gt; Nicolas<br>
&gt;<br>
&gt;<br>
&gt; 2012/7/13 Sven Van Caekenberghe &lt;<a href="mailto:sven@beta9.be">sven@beta9.be</a>&gt;:<br>
&gt;&gt; Yeah, but sometimes a clever algorithm comes from a book, so to speak.<br>
&gt;&gt;<br>
&gt;&gt; Giving the variables long names, or the constants names, would not make that much difference, it would still be pretty hard to understand.<br>
&gt;&gt;<br>
&gt;&gt; ZTimestamp class&gt;&gt;withJdn: jdn dayMonthYearDo: block<br>
&gt;&gt;        &quot;Return the value of executing block with the Gregorian Calender day, month and year as arguments,<br>
&gt;&gt;        as computed from my Julian Day Number, jdn.<br>
&gt;&gt;        See <a href="http://en.wikipedia.org/wiki/Julian_date#Gregorian_calendar_from_Julian_day_number" target="_blank">http://en.wikipedia.org/wiki/Julian_date#Gregorian_calendar_from_Julian_day_number</a>&quot;<br>

&gt;&gt;<br>
&gt;&gt;        | j g dg c dc b db a da y m d |<br>
&gt;&gt;        j := jdn + 32044.<br>
&gt;&gt;        g := j // 146097.<br>
&gt;&gt;        dg := j \\ 146097.<br>
&gt;&gt;        c := ((dg // 36524) + 1) * 3 // 4.<br>
&gt;&gt;        dc := dg - (c * 36524).<br>
&gt;&gt;        b := dc // 1461.<br>
&gt;&gt;        db := dc \\ 1461.<br>
&gt;&gt;        a := ((db // 365) + 1) * 3 // 4.<br>
&gt;&gt;        da := db - (a * 365).<br>
&gt;&gt;        y := (g * 400) + (c * 100) + (b * 4) + a.<br>
&gt;&gt;        m := ((((da * 5) + 308)) // 153) - 2.<br>
&gt;&gt;        d := da - ((m + 4) * 153 // 5) + 122.<br>
&gt;&gt;        ^ block<br>
&gt;&gt;                value: d + 1<br>
&gt;&gt;                value: ((m + 2) \\ 12) + 1<br>
&gt;&gt;                value: (y - 4800 + ((m * 2) // 12))<br>
&gt;&gt;<br>
&gt;&gt; On 13 Jul 2012, at 17:53, Camillo Bruni wrote:<br>
&gt;&gt;<br>
&gt;&gt;&gt; comments? decent variable names? no magic numbers?<br>
&gt;&gt;&gt; NOW you can find NONE of that in dayMonthYearDo!<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; ==================================================================<br>
&gt;&gt;&gt; dayMonthYearDo: aBlock<br>
&gt;&gt;&gt;      &quot;Evaluation the block with three arguments: day month, year.&quot;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;      | l n i j dd mm yyyy |<br>
&gt;&gt;&gt;      l := jdn + 68569.<br>
&gt;&gt;&gt;      n := 4 * l // 146097.<br>
&gt;&gt;&gt;      l := l - (146097 * n + 3 // 4).<br>
&gt;&gt;&gt;      i := 4000 * (l + 1) // 1461001.<br>
&gt;&gt;&gt;      l := l - (1461 * i // 4) + 31.<br>
&gt;&gt;&gt;      j := 80 * l // 2447.<br>
&gt;&gt;&gt;      dd := l - (2447 * j // 80).<br>
&gt;&gt;&gt;      l := j // 11.<br>
&gt;&gt;&gt;      mm := j + 2 - (12 * l).<br>
&gt;&gt;&gt;      yyyy := 100 * (n - 49) + i + l.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;      ^ aBlock<br>
&gt;&gt;&gt;              value: dd<br>
&gt;&gt;&gt;              value: mm<br>
&gt;&gt;&gt;              value: yyyy.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; ==================================================================<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; so can anyone explain me the magic numbers here??<br>
&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;<br>
<br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>best,<div>Eliot</div><br>