Skip to content

Instantly share code, notes, and snippets.

@Trucido
Created May 13, 2019 12:31
Show Gist options
  • Save Trucido/efbcd9de77d39edb1aafa2e4a370fd43 to your computer and use it in GitHub Desktop.
Save Trucido/efbcd9de77d39edb1aafa2e4a370fd43 to your computer and use it in GitHub Desktop.
## Convert-StarDate.ps1
#Requires -Version 3
<#
.SYNOPSIS
Calculates the stardate.
.DESCRIPTION
Returns the current stardate with no parameters, or you can enter a specific date as a string or as a DateTime `
object and Get-Stardate will return the stardate for that time.
.PARAMETER DateTime
The date and/or time you want to calculate the stardate for. Defaults to the current time. This may be entered `
either as a DateTime object or as a string.
.PARAMETER Decimal
A number specifying how many decimal places you want to carry the calculation to.
.EXAMPLE
Get-Stardate
Returns the current stardate.
.EXAMPLE
Get-Stardate "4/7/2014 10:20 am"
Returns the stardate at that time in the current time zone.
.EXAMPLE
Get-Stardate -Decimal 2
Returns the current stardate with two decimal points of precision.
.EXAMPLE
Get-Stardate -Stardate 15814.2
Returns the datetime of that particular stardate.
.EXAMPLE
(Captains-Log -Stardate (Get-Stardate)).NewEntry('Meme us up scotty')
Example being used in another function.
.INPUTS
You can pipe in either a DateTime object or a string that can be converted into a DateTime object. Input is `
optional; with no input the current date is used.
.OUTPUTS
A string representing a formatted stardate number. A string is returned instead of a double so that `
stardates ending in ".0" do not have the decimal lopped off.
.NOTES
Cast the output to [double] if you need to do math with it.
#>
function Convert-StarDate {
[CmdletBinding(DefaultParameterSetName="ConvertToStardate")]
[OutputType("ConvertToStardate", [string])] # cast to double if math'ing the outhput
[OutputType("ConvertFromStardate", [DateTime])]
param
(
[Parameter(Mandatory=$false,
Position=0,
ParameterSetName="ConvertToStardate",
ValueFromPipeline=$true)]
[ValidateScript({$_ -gt [DateTime] "1/1/2000Z"})]
[Alias("To")]
[DateTime] $DateTime,
[Parameter (Mandatory=$false,
ParameterSetName = "ConvertToStardate")]
[ValidateRange (0, 10)]
[Decimal] $Decimal,
[Parameter (Mandatory=$false,
ParameterSetName="ConvertFromStardate",
ValueFromPipeline=$true)]
[ValidateScript({$_ -ge 0})]
[Alias("From")]
[double] $Stardate
)
switch ($PSCmdlet.ParameterSetName)
{
"ConvertToStardate"
{
$DateTime = try{if(!$Datetime){throw}}catch{Get-Date}
$Decimal = try{if(!$Decimal){throw}}catch{[Decimal]"1"}
$Result = ($DateTime - (Get-Date "1/1/2000Z")).TotalDays / 0.36525
"{0:F$Decimal}" -f $Result
}
"ConvertFromStardate"
{
$TotalDays = (Get-Date "1/1/2000Z").TotalDays + $Stardate * 0.36525
$TotalDays = [TimeSpan]::FromDays($TotalDays)
(Get-Date "1/1/2000Z") + $TotalDays
}
}
}
if ($MyInvocation.ExpectingInput)
{
$input | Convert-StarDate @args
}
elseif (!($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq ''))
{
Convert-StarDate @args
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment