C++ exercise: time class

C++ exercise: time class

#include <iostream>
#include <time.h>
using namespace std;
class Date {
    
    
  private:                 //私有成员,只能在类内函数调用,外部不能直接调用
    int year, month, day,hour;
    void setsystemdate();

  public:                    //公有成员,主函数通过公有成员来对其私有成员进行修改
    void init(int,int,int,int);
    void print_ymd();
    void print_mdy();
    int get_year(){
    
    return year;};
    int get_month(){
    
    return month;};
    int get_day(){
    
    return day;}
    int get_hour(){
    
    return hour;};
    bool Isleapyear();
};

void Date::setsystemdate(){
    
      //不符合条件时的构造函数
   tm *gm;
   time_t t=time(NULL); //秒数,time_t为长整型
   gm=localtime(&t);
   //gmtime为格林尼治时间,localtime为当地时间
   year=1900+gm->tm_hour;
   month=gm->tm_mon+1;
   day=gm->tm_mday;
   hour=gm->tm_hour;
}
void Date::init(int yy,int mm,int dd,int hh){
    
     //构造函数
    if(yy>=1900&&yy<=9999) year=yy;
    else{
    
    
        setsystemdate();
        return;
    }
    if(mm>=1&&mm<=12) month==mm;
    else{
    
    
        setsystemdate();
        return;
    }
    if(dd>=1&&dd<=31) day=dd;
    else{
    
    
        setsystemdate();
        return;
    }
    if(hh>=1&&hh<=24) hour=hh;
    else{
    
    
        setsystemdate();
        return;
    }
}
void Date::print_ymd(){
    
    
    cout<<year<<"-"<<month<<"-"<<day<<"-"<<hour<<endl;
}
void Date::print_mdy(){
    
    
    cout<<month<<"-"<<day<<"-"<<year<<endl;
}
bool Date::Isleapyear(){
    
    
    if(year%400==0||(year%100!=0&&year%4==0)) return true;
    else return false;
}
int main(){
    
    
    Date date1;
    date1.init(2008,13,8,20);
    date1.print_ymd();
    if(date1.Isleapyear()) cout<<date1.get_year()<<"leapyear"<<endl;
    else cout<<date1.get_year()<<"notleapyear"<<endl;
    return 0;
}

time.h file function

Function name: time
Function prototype: time_t time(time_t *timer)
Function: Get the current calendar time of the
system Function return: The current calendar time of the system, if the current calendar time cannot be obtained, return -1
Parameter description: Get when timer=NULL The calendar time of the machine. When the timer is a valid pointer, update the timer to the current time of the system, and time_t is a long type.
File: <time.h>

#include<time.h>
#include<stdio.h>
#include<dos.h>
int main()
{
    
    
    time_t t;
    t = time(NULL);//默认1970-1-1
    printf("The number of seconds since January1, 1970 is %ld", t);
    return 0;
}

Function name: localtime
Function prototype: struct tm *localtime(const time_t *timer)
Function function: Return a machine time information expressed in tm structure
Function return: Time expressed in tm structure, structure tm is defined as follows:

#ifndef _TM_DEFINED
  struct tm {
    
    
  int tm_sec; /* 秒 – 取值区间为[0,59] */
  int tm_min; /* 分 - 取值区间为[0,59] */
  int tm_hour; /* 时 - 取值区间为[0,23] */
  int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
  int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
  int tm_year; /* 年份,其值等于实际年份减去1900 */
  int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
  int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
  int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
  };
  #define _TM_DEFINED
  #endif

Parameter description: timer-the machine time obtained by using the time() function
File: <time.h>

#include<time.h>
#include<stdio.h>
#include<dos.h>
int main()
{
    
    
    time_t timer;
    struct tm *tblock;
    timer = time(NULL);
    tblock = localtime(&timer);
    printf("Local time is: %s", asctime(tblock));
    return 0;
}

Function name: gmtime
function prototype: struct tm *gmtime(time_t *time)
function function: get the time information represented by the structure tm
function return: the time information pointer represented by the structure tm
Parameter description: time-obtained by the function time() Time information
File: <time.h>

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
char*tzstr="TZ=PST8PDT";
int main()
{
    
    
    time_t t;
    struct tm *gmt,*area;
    putenv(tzstr);
    tzset();
    t = time(NULL);
    area = localtime(&t);
    printf("Local time is: %s", asctime(area));
    gmt = gmtime(&t);
    printf("GMT is: %s", asctime(gmt));
    return 0;
}

Function name: asctime
Function prototype: char* asctime(struct tm * ptr)
Function function: Get machine time (date and time converted to ASCII code)
Function return: The format of the returned time string is: week, month, day, hour: minute :Second, year
Parameter description: The structure pointer ptr should be obtained through the functions localtime() and gmtime()

#include<stdio.h>
#include<time.h>
int main()
{
    
    
    time_t t;
    time(&t);
    tm *gm;
    gm=localtime(&t);
    printf("Today's date and time: %s", asctime(gm));
    return 0;
}

Function name: ctime
Function prototype: char *ctime(const time_t *time)
Function: Get calendar time
Function return: Return string format: week, month, day, hour: minute: second, year.
Parameter description: time-this parameter Should be obtained by the function time
Owning file: <time.h>

#include<stdio.h>
#include<time.h>
int main()
{
    
    
    time_t t;
    time(&t);
    printf("Today's date and time: %s", ctime(&t));
    return 0;
}

Function name: difftime
Function prototype: double difftime(time_t time2, time_t time1)
Function: Get the time difference between two machines, in seconds.
Function return: Time difference, in seconds.
Parameter description: time1-machine time one, time2-machine time two
.This parameter should be obtained using the time function. File: <time.h>

#include<time.h>
#include<stdio.h>
#include<windows.h>
int main()
{
    
    
    time_t start, end;
    system("cls");//清屏
    time(&start);
    Sleep(5000);//等待5秒,Sleep()函数包含在windows.h的头文件里,以毫秒为单位
    time(&end);
    printf("The difference is: %f seconds", difftime(end, start));
    return 0;
}

Function name: tzset
Function prototype: void tzset(void)
Function function: UNIX compatible function, used to get the time zone, no use in DOS environment
Function return:
Parameter description:
Owned file: <time.h>

#include<time.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
    
    
    time_t td;
    putenv("TZ=PST8PDT");
    tzset();
    time(&td);
    printf("Current time = %s", asctime(localtime(&td)));
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_40602655/article/details/113704424