{"id":858,"date":"2024-11-03T16:26:12","date_gmt":"2024-11-03T16:26:12","guid":{"rendered":"http:\/\/192.168.8.14\/?p=858"},"modified":"2024-11-03T16:26:12","modified_gmt":"2024-11-03T16:26:12","slug":"converting-local-time-to-sidereal-time","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=858","title":{"rendered":"Converting local time to sidereal time"},"content":{"rendered":"\n<p>Its been a while since my last post. In that time I have been busy with life stuff.<\/p>\n\n\n\n<p>Here is a script I wrote to convert the current time in to sidereal time (GM sidereal time and also local sidereal time)<br>I have modified it in to functions so its a little more user friendly to non scripters.<\/p>\n\n\n\n<p>Its has 4 functions<br>1 convert the time and location data in to sidereal time<br>2 ask the user to enter a location or use the current location of there geolocated IP<br>3 ask the user to enter a date time or use the current date\/time<br>4 a nice function a human can run <\/p>\n\n\n\n<p>Running this script will load all the functions in to memory and run the very last command to call them.<br>I have put in some comments with different uses for this command. Just uncomment (remove the &#8220;#&#8221;) out the like you want to run.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n### start of functions ###\n\n# this function will get current location and time or ask for them, then convert the sidereal time (both local and Greenwhich Mean)\n# it is a wraper for the following 3 functions in this script\n# usage\n# Get-SiderealTime -- will get the current location and time and convert to sidereal time\n# Get-SiderealTime -AskLocation &quot;no&quot; -AskTime &quot;no&quot; -- will get the current location and time and convert to sidereal time\n# Get-SiderealTime -AskLocation &quot;no&quot; -AskTime &quot;yes&quot; -- will get the current location and prompt for time. then convert to sidereal time\n# Get-SiderealTime -AskLocation &quot;yes&quot; -AskTime &quot;yes&quot; -- will promt for the current location time. then convert to sidereal time\n# the function will write to the screen, it currenlty has no output\nfunction Get-SiderealTime\n{\n   &#x5B;CmdletBinding()]\n    param( \n        &#x5B;Parameter(Mandatory = $false)] $AskLocation = &quot;no&quot;,\n        &#x5B;Parameter(Mandatory = $false)] $AskTime = &quot;no&quot;\n    )\n\n    $LocationObject = Get-UserLocation -AskLocation $AskLocation\n    $DateTimeObject = Get-UserTime -AskTime $AskTime\n\n    Get-SiderealDateTime -LocationObj $LocationObject -DateTimeObj $DateTimeObject\n}\n\n\n# will use the current external IP address found by requesting it form ifconfig.me  and send it to &quot;http:\/\/ip-api.com\/json\/&quot; to get giolocated\n# will not work if the user is using a VPN\n# usage\n# Get-UserLocation -asklocation &quot;yes&quot;  -- will prompt for Latitude and Longitude\n# Get-UserLocation -asklocation &quot;No&quot;   -- (default) will giolocate external IP\nfunction Get-UserLocation\n{\n   &#x5B;CmdletBinding()]\n    param( \n        &#x5B;Parameter(Mandatory = $false)] $AskLocation = &quot;no&quot;\n    )\n\n    if ($AskLocation -eq &quot;no&quot;)\n    {\n        $LocationData = Invoke-RestMethod -Method Get -Uri (&quot;http:\/\/ip-api.com\/json\/&quot; + (curl ifconfig.me).content )\n    }else{\n        $LocationData = &#039;&#039; | select lat,lon,status,city,regionName\n        $LocationData.city = &quot;NA&quot;\n        $LocationData.regionName = &quot;NA&quot;\n        write-host &quot;Enter Latitude in degrees (eg Latitude of Brighton is 50.8229)&quot;\n        &#x5B;Decimal]$LocationData.lat = read-host &quot;Latitude&quot;\n        write-host &quot;Enter Longitude in degrees (eg Longitude of Brighton is -0.1363)&quot;\n        &#x5B;Decimal]$LocationData.lon = read-host &quot;Longitude&quot;  \n        if ($LocationData.lat -and $LocationData.lon){$LocationData.status = &quot;success&quot;}      \n    }\n\n    if ($LocationData.status -eq &quot;success&quot;)\n    {\n        write-host (&quot;     &quot; + $LocationData.city + &quot; , &quot; + $LocationData.regionName) -ForegroundColor Green\n        write-host (&quot;     Long &quot; + $LocationData.lon + &quot; , Lat &quot; + $LocationData.lat) -ForegroundColor Green\n        write-host &quot;  ===============================   &quot; -ForegroundColor Green\n    }\n    return $LocationData\n}\n\n# will use get the current date and time unless instrusted to promp for a date and time\n# usage\n# Get-UserTime -AskTime &quot;yes&quot;  -- will prompt for date time in the following format 17\/07\/2017 09:00:00\n# Get-UserTime -AskTime &quot;No&quot;   -- (default) will get current date and time\n# Get-UserTime -AskTime &quot;any&quot; -DateTimeObj (get-date)   -- will check and return the datetime object, or the current date time if its invalid\nfunction Get-UserTime\n{\n   &#x5B;CmdletBinding()]\n    param( \n        &#x5B;Parameter(Mandatory = $false)] $AskTime = &quot;no&quot;,\n        &#x5B;Parameter(Mandatory = $false)] $DateTimeObj\n    )\n\n    if($DateTimeObj)\n    {\n        if ($DateTimeObj.gettype() = &quot;DataTime&quot;)\n        {\n            return $DateTimeObj\n        }else{\n            # not vailid, get getting current date time\n            return get-date\n        }\n    }\n\n    if ($AskTime -eq &quot;no&quot;)\n    {\n        return get-date\n    }else{\n        write-host &quot;Enter the date and time in the following format&quot;\n        $StartDate = Get-Date (Read-Host -Prompt &#039;Enter the date and time: 17\/07\/2017 or 17\/07\/2017 09:00:00&#039;)\n        if ($StartDate.gettype().name -eq &quot;datetime&quot;){return $StartDate}\n    }\n    return get-date\n}\n\n# will take the location, datetime objects and output the Greenwhich and local sidereal time\n# formula from            https:\/\/aa.usno.navy.mil\/faq\/GAST\n# some code ideas from    https:\/\/github.com\/jhaupt\/Sidereal-Time-Calculator\/blob\/master\/SiderealTimeCalculator.py\nfunction Get-SiderealDateTime\n{\n   &#x5B;CmdletBinding()]\n    param( \n        &#x5B;Parameter(Mandatory = $true)] $LocationObj,\n        &#x5B;Parameter(Mandatory = $true)] $DateTimeObj\n    )\n\n    $Datenow = $DateTimeObj\n    if($Datenow.IsDaylightSavingTime()){$DateNow = $DateNow.addhours(-1)}\n\n    $LocationData = $LocationObj\n\n    # split TD into individual variables for month, day, etc. and convert to floatingPoint\n    &#x5B;float]$Month = $DateNow.month\n    &#x5B;float]$Day = $DateNow.day\n    &#x5B;float]$Year = $DateNow.year\n    &#x5B;float]$Hour = $DateNow.hour\n    &#x5B;float]$Min = $DateNow.Minute\n\n    # convert mm to fractional time:\n    &#x5B;float]$Min = $Min\/60\n\n    # reformat UTC time as fractional hours:\n    &#x5B;float]$UT = $Hour + $Min\n\n    # get the Julian date:\n    $JD = (367 * $Year) - &#x5B;Math]::Truncate(( 7 * ($Year + &#x5B;Math]::Truncate(($Month + 9) \/ 12)  )) \/ 4) + &#x5B;Math]::Truncate((275 * $Month) \/ 9) + $Day + 1721013.5 + ( $UT \/ 24)\n    write-host (&quot;Julian date        &quot; + $JD)  -ForegroundColor Green\n    write-host (&quot;Date \/ time        &quot; + $DateNow)  -ForegroundColor Green\n\n    # get the Greenwhich mean sidereal time:\n    $GMST = 18.697374558 + 24.06570982441908 * ($JD - 2451545)\n    $GMST = $GMST % 24    #use modulo operator to convert to 24 hours\n    $GMSTmm = ($GMST - &#x5B;Math]::Truncate($GMST)) * 60          #convert fraction hours to minutes\n    $GMSTss = ($GMSTmm - &#x5B;Math]::Truncate($GMSTmm)) * 60      #convert fractional minutes to seconds\n    $GMSThh = &#x5B;Math]::Truncate($GMST)\n    $GMSTmm = &#x5B;Math]::Truncate($GMSTmm)\n    $GMSTss = &#x5B;Math]::Truncate($GMSTss)\n    write-host (&quot;Greenwhich Mean Sidereal Time: &quot; + $GMSThh + &quot;:&quot; + $GMSTmm + &quot;:&quot; + $GMSTss) -ForegroundColor Green\n\n    # Convert to the local sidereal time by adding the longitude (in hours) from the GMST.\n    # (Hours = Degrees\/15, Degrees = Hours*15)\n    $Long = $LocationData.lon \/ 15      #Convert longitude to hours\n    $LST = $GMST + $Long     #Fraction LST. If negative add 24\n    if ($LST -lt 0)\n    {$LST = $LST +24}\n    $LSTmm = ($LST - &#x5B;Math]::Truncate($LST)) * 60          #convert fraction hours to minutes\n    $LSTss = ($LSTmm - &#x5B;Math]::Truncate($LSTmm )) * 60     #convert fractional minutes to seconds\n    $LSThh = &#x5B;Math]::Truncate($LST)\n    $LSTmm = &#x5B;Math]::Truncate($LSTmm)\n    $LSTss = &#x5B;Math]::Truncate($LSTss)\n\n    write-host (&quot;          Local Sidereal Time: &quot; + $LSThh + &quot;:&quot; + $LSTmm + &quot;:&quot; + $LSTss) -ForegroundColor Green\n\n}\n\n### end of functions ###\n\n#Get-SiderealTime # -- will get the current location and time and convert to sidereal time\n#Get-SiderealTime -AskLocation &quot;no&quot; -AskTime &quot;no&quot; # -- will get the current location and time and convert to sidereal time\n#Get-SiderealTime -AskLocation &quot;no&quot; -AskTime &quot;yes&quot; # -- will get the current location and prompt for time. then convert to sidereal time\n#Get-SiderealTime -AskLocation &quot;yes&quot; -AskTime &quot;yes&quot; # -- will prompt for the current location time. then convert to sidereal time\n\nGet-SiderealTime\n<\/pre><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Its been a while since my last post. In that time I have been busy with life stuff. Here is a script I wrote to convert the current time in to sidereal time (GM sidereal time and also local sidereal time)I have modified it in to functions so its a little more user friendly to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[164,4,165],"tags":[166],"class_list":["post-858","post","type-post","status-publish","format-standard","hentry","category-astronomical","category-powershell","category-sidereal","tag-sidereal-time"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=858"}],"version-history":[{"count":4,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":862,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions\/862"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}