Bug 87642 - sunrise and sunset times incorrect
Summary: sunrise and sunset times incorrect
Status: RESOLVED FIXED
Alias: None
Product: kweather
Classification: Unclassified
Component: general (show other bugs)
Version: unspecified
Platform: Debian testing Linux
: NOR normal with 20 votes (vote)
Target Milestone: ---
Assignee: John Ratke
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-08-20 22:17 UTC by Berck E. Nash
Modified: 2006-12-25 16:10 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments
fixes the out of range times in kweather (960 bytes, patch)
2005-12-09 14:31 UTC, J.O. Aho
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Berck E. Nash 2004-08-20 22:17:12 UTC
Version:            (using KDE KDE 3.3.0)
Installed from:    Debian testing/unstable Packages
OS:                Linux

Debian Unstable which is currently KDE 3.3.  Sunrise/Sunset times displayed in KWeather have never been correct in any KDE version on any Debian system I've looked at.  This bug seems to been filed and marked fixed endless times, but it still does not work.

If it matters:  I'm using KOUN as the reporting station.  My /etc/timezone is set to America/Chicago.  /etc/defaults/rcS has UTC=no set.
Comment 1 Magnus Kessler 2004-08-26 16:20:58 UTC
Some more info for what seems to go wrong:

The displayed times seem to be off by the timezone difference from GMT.

In my current setup the timezone is GMT0BST (UTC=yes). Stations in the UK (e.g EGLL) display a correct value, whereas stations further east (EDLW, 1 timezone east, NZCH 11h east) have their times too early and stations to the west (CYYT, KOAK 8h west, PHNL 11h west) are too late. Note that default values (6:00; 19:00) are displayed when times would be negative (NZCH) for sunrise or > 23:59:59 for sunset (KOAK and PHNL).

NZCH currently gives me 6:00 / 6:56 and PHNL 17:13 / 19:00; pretty short days :)

It looks like the times are normalized to the user's local timezone, but users would expect to see the time displayed as in the station's timezone.
Comment 2 Berck E. Nash 2004-08-26 18:10:34 UTC
At first, I thought that it might be off by UTC, too.  But that's not the case here.  Sunrise/Sunset, today is 06:58/20:04.  Kweather is reporting it as 00:37/13:26.  This error is more than simply displaying the times in UTC, which I would be more likely to live with.
Comment 3 Magnus Kessler 2004-08-26 20:19:29 UTC
The values you report could be begin/end of civil twilight (forgot the exact definition, it's the sun being a certain amount below the horizon, hence starting earlier and ending later) rather than exact sunrise/sunset. FWIW, KOUN gives me a sunrise of 12:58 (and BST being 6 hours off CDT this would correspond to 06:58). Sunset is reported as 19:00 (default value for >24:00).

You mentioned your computer does not keep its time in UTC. Have you tried setting your hardware clock to UTC and see if this makes a difference?
Comment 4 Berck E. Nash 2004-08-26 20:39:42 UTC
Okay, did some more research and discoverd Bug #74999.  I'm still having the same problems described, but they were supposedly fixed.  Once again, I'm using KDE 3.3 packages out of debian-unstable.

Turns out that if I set my identifier to 'KOUN' instead of 'koun', THEN it displays the correct civil twilight times but claims that those times are sunrise sunset.  From the USNO:

 Thursday 
        26 August 2004        Central Daylight Time          

                         SUN
        Begin civil twilight       6:32 a.m.                 
        Sunrise                    6:58 a.m.                 
        Sun transit                1:31 p.m.                 
        Sunset                     8:04 p.m.                 
        End civil twilight         8:30 p.m.

KWeather is reporting Sunrise/Sunset of 06:32 and 20:30.   This is not correct, and I shoudl be able to enter lowercase ICAO identifiers.
Comment 5 John Ratke 2004-08-27 05:58:05 UTC
Berck,  In current CVS it is not possible to enter the ICAO code in lower case.  

I wonder what that debian package is based on, because it has not been possible to enter lower case stations since version 1.11 of prefdialogdata.ui (which is tagged with 3.3 ALPHA 1).  

See http://webcvs.kde.org/cgi-bin/cvsweb.cgi/kdetoys/kweather/prefdialogdata.ui

Yes, we do still have a problem with assuming that the user's time zone is the time zone of the station because we have not yet hacked in an algorithm to determine time zone from the station's latitude, for example.  Another option would be to store the tz with each station's info, but we would have to get that info from somewhere.


Comment 6 J.O. Aho 2005-12-09 13:55:53 UTC
This problem is still in KDE3.5 (Kweather 2.1.0)

My system is set to use CET, time is correct thanks to ntp

For Seattle, Washington, USA
  Sunrise: 16.45
  Sunset: 19.00

According timeanddate
  Sunrise: 7:46 AM (16:46CET)
  Sunset: 4:18 PM (01:18CET)


For Hong Kong, China
  Sunrise: 06:00
  Sunset: 10:39

According timeanddate
  Sunrise: 6:47 AM (23:47CET)
  Sunset: 5:38 PM (10:38CET)

Seems like the kweather don't manage to add 24h to values under 0 and remove 24h for values over 24.
Comment 7 J.O. Aho 2005-12-09 14:31:42 UTC
Created attachment 13835 [details]
fixes the out of range times in kweather

A fast hack that solves the whole problem, it checks if the hour is belove 0,
in that case adds 24 or if the hour is more than 25 it reduces 24. This seems
to work fine in my KDE 3.5 environment.
Comment 8 J.O. Aho 2006-03-18 07:41:03 UTC
There seems to be a problem with selection of the image (day/night), the selection of image don't match the time of day at the remote. For Hong Kong

HK Time: 14:36 (07:36CET)
Sunraise: 23.29CET
Sunset: 11.33CET

The selected icon is cloudy2_night.png, while it should still have been cloudy2.png, I suspect the remote time is compared to the local times for sunraise/sunset. I'm not really sure as I'm not really cleare where the code for image selection is located.
Comment 9 Martin Koller 2006-12-25 16:10:29 UTC
SVN commit 616456 by mkoller:

BUG: 136312
BUG: 129679
BUG: 115920
BUG: 113339
BUG: 87642

Fix the calculation of sunrise/sunset and the calculation of
isNight.
Patch provided by J.O. Aho


 M  +18 -1     metar_parser.cpp  
 M  +26 -4     sun.cpp  


--- branches/KDE/3.5/kdetoys/kweather/metar_parser.cpp #616455:616456
@@ -411,6 +411,8 @@
 {
 #define E(t) ::pow(10, 7.5*t/(237.7+t))
 	float fRelHumidity = E(weatherInfo.dewC)/E(weatherInfo.tempC) * 100;
+        if (fRelHumidity > 100.0) fRelHumidity = 100.0; 
+
 	weatherInfo.qsRelHumidity.sprintf("%.1f", fRelHumidity);
 	removeTrailingDotZero(weatherInfo.qsRelHumidity);
 	weatherInfo.qsRelHumidity += "%";
@@ -841,7 +843,22 @@
 				longitude << " " << civilStart << " " << civilEnd << " " <<
 				m_localUTCOffset << endl;
 
-		return (currently < civilStart || currently > civilEnd);
+		if (civilStart != civilEnd)
+		{
+			if (civilEnd < civilStart)
+				/* Handle daylight past midnight in local time     */
+				/* for weather stations located at other timezones */
+				return (currently < civilStart && currently > civilEnd);
+			else
+				return (currently < civilStart || currently > civilEnd);
+		}
+		else
+		{
+			// Midnight Sun & Polar Night - In summer, the Sun is always 
+			// over the horizon line ... so use latitude & today date to 
+			// set isNight() value. 
+			return ((m_date.daysInYear() >= 80 || m_date.daysInYear() <= 264) && latitude.contains("S")); 
+		}
 	}
 }
 
--- branches/KDE/3.5/kdetoys/kweather/sun.cpp #616455:616456
@@ -204,20 +204,42 @@
 	QTime result;
 	
 	// Example:  say time is 17.7543  Then hours = 17 and minutes = 0.7543 * 60 = 45.258
+        // We need to convert the time to CORRECT local hours
 	int hours   = (int)floor(time);
+	int localhours = hours + (m_localUTCOffset / 60);
+
+	// We need to convert the time to CORRECT local minutes
 	int minutes = (int)floor((time - hours) * 60);
+	int localminutes = minutes + (m_localUTCOffset % 60);
 
-	int localhours = hours + (m_localUTCOffset / 60); 
-	if (localhours < 0) { localhours += 24; } 
-	if (localhours >= 24) { localhours -= 24; } 
+	// We now have to adjust the time to be within the 60m boundary
+	if (localminutes < 0)
+	{
+		//As minutes is less than 0, we need to
+		//reduce a hour and add 60m to minutes.
+		localminutes += 60;
+		localhours--;
+	}
+	if (localminutes >= 60)
+  {
+		//As minutes are more than 60, we need to
+		//add one more hour and reduce the minutes to
+		//a value between 0 and 59.
+		localminutes -= 60;
+		localhours++;
+	}
 
 	// 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 );
 
+	// We now have to adjust the time to be within the 24h boundary
+	if (localhours < 0) { localhours += 24; }
+	if (localhours >= 24) { localhours -= 24; }
+
 	// 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 );
+	result.setHMS( localhours, localminutes, seconds );
 
 	return result;	
 }