用C++操作時間的所有函數庫

來源:互聯網
上載者:User
 /*
Name is :time_class.h

功能:    使用者操作時間日期

作者:張樹林           Author:woods.zhang
日期:2004-12-15       Date:2004-12-15
版本:1.0.0            Version:1.0.0
Email:hoojar@163.com   QQ:37894354
MSN:hoojar@hotmail.com

*/
#ifndef TIME_CLASS_H_
#define TIME_CLASS_H_

        #include <time.h>;
        #include <string>;

        using namespace std;

        class time_class
        {
             public:

            string get_now(bool is_cn = false)
             //___________擷取當前系統時間______________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               if (is_cn == true)
             //___________以中國漢字顯示_____________
               {
                  sprintf(str_tmp,"%d年%d月%d日 %d時%d分%d秒",ptm->;tm_year + 1900, ptm->;tm_mon +1 , ptm->;tm_mday, ptm->;tm_hour, ptm->;tm_min, ptm->;tm_sec);
                  return str_tmp;
               }
               else
             //_________以時間日期方式顯示___________
               {
                  sprintf(str_tmp,"%d-%d-%d %d:%d:%d",ptm->;tm_year + 1900, ptm->;tm_mon + 1, ptm->;tm_mday, ptm->;tm_hour, ptm->;tm_min, ptm->;tm_sec);
                  return str_tmp;
               }
             }

             string get_date(bool is_cn = false)
             //____________擷取系統日期__________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);
               if (is_cn == true)
               //___________以中國漢字顯示_____________
               {
                  sprintf(str_tmp,"%d年%d月%d日", ptm->;tm_year + 1900, ptm->;tm_mon +1 , ptm->;tm_mday);
                  return str_tmp;
               }
               else
               //_________以時間日期方式顯示___________
               {
                  sprintf(str_tmp,"%d-%d-%d", ptm->;tm_year + 1900, ptm->;tm_mon + 1, ptm->;tm_mday);
                  return str_tmp;
               }
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_year()
             //___________擷取當前年份___________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_year + 1900;
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_month()
             //___________擷取當前月份___________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_mon + 1;
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_day()
             //___________擷取當前日份___________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_mday;
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_hour()
             //___________擷取當前時份___________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_hour;
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_minute()
             //___________擷取當前分鐘___________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_min;
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_second()
             //___________擷取當前秒____________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_sec;
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_day_in_year()
             //___________擷取當前是一年當中的第幾天___________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_yday;

             }

             /*------------------------------------------------------------------------------------------------*/

             int get_week()
             //___________擷取當前是周幾________數字____________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return ptm->;tm_wday;
             }

             /*------------------------------------------------------------------------------------------------*/

             string get_week(bool is_cn)
             //___________擷取當前是周幾________字母___________
             {
               string week_cn[7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
               string week[7] = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               if (is_cn == true)
               {
                   return week_cn[ptm->;tm_wday];
               }
               else
               {
                   return week[ptm->;tm_wday];
               }
             }

             /*------------------------------------------------------------------------------------------------*/

             int get_how_week()
             //_______________擷取本周是一年當中第幾周____________________
             {
               time_t rawtime;
               struct tm* ptm;
               char str_tmp[30];

               time (&rawtime);
               ptm = localtime(&rawtime);

               return int(ptm->;tm_yday / 7) + 1;
             }

             long get_time_second()
             //___________擷取到目前為此的秒數________________
             {
                  string tmp_str;
                  char tmstr[15];
                  sprintf(tmstr, "%dl", time(NULL));
                  tmp_str = tmstr;
                  tmp_str = tmp_str.substr(0,10);
                  return atol(tmp_str.c_str()); //轉換成雙精確度
             }

        };

#endif

/*
Name is :time.cpp
編譯指令:g++ -o time time.cpp
時間處理函數
作者:張樹林
日期:2004-11-22
*/
#include <iostream>;
#include "./time_class.h"

int main ()
{
  time_class tc;
  cout << "系統時間是:" << tc.get_now() << endl;
  cout << "系統時間是:" <<  tc.get_now(true) << endl;
  cout << endl;

  cout << "系統日期是:" << tc.get_date() << endl;
  cout << "系統日期是:" <<  tc.get_date(true) << endl;
  cout << endl;

  cout << "系統(年)是:" << tc.get_year() << endl;
  cout << "系統(月)是:" << tc.get_month() << endl;
  cout << "系統(日)是:" << tc.get_day() << endl;
  cout << endl;

  cout << "系統(時)是:" << tc.get_hour() << endl;
  cout << "系統(分)是:" << tc.get_minute() << endl;
  cout << "系統(秒)是:" << tc.get_second() << endl;
  cout << endl;

  cout << "一年之中的哪天:" << tc.get_day_in_year() << endl;
  cout << endl;

  cout << "系統(周)是:" << tc.get_week() << endl;
  cout << "系統(周)是:" << tc.get_week(true) << endl;
  cout << "系統(周)是:" << tc.get_week(false) << endl;
  cout << endl;

  cout << "一年中第幾周:" << tc.get_how_week() << endl;

  return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.