Bug 137448

Summary: Kweather reports wrong sunset/sunrse times for none local places
Product: [Unmaintained] kicker Reporter: J.O. Aho <trizt>
Component: generalAssignee: Aaron J. Seigo <aseigo>
Status: RESOLVED FIXED    
Severity: normal CC: kollix
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: Fixes the time set to 06:00 or 19:00 for sunrise/sunset that occures on another day than today.
Fixes time for sunrise and sunset for remote locations

Description J.O. Aho 2006-11-16 13:17:23 UTC
Version:           3.5.5 (using KDE 3.5.5, Gentoo)
Compiler:          Target: i686-pc-linux-gnu
OS:                Linux (i686) release 2.6.18-gentoo-r2

For me who lives in Europe (CET) and I select locations far east of me, then Sunrise time is always set to 6:00, while locations far west of me has always a sunset time of 19:00.

This has to do with a bug in the kweather/sun.cpp file, the Sun::convertDoubleToLocalTime() function, neither does Sun::computeRiseTime(), Sun::computeSetTime(), Sun::computeCivilTwilightStart() and Sun::computeCivilTwilightEnd().

With a small patch to Sun::convertDoubleToLocalTime() it can be fixed (see last how the function should look to work more properly).


As there are a fix for the problem and the bug been know for years, it's still a mystery why it has never been fixed. PLEASE fix it, it's anoying to have to recompile kweather to fix this.


--- fixed function ---
QTime Sun::convertDoubleToLocalTime( const double time )
{
        QTime result;

        // Example:  say time is 17.7543  Then hours = 17 and minutes = 0.7543 *
 60 = 45.258
        int hours   = (int)floor(time);
        int minutes = (int)floor((time - hours) * 60);
        int localhours = hours + (m_localUTCOffset / 60);
        if(localhours < 0) { localhours += 24; }
        if(localhours >= 24) { localhours -= 24; }

        // Round up or down to nearest second.
        // Use rint instead of nearbyint because rint is in FreeBSD
        int seconds = (int)rint( fabs( minutes - ((time - hours) * 60) ) * 60 );

        // Try to set the hours, minutes and seconds for the local time.
        // If this doesn't work, then we will return the invalid time.
        result.setHMS( localhours, minutes + (m_localUTCOffset % 60), seconds );

        return result;
}
--- eof ---
Comment 1 Martin Koller 2006-12-23 19:58:45 UTC
I'd like to apply your patch, but for me the current applet also works ...
E.g. I live near Vienna, I have selected Singapore / Changi Airport and I get
Sunrise:00:01     Sunset: 12:04
What settings do you use ?

Another point: when you submit patches, please use diff -u output, so that we can easily apply your changes with the patch command.
And second, coders and other contributors are always welcome!
If your time permits, you can help much more and you can even apply for an account so that you can apply changes on your own, if you like.
Comment 2 J.O. Aho 2006-12-23 20:49:59 UTC
My system uses CET as timezone and target city is Hong Kong, which has a sunraise before midnigh, as Singapore has it's after midnight the bug won't be noticed, the same when the sunset is before midnight.

When I posted the bug, I noticed that my old patch didn't work (there been other changes in the kweather/sun.cpp, which resulted that patch didn't find the location where to apply the patch), I'm sorry I didn't take the time to make a new patch for you, I think my old one is still here in the bugzilla somewhere.
Comment 3 J.O. Aho 2006-12-23 22:18:42 UTC
Created attachment 19023 [details]
Fixes the time set to 06:00 or 19:00 for sunrise/sunset that occures on another day than today.

If you have selected a remote location where the supposed sunrise occures
before midnight, then the time is by default set to 06:00, if you select a
location where sunset occures after midnight, then the time is by default set
to 19:00.
This fix Sun::convertDoubleToLocalTime() function to take care of times that
are outside the normal 24h and converts them back to the 24h clock time.

To test this patch, run in CET timezone and select Hong Kong and Seattle, then
rebuild kweather with patch applied and see the difference.

The patch has been made against the 3.80.2 source code, hopefully this will be
applied before release of KDE4.
Comment 4 Martin Koller 2006-12-24 00:11:45 UTC
SVN commit 616157 by mkoller:

BUG: 137448

correct calculation of sunset/sunrise


 M  +6 -2      sun.cpp  


--- branches/KDE/3.5/kdetoys/kweather/sun.cpp #616156:616157
@@ -206,14 +206,18 @@
 	// Example:  say time is 17.7543  Then hours = 17 and minutes = 0.7543 * 60 = 45.258
 	int hours   = (int)floor(time);
 	int minutes = (int)floor((time - hours) * 60);
-	
+
+	int localhours = hours + (m_localUTCOffset / 60); 
+	if (localhours < 0) { localhours += 24; } 
+	if (localhours >= 24) { localhours -= 24; } 
+
 	// Round up or down to nearest second.
 	// Use rint instead of nearbyint because rint is in FreeBSD
 	int seconds = (int)rint( fabs( minutes - ((time - hours) * 60) ) * 60 );
 
 	// Try to set the hours, minutes and seconds for the local time.
 	// If this doesn't work, then we will return the invalid time.
-	result.setHMS( hours + (m_localUTCOffset / 60), minutes + (m_localUTCOffset % 60), seconds );
+	result.setHMS( localhours, minutes + (m_localUTCOffset % 60), seconds );
 
 	return result;	
 }
Comment 5 J.O. Aho 2006-12-24 01:35:00 UTC
Not all zones uses full hours, say the remote sunset is 15:35 and there is a timedifference for you that is -5h 30min, then we have that the time will be 20:65, as you can see this isn't a valid time.

Even the patch I had uploaded earlier has a bug, it don't consider if to fix the hour correcly in those cases where the minutes are less than 0 or more than 60.
Comment 6 Martin Koller 2006-12-25 12:42:34 UTC
On So Dez 24 2006, J.O.Aho wrote:
> Even the patch I had uploaded earlier has a bug, it don't consider if to
> fix the hour correcly in those cases where the minutes are less than 0 or
> more than 60.


So, what is now the correct fix ?
Can you simply send me a new patch, please ?
Comment 7 J.O. Aho 2006-12-25 13:44:19 UTC
Created attachment 19029 [details]
Fixes time for sunrise and sunset for remote locations

We now take care of the minutes, as some "timezones" are +/-Xh and 30min
compared to GMT.

Please don't use the old patch, it has a major bug which adjusts the minutes
wrongly.