Skyaz123 / imagesnw

0 stars 0 forks source link

juliandate conversion #4

Open Skyaz123 opened 12 months ago

Skyaz123 commented 12 months ago
def date_str_to_julian(date_str):
    year, month, day = map(int, date_str.split('-'))

    def is_leap_year(year):
        return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    if is_leap_year(year):
        days_in_month[1] = 29

    julian_day = day
    for m in range(1, month):
        julian_day += days_in_month[m - 1]

    for y in range(4713, year):
        julian_day += 366 if is_leap_year(y) else 365

    julian_date = int(f"{year}{julian_day:03d}")
    return julian_date

# Example of using the function with a date from the database
date_from_database = "2023-07-11"
julian_date = date_str_to_julian(date_from_database)
print("Julian date for", date_from_database, "is", julian_date)