Convert TLE Epoch Time To UTC Time - GitMasterNikanjam/C_WiKi GitHub Wiki

Convert TLE time to UTC Juilian date

Suppose a satellite that has epoch time equal "24058.89656339", it is mean how much time passed since January 1, 1970 in seconds.
In this example, the code convert TLE epoch time to UTC time.

// Convert TLE time to UTC Juilian date.

// The code provided already converts the epoch time to UTC time. The gmtime function converts the time to UTC time. 
// The resulting tm structure represents Coordinated Universal Time (UTC).
// This code correctly extracts the year and the days elapsed in the year ...
// from the epoch time and then calculates the seconds since January 1, 1970

// We'll calculate the number of days in each year, considering leap years, and then convert those days into seconds.
/*
In this code:

We define a function isLeapYear to check if a given year is a leap year.
We define a function daysInYear to calculate the number of days in a given year, accounting for leap years.
We calculate the total number of seconds since January 1, 1970, by iterating over each year and adding the appropriate number of seconds for each day.
We add the seconds for the remaining days in the current year.
We then convert this total number of seconds to a time_t value representing the Unix epoch time.
Finally, we convert this time_t value to a tm structure representing UTC time and format it as desired.
*/

#include <iostream>
#include <ctime>

bool isLeapYear(int year) {
    return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}

int daysInYear(int year) {
    return isLeapYear(year) ? 366 : 365;
}

int main() {
    // TLE data
    double epoch_time = 24058.89656339; // Epoch time in days

    // Extract year and days
    int year = static_cast<int>(epoch_time / 1000); // First two digits represent the year since 2000.
    double days_elapsed = epoch_time - year * 1000; // Remaining digits represent the days elapsed in the year

    year += 2000;       // Add 2000 to correct year.

    // Calculate seconds since January 1, 1970
    const int seconds_in_day = 86400;
    int total_seconds = 0;
    for (int y = 1970; y < year; ++y) {
        total_seconds += daysInYear(y) * seconds_in_day;
    }
    total_seconds += static_cast<int>(days_elapsed * seconds_in_day);

    // Convert to struct tm
    time_t unix_epoch = total_seconds;
    struct tm * timeinfo;
    timeinfo = gmtime(&unix_epoch);

    // Format the time
    char buffer[80];

    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);

    // Output
    std::cout << "Converted time: " << buffer << std::endl;


        // Get current system time
    time_t current_time = time(nullptr);

    timeinfo = localtime(&current_time);

    // Convert struct tm to an array of doubles
    double t[6];
    t[0] = timeinfo->tm_year + 1900; // Year
    t[1] = timeinfo->tm_mon + 1;      // Month (January is 0)
    t[2] = timeinfo->tm_mday;         // Day of the month
    t[3] = timeinfo->tm_hour;         // Hour
    t[4] = timeinfo->tm_min;          // Minute
    t[5] = timeinfo->tm_sec;          // Second

    // Output the result
    std::cout << "Year: " << t[0] << std::endl;
    std::cout << "Month: " << t[1] << std::endl;
    std::cout << "Day: " << t[2] << std::endl;
    std::cout << "Hour: " << t[3] << std::endl;
    std::cout << "Minute: " << t[4] << std::endl;
    std::cout << "Second: " << t[5] << std::endl;

    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️