📌  相关文章
📜  将Unix时间戳转换为DD / MM / YYYY HH:MM:SS格式

📅  最后修改于: 2021-06-01 01:06:34             🧑  作者: Mango

给定时间点的Unix时间戳记T (以秒为单位),任务是将其转换为人类可读的格式(DD / MM / YYYY HH:MM:SS)

例子:

方法:

  1. 将给定的秒数除以一天中的秒数即可将其转换为天(86400),然后存储剩余的秒数。
  2. 由于我们计算的是自1970年1月1日以来的天数。因此,在计算当前年份时要牢记leap年的概念。从1970年开始。如果年份是a年,则从天中减去366,否则减去365。将年份增加1。
  3. 重复步骤2,直到天数少于365(不能构成一年)。
  4. 将1加到剩余天数(计算年份后的额外天数),因为剩余天数会给我们直到前一天的天数,并且我们必须包括当前日期以进行DATE和MONTH计算。
  5. 将月份数增加1,并从额外的天数中减去该月的天数(请记住,February年2月将有29天,否则将有28天)。
  6. 重复步骤5,直到从多余的天中减去一个月中的某几天将得到否定的结果。
  7. 如果多余的天数大于零,则将月份增加1。
  8. 现在,利用从第1步开始的额外时间。
  9. 用多余的时间除以3600来计算小时,用剩余的秒除以60来计算分钟,而将是剩余的秒。

下面是上述方法的实现:

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 
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 currrent 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 currrent 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 currrent 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 currrent 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


输出:
23/7/2020 9:52:36


想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”