Skip to content

Instantly share code, notes, and snippets.

@blueskyjunkie
Created June 24, 2016 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blueskyjunkie/5775e241fd5d2a3d6fdb244ca1ab8f25 to your computer and use it in GitHub Desktop.
Save blueskyjunkie/5775e241fd5d2a3d6fdb244ca1ab8f25 to your computer and use it in GitHub Desktop.
Acquire current UTC leap second from Internet sources
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import logging
import requests
log = logging.getLogger( __name__ )
offsetUrls = {
'mil' : 'http://maia.usno.navy.mil/ser7/tai-utc.dat',
'nasa' : 'http://cdf.gsfc.nasa.gov/html/CDFLeapSeconds.txt'
}
def parseNasa( ) :
leapSecondColumn = 3
r = requests.get( offsetUrls[ 'nasa' ] )
tableData = r.text.split( '\n' )
splitTable = [ ]
for row in tableData :
columns = row.split( )
# Ignore blank lines and comment lines (begin with ';')
if columns and (columns[ 0 ] != ';') :
splitTable.append( columns )
numberRows = len( splitTable )
currentLeapSecond = splitTable[ numberRows - 1 ][ leapSecondColumn ]
return currentLeapSecond
def parseMil( ) :
leapSecondColumn = 6
r = requests.get( offsetUrls[ 'mil' ] )
tableData = r.text.split( '\n' )
splitTable = [ ]
for row in tableData :
columns = row.split( )
# Ignore blank lines
if columns :
splitTable.append( columns )
numberRows = len( splitTable )
currentLeapSecond = splitTable[ numberRows - 1 ][ leapSecondColumn ]
return currentLeapSecond
def getCurrentOffset( ) :
try :
currentLeapSecond = parseMil( )
except :
# Try an alternative URL
currentLeapSecond = parseNasa( )
return currentLeapSecond
if __name__ == '__main__' :
currentLeapSecond = getCurrentOffset( )
log.info( 'Current leap second: ' + repr( currentLeapSecond ) )
print( 'Current leap second: ' + repr( currentLeapSecond ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment