5120 views|5 replies

459

Posts

0

Resources
The OP

Programs and Calendar [Copy link]

I had no intention of connecting the program to the calendar in the 5 hours before writing this article. But now I have started writing it.

This all stems from a question one of my classmates asked me.

The problem is probably this: Convert a current date to a day of the GPS week.

At that time, my classmate mentioned GPS week, GPS day, Julian day and other terms on the phone. I could barely guess what the first two meant (after all, I have written GPS programs for two years). But I didn't know what Julian day meant. So I searched Baidu in my spare time. I'll record it here as a study note.

Let me introduce a few concepts first

1. Julian calendar

The Julian calendar is the predecessor of the Gregorian calendar that is used internationally today. Most Western countries adopted it in the 16th century. In 46 BC, the Roman commander Gaius Julius Caesar, with the help of the Greek mathematician and astronomer Sosigenes in Alexandria, Egypt, formulated a calendar that was implemented on January 1, 46 BC, replacing the old Roman calendar. So people call this calendar the "Julian calendar".

2. Gregorian calendar

The Gregorian calendar, also known as the Gregorian calendar, is the current internationally accepted calendar. It is a type of solar calendar, commonly known as the solar calendar. Its predecessor is the Augustan calendar, and the predecessor of the Augustan calendar is the Julian calendar. Its calendar year is a tropical year (365.2425 days), divided into 12 calendar months. It was the calendar formulated by Pope Gregory XIII (also translated as Gregory) in 1582 AD to reform the Julian calendar. The tropical year of the Julian calendar is 365.25, which is very different from the actual tropical year of 365.2422. At that time, the error between the Julian calendar and the actual position of the earth had reached 14 days. The Gregorian calendar corrected the error and determined that all integer century years except those that are divisible by 400 will not have leap years. At the same time, it was stipulated that the day after October 4, 1582 would be October 15, 1582, but the original week would remain unchanged. The new calendar can theoretically achieve an error of no more than one day within 20,000 years, but due to changes in the Earth's rotation, the error can actually reach one day by 4909 AD. Compared with the Julian calendar, the Gregorian calendar is a newly formulated calendar, so sometimes, including the Republic of China's "Chinese National Standard" CNS 7648 "Data Elements and Exchange Formats - Information Exchange - Representation of Dates and Times", it is also called the new calendar.

3. Julian Day

From the perspective of astronomical calculation, as long as the time is kept continuously in days, this is the Julian day. The setting of years and months is more for the needs of life and production, and is not necessary for astronomical calculation.

The Julian day system proposed by French scholar Joseph Justus Scaliger (1540-1609) is now widely used. The reason why it is called Julian day has nothing to do with the Julian calendar mentioned above, but because Scaliger's father, Italian scholar Julius Caesar Scaliger (1484-1558), has the same name as Julius Caesar, the Roman ruler who promulgated and implemented the Julian calendar. The system starts at noon on January 1, 4713 BC, and counts the days continuously, abbreviated as JD. Accumulated to the present, it is already a very large number. For example, the Julian day system at 12 o'clock on January 1, 2000, geodynamic time, is JD 2451545.0, which is a very important moment, specially recorded as J 2000.0. Because the Julian day has too many digits, the International Astronomical Union adopted the simplified Julian day (MJD) in 1973, which is defined as MJD = JD - 2400000.5. The corresponding starting point for the MJD is 17 November 1858, 0000 UT.

So why was the Julian day set to start at noon on January 1, 4713 BC?

The reason is this: When Scaliger constructed this system, he considered three cycles: the 28-year cycle (365.25×4×7 days) when the solar calendar date coincides with the week, the 19-year cycle (29.53×(12×19+7)≈365.25×19=6939.75 days when the solar calendar coincides with the lunar calendar, and the 15-year cycle when the Roman government registered property for taxation. The lowest common multiple of the three, 7,980 years, was taken as the Julian cycle, and then calculated upwards to obtain January 1, 4713 BC as the epoch when all three cycles started at the same time. In other words, this day is not only the Gregorian New Year's Day, but also the first day of the lunar calendar and Sunday, and the day when the Roman government (if there was one) registered property. This is the origin of the Julian day for calculating the epoch.
The introduction of the Julian day raised two problems that needed to be solved, namely how to interchange the Gregorian year, month, and day ordinal numbers with the Julian day ordinal numbers. Next, we will discuss how to calculate the Julian day ordinal number J(y,m,d) of the current day from the Gregorian calendar year, month, and day ordinal numbers y,m,d. This problem is a little difficult, so let's take a step back and limit the time range to be considered to within one year, and consider how to calculate the cumulative day S(m,d) of the current day from the month and day ordinal numbers m,d. This may help us find a solution to the problem.

4. Accumulated days

The idea of introducing the concept of accumulated days on a certain day in a year is consistent with the establishment of the Julian day. That is, the existence of the month is ignored, and the days are counted continuously from the first day of the year to the last day of the year. However, its scope of application is limited to one year, and the calculation starts again in the next year, unlike the Julian cycle which lasts for 7,980 years. In this way, the factors of year, especially leap year, do not need to be considered. The accumulated days of January 1st are 1, and the accumulated days of December 31st are 365 in ordinary years and 366 in leap years.
The calculation of accumulated days is not difficult at first, but it becomes complicated due to the special circumstances of February. Caesar set the leap month in February, and Augustus subtracted one day from February, both because February was an unlucky month for the execution of death row prisoners in ancient Rome, which was quite humane. However, since the leap month is set in February, the number of days in February becomes non-unique and must be adjusted according to ordinary years and leap years, which affects the accumulated days of the next ten months. Imagine that if the leap month is set on the last day of the year, then except for this day, the accumulated days of all other days in the year are unique and unchanged, including the accumulated days of day 0 of each month, and the problem becomes simple.
However, although the setting of the leap month in February is inconvenient, it cannot be changed casually. We'd better change our thinking and consider whether March can be regarded as the first month of the year and February of the following year as the last month of the year in the calculation process? As long as January and February of the following year are called the 13th and 14th months of the year, and each year consists of twelve months from March to 14, in this way, the leap month falls on the last month of the year. As long as the original relationship between the year and the month is restored after the calculation is completed, the difficulty will be solved.
Under this arrangement, for a day with month number m and day number d, the corresponding cumulative days can be expressed as:
S(m,d)=S0(m)+d (3<=m<=14, d<=31) (1)
where S0(m) is the cumulative day of day 0 of this month, called the first cumulative day of the month. The day with day number 0 does not exist originally, and it is introduced only for the convenience of calculation. Counting one day before April 1, we get March 31, which is April 0. March 0 should be the last day of April 14 of the previous year. Depending on whether the previous year is a leap year, it is April 29 or 28, and the rest are similar. The following formula can be used to calculate the first day of the month:
S0(m)=Floor(30.6*(m+1))-122 (3<=m<=14) (2)
The symbol Floor in the formula is the floor operation of real numbers. Floor(x) represents the largest integer not greater than the real number x in the brackets. [x] is often used in mathematics books. Since we are going to talk about programming here, we directly borrow the expression of the programming language. The correctness of (2) formula can be verified by the reader. Combining equations (1) and (2), we get the formula for calculating the accumulated day:
S(m,d)=Floor(30.6*(m+1))+d-122 (3<=m<=14, d<=31) (3)
The above thinking process gives us the following inspiration: Similar to the method of calculating the accumulated day, if we can calculate the Julian day J0(y) on March 0 of the year, then the Julian day J(y,m,d) of that day can be expressed as
J(y,m,d)=J0(y)+S(m,d) (4)
The problem boils down to how to calculate the Julian day of the first day of the year.

5. GPS Week

GPS week (hour) is the time system used within the GPS system.
Time zero is defined as the night between January 5, 1980 and the morning of January 6, 1980. The largest time unit is week (week: 604800 seconds).
Representation method: the number of weeks starting from 0:00 on January 6, 1980 plus the number of seconds in the week (the number of seconds starting from the night of Saturday/Sunday every week, for example: GPS week at 0:00:00 on January 6, 1980: Week 0, second 0
GPS week at 10:05:15 on May 1, 2004: Week 1268, second 554715

6. UTC time

UTC is the abbreviation of Universal Time Coordinated, which is a time scale based on seconds and is set and recommended by the International Radio Consultative Committee and maintained by the International Time Bureau (BIH). UTC is equivalent to the mean solar time on the prime meridian (i.e. longitude 0°), and was previously expressed as Greenwich Mean Time (GMT).
So how should UTC be converted to the time around the world? It divides the world into 24 time zones. The 180° east and west longitudes of the earth (a total of 360°) are equally divided by 24 time zones, and each time zone occupies 15°. Taking longitude 0° (i.e. the prime meridian) as the benchmark, the area between 7°30′ east longitude and 7°30′ west longitude is the zero time zone; the areas between 7°30′ and 22°30′ east and west longitude are East Zone 1 and West Zone 1 respectively; and so on. Starting from the zero time zone, the time increases by 1 hour for each additional time zone to the east, and decreases by 1 hour for each additional time zone to the west. UTC is the same as the time at the zero time zone. Taking 0000 UTC on July 15, 2004 (i.e., zero o'clock on July 15, 2004 on the prime meridian) as an example, San Francisco, USA, is located in the West 8th District, 8 hours later than the zero time zone, so the time in San Francisco at this time is 16:00 on July 14, 2004; and Beijing is located in the East 8th District, 8 hours earlier than the zero time zone, so the time in Beijing at this time is 8:00 on July 15, 2004.
According to the International Telecommunication Union (ITU), for the sake of uniformity, a unified time should be used. Unless otherwise specified in international radio communications, UTC should be used and expressed in 4 digits.

I have been a bit wordy, having talked about so many concepts. Next, let's talk about how to handle it in the program. It is still about the problem of my classmate. Convert Gregorian calendar time to GPS week. Based on the above definition, I have thought of two methods:

1. Calculate the number of days between the input date and the GPS start time according to the definition of GPS time. Then you can easily get the number of weeks and days of the GPS input time.

2. First convert the input time into Julian day, and then get the GPS week and day of the input time based on the relationship between Julian day and GPS time.

The key point of these two methods is how to calculate leap years. Once the algorithm is done, the rest is a piece of cake.

The following is an introduction to the calculation of leap years.

Every year divisible by 4 is a leap year.
However, a year divisible by 100 is not a leap year.
However, a year divisible by 400 is still a leap year. See the first function of the GPS week algorithm program for the algorithm.

Some friends may ask if the second algorithm is too long-winded. In fact, the reason why there is a second algorithm is that there are functions for calculating Julian days in the dictation programming language. For example, Delphi provides the DateTimeToJulianDate function. In this way, you can directly use this function to calculate the natural day. This is much simpler than the first algorithm, but I recommend using the first method in languages that do not provide this algorithm.

The following is a method I wrote in VB. I'm just throwing out some ideas. Please don't laugh at me.

Create a VB project and add a button control and 5 text boxes.

The shape is as follows:

Copy the code to the code area and compile it!

''Calculate the number of leap years between the input year and the GPS initial year, because each leap year requires an extra day

Private Function leap(ye As Long) As Long

Dim k As Long

k = 0

Do While (ye >= 1980)

If (ye Mod 4 = 0 And ye Mod 100 <> 0 Or ye Mod 400 = 0) Then

k = k + 1

End If

ye = ye - 1

Loop

leap = k

End Function

Private Function tianshu(ByVal y As Long, ByVal m As Long, ByVal d As Long) As Long

Dim total As Long

total = 0

total = (y - 1980) * 365

total = total + d - 6

If m >= 3 Then

total = total + leap(y)

Else

If y Mod 4 = 0 And y Mod 100 <> 0 Or y Mod 400 = 0 Then

total = total + leap(y) - 1

Else

total = total + leap(y)

End If

End If

m = m - 1

Do While (m >= 1)

If (m = 1 Or m = 3 Or m = 5 Or m = 7 Or m = 8 Or m = 10) Then

total = total + 31

Else

If (m = 4 Or m = 6 Or m = 9 Or m = 11) Then

total = total + 30

Else

total = total + 28

End If

End If

m = m - 1

Loop

tianshu = total

End Function

Private Sub Command1_Click()

Dim year As Long

Dim manth As Long

Dim day As Long

Dim i As Long

i = 0

year = Val(Text1.Text)

i = 0

manth = Val(Text2.Text)

If manth <= 0 Or manth > 12 Then

i = MsgBox("The month entered is illegal", 0 + vbExclamation, "Hint")

End If

day = Val(Text3.Text)

If i <> 1 Then

i = 0

If (manth = 1 Or manth = 3 Or manth = 5 Or manth = 7 Or manth = 8 Or manth = 10 Or manth = 12) Then

If day <= 0 Or day > 31 Then

i = MsgBox("The entered date is illegal", 0 + vbExclamation, "Hint")

Text3.SetFocus

End If

Else

If (manth = 4 Or manth = 6 Or manth = 9 Or manth = 11) Then

If day <= 0 Or day > 30 Then

i = MsgBox("The entered date is illegal", 0 + vbExclamation, "Hint")

Text3.SetFocus

End If

Else

If (year Mod 4 = 0 And year Mod 100 <> 0 Or year Mod 400 = 0) Then

If day <= 0 Or day > 29 Then

i = MsgBox("The entered date is illegal", 0 + vbExclamation, "Hint")

Text3.SetFocus

End If

Else

If day <= 0 Or day > 28 Then

i = MsgBox("The entered date is illegal", 0 + vbExclamation, "Hint")

Text3.SetFocus

End If

End If

End If

End If

Else

Text2.SetFocus

End If

If i <> 1 Then

Text4.Text = tianshu(year, manth, day) \ 7

Text5.Text = tianshu(year, manth, day) Mod 7

End If

End Sub

This post is from Creative Market

Latest reply

Why is there no Chinese lunar calendar? The lunar calendar is the most suitable calendar for China.  Details Published on 2011-2-23 08:17

2125

Posts

0

Resources
2
When calculating the time, I referred to the time conversion format under Linux, which was converted in seconds.
This post is from Creative Market
Personal signature处处留心皆学问!

217

Posts

0

Resources
3
I don't understand VB, it looks so difficult!
This post is from Creative Market

1w

Posts

15

Resources
4
Why is there no Chinese lunar calendar? The lunar calendar is the most suitable calendar for China.
This post is from Creative Market
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr

459

Posts

0

Resources
5
The Linux timestamp format is easy to use. I also use it, but you must pay attention to its 2028 BUG when using it.
This post is from Creative Market

459

Posts

0

Resources
6
Sorry, I got it wrong, it's 2037
This post is from Creative Market

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list