C語言UNIXTimeStamp(時間戳)轉年月日時分秒代碼(含時區計算)

代碼的風格有點像java,不夠湊合着用先吧。(不敢列入正確答案,因爲測試不是非常嚴謹)

如果有問題,還請不吝賜教。謝謝。

//設置時間
void parseTimestamp(long unixTimeStamp)
{
  int timezone = 8;                    // 北京時區
  int currentYear = 0;                 // 當前年份
  int currentMonth = 0;                // 當前月份
  int currentDay = 0;                  // 當前日期
  int currentHour = 0;                 // 當前時
  int currentMinute = 0;               // 當前分
  int currentSecond = 0;               // 當前秒
  long secondsPassedInCurrentYear = 0; // 今年已過去了多少秒
  int currentYearIsLeapYear = 0;       // 今年是閏年(0:不是,1:是)

  int daysForMonth_NotALeapYear[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 平年每月的天數
  int daysForMonth_LeapYear[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};     // 閏年每月的天數
  long timestampFor1years_NotALeapYear = 31536000;                                    // 非閏年的一年的時間戳總數   (365*24*60*60)
  long timestampFor4years = 126230400;                                                // 四年的時間戳總數,計算方法:(365+365+365+366)*(24*60*60)
  long secondsInADay = 86400;                                                         // 一天的秒數(24*60*60)

  unixTimeStamp += timezone * 60 * 60;                                         // 修復時區
  int pastYears = ((int)(unixTimeStamp / timestampFor4years)) * 4;             // 獲取過去了多少個四年
  long secondsPassedSinceTheLastLeapYear = unixTimeStamp % timestampFor4years; // 距離上一個閏年過去了多少秒

  currentYear = 1970 + pastYears; //粗計算過了多少年

  //---------------------------------------- 獲取當前的年份 ------------------------------------------
  if (secondsPassedSinceTheLastLeapYear < timestampFor1years_NotALeapYear) //當前爲閏年後第一年
  {
    secondsPassedInCurrentYear = secondsPassedSinceTheLastLeapYear;
  }
  else if (secondsPassedSinceTheLastLeapYear < timestampFor1years_NotALeapYear * 2) //當前爲閏年後第二年
  {
    currentYear += 1;
    secondsPassedInCurrentYear = secondsPassedSinceTheLastLeapYear - timestampFor1years_NotALeapYear;
  }
  else if (secondsPassedSinceTheLastLeapYear < timestampFor1years_NotALeapYear * 3) //當前爲閏年後第三年
  {
    currentYear += 2;
    secondsPassedInCurrentYear = secondsPassedSinceTheLastLeapYear - timestampFor1years_NotALeapYear * 2;
  }
  else //當前爲閏年
  {
    currentYear += 3;
    secondsPassedInCurrentYear = secondsPassedSinceTheLastLeapYear - timestampFor1years_NotALeapYear * 3;
    currentYearIsLeapYear = 1;
  }

  //---------------------------------------- 獲取當前的月份 ------------------------------------------
  //如果當前年爲閏年
  int daysPassedInCurrentYear = 0;               // 假設今年已度過的日子
  int daysPassedInCurrentYear_CompleteMonth = 0; // 完整過完的月份的總日數
  if (currentYearIsLeapYear)
  {
    for (currentMonth = 1; currentMonth <= sizeof(daysForMonth_LeapYear); currentMonth++)
    {
      daysPassedInCurrentYear += daysForMonth_LeapYear[currentMonth - 1];
      if (daysPassedInCurrentYear * secondsInADay >= secondsPassedInCurrentYear)
      {
        daysPassedInCurrentYear_CompleteMonth = daysPassedInCurrentYear - daysForMonth_LeapYear[currentMonth - 1];
        break;
      }
    }
  }
  else
  {
    for (currentMonth = 1; currentMonth <= sizeof(daysForMonth_NotALeapYear); currentMonth++)
    {
      daysPassedInCurrentYear += daysForMonth_NotALeapYear[currentMonth - 1];
      if (daysPassedInCurrentYear * secondsInADay >= secondsPassedInCurrentYear)
      {
        daysPassedInCurrentYear_CompleteMonth = daysPassedInCurrentYear - daysForMonth_NotALeapYear[currentMonth - 1];
        break;
      }
    }
  }

  //---------------------------------------- 獲取當前的日期 ------------------------------------------
  long secondsPassedInCurrentMonth = secondsPassedInCurrentYear - (daysPassedInCurrentYear_CompleteMonth * secondsInADay); //當月已過的秒數
  currentDay = secondsPassedInCurrentMonth / secondsInADay;
  currentDay = currentDay ? currentDay : 1;

  //---------------------------------------- 獲取當前的小時 ------------------------------------------
  long secondsPassedInCurrentDay = secondsPassedInCurrentMonth % secondsInADay;
  currentHour = secondsPassedInCurrentDay / (60 * 60);

  //---------------------------------------- 獲取當前的分鐘 ------------------------------------------
  long secondsPassedInCurrentHour = secondsPassedInCurrentDay % (60 * 60);
  currentMinute = secondsPassedInCurrentHour / 60;

  //---------------------------------------- 獲取當前的秒 ------------------------------------------
  currentSecond = secondsPassedInCurrentHour % 60;

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章