把自1970年1月1日以来的秒数转化成年月日

#include <iostream>

using namespace std;

bool IsRound(int year){

 if((year%100)&&(year%4==0)) return 1;

 if((year%100==0)&&(year%400==0)) return 1;

 return 0;

}

int main() {

 int n;

 int year_s[2]= {365*24*60*60, 366*24*60*60};

 int month_s[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},

   {31,29,31,30,31,30,31,31,30,31,30,31}};

 int day_s = 24*60*60;

 int hour_s = 60*60;

 int minute_s = 60;

 

 while(cin>>n){

    int temp = n;

    int year=1970;

         int month=1;

         int day=1;

         int hour=0;

         int minute=0;

         int second=0;

    while(temp>=60){

     int flag= IsRound(year);

     if(temp>=year_s[flag]) { year++; temp-=year_s[flag]; }

     else if(temp>=day_s){

      int days = temp/day_s;

      temp=temp%day_s;

      int i=0;

      int flag = IsRound(year);

      int hh=31;

      while(days>=hh){

       days-=month_s[flag][i++];

       hh=month_s[flag][i];

       }

      month+= i;

      day+=days;

     }else if(temp>=hour_s){

          hour=temp/hour_s;

          temp%=hour_s;

     }else if(temp>=minute_s){

           minute = temp/minute_s;

           temp%=minute_s;

     }

     }

     second = temp;

        cout<<year<<"-";

      if(month/10==0) cout<<"0";

      cout<<month<<"-";

      if(day/10==0) cout<<"0";

      cout<<day<<" ";

      if(hour/10==0) cout<<"0";

      cout<<hour<<":";

      if(minute/10==0) cout<<"0";

      cout<<minute<<":";

      if(second/10==0) cout<<"0";

      cout<<second<<endl;

 }

 return 0;

}

时间: 2024-08-19 21:29:12

把自1970年1月1日以来的秒数转化成年月日的相关文章

日期类的时间从为什么是从1970年1月1日(格林威治时间)

I suspect that Java was born and raised on a UNIX system.UNIX considers the epoch (when did time begin) to be midnight, January 1, 1970.是说java起源于UNIX系统,而UNIX认为1970年1月1日0点是时间纪元. 但这依然没很好的解释"为什么",出于好奇,继续Google,总算找到了答案: http://en.wikipedia.org/wiki/

为什么编程时间从1970年1月1日開始?

查看原文:http://www.ibloger.net/article/136.html 最初计算机操作系统是32位,而时间也是用32位表示. System.out.println(Integer.MAX_VALUE); 2147483647 Integer在JAVA内用32位表示,因此32位能表示的最大值是2147483647. 另外1年365天的总秒数是31536000, 2147483647/31536000 = 68.1 也就是说32位能表示的最长时间是68年.而实际上到2038年01月

java为啥计算时间从1970年1月1日开始

http://www.myexception.cn/program/1494616.html ---------------------------------------------------------- java为什么计算时间从1970年1月1日开始 今天在看Python  API 时,看到 time 模块 : The epoch is the point where the time starts. On January 1st of that year, at 0 hours,the

为什么编程语言以及数据库要从1970年1月1日开始计算时

今天在看Python  API时,看到time模块: The epoch is the point where the time starts. On January 1st of that year, at 0 hours,the “time since the epoch” is zero. For Unix, the epoch is 1970. To find out what the epoch is, look at gmtime(0). 定义time从1970年1月1日开始,忽然想

判断距离1970年1月1日的天数

#include <stdlib.h> /****************************************************************** 功能: 输入年月日,计算距离1970年1月1日的天数 输入: 年月日,输入年份范围[1970,2100],输入年月日的有效性需要判断 输出: DaysSince1970:距离1970年1月1日的天数 已知 1970年1月1日为星期四 异常时,输出不需要关注 返回: 1 上班 0 休假 -1 表示异常 根据国家规定:周一到

Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数

格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: 1 /** 2 * 获取指定时间到格林威治时间的秒数 3 * UTC:格林威治时间1970年01月01日00时00分00秒(UTC+8北京时间1970年01月01日08时00分00秒) 4 * @param time 5 * @return 6 */ 7 public static long diffSeconds(String time){

为什么编程时间从1970年1月1日开始?

最初计算机操作系统是32位,而时间也是用32位表示. System.out.println(Integer.MAX_VALUE); 2147483647 Integer在JAVA内用32位表示,因此32位能表示的最大值是2147483647.另外1年365天的总秒数是31536000, 2147483647/31536000 = 68.1 也就是说32位能表示的最长时间是68年,而实际上到2038年01月19日03时14分07秒,便会到达最大时间,过了这个时间点,所有32位操作系统时间便会变为

从1970年1月1日00:00:00 GMT以来此时间对象表示的毫秒数转化为Datetime

将Long类型转换为DateTime类型 /// <summary> /// 将Long类型转换为DateTime类型 /// </summary> /// <param name="d">long</param> /// <returns></returns> public static DateTime ConvertLongDateTime(long ticks) { DateTime dtBase = ne

格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数。

(* Delphi获取13位格林治时间实现方法, 与java中的java.lang.System.currentTimeMillis()效果相同 *) var SysTime: TSystemTime; begin GetSystemTime(SysTime); // 方法1 Memo1.Lines.Add(FormatFloat('#', CompToDouble(TimeStampToMSecs( DateTimeToTimeStamp(SystemTimeToDateTime(SysTim