Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


mjd_convert_modified_julian_date

Convert Modified Julian Date (MJD)

If you want to convert NTP statistics to human readable output, you'll want to convert the Modified Julian Day field.

  • Unix time = 1970-01-01 00:00:00 UTC
  • MJD at Unix time = 40587
  • MJD to Unix time conversion: (MJD - 40587) * 86400 + seconds past UTC midnight

Example NTP loopstats

Convert MJD and seconds to human readable date, found in the NTP loopstats file.

  • Example loopstats line:
    57209 28138.007 -0.000342524 -7.250 0.000187703 0.014332 10
    • 1st field: date (Modified Julian Day)
    • 2nd field: time (seconds and fraction past UTC midnight)
  • Calculate seconds since Unix time: (57209 - 40587) * 86400 + 28138.007 = 1436168938.01
  • Convert Unix time:
    $ date -d @1436168938 +"%d-%m-%Y %T %z"
    06-07-2015 09:48:58 +0200

AWK example

  • Convert MJD:
    awk '{ printf "%s -- %s\n", strftime("%c", ($1-40587)*86400 + $2), $0 }' ../loopstats
    ma 06 jul 2015 02:22:38 CEST -- 57209 1358.006 -0.000124723 -7.284 0.000219716 0.017353 10
    ma 06 jul 2015 02:40:01 CEST -- 57209 2401.006 -0.000144317 -7.285 0.000205643 0.016233 10
    ma 06 jul 2015 02:57:15 CEST -- 57209 3435.006 -0.000213610 -7.285 0.000193915 0.015188 10
    ma 06 jul 2015 03:33:16 CEST -- 57209 5596.006 -0.000083461 -7.296 0.000187136 0.014669 10
    ma 06 jul 2015 04:08:49 CEST -- 57209 7729.007 0.000072315 -7.287 0.000183510 0.014091 10
    ma 06 jul 2015 04:16:00 CEST -- 57209 8160.007 -0.000097146 -7.287 0.000181813 0.013181 10
    ma 06 jul 2015 04:33:57 CEST -- 57209 9237.007 -0.000100699 -7.287 0.000170075 0.012330 10
    ma 06 jul 2015 04:51:06 CEST -- 57209 10266.007 -0.000076373 -7.288 0.000159323 0.011534 10
    ma 06 jul 2015 05:26:30 CEST -- 57209 12390.007 -0.000323501 -7.328 0.000172757 0.017967 10
    ma 06 jul 2015 05:38:01 CEST -- 57209 13081.006 0.000112221 -7.328 0.000223263 0.016807 10
    ma 06 jul 2015 06:13:57 CEST -- 57209 15237.007 0.000418093 -7.276 0.000235181 0.024149 10
    ma 06 jul 2015 06:31:42 CEST -- 57209 16302.006 0.000157729 -7.275 0.000238474 0.022591 10
    ma 06 jul 2015 07:06:08 CEST -- 57209 18368.007 0.000226313 -7.246 0.000224386 0.023508 10
    ma 06 jul 2015 07:23:48 CEST -- 57209 19428.007 0.000048927 -7.246 0.000219063 0.021990 10
    ma 06 jul 2015 07:59:14 CEST -- 57209 21554.007 0.000075495 -7.237 0.000205130 0.020840 10
    ma 06 jul 2015 08:16:42 CEST -- 57209 22602.006 -0.000101478 -7.237 0.000201826 0.019495 10
    ma 06 jul 2015 08:51:46 CEST -- 57209 24706.007 -0.000093303 -7.249 0.000188813 0.018708 10
    ma 06 jul 2015 09:08:57 CEST -- 57209 25737.008 0.000078355 -7.249 0.000186755 0.017500 10
    ma 06 jul 2015 09:26:46 CEST -- 57209 26806.007 -0.000016954 -7.249 0.000177914 0.016370 10
    ma 06 jul 2015 09:31:32 CEST -- 57209 27092.007 -0.000333920 -7.249 0.000200637 0.015313 10
    ma 06 jul 2015 09:48:58 CEST -- 57209 28138.007 -0.000342524 -7.250 0.000187703 0.014332 10

GnuPlot example

  • Script:
    #!/usr/bin/gnuplot
    #
    # Creates NTP statistics plot.
    # AUTHOR: BS
    
    set terminal pngcairo size 800,600 enhanced font 'Verdana,10'
    set output 'output.png'
    
    set grid
    
    set timefmt "%s"
    set xdata time
    set format x "%d-%m-%Y %H:%M"
    set xtics rotate
    set xlabel "UTC"
    
    set ylabel "Frequency offset in PPM"
    
    set title 'Loopstats ntp1.polaire.nl'
    plot "< awk '{ printf \"%s %s\\n\", strftime(\"%s\", ($1-40587)*86400 + $2), $0 }' ../allloopstats" u 1:5 title "PPM" with lines
  • Output:

mjd_convert_modified_julian_date.txt · Last modified: 2021/10/09 15:14 by 127.0.0.1