用C++操作时间的所有函数库

/*
Name is :time_class.h

功能: 用户操作时间日期

作者:张树林 Author:woods.zhang
日期:2004-12-15 Date:2004-12-15
版本:1.0.0 Version:1.0.0
Email:[email protected] QQ:37894354
MSN:[email protected]

*/
#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;
}

你可能感兴趣的:(C++)