Use C ++ to operate all function libraries of time

Source: Internet
Author: User
/*
Name is: time_class.h

Function: UserOperationTimeDate

Author: Zhang Shulin Author: woods. Zhang
Date: 2004-12-15 Date: 2004-12-15
Version: 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)
// ___________ Obtain the current systemTime______________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

If (is_cn = true)
// ___________ Display Chinese characters _____________
{
Sprintf (str_tmp, "% d seconds", PTM->; tm_year + 1900, PTM->; tm_mon + 1, PTM->; tm_mday, PTM->; tm_hour, PTM->; tm_min, PTM->; tm_sec );
Return str_tmp;
}
Else
// _________TimeDate display ___________
{
Sprintf (str_tmp, "% 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)
// ____________ Obtain the system date __________________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );
If (is_cn = true)
// ___________ Display Chinese characters _____________
{
Sprintf (str_tmp, "% d", PTM->; tm_year + 1900, PTM->; tm_mon + 1, PTM->; tm_mday );
Return str_tmp;
}
Else
// _________TimeDate display ___________
{
Sprintf (str_tmp, "% d-% d", PTM-& gt; tm_year + 1900, PTM-& gt; tm_mon + 1, PTM-& gt; tm_mday );
Return str_tmp;
}
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_year ()
// ___________ Obtain the current year ___________________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM-& gt; tm_year + 1900;
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_month ()
// ___________ Obtain the current month ___________________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM->; tm_mon + 1;
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_day ()
// ___________ Obtain the current day ___________________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM->; tm_mday;
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_hour ()
// ___________ Obtain the current hour ___________________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM->; tm_hour;
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_minute ()
// ___________ Obtain the current minute ___________________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM->; tm_min;
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_second ()
// ___________ Obtain the current second ____________________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM->; tm_sec;
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_day_in_year ()
// ___________ Obtain the day of the current year ___________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM->; tm_yday;

}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_week ()
// ___________ Obtain the current week ________ number ____________
{
Time_t rawtime;
Struct TM * PTM;
Char str_tmp [30];

Time (& rawtime );
PTM = localtime (& rawtime );

Return PTM->; tm_wday;
}

/* Optional /*------------------------------------------------------------------------------------------------*/

String get_week (bool is_cn)
// ___________ Obtain the current week ________ letters ___________
{
String week_cn [7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "};
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];
}
}

/* Optional /*------------------------------------------------------------------------------------------------*/

Int get_how_week ()
// _______________ Obtain the week of the year ____________________
{
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 ()
// ___________ Obtain the current number of seconds ________________
{
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 (); // convert to Double Precision
}

};

# Endif

/*
Name is: Time. cpp
Compilation command: G ++-O time. cpp
TimeProcessing functions
Author: Zhang Shulin
Date: 2004-11-22
*/
# Include <iostream>;
# Include "./time_class.h"

Int main ()
{
Time_class TC;
Cout <"SystemTimeYes: "<TC. get_now () <Endl;
Cout <"SystemTimeYes: "<TC. get_now (true) <Endl;
Cout <Endl;

Cout <"system date is:" <TC. get_date () <Endl;
Cout <"system date is:" <TC. get_date (true) <Endl;
Cout <Endl;

Cout <"system (year):" <TC. get_year () <Endl;
Cout <"system (month) is:" <TC. get_month () <Endl;
Cout <"system (day):" <TC. get_day () <Endl;
Cout <Endl;

Cout <"system (time):" <TC. get_hour () <Endl;
Cout <"system (points):" <TC. get_minute () <Endl;
Cout <"system (seconds):" <TC. get_second () <Endl;
Cout <Endl;

Cout <"day of the year:" <TC. get_day_in_year () <Endl;
Cout <Endl;

Cout <"system (week):" <TC. get_week () <Endl;
Cout <"system (week):" <TC. get_week (true) <Endl;
Cout <"system (week):" <TC. get_week (false) <Endl;
Cout <Endl;

Cout <"week of the year:" <TC. get_how_week () <Endl;

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.