時間擷取,以及時間格式字串轉化

來源:互聯網
上載者:User

std::string IntToString(int i)
  {
   char tmpBuf[20];
   itoa(i, tmpBuf, 10);
   return std::string(tmpBuf);
  }

 

int StringToInt(std::string str)
  {
   return atoi(str.c_str());
  }

 

 

//將時間轉換為"年-月-日 時:分:秒"字串格式
  std::string TimeToString(tm* time, std::string DaySplite = "-", std::string DayTimeSplite = " ", std::string TimeSplite = ":");

  std::string TimeToString(tm* time, std::string DaySplite, std::string DayTimeSplite, std::string TimeSplite)
  {
   string tStr = IntToString(time->tm_year + 1900);
   tStr += DaySplite;
   if(IntToString(time->tm_mon + 1).length() == 1)
    tStr += "0";
   tStr += IntToString(time->tm_mon + 1);
   tStr += DaySplite;
   if(IntToString(time->tm_mday).length() == 1)
    tStr += "0";
   tStr += IntToString(time->tm_mday);
   tStr += DayTimeSplite;
   if(IntToString(time->tm_hour).length() == 1)
    tStr += "0";
   tStr += IntToString(time->tm_hour);
   tStr += TimeSplite;
   if(IntToString(time->tm_min).length() == 1)
    tStr += "0";
   tStr += IntToString(time->tm_min);
   tStr += TimeSplite;
   if(IntToString(time->tm_sec).length() == 1)
    tStr += "0";
   tStr += IntToString(time->tm_sec);
   return tStr;
  }

 

//將格林威治時間(1900.1.1起到現在秒數)轉化為"年-月-日 時:分:秒"的格式
  string _API TimeToString(time_t max);

 

string TimeToString(time_t max)
  {
   time_t value = max;
   tm* time = localtime(&value);
   string tStr = TimeToString(time,"-"," ",":");
   return tStr;
  }
  //將"年-月-日 時:分:秒"格式的時間轉化為格林威治時間(1900.1.1起到現在秒數)
  time_t ConvertMyTimeToTimet(string strTime);

 

time_t ConvertMyTimeToTimet(string strTime)
  {
   tm t;
   memset(&t, 0, sizeof(t));
   t.tm_year = StringToInt(strTime.substr(0,4)) - 1900;
   t.tm_mon =  StringToInt(strTime.substr(5,2)) - 1;
   t.tm_mday = StringToInt(strTime.substr(8,2));
   t.tm_hour = StringToInt(strTime.substr(11,2));
   t.tm_min = StringToInt(strTime.substr(14,2));
   t.tm_sec = StringToInt(strTime.substr(17,2));
   return _mktime64(&t);
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.