时间和日期迭代

时间迭代一秒,分钟、小时、天进行变化

DateAndTime.h

#ifndef DATEANDTIME_H
#define DATEANDTIME_H

class DateAndTime 
{
public:
   DateAndTime( int = 1, int = 1, int = 2000, 
      int = 0, int = 0, int = 0 ); // default constructor
   void setDate( int, int, int ); // set month, day, year
   void setMonth( int ); // set month
   void setDay( int ); // set day   
   void setYear( int ); // set year
   void nextDay(); // next day
   void setTime( int, int, int ); // set hour, minute, second
   void setHour( int ); // set hour
   void setMinute( int ); // set minute 
   void setSecond( int ); // set second
   void tick(); // tick function 
   int getMonth(); // get month
   int getDay(); // get day
   int getYear(); // get year
   int getHour(); // get hour
   int getMinute(); // get minute
   int getSecond(); // get second 
   void printStandard(); // print standard time
   void printUniversal(); // print universal time
private:
   int month; // 1-12 
   int day; // 1-31 (except February(leap year), April, June, Sept, Nov)
   int year; // 2000+
   int hour; // 0-23 (24 hour clock format)
   int minute; // 0-59 
   int second; // 0-59
   bool leapYear(); // leap year
   int monthDays(); // days in month 
}; // end class DateAndTime

#endif

DateAndTime.cpp

#include <iostream> 
#include "DateAndTime.h" // include definition of class DateAndTime
using namespace std;

DateAndTime::DateAndTime(  
    int m, int d, int y, int hr, int min, int sec )
{
   setDate( m, d, y ); // sets date
   setTime( hr, min, sec ); // sets time
} // end DateAndTime constructor

void DateAndTime::setDate( int mo, int dy, int yr )
{   
   setMonth( mo ); // invokes function setMonth
   setDay( dy ); // invokes function setday
   setYear( yr ); // invokes function setYear 
} // end function setDate

void DateAndTime::setDay( int d )
{
   if ( month == 2 && leapYear() )
      day = ( d <= 29 && d >= 1 ) ? d : 1;
   else
      day = ( d <= monthDays() && d >= 1 ) ? d : 1;
} // end function setDay

void DateAndTime::setMonth( int m ) 
{ 
   month = m <= 12 && m >= 1 ? m : 1; // sets month
} // end function setMonth

void DateAndTime::setYear( int y ) 
{ 
   year = y >= 2000 ? y : 2000; // sets year
} // end function setYear

void DateAndTime::nextDay()
{
   setDay( day + 1 ); // increments day by 1

   if ( day == 1 ) 
   {
      setMonth( month + 1 ); // increments month by 1

      if ( month == 1 )
         setYear( year + 1 ); // increments year by 1
   } // end if statement 
} //end function nextDay

void DateAndTime::setTime( int hr, int min, int sec )
{
   setHour( hr ); // invokes function setHour
   setMinute( min ); // invokes function setMinute
   setSecond( sec ); // invokes function setSecond
} // end function setTime

void DateAndTime::setHour( int h ) 
{ 
   hour = ( h >= 0 && h < 24 ) ? h : 0; // sets hour 
} // end function setHour

void DateAndTime::setMinute( int m ) 
{ 
   minute = ( m >= 0 && m < 60 ) ? m : 0; // sets minute
} // end function setMinute

void DateAndTime::setSecond( int s ) 
{ 
   second = ( s >= 0 && s < 60 ) ? s : 0; // sets second
} // end function setSecond

void DateAndTime::tick()
{
   setSecond( second + 1 ); // increments second by 1

   if ( second == 0 ) 
   {
      setMinute( minute + 1 ); // increments minute by 1

      if ( minute == 0 ) 
      {
         setHour( hour + 1 ); // increments hour by 1

         if ( hour == 0 )
            nextDay(); // increments day by 1
      } // end if 
   } // end if 
} // end function tick

int DateAndTime::getDay() 
{ 
   return day; 
} // end function getDay

int DateAndTime::getMonth()
{
   return month; 
} // end function getMonth

int DateAndTime::getYear() 
{ 
   return year; 
} // end function getYear

int DateAndTime::getHour() 
{ 
   return hour; 
} // end function getHour

int DateAndTime::getMinute() 
{ 
   return minute; 
} // end function getMinute

int DateAndTime::getSecond() 
{ 
   return second; 
} // end function getSecond

void DateAndTime::printStandard()
{
   cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':'
      << ( minute < 10 ? "0" : "" ) << minute << ':'
      << ( second < 10 ? "0" : "" ) << second
      << ( hour < 12 ? " AM " : " PM " )
      << month << '-' << day << '-' << year << endl;
} // end function printStandard

void DateAndTime::printUniversal()
{
   cout << ( hour < 10 ? "0" : "" ) << hour << ':'
      << ( minute < 10 ? "0" : "" ) << minute << ':'
      << ( second < 10 ? "0" : "" ) << second << "    "
      << month << '-' << day << '-' << year << endl;
} // end function printUniversal

bool DateAndTime::leapYear()
{
   if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
      return true; // is a leap year
   else
      return false; // is not a leap year
} // end function leapYear

int DateAndTime::monthDays()
{
   const int days[ 12 ] = { 
      31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   return ( month == 2 && leapYear() ) ? 29 : days[ ( month - 1 ) ];
} // end function monthDays

Main

#include <iostream> 
#include "DateAndTime.h" // include definitions of class DateAndTime
using namespace std;

int main()
{
   const int MAXTICKS = 30;
   DateAndTime d( 12, 31, 2004, 23, 59, 57 ); // instantiates object d 
                                              // of class DateAndTime

   for ( int ticks = 1; ticks <= MAXTICKS; ticks++ ) 
   {
      cout << "Universal time: ";
      d.printUniversal(); // invokes function printUniversal
      cout << "Standard  time: ";
      d.printStandard(); // invokes function printStandard
      d.tick(); // invokes function tick            
   } // end for

   cout << endl;
} // end main 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值