Converting Linux/Unix Time to Human-Readable Date FormatThe Unix time period also referred to simply as the "epoch period", the "POSIX period", or the "Unix period", is a time tracking system that represents the number of seconds that have elapsed since the Unix period of the Unix period started on February 1, 1970, at 00:00. 00:00 UTC is defined. Unix Time begins at midnight on January 1, 1970, UTC. It counts the total number of seconds that have passed since then. For example, the Unix timestamp of 1716620142 represents the number of seconds that have elapsed since January 1, 1970. The Unix epoch period has been largely calculated for various purposes, including file systems, databases, and network protocols, because it is simple and provides a consistent way of dealing with dates and seasons. The Unix epoch time does not account for leap seconds. It assumes that each day is exactly 86400 seconds long, simplifying the calculations but introducing some errors over time. It provides consistent measurement time in systems. Using a single integer value to represent time simplifies time calculations and comparisons. Transitions between Unix time and human-readable dates can be performed using Linux Bash Commands and programming languages. For example, the date command can be used on Unix-like systems, and most programming languages provide libraries to handle such conversions. Using Command in Linux/UnixIn Linux, you can utilize the date command to convert the Linux time to a human-readable format. Open the terminal and run the below-given command to generate a Unix timestamp: Command Output Now, to convert it into a human-readable format, run the below command: Command: Here, input the generated timestamp from the earlier command. Like this, Output: In Mac OSIn macOS, the date command syntax is a little different. Use the below command to generate a timestamp: Command Output To convert into human-readable format, execute the below command Command Replace the <timestamp> with the generated timestamp. Output Converting Seconds into DateTime Format
ExampleLet's understand by an example. Here, we have taken an example of 1716547702 seconds. Total Seconds = 1716547702 Total Seconds in a Day: 86400 Convert to Days: Total Days = 1716547702 / 86400 = 19867 Remaining Seconds: 1716547702 % 86400 = 41102 Hours, Minutes and Seconds 1 Hour = 3600 Seconds 1 Minute = 60 Seconds Hours: 41200 / 3600 = 11 Remaining Seconds: 41102 % 3600 = 4102 Minutes = 4102 / 60 = 68 Seconds = 4102 % 60 = 2 Determine Year:Starting from 1970, subtract the number of days in each year until the total days fit into the current year. Calculation of Leap and Non-Leap years:
This process will continue until the year is determined. Month and Day:Identify days in each month and subtract days for each month. Calculation of Month and Day January: 31 Days February: 28 Days March: 31 Days This process will continue until the month is determined. Calculate the time of day:Convert the remaining seconds into hours, minutes and seconds. Hours = 11 Minutes = 68 Seconds: 2 Implementation of the Above Approach Using JavascriptWe have used the approach in the JavaScript we saw above here. Let's see the program. Code Output: Human-Readable DateTime: 2024-05-24 10:48:22 Explanation The 'convertUnixToReadable' function in the above JS code snippet converts the Unix timestamp to a human-readable date and time format. Next, the code starts by using a 'Date' object to convert the Unix timestamp from seconds to milliseconds. The program then extracts the year, month, day, hours, minutes and seconds from the 'Date' object and inserts leading zeros where needed to ensure that each element is two digits and then these parts are 'YYYY-'. MM-DD HH:MM :. it is formatted as a string in SS format and returned. Using PythonHere is the complete Python code in which we have implemented the epoch approach. Code Output: Human-Readable DateTime Format: 2024-05-24 10:48:22 Explanation In the above Python code, first calculate all the days, hours, minutes, and seconds of a given Unix timestamp and then repeat since 1970 to account for overages. Subtract the days until the remaining days are less than the days of the current year. Subtract the days in each month from the remaining days, and the month and day are calculated by adjusting the excess years of the account. Finally, the calculated year, month, day, hour, minute, and second are formatted into an easy-to-read string and printed as a result. Using JavaHere is the complete Java code in which we have implemented the epoch approach. Code Output: Human Readable DateTime: 2024-05-24 16:18:22 Explanation The provided Java code converts Unix timestamps to a human-readable date-time format by defining the first timestamp using 'Instant.ofEpochSecond' with the system's default timestamp as a 'LocalDateTime' object. It then converts this 'LocalDateTime' object to yyyy-MM-dd HH:mm:. It formats human-readable to a string by using 'DateTimeFormatter' with its counterpart ss'. Finally, it prints an array of date-time strings. Using C++Here is the complete C++ code in which we have implemented the epoch approach. Code Output: Human Readable Date-Time: 2024-05-24 10:48:22 Explanation In the above code, define the first timestamp and change it to 'std::chrono::system_clock::time_point'. Then, this time point is 'std::time_t' to use with 'std::gmtime'. They are converted and formatted to a string readable by 'std::put_time' to specify the date and time in UTC. Using CHere is the complete C code in which we have implemented the epoch approach. Code Output: Human Readable Date-Time: 2024-05-24 10:48:22 Explanation In the above C code, defining the first timestamp and using the 'gmtime' function to modify a standing 'struct tm' for UTC time and then 'strftime to get the "YYYY- MM-DD HH:MM:SS" format Formats this time to the string used '. Using C#Here is the complete C# code in which we have implemented the epoch approach. Code Output: Human Readable Date-Time: 2024-05-24 10:48:22 Explanation In the given C# code, define a timestamp and convert it to a 'DateTimeOffset' using the 'FromUnixTimeSeconds' method and then setting this 'DateTimeOffset' go to a string with the format "yyyy- MM-dd HH:mm:ss". Additionally, the code manually calculates all days, hours, minutes, and seconds from the Unix timestamp, including the remaining hours and minutes when the days and hours are divided Finally, it uses a date-time string listed with a detailed breakdown of the timestamps, including components. Using PHPHere is the complete C# code in which we have implemented the epoch approach. Code Output: Human Readable Date-Time: 2024-05-24 10:48:22 Explanation In the above PHP code, we have converted the Unix timestamp to a human-readable date-time format using an interpreter. It then converts it to a 'DateTime' object using the 'DateTime' constructor, which uses a Unix timestamp set to " . @" overrides the face and then inverts this 'DateTime' against "Y-m-d H:i:s." It formats the string in format. ConclusionConverting Linux/Unix time to a human-readable date format is necessary for defining timestamps representing seconds since January 1, 1970. This conversion is also necessary for analyzing logs, viewing file types, and creating time-dependent data-driven applications. This ensures that Temporal information is accessible and meaningful and facilitates efficient debugging, data analysis, and communication. By converting Unix time to a standard date and time format, we make the data more meaningful and actionable for users and programs. |