Skip to content

Commit

Permalink
Add Suntime (#6)
Browse files Browse the repository at this point in the history
* v1.0.2 release
  • Loading branch information
mdowst authored Jul 9, 2024
1 parent 85699ca commit f822808
Show file tree
Hide file tree
Showing 21 changed files with 815 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module ModuleBuilder,PSScriptAnalyzer
Install-Module ModuleBuilder,PSScriptAnalyzer,EZOut
Install-Module Pester -MinimumVersion 5.5.0
- name: Run build
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Install-Module PSDates
| [Get-DateFormat](docs/Get-DateFormat.md) | Returns common date and time formats |
| [Get-Easter](docs/Get-Easter.md) | This function offers a generic Easter computing method for any given year, using Western, Orthodox or Julian algorithms. |
| [Get-PatchTuesday](docs/Get-PatchTuesday.md) | Returns the second Tuesday of the month |
| [New-Duration](docs/New-Duration.md) | Short description |
| [Get-SunTime](docs/Get-SunTime.md) | Find sunrise and sunset times for any location on planet Earth. |
| [New-Duration](docs/New-Duration.md) | Calculates the time span between two dates and returns the duration in the ISO 8601 format |
| [Test-CrontabSchedule](docs/Test-CrontabSchedule.md) | Tests that a crontab string is valid |

4 changes: 2 additions & 2 deletions Source/Classes/DateTimeExtended.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DateTimeExtended {
[datetime]$EndOfMonth
[string]$WeekOfYear
[System.TimeZoneInfo]$TimeZone
[int]$Quater
[int]$Quarter
[datetime]$Date
[int]$Day
[System.DayOfWeek]$DayOfWeek
Expand Down Expand Up @@ -39,7 +39,7 @@ class DateTimeExtended {
$this.EndOfMonth = ((($StartOfMonth).AddMonths(1).AddSeconds(-1)))
$this.WeekOfYear = (Get-Date $date -uformat %V)
$this.TimeZone = ([System.TimeZoneInfo]::Local)
$this.Quater = [Math]::ceiling($Date.Month / 3)
$this.Quarter = [Math]::ceiling($Date.Month / 3)
$this.Date = $Date.Date
$this.Day = $Date.Day
$this.DayOfWeek = $Date.DayOfWeek
Expand Down
46 changes: 46 additions & 0 deletions Source/Classes/SunTime.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class SunTime {
[double] $Latitude
[double] $Longitude
[int64] $Now
[double] $JulianDate
[double] $JulianDay
[double] $MeanSolarTime
[double] $SolarMeanAnomaly
[double] $EquationOfTheCenter
[double] $EclipticLongitude
[double] $SolarTransitTime
[double] $HourAngle
[DateTime] $Sunrise
[DateTime] $Sunset
[double] $DayLength
[TimeZoneInfo] $TimeZone

[string] ToDegreeString([double] $value) {
$x = [math]::Round($value * 3600)
$num = "∠{0:N3}°" -f $value
$rad = "∠{0:N3}rad" -f ($value * ([math]::PI / 180))
$human = "∠{0}°{1}′{2}″" -f ($x / 3600), ($x / 60 % 60), ($x % 60)
return "$rad = $human = $num"
}

[string] FromTimestamp([double]$Timestamp,
[System.TimeZoneInfo]$TimeZone = $null) {
$datetime = ConvertFrom-UnixTime $Timestamp
if ($TimeZone) {
$datetime = [System.TimeZoneInfo]::ConvertTimeFromUtc($datetime, $TimeZone)
}
return $datetime.ToString()
}

[double] JulianToTimestamp(
[double]$Julian
) {
return ($Julian - 2440587.5) * 86400
}

[double] TimestampToJulian (
[double]$Timestamp
) {
return $Timestamp / 86400.0 + 2440587.5
}
}
19 changes: 19 additions & 0 deletions Source/Formatting/DateTimeExtended.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
$PropertiesAndWidths = [ordered]@{
Date = 25
WeekOfYear = 10
DayOfWeek = 15
StartOfWeek = 15
EndOfWeek = 15
StartOfMonth = 15
EndOfMonth = 15
}
$VirtualProperties = [ordered]@{
"StartOfWeek"= {$_.EndOfWeek.ToShortDateString()}
"EndOfWeek"={$_.EndOfWeek.ToShortDateString()}
"StartOfMonth"={$_.StartOfMonth.ToShortDateString()}
"EndOfMonth"={$_.EndOfMonth.ToShortDateString()}
}

$Property = $PropertiesAndWidths.GetEnumerator() | ForEach-Object { $_.Name }
$Width = $PropertiesAndWidths.GetEnumerator() | ForEach-Object { $_.Value }
Write-FormatView -TypeName DateTimeExtended -Property $Property -Width $Width -VirtualProperty $VirtualProperties
29 changes: 29 additions & 0 deletions Source/Formatting/SunTime.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
$VirtualProperties = [ordered]@{
"Date" = { $_.Sunrise.ToString('d') }
"Sunrise" = { $_.Sunrise.ToString('t') }
"Sunset" = { $_.Sunset.ToString('t') }
"DayLength" = { "{0:N3} hours" -f $_.DayLength }
}
$Property = $VirtualProperties.GetEnumerator() | ForEach-Object { $_.Name }
Write-FormatView -TypeName SunTime -Property $Property -VirtualProperty $VirtualProperties

$VirtualProperties = [ordered]@{
Date = { $_.Sunrise.ToString('d') }
Sunrise = { $_.Sunrise.ToString('t') }
Sunset = { $_.Sunset.ToString('t') }
DayLength = { "{0:N3} hours" -f $_.DayLength }
Latitude = { $_.ToDegreeString($_.Latitude) }
Longitude = { $_.ToDegreeString($_.Longitude) }
Now = { $_.FromTimestamp($_.Now, $_.TimeZone) }
JulianDate = { "{0:N3} days" -f $_.JulianDate }
JulianDay = { "{0:N3} days" -f $_.JulianDay }
MeanSolarTime = { "{0:N9} days" -f $_.MeanSolarTime }
SolarMeanAnomaly = { $_.ToDegreeString($_.SolarMeanAnomaly) }
EquationOfTheCenter = { $_.ToDegreeString($_.EquationOfTheCenter) }
EclipticLongitude = { $_.ToDegreeString($_.EclipticLongitude) }
SolarTransitTime = { $_.FromTimestamp($_.JulianToTimestamp($_.SolarTransitTime), $_.TimeZone) }
HourAngle = { $_.ToDegreeString($_.HourAngle) }
}

$Property = $VirtualProperties.GetEnumerator() | ForEach-Object { $_.Name }
Write-FormatView -TypeName SunTime -Property $Property -VirtualProperty $VirtualProperties -AsList
39 changes: 39 additions & 0 deletions Source/PSDates.EzFormat.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#requires -Module EZOut
# Install-Module EZOut or https://github.com/StartAutomating/EZOut
$myFile = $MyInvocation.MyCommand.ScriptBlock.File
$myModuleName = $($myFile | Split-Path -Leaf) -replace '\.ezformat\.ps1', '' -replace '\.ezout\.ps1', ''
$myRoot = $myFile | Split-Path
Push-Location $myRoot
$formatting = @(
# Add your own Write-FormatView here,
# or put them in a Formatting or Views directory
foreach ($potentialDirectory in 'Formatting','Views','Types') {
Join-Path $myRoot $potentialDirectory |
Get-ChildItem -ea ignore |
Import-FormatView -FilePath {$_.Fullname}
}
)

$destinationRoot = $myRoot

if ($formatting) {
$myFormatFilePath = Join-Path $destinationRoot "$myModuleName.format.ps1xml"
# You can also output to multiple paths by passing a hashtable to -OutputPath.
$formatting | Out-FormatData -Module $MyModuleName -OutputPath $myFormatFilePath
}

$types = @(
# Add your own Write-TypeView statements here
# or declare them in the 'Types' directory
Join-Path $myRoot Types |
Get-Item -ea ignore |
Import-TypeView

)

if ($types) {
$myTypesFilePath = Join-Path $destinationRoot "$myModuleName.types.ps1xml"
# You can also output to multiple paths by passing a hashtable to -OutputPath.
$types | Out-TypeData -OutputPath $myTypesFilePath
}
Pop-Location
188 changes: 188 additions & 0 deletions Source/PSDates.format.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<!-- Generated with EZOut 2.0.6: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Configuration>
<ViewDefinitions>
<View>
<Name>DateTimeExtended</Name>
<ViewSelectedBy>
<TypeName>DateTimeExtended</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Alignment>left</Alignment>
<Width>25</Width>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>left</Alignment>
<Width>10</Width>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>left</Alignment>
<Width>15</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>StartOfWeek</Label>
<Alignment>left</Alignment>
<Width>15</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>EndOfWeek</Label>
<Alignment>left</Alignment>
<Width>15</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>StartOfMonth</Label>
<Alignment>left</Alignment>
<Width>15</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>EndOfMonth</Label>
<Alignment>left</Alignment>
<Width>15</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Date</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>WeekOfYear</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>DayOfWeek</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.EndOfWeek.ToShortDateString()</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.EndOfWeek.ToShortDateString()</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.StartOfMonth.ToShortDateString()</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.EndOfMonth.ToShortDateString()</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>SunTime</Name>
<ViewSelectedBy>
<TypeName>SunTime</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Date</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Sunrise</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Sunset</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>DayLength</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock> $_.Sunrise.ToString('d') </ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock> $_.Sunrise.ToString('t') </ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock> $_.Sunset.ToString('t') </ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock> "{0:N3} hours" -f $_.DayLength </ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>SunTime</Name>
<ViewSelectedBy>
<TypeName>SunTime</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>Date</Label>
<ScriptBlock> $_.Sunrise.ToString('d') </ScriptBlock>
</ListItem>
<ListItem>
<Label>Sunrise</Label>
<ScriptBlock> $_.Sunrise.ToString('t') </ScriptBlock>
</ListItem>
<ListItem>
<Label>Sunset</Label>
<ScriptBlock> $_.Sunset.ToString('t') </ScriptBlock>
</ListItem>
<ListItem>
<Label>DayLength</Label>
<ScriptBlock> "{0:N3} hours" -f $_.DayLength </ScriptBlock>
</ListItem>
<ListItem>
<Label>Latitude</Label>
<ScriptBlock> $_.ToDegreeString($_.Latitude) </ScriptBlock>
</ListItem>
<ListItem>
<Label>Longitude</Label>
<ScriptBlock> $_.ToDegreeString($_.Longitude) </ScriptBlock>
</ListItem>
<ListItem>
<Label>Now</Label>
<ScriptBlock> $_.FromTimestamp($_.Now, $_.TimeZone) </ScriptBlock>
</ListItem>
<ListItem>
<Label>JulianDate</Label>
<ScriptBlock> "{0:N3} days" -f $_.JulianDate </ScriptBlock>
</ListItem>
<ListItem>
<Label>JulianDay</Label>
<ScriptBlock> "{0:N3} days" -f $_.JulianDay </ScriptBlock>
</ListItem>
<ListItem>
<Label>MeanSolarTime</Label>
<ScriptBlock> "{0:N9} days" -f $_.MeanSolarTime </ScriptBlock>
</ListItem>
<ListItem>
<Label>SolarMeanAnomaly</Label>
<ScriptBlock> $_.ToDegreeString($_.SolarMeanAnomaly) </ScriptBlock>
</ListItem>
<ListItem>
<Label>EquationOfTheCenter</Label>
<ScriptBlock> $_.ToDegreeString($_.EquationOfTheCenter) </ScriptBlock>
</ListItem>
<ListItem>
<Label>EclipticLongitude</Label>
<ScriptBlock> $_.ToDegreeString($_.EclipticLongitude) </ScriptBlock>
</ListItem>
<ListItem>
<Label>SolarTransitTime</Label>
<ScriptBlock> $_.FromTimestamp($_.JulianToTimestamp($_.SolarTransitTime), $_.TimeZone) </ScriptBlock>
</ListItem>
<ListItem>
<Label>HourAngle</Label>
<ScriptBlock> $_.ToDegreeString($_.HourAngle) </ScriptBlock>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
6 changes: 3 additions & 3 deletions Source/PSDates.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '.\PSDates.psm1'

# Version number of this module.
ModuleVersion = '1.0.1'
ModuleVersion = '1.0.2'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -63,13 +63,13 @@ RequiredAssemblies = @('.\Resources\NCrontab.dll','.\Resources\CronExpressionDes
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = '.\Resources\DateTimeExtensions.format.ps1xml'
FormatsToProcess = '.\PSDates.format.ps1xml'

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Convert-TimeZone','ConvertFrom-UnixTime','ConvertFrom-WmiDateTime','ConvertTo-UnixTime','ConvertTo-WmiDateTime','Find-TimeZone','Get-CronNextOccurrence','Get-DateExtended','Get-DateFormat','Get-Easter','Get-PatchTuesday','New-Duration','Test-CrontabSchedule')
FunctionsToExport = '*'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
Loading

0 comments on commit f822808

Please sign in to comment.