Fiddling around with my date/time algorithms, I finally had to determine if we have Daylight Saving Time at a given date. So, I have in eax the day when DST begins and in ecx the date when DST ends. Both days are expressed as DoY (Day of Year, 1-366). In edx I have the current date, also as DoY.
It would seem a matter of a simple bounds check to see whether we have DST or not, but the northern/ southern hemisphere complicates it a bit. You see, in the southern hemisphere DST begins around October and ends around March. So, if eax <= edx < ecx, this would signal DST in the northern hemisphere, but in the southern hemisphere we have DST if edx >= eax OR edx < ecx...
Assuming that eax = start of DST, ecx = end of DST and edx = current date, what would be the shortest algorithm that would return TRUE (if DST) or FALSE (if no DST) in eax ? I'm pretty sure I got the smallest one :)
Posted on 2002-11-05 17:54:38 by micmic
hemisphere = 1 if south, 0 if north.

[size=12]	sub ecx, eax

sub edx, eax
sub edx, ecx
sbb eax, eax
add eax, [hemisphere]
;and eax, 1 (if you need a 1 instead of nonzero for TRUE)
Posted on 2002-11-06 06:37:58 by iblis
Very good! The idea is the same as mine (but I don't need :)):

sub eax, ecx
sub edx, ecx
cmp eax, edx
setbe al
movzx eax, al ; <- [COLOR=red]edited[/COLOR] :)

Asm programming will die the day you can give a C compiler the following algo...

if a < b
if (x>=a && x<b)
then TRUE
elseif b < a
if (x<a || x>=b)
then TRUE

....and get an asm listing like the one above :)
Posted on 2002-11-06 07:12:26 by micmic
You asked to return true if it's Daylight Savings Time, but what's true in the northern hemisphere is false in the southern. IMO you do indeed need to know what hemisphere it is in order for eax to be TRUE if it's DST. Otherwise all that eax tells you is if the date is between March and October. Perhaps I misunderstood?

A suggestion for your code:
Instead of "and eax, 0FFh", use "movzx eax, al"
Save 2 bytes :alright:
Posted on 2002-11-06 08:17:04 by iblis
You asked to return true if it's Daylight Savings Time, but what's true in the northern hemisphere is false in the southern.

Not so. Some countries change for example at the 3rd Sunday of October, others at a fixed date of a certain month, or the last Friday of March. It is possible to have DST in both northern & southern hemispheres at the same date. What your algo will have, is the start & end dates (as DoY) and you need to determine if you have DST, no matter what hemisphere it is. My algo will return TRUE if DST, no matter what hemisphere. Thanks for the suggestion :alright:
Posted on 2002-11-06 09:36:06 by micmic

Yes I see now what you are trying to do. (Your recent edit of your C code helped clarify) I was under the impression that a and b were constants (Mar-Oct). I guess I'm too used to my own DST schedule. :grin: I didn't see that the exact geographic location, and its respective DST schedule was known beforehand. I figured you were just trying to generate a rough estimate based on hemisphere.

In that case your algo is very nice! :alright:
Posted on 2002-11-06 10:34:40 by iblis