Skip to content

Commit

Permalink
Fix bug where calculating sunrise/sunset times crashed in some Europe…
Browse files Browse the repository at this point in the history
…an countries
  • Loading branch information
t1m0thyj committed May 7, 2019
1 parent 29a951a commit 607838a
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 49 deletions.
5 changes: 2 additions & 3 deletions src/InputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ private async void okButton_Click(object sender, EventArgs e)
SolarData solarData = SunriseSunsetService.GetSolarData(DateTime.Today);

DialogResult result = MessageBox.Show(string.Format(_("Is this location " +
"correct?\n\n{0}\nSunrise: {1}, Sunset: {2}"), data.display_name,
solarData.sunriseTime.ToShortTimeString(),
solarData.sunsetTime.ToShortTimeString()), _("Question"),
"correct?\n\n{0}\n{1}"), data.display_name,
SunriseSunsetService.GetSunriseSunsetString(solarData)), _("Question"),
MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.Yes)
Expand Down
1 change: 1 addition & 0 deletions src/Localization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static void Initialize()
{
string systemLocale = CultureInfo.CurrentUICulture.Name.Replace('-', '_');
currentLocale = localeNames.Contains(systemLocale) ? systemLocale : "en_US";
JsonConfig.settings.language = currentLocale;
}

LoadLocale();
Expand Down
2 changes: 1 addition & 1 deletion src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.2.0")]
[assembly: AssemblyVersion("3.2.1")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
82 changes: 60 additions & 22 deletions src/SunriseSunset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,86 @@

namespace WinDynamicDesktop
{
public enum PolarPeriod { None, PolarDay, PolarNight };

public class SolarData
{
public PolarPeriod polarPeriod = PolarPeriod.None;
public DateTime sunriseTime { get; set; }
public DateTime sunsetTime { get; set; }
public DateTime[] solarTimes { get; set; }
}

class SunriseSunsetService
{
private static Dictionary<string,DateTime> GetSunPhases(DateTime date, string lat,
string lng)
{
double latitude = double.Parse(lat, CultureInfo.InvariantCulture);
double longitude = double.Parse(lng, CultureInfo.InvariantCulture);
var sunPhases = new Dictionary<string, DateTime>();
private static readonly Func<string, string> _ = Localization.GetTranslation;

foreach (SunPhase phase in SunCalcNet.SunCalc.GetSunPhases(date, latitude, longitude))
{
sunPhases.Add(phase.Name.Value, phase.PhaseTime);
}
private static List<SunPhase> GetSunPhases(DateTime date, double latitude,
double longitude)
{
return SunCalcNet.SunCalc.GetSunPhases(date.AddHours(12).ToUniversalTime(),
latitude, longitude).ToList();
}

return sunPhases;
private static DateTime GetSolarTime(List<SunPhase> sunPhases, SunPhaseName desiredPhase)
{
return sunPhases.Single(sunPhase =>
sunPhase.Name.Value == desiredPhase.Value).PhaseTime.ToLocalTime();
}

public static SolarData GetSolarData(DateTime date)
{
var sunPhases = GetSunPhases(date.AddHours(12).ToUniversalTime(),
JsonConfig.settings.latitude, JsonConfig.settings.longitude);
double latitude = double.Parse(JsonConfig.settings.latitude,
CultureInfo.InvariantCulture);
double longitude = double.Parse(JsonConfig.settings.longitude,
CultureInfo.InvariantCulture);
var sunPhases = GetSunPhases(date, latitude, longitude);
SolarData data = new SolarData();

SolarData data = new SolarData
try
{
sunriseTime = sunPhases[SunPhaseName.Sunrise.Value].ToLocalTime(),
sunsetTime = sunPhases[SunPhaseName.Sunset.Value].ToLocalTime(),
solarTimes = new DateTime[4]
};
data.sunriseTime = GetSolarTime(sunPhases, SunPhaseName.Sunrise);
data.sunsetTime = GetSolarTime(sunPhases, SunPhaseName.Sunset);
data.solarTimes = new DateTime[4]
{
GetSolarTime(sunPhases, SunPhaseName.Dawn),
GetSolarTime(sunPhases, SunPhaseName.GoldenHourEnd),
GetSolarTime(sunPhases, SunPhaseName.GoldenHour),
GetSolarTime(sunPhases, SunPhaseName.Dusk)
};
}
catch (InvalidOperationException) // Handle polar day/night
{
DateTime solarNoon = GetSolarTime(sunPhases, SunPhaseName.SolarNoon);
double sunAltitude = SunCalcNet.SunCalc.GetSunPosition(solarNoon, latitude,
longitude).Altitude;

data.solarTimes[0] = sunPhases[SunPhaseName.Dawn.Value].ToLocalTime();
data.solarTimes[1] = sunPhases[SunPhaseName.GoldenHourEnd.Value].ToLocalTime();
data.solarTimes[2] = sunPhases[SunPhaseName.GoldenHour.Value].ToLocalTime();
data.solarTimes[3] = sunPhases[SunPhaseName.Dusk.Value].ToLocalTime();
if (sunAltitude > 0)
{
data.polarPeriod = PolarPeriod.PolarDay;
}
else
{
data.polarPeriod = PolarPeriod.PolarNight;
}
}

return data;
}

public static string GetSunriseSunsetString(SolarData solarData)
{
switch (solarData.polarPeriod)
{
case PolarPeriod.PolarDay:
return _("Sunrise/Sunset: Up all day");
case PolarPeriod.PolarNight:
return _("Sunrise/Sunset: Down all day");
default:
return string.Format(_("Sunrise: {0}, Sunset: {1}"),
solarData.sunriseTime.ToShortTimeString(),
solarData.sunsetTime.ToShortTimeString());
}
}
}
}
35 changes: 31 additions & 4 deletions src/WallpaperChangeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace WinDynamicDesktop
{
class WallpaperChangeScheduler
{
private enum DaySegment { Sunrise, Day, Sunset, Night };
private enum DaySegment { AllDay, AllNight, Sunrise, Day, Sunset, Night };

private string lastImagePath;
private DateTime? nextUpdateTime;
Expand Down Expand Up @@ -65,7 +65,11 @@ public void RunScheduler(bool forceImageUpdate = false)

SystemThemeChanger.TryUpdateSystemTheme();

if (isSunUp)
if (data.polarPeriod != PolarPeriod.None)
{
nextUpdateTime = DateTime.Today.AddDays(1);
}
else if (isSunUp)
{
nextUpdateTime = data.sunsetTime;
}
Expand All @@ -90,7 +94,15 @@ public void RunScheduler(bool forceImageUpdate = false)

private DaySegment GetCurrentDaySegment(SolarData data)
{
if (data.solarTimes[0] <= DateTime.Now && DateTime.Now < data.solarTimes[1])
if (data.polarPeriod == PolarPeriod.PolarDay)
{
return DaySegment.AllDay;
}
else if (data.polarPeriod == PolarPeriod.PolarNight)
{
return DaySegment.AllNight;
}
else if (data.solarTimes[0] <= DateTime.Now && DateTime.Now < data.solarTimes[1])
{
return DaySegment.Sunrise;
}
Expand Down Expand Up @@ -118,6 +130,16 @@ public Tuple<int, long> GetImageData(SolarData data, ThemeConfig theme)
{
switch (GetCurrentDaySegment(data))
{
case DaySegment.AllDay:
imageList = theme.dayImageList;
segmentStart = DateTime.Today;
segmentEnd = DateTime.Today.AddDays(1);
break;
case DaySegment.AllNight:
imageList = theme.nightImageList;
segmentStart = DateTime.Today;
segmentEnd = DateTime.Today.AddDays(1);
break;
case DaySegment.Sunrise:
imageList = theme.sunriseImageList;
segmentStart = data.solarTimes[0];
Expand Down Expand Up @@ -158,7 +180,12 @@ public Tuple<int, long> GetImageData(SolarData data, ThemeConfig theme)
{
imageList = theme.nightImageList;

if (isSunUp)
if (data.polarPeriod != PolarPeriod.None)
{
segmentStart = DateTime.Today;
segmentEnd = DateTime.Today.AddDays(1);
}
else if (isSunUp)
{
segmentStart = data.sunriseTime;
segmentEnd = data.sunsetTime;
Expand Down
30 changes: 17 additions & 13 deletions src/WinDynamicDesktop.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\PropertyChanged.Fody.2.6.1\build\PropertyChanged.Fody.props" Condition="Exists('packages\PropertyChanged.Fody.2.6.1\build\PropertyChanged.Fody.props')" />
<Import Project="packages\Costura.Fody.3.3.3\build\Costura.Fody.props" Condition="Exists('packages\Costura.Fody.3.3.3\build\Costura.Fody.props')" />
<Import Project="packages\PropertyChanged.Fody.3.0.0\build\PropertyChanged.Fody.props" Condition="Exists('packages\PropertyChanged.Fody.3.0.0\build\PropertyChanged.Fody.props')" />
<Import Project="packages\Costura.Fody.4.0.0\build\Costura.Fody.props" Condition="Exists('packages\Costura.Fody.4.0.0\build\Costura.Fody.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -42,8 +42,9 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="Costura, Version=3.3.3.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>packages\Costura.Fody.3.3.3\lib\net40\Costura.dll</HintPath>
<Reference Include="Costura, Version=4.0.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>packages\Costura.Fody.4.0.0\lib\net40\Costura.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="DesktopBridge.Helpers, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\DesktopBridge.Helpers.1.2.1\lib\net45\DesktopBridge.Helpers.dll</HintPath>
Expand All @@ -55,19 +56,22 @@
<HintPath>packages\NamedPipeWrapper.1.5.0\lib\net40\NamedPipeWrapper.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NGettext, Version=0.6.1.0, Culture=neutral, PublicKeyToken=08d3d1c89dfd2985, processorArchitecture=MSIL">
<HintPath>packages\NGettext.0.6.3\lib\net46\NGettext.dll</HintPath>
</Reference>
<Reference Include="PropertyChanged, Version=2.6.1.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
<HintPath>packages\PropertyChanged.Fody.2.6.1\lib\net452\PropertyChanged.dll</HintPath>
<Reference Include="PropertyChanged, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
<HintPath>packages\PropertyChanged.Fody.3.0.0\lib\net452\PropertyChanged.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="RestSharp, Version=106.6.9.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>packages\RestSharp.106.6.9\lib\net452\RestSharp.dll</HintPath>
</Reference>
<Reference Include="SunCalcNet, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\SunCalcNet.1.0.2\lib\net461\SunCalcNet.dll</HintPath>
<Reference Include="SunCalcNet, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\SunCalcNet.1.0.3\lib\net461\SunCalcNet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -201,9 +205,9 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Costura.Fody.3.3.3\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.3.3.3\build\Costura.Fody.props'))" />
<Error Condition="!Exists('packages\Fody.4.2.1\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.4.2.1\build\Fody.targets'))" />
<Error Condition="!Exists('packages\PropertyChanged.Fody.2.6.1\build\PropertyChanged.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\PropertyChanged.Fody.2.6.1\build\PropertyChanged.Fody.props'))" />
<Error Condition="!Exists('packages\Costura.Fody.4.0.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.4.0.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('packages\PropertyChanged.Fody.3.0.0\build\PropertyChanged.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\PropertyChanged.Fody.3.0.0\build\PropertyChanged.Fody.props'))" />
<Error Condition="!Exists('packages\Fody.5.0.5\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.5.0.5\build\Fody.targets'))" />
</Target>
<Import Project="packages\Fody.4.2.1\build\Fody.targets" Condition="Exists('packages\Fody.4.2.1\build\Fody.targets')" />
<Import Project="packages\Fody.5.0.5\build\Fody.targets" Condition="Exists('packages\Fody.5.0.5\build\Fody.targets')" />
</Project>
10 changes: 5 additions & 5 deletions src/packages.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="3.3.3" targetFramework="net461" />
<package id="Costura.Fody" version="4.0.0" targetFramework="net461" />
<package id="DesktopBridge.Helpers" version="1.2.1" targetFramework="net461" />
<package id="Fody" version="4.2.1" targetFramework="net461" developmentDependency="true" />
<package id="Fody" version="5.0.5" targetFramework="net461" developmentDependency="true" />
<package id="ImageListView" version="13.4.0" targetFramework="net461" />
<package id="NamedPipeWrapper" version="1.5.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
<package id="NGettext" version="0.6.3" targetFramework="net461" />
<package id="PropertyChanged.Fody" version="2.6.1" targetFramework="net461" />
<package id="PropertyChanged.Fody" version="3.0.0" targetFramework="net461" />
<package id="RestSharp" version="106.6.9" targetFramework="net461" />
<package id="SunCalcNet" version="1.0.2" targetFramework="net461" />
<package id="SunCalcNet" version="1.0.3" targetFramework="net461" />
</packages>
2 changes: 1 addition & 1 deletion uwp/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" IgnorableNamespaces="uap uap3 mp rescap desktop">
<Identity Name="38719TimothyJohnson.WinDynamicDesktop" Publisher="CN=3C822DA5-D64C-40A9-A84A-5502C3EDD8CD" Version="3.1.0.0" />
<Identity Name="38719TimothyJohnson.WinDynamicDesktop" Publisher="CN=3C822DA5-D64C-40A9-A84A-5502C3EDD8CD" Version="3.2.1.0" />
<Properties>
<DisplayName>WinDynamicDesktop</DisplayName>
<PublisherDisplayName>Timothy Johnson</PublisherDisplayName>
Expand Down

0 comments on commit 607838a

Please sign in to comment.