Dada una marca de tiempo T de Unix (en segundos) para un punto dado en el tiempo, la tarea es convertirlo a un formato legible por humanos (DD/MM/YYYY HH:MM:SS)
Ejemplo:
Entrada: T = 1595497956
Salida: 23/7/2020 9:52:36
Explicación: En tiempo unix T tiene 1595497956 segundos, por lo que hace un total de 50 años, 7 meses, 23 días y 9 horas, 52 minutos y 36 segundos.Entrada: T = 345234235
Salida: 12/9/1980 18:23:55
Acercarse:
- Convierta los segundos dados en días dividiéndolos por la cantidad de segundos en un día (86400) y almacene el segundo restante.
- Dado que contamos el número de días desde el 1 de enero de 1970. Por lo tanto, para calcular el año actual tenga en cuenta el concepto de años bisiestos. Comience en 1970. Si el año es un año bisiesto, reste 366 a los días; de lo contrario, reste 365. Aumente el año en 1.
- Repita el paso 2 hasta que los días sean menos de 365 (no puede constituir un año).
- Agregue 1 a los días restantes (días adicionales después de calcular el año) porque los días restantes nos darán días hasta el día anterior, y tenemos que incluir el día actual para el cálculo de FECHA y MES.
- Incremente el número de meses en 1 y siga restando el número de días del mes de los días adicionales (teniendo en cuenta que febrero tendrá 29 días en un año bisiesto y 28 en caso contrario).
- Repita los pasos 5 hasta que restar los días del mes de los días adicionales dé un resultado negativo.
- Si los días adicionales son más de cero, incremente el mes en 1.
- Ahora aproveche el tiempo extra del paso 1.
- Calcula las horas dividiendo el tiempo extra por 3600, los minutos dividiendo los segundos restantes por 60, y los segundos serán los segundos restantes.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach // Unix time is in seconds and // Humar Readable Format: // DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS, // Start of unix time:01 Jan 1970, 00:00:00 #include <bits/stdc++.h> using namespace std; // Function to convert unix time to // Human readable format string unixTimeToHumanReadable(long int seconds) { // Save the time in Human // readable format string ans = ""; // Number of days in month // in normal year int daysOfMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; long int currYear, daysTillNow, extraTime, extraDays, index, date, month, hours, minutes, secondss, flag = 0; // Calculate total days unix time T daysTillNow = seconds / (24 * 60 * 60); extraTime = seconds % (24 * 60 * 60); currYear = 1970; // Calculating current year while (daysTillNow >= 365) { if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) { daysTillNow -= 366; } else { daysTillNow -= 365; } currYear += 1; } // Updating extradays because it // will give days till previous day // and we have include current day extraDays = daysTillNow + 1; if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) flag = 1; // Calculating MONTH and DATE month = 0, index = 0; if (flag == 1) { while (true) { if (index == 1) { if (extraDays - 29 < 0) break; month += 1; extraDays -= 29; } else { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; } index += 1; } } else { while (true) { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; index += 1; } } // Current Month if (extraDays > 0) { month += 1; date = extraDays; } else { if (month == 2 && flag == 1) date = 29; else { date = daysOfMonth[month - 1]; } } // Calculating HH:MM:YYYY hours = extraTime / 3600; minutes = (extraTime % 3600) / 60; secondss = (extraTime % 3600) % 60; ans += to_string(date); ans += "/"; ans += to_string(month); ans += "/"; ans += to_string(currYear); ans += " "; ans += to_string(hours); ans += ":"; ans += to_string(minutes); ans += ":"; ans += to_string(secondss); // Return the time return ans; } // Driver Code int main() { // Given unix time long int T = 1595497956; // Function call to convert unix // time to human read able string ans = unixTimeToHumanReadable(T); // Print time in format // DD:MM:YYYY:HH:MM:SS cout << ans << "\n"; return 0; }
Java
// Java program for the above approach // Unix time is in seconds and // Humar Readable Format: // DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS, // Start of unix time:01 Jan 1970, 00:00:00 import java.util.*; class GFG{ // Function to convert unix time to // Human readable format static String unixTimeToHumanReadable(int seconds) { // Save the time in Human // readable format String ans = ""; // Number of days in month // in normal year int daysOfMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int currYear, daysTillNow, extraTime, extraDays, index, date, month, hours, minutes, secondss, flag = 0; // Calculate total days unix time T daysTillNow = seconds / (24 * 60 * 60); extraTime = seconds % (24 * 60 * 60); currYear = 1970; // Calculating current year while (daysTillNow >= 365) { if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) { daysTillNow -= 366; } else { daysTillNow -= 365; } currYear += 1; } // Updating extradays because it // will give days till previous day // and we have include current day extraDays = daysTillNow + 1; if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) flag = 1; // Calculating MONTH and DATE month = 0; index = 0; if (flag == 1) { while (true) { if (index == 1) { if (extraDays - 29 < 0) break; month += 1; extraDays -= 29; } else { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; } index += 1; } } else { while (true) { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; index += 1; } } // Current Month if (extraDays > 0) { month += 1; date = extraDays; } else { if (month == 2 && flag == 1) date = 29; else { date = daysOfMonth[month - 1]; } } // Calculating HH:MM:YYYY hours = extraTime / 3600; minutes = (extraTime % 3600) / 60; secondss = (extraTime % 3600) % 60; ans += String.valueOf(date); ans += "/"; ans += String.valueOf(month); ans += "/"; ans += String.valueOf(currYear); ans += " "; ans += String.valueOf(hours); ans += ":"; ans += String.valueOf(minutes); ans += ":"; ans += String.valueOf(secondss); // Return the time return ans; } // Driver Code public static void main(String[] args) { // Given unix time int T = 1595497956; // Function call to convert unix // time to human read able String ans = unixTimeToHumanReadable(T); // Print time in format // DD:MM:YYYY:HH:MM:SS System.out.print(ans + "\n"); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach # Unix time is in seconds and # Humar Readable Format: # DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS, # Start of unix time:01 Jan 1970, 00:00:00 # Function to convert unix time to # Human readable format def unixTimeToHumanReadable(seconds): # Save the time in Human # readable format ans = "" # Number of days in month # in normal year daysOfMonth = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] (currYear, daysTillNow, extraTime, extraDays, index, date, month, hours, minutes, secondss, flag) = ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) # Calculate total days unix time T daysTillNow = seconds // (24 * 60 * 60) extraTime = seconds % (24 * 60 * 60) currYear = 1970 # Calculating current year while (daysTillNow >= 365): if (currYear % 400 == 0 or (currYear % 4 == 0 and currYear % 100 != 0)): daysTillNow -= 366 else: daysTillNow -= 365 currYear += 1 # Updating extradays because it # will give days till previous day # and we have include current day extraDays = daysTillNow + 1 if (currYear % 400 == 0 or (currYear % 4 == 0 and currYear % 100 != 0)): flag = 1 # Calculating MONTH and DATE month = 0 index = 0 if (flag == 1): while (True): if (index == 1): if (extraDays - 29 < 0): break month += 1 extraDays -= 29 else: if (extraDays - daysOfMonth[index] < 0): break month += 1 extraDays -= daysOfMonth[index] index += 1 else: while (True): if (extraDays - daysOfMonth[index] < 0): break month += 1 extraDays -= daysOfMonth[index] index += 1 # Current Month if (extraDays > 0): month += 1 date = extraDays else: if (month == 2 and flag == 1): date = 29 else: date = daysOfMonth[month - 1] # Calculating HH:MM:YYYY hours = extraTime // 3600 minutes = (extraTime % 3600) // 60 secondss = (extraTime % 3600) % 60 ans += str(date) ans += "/" ans += str(month) ans += "/" ans += str(currYear) ans += " " ans += str(hours) ans += ":" ans += str(minutes) ans += ":" ans += str(secondss) # Return the time return ans # Driver code if __name__=="__main__": # Given unix time T = 1595497956 # Function call to convert unix # time to human read able ans = unixTimeToHumanReadable(T) # Print time in format # DD:MM:YYYY:HH:MM:SS print(ans) # This code is contributed by rutvik_56
C#
// C# program for the above approach // Unix time is in seconds and // Humar Readable Format: // DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS, // Start of unix time:01 Jan 1970, 00:00:00 using System; class GFG{ // Function to convert unix time to // Human readable format static String unixTimeToHumanReadable(int seconds) { // Save the time in Human // readable format String ans = ""; // Number of days in month // in normal year int []daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int currYear, daysTillNow, extraTime, extraDays, index, date, month, hours, minutes, secondss, flag = 0; // Calculate total days unix time T daysTillNow = seconds / (24 * 60 * 60); extraTime = seconds % (24 * 60 * 60); currYear = 1970; // Calculating current year while (daysTillNow >= 365) { if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) { daysTillNow -= 366; } else { daysTillNow -= 365; } currYear += 1; } // Updating extradays because it // will give days till previous day // and we have include current day extraDays = daysTillNow + 1; if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) flag = 1; // Calculating MONTH and DATE month = 0; index = 0; if (flag == 1) { while (true) { if (index == 1) { if (extraDays - 29 < 0) break; month += 1; extraDays -= 29; } else { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; } index += 1; } } else { while (true) { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; index += 1; } } // Current Month if (extraDays > 0) { month += 1; date = extraDays; } else { if (month == 2 && flag == 1) date = 29; else { date = daysOfMonth[month - 1]; } } // Calculating HH:MM:YYYY hours = extraTime / 3600; minutes = (extraTime % 3600) / 60; secondss = (extraTime % 3600) % 60; ans += String.Join("", date); ans += "/"; ans += String.Join("", month); ans += "/"; ans += String.Join("", currYear); ans += " "; ans += String.Join("", hours); ans += ":"; ans += String.Join("", minutes); ans += ":"; ans += String.Join("", secondss); // Return the time return ans; } // Driver Code public static void Main(String[] args) { // Given unix time int T = 1595497956; // Function call to convert unix // time to human read able String ans = unixTimeToHumanReadable(T); // Print time in format // DD:MM:YYYY:HH:MM:SS Console.Write(ans + "\n"); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript program for the above approach // Unix time is in seconds and // Humar Readable Format: // DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS, // Start of unix time:01 Jan 1970, 00:00:00 // Function to convert unix time to // Human readable format function unixTimeToHumanReadable(seconds) { // Save the time in Human // readable format let ans = ""; // Number of days in month // in normal year let daysOfMonth = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; let currYear, daysTillNow, extraTime, extraDays, index, date, month, hours, minutes, secondss, flag = 0; // Calculate total days unix time T daysTillNow = parseInt(seconds / (24 * 60 * 60), 10); extraTime = seconds % (24 * 60 * 60); currYear = 1970; // Calculating current year while (daysTillNow >= 365) { if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) { daysTillNow -= 366; } else { daysTillNow -= 365; } currYear += 1; } // Updating extradays because it // will give days till previous day // and we have include current day extraDays = daysTillNow + 1; if (currYear % 400 == 0 || (currYear % 4 == 0 && currYear % 100 != 0)) flag = 1; // Calculating MONTH and DATE month = 0; index = 0; if (flag == 1) { while (true) { if (index == 1) { if (extraDays - 29 < 0) break; month += 1; extraDays -= 29; } else { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; } index += 1; } } else { while (true) { if (extraDays - daysOfMonth[index] < 0) { break; } month += 1; extraDays -= daysOfMonth[index]; index += 1; } } // Current Month if (extraDays > 0) { month += 1; date = extraDays; } else { if (month == 2 && flag == 1) date = 29; else { date = daysOfMonth[month - 1]; } } // Calculating HH:MM:YYYY hours = parseInt(extraTime / 3600, 10); minutes = parseInt((extraTime % 3600) / 60, 10); secondss = parseInt((extraTime % 3600) % 60, 10); ans += date.toString(); ans += "/"; ans += month.toString(); ans += "/"; ans += currYear.toString(); ans += " "; ans += hours.toString(); ans += ":"; ans += minutes.toString(); ans += ":"; ans += secondss.toString(); // Return the time return ans; } // Given unix time let T = 1595497956; // Function call to convert unix // time to human read able let ans = unixTimeToHumanReadable(T); // Print time in format // DD:MM:YYYY:HH:MM:SS document.write(ans + "</br>"); // This code is contributed by decode2207. </script>
Producción:
23/7/2020 9:52:36
Publicación traducida automáticamente
Artículo escrito por AshutoshShisodia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA