Several common time systems, such as GMT and UTC, and the use of Python code to convert GPS time to UTC time

Content reproduced from My blog

Time standard

  • Greenwich Mean Time GMT (Greenwich Mean Time): it refers to the local flat solar time at the Royal Greenwich Observatory in suburban London, UK, because the primary meridian is defined as the longitude passing through there. Since February 5, 1924, the Greenwich Observatory has been responsible for distributing time transfer information to the world every hour. Noon of Greenwich Mean Time refers to the time when the flat sun crosses the Greenwich meridian (that is, when it is at the highest point over Greenwich). Because the earth's daily rotation is somewhat irregular and is slowing down slowly, the defect of astronomical observation in Greenwich has been replaced by coordinated universal time (UTC) of atomic clock
  • Universal Time (UT): it is a kind of flat solar time calculated from midnight in Greenwich. Universal Time is a time scale based on the earth's rotation, and its accuracy is affected by the uneven change of the earth's rotation and the pole shift. In order to solve this influence, the International Astronomical Union defined three systems UT0, UT1 and UT2 in 1955: UTO represents the uncorrected world time, UT1 represents the world time corrected by the pole shift, and UT2 represents the world time further seasonally corrected by the earth's rotation speed
  • International Atomic Time (TAI): it is referred to as IAT in English, Tai in French and Tai in French. It is an international reference time scale based on the definition of second (one second is the duration of 9192631770 cycles of transition radiation oscillation between two hyperfine energy levels in the ground state of cesium-133 atom). It is a uniform time scale and is not related to the spatial position of the earth. The starting point is 0:00:00 on January 1, 1958. As time goes on, the time difference between Tai and UT1 becomes larger and larger. To this end, coordination is carried out between UT1 and Tai, which produces coordinated universal time UTC
  • Coordinated Universal Time (UTC): it is the main world time standard. It is based on International Atomic Time and offsets the impact of the earth's slowing rotation by irregularly adding leap seconds. It is also known as world unified time, world standard time and international coordinated time. Due to the different abbreviations of English (CUT) and French (TUC), UTC is abbreviated as a compromise. Coordinated Universal Time (UTC) is the main time standard for adjusting clock and time in the world. It is one of several alternative time systems closest to Greenwich mean time (GMT). For most purposes, UTC time is considered interchangeable with GMT time. The time system was adjusted several times until the leap second mechanism was introduced in 1972, which simplified the adjustment work. Leap seconds will be inserted into UTC when necessary to ensure that the difference between Coordinated Universal Time (UTC) and universal time (UT1) does not exceed 0.9 seconds
  • UNIX or POSIX timestamp: it is the time representation used by UNIX or UNIX like systems. It is generally defined as the total number of seconds (10 bits are accurate to seconds and 13 bits are accurate to milliseconds) from 0:00:0 on January 1, 1970, coordinated universal time (UTC). Taking leap seconds into account, it is more accurately defined as the total number of seconds after leap second adjustment from 0:0:0 on January 1, 1970 UTC to now
  • GPS time: it is GPS atomic time. Its time benchmark is 0:0:0 on January 6, 1980, which is consistent with the world coordinated time UTC. Later, it will be counted according to the cumulative length of atomic time TAI seconds. The difference between GPS time and UTC time is an integral multiple of seconds (because UTC is slower than TAI, and GPS is timed according to TAI, UTC is also slower than TAI). For example, it was 5 seconds in 1989, 11 seconds in 1996, 13 seconds in 2002 and 18 seconds until August 2020.

Leap second

It is an adjustment made to increase or decrease one second in coordinated universal time (UTC) to make it close to flat solar time. The need for leap seconds is partly due to the fact that the length of the average solar day is increasing at a very slow rate. Another reason is that the atomic clock gives seconds a fixed length of time. When the two are combined, it is a little less than the second of the solar time at that time. Time is now measured by a stable atomic clock (TAI or international atomic time). Because there are many variables in the rotation of the earth, the previous definition of seconds (the rotation of the earth around its axis and the revolution around the sun, defined as 1 / 86400 of the average solar day) has been abolished. When you want to increase the positive leap second, this second is increased before 00:00:00 of the next day. The effect is to delay the beginning of the next day of UTC. The second after 23:59:59 of the day is recorded as 23:59:60, and then 00:00:00 of the next day. If it is a negative leap second, the next second of 23:59:58 is 00:00:00 the next day, but there is no need to adjust the negative leap second
Leap_Second.dat You can view the time of each leap second and the number of leap seconds. The main contents are as follows:

#    MJD        Date        TAI-UTC (s)
#           day month year
#    ---    --------------   ------   
    41317.0    1  1 1972       10
    41499.0    1  7 1972       11
    41683.0    1  1 1973       12
    42048.0    1  1 1974       13
    42413.0    1  1 1975       14
    42778.0    1  1 1976       15
    43144.0    1  1 1977       16
    43509.0    1  1 1978       17
    43874.0    1  1 1979       18
    44239.0    1  1 1980       19
    44786.0    1  7 1981       20
    45151.0    1  7 1982       21
    45516.0    1  7 1983       22
    46247.0    1  7 1985       23
    47161.0    1  1 1988       24
    47892.0    1  1 1990       25
    48257.0    1  1 1991       26
    48804.0    1  7 1992       27
    49169.0    1  7 1993       28
    49534.0    1  7 1994       29
    50083.0    1  1 1996       30
    50630.0    1  7 1997       31
    51179.0    1  1 1999       32
    53736.0    1  1 2006       33
    54832.0    1  1 2009       34
    56109.0    1  7 2012       35
    57204.0    1  7 2015       36
    57754.0    1  1 2017       37

TAI, GPST, UTC conversion

According to the document Leap_Second.dat It can be seen that as of August 2020:
TAI = UTC + 37
Since GPST starts counting from 0:00:00 on January 6, 1980, leap seconds on and before January 1, 1980 are not considered, then:
GPST = UTC + 18
In addition, leapsecond You can view UTC, GPST and TAI time in real time

Time zone division

A time zone refers to a region on the earth that uses the same time definition. GMT time or UT time is a form of indicating the rotation rate of the earth. From sunrise to sunset, the time changes from 0 to 24. In this way, the time will be different in different longitudes. In order to solve this problem, people divide the earth into different regions according to longitude. Each region uses the same time definition, and the time difference between adjacent regions is 1 hour. Time zone is divided into theoretical time zone and legal time zone

  • Theoretical time zone: according to longitude, every 15 ° is a time zone. The earth is divided into 24 time zones. The area extending 7.5 ° to the East and west sides with the primary meridian as the center is time zone 0
  • Legal time zone: legal time zone is the time zone adjusted according to the national boundaries of some regions on the basis of theoretical time zone. Is the time zone actually used. For example, China spans five time zones from Dongwu district to Dongjiu District, but uses Dongba District time (Beijing time) uniformly
  • Time difference: the time difference between the time of a place and time zone 0 is called time difference. The time difference is positive in the East and negative in the West. With the primary meridian as the center, the time increases by one hour for each time zone crossing to the East, and decreases by one hour for each time zone crossing to the West
  • International date change line: generally take the 180 degree longitude as the date line. When crossing the date change line from west to East, the date needs to be reduced by one day, otherwise, the date increases by one day

UTC to local time (East 8th District)

According to this code, modify the last line from timestamp (timestamp, timezone (timedelta (hours = 8))) to realize UTC to different time zones

from datetime import datetime, timedelta, timezone

# UTC time to local time (Beijing time)
# 1. Convert str of utc to datetime (no time zone information)
# 2. Add time zone information as utc time zone
# 3. datetime to timestamp
# 4. Get the local time datetime from the timestamp
# The input format is: '2020-08-05 02:03:03.815650'
# The output format is datetime datetime(2020, 8, 5, 10, 3, 3, 815650)
def utc_to_local(utc_time):
    datetimeformat = "%Y-%m-%d %H:%M:%S.%f"
    # Get datetime without time zone
    dt_no_tz = datetime.strptime(utc_time, datetimeformat)
    # Set the time zone to UTC
    # timezone.utc is the same as timezone(timedelta(hours=0))
    utc_datetime = dt_no_tz.replace(tzinfo=timezone(timedelta(hours=0)))
    t = utc_datetime.timestamp()
    # Get UTC time according to timestamp
    # datetime.utcfromtimestamp(t)
    # If you want to convert the timestamp to datetime in the East eighth District
    # fromtimestamp(timestamp, timezone(timedelta(hours=8)))
    # Get the local time from timestamp (T, TZ = none)
    return datetime.fromtimestamp(t)

Local time (East 8th District) to UTC

According to this code, change replace(tzinfo=timezone(timedelta(hours=8))) to convert a time zone to UTC time

from datetime import datetime, timedelta, timezone

# Local time to UTC time
# The input format is: '2020-08-05 10:03:03.815650'
# The output format is datetime datetime(2020, 8, 5, 2, 3, 3, 815650)
def local_to_utc(local_time):
    datetimeformat = "%Y-%m-%d %H:%M:%S.%f"
    # Get datetime without time zone
    dt_no_tz = datetime.strptime(local_time, datetimeformat)
    # Set the time zone as the local time zone (Beijing, Dongba District)
    # timezone.utc is the same as timezone(timedelta(hours=0))
    local_datetime = dt_no_tz.replace(tzinfo=timezone(timedelta(hours=8)))
    t = local_datetime.timestamp()
    # Get UTC time according to timestamp
    return datetime.utcfromtimestamp(t)

GPS time to UTC time

Convert GPS time to UTC time

from datetime import datetime, timedelta

# Leap second

# Input: GPS week, second in GPS week and leap second (optional. The leap second value varies with GPS time, which is determined by Leap_Second.dat file)
# Output: UTC time (Greenwich mean time)
# Input example: gps_week_seconds_to_utc(2119, 214365.000)
# Output example: '2020-08-18 11:32:27.000000'
def gps_week_seconds_to_utc(gpsweek, gpsseconds, leapseconds=LEAP_SECONDS):
    datetimeformat = "%Y-%m-%d %H:%M:%S.%f"
    epoch = datetime.strptime("1980-01-06 00:00:00.000", datetimeformat)
    # The timedelta function handles the case where seconds is a negative number
    elapsed = timedelta(days=(gpsweek*7), seconds=(gpsseconds-leapseconds))
    return datetime.strftime(epoch+elapsed, datetimeformat)

UTC time to GPS time

Convert UTC time to GPS time

from datetime import datetime, timedelta

# Leap second

# Input: UTC time (datetime type)
# Output: GPS week, day of the week, second of the week, millisecond
def utc_to_gps_week_seconds(utc, leapseconds=LEAP_SECONDS):
    datetimeformat = "%Y-%m-%d %H:%M:%S.%f"
    epoch = datetime.strptime("1980-01-06 00:00:00.000", datetimeformat)
    tdiff = utc - epoch + timedelta(seconds=leapseconds)
    gpsweek = tdiff.days // 7
    gpsdays = tdiff.days - 7*gpsweek
    gpsseconds = tdiff.seconds + 86400*(tdiff.days -7*gpsweek)
    return gpsweek, gpsdays, gpsseconds, tdiff.microseconds

Tags: Python def epoch

Posted by kuri7548 on Fri, 15 Apr 2022 00:50:34 +0930