⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otime.cpp

📁 联通接收发送新程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright(C) 2000 by JiangSu Bell Software CO.,LTD. */
/*
****************************************************************************
Content:        Implementation of class OTime
Name:           otime.cpp		        Version: 1.0.0
Created by:     CaoGuiRong       		Date: 2000-08-23
Comment:

All rights reserved
****************************************************************************
*/

#include <sys/types.h>
#include <time.h>

#include "common.h"
#include "otime.h"

extern "C" bool ReadTime( char *pcdate, char *pctime );
//extern bool ReadTime( char*pcdate, char*pctime ) ;
bool IsLeap( const long sy )
{
  bool j = false ;
  if ( sy % 400 == 0 )
    j = true ;
  else if( ( sy % 4 == 0 )&& ( sy % 100 != 0 ) )
    j = true ;
  return j ;
}
/********************************************************************
  Return Value: 1
  Parameters: pointer of date and time
********************************************************************/
void GetLocalTime_CXX ( char* pdate, char*ptime  )
{
  //time_t time(time_t *timer);
  time_t nTime = 0 ;
  time( &nTime ) ;
  struct tm *tm1 = localtime( &nTime ) ;
  sprintf( pdate,"%d%02d%02d",
                    tm1 -> tm_year + 1900,
                    tm1 -> tm_mon+1,
                    tm1 -> tm_mday ) ;
  sprintf( ptime, "%02d%02d%02d",
                    tm1 -> tm_hour,
                    tm1 -> tm_min,
                    tm1 -> tm_sec ) ;
}


/********************************************************************
  Return Value:  no
  Parameters: no
********************************************************************/
OTime::OTime()
{
}
/********************************************************************
  Return Value: no
  Parameters: an existed OTime object
********************************************************************/
OTime::OTime( OTime& otime )
{
  strcpy( date, otime.GetDate() ) ;
  strcpy( time, otime.GetTime() );
}
/********************************************************************
  Return Value:  no
  Parameters: pointer of date and time
********************************************************************/
OTime::OTime( const char* pcdate, const char* pctime)
{
  SetDateTime( pcdate, pctime ) ;
}
/********************************************************************
  Return Value:  no
  Parameters: no
********************************************************************/
OTime::~OTime( )
{
}
/********************************************************************
  Return Value: OTime instance
  Parameters: an existed OTime instance
********************************************************************/
OTime& OTime::operator =( OTime& otime )
{
  SetDateTime( otime.GetDate(), otime.GetTime() ) ;
  return *this;
}
/********************************************************************
  Function Name: SetDateTime
  Return Value:  no
  Parameters: pointer of date and time
********************************************************************/
short OTime::SetDateTime( const char *pcdate, const char* pctime )
{
  int j = 1 ;
  short y = 0 , m = 0 ,d = 0 ;
  char c1,c2 ;
  if( pcdate )
  {
    switch( strlen( pcdate ) )
    {
      case 10:
        sscanf( pcdate,"%04hd%c%02hd%c%02hd",&y,&c1,&m,&c2,&d) ;
        break;
      case  8:
        sscanf( pcdate,"%04hd%02hd%02hd",&y,&m,&d) ;
        break ;
      default:
        ;
    }
    if( j > 0 && m>=1 && m<=12 && d>=1 && d<=31 )
    {
      sprintf( date,"%04hd%02hd%02hd",y,m,d ) ;
    }
  }
  else
  {
    strcpy( date,"00000101" ) ;
  }

  if( j > 0 )
  {
    if( pctime )
    {
      switch( strlen( pctime ) )
      {
        case 8:
          sscanf( pctime,"%02hd%c%02hd%c%02hd",&y,&c1,&m,&c2,&d) ;
          break;
        case 6:
          sscanf( pctime,"%02hd%02hd%02hd",&y,&m,&d) ;
          break ;
        default:
             ;
      }
      if( j > 0 && y>=0 && y<=24 && m>=0 && m<=59 && d>=0 && d<=59 )
      {
        sprintf( time,"%02hd%02hd%02hd",y,m,d ) ;
      }
    }
    else
    {
      strcpy( time,"000000" ) ;
    }
  }
  return j ;
}


/********************************************************************
  Function Name: absday
  Class Name: OTime
  Return Value:  total days from 1970/01/01 base 0
  Parameters: pointer of date
********************************************************************/
/*
long OTime::AbsDay( )
{
  unsigned month_day[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  int d1 = 0, m1 = 0, y1 = 0;
  sscanf( date, "%04d%02d%02d", &y1, &m1, &d1 );
  if ( IsLeap( y1 ) )
    month_day[ 1 ] = 29;
  m1 = ( m1 - 1 ) % 12 + 1;
  d1 = ( d1 - 1 ) % month_day[ m1 - 1 ] + 1;

  unsigned k = ( y1 - 1980 ) * 365;
  for (int  i = 0; i < m1-1; i++ )
    k += month_day[ i ] ;

  k += d1 + ( y1 - 1980 + 3 ) / 4  ;
  return k;
}
*/

long OTime::AbsDay( )
{
  unsigned month_day[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  int d1 = 0, m1 = 0, y1 = 0;
  sscanf( date, "%04d%02d%02d", &y1, &m1, &d1 );
//if ( IsLeap( y1 ) )
  if( y1 % 4 == 0 )
    month_day[ 1 ] = 29;
  m1 = ( m1 - 1 ) % 12 + 1;
  d1 = ( d1 - 1 ) % month_day[ m1 - 1 ] + 1;

  unsigned k = y1 * 365;
  for ( int  i = 0; i < m1-1; i++ )
    k += month_day[ i ] ;

  k += d1 + ( y1 + 3 ) / 4  ;
  return k;
}


/********************************************************************
  Return Value:  total seconds from 00:00:00
  Parameters: pointer of time
********************************************************************/
long OTime::AbsSecond(  )
{
  short sh = 0, sf = 0, ss = 0;
  sscanf( time, "%02hd%02hd%hd", &sh, &sf, &ss );
  return sh * SECOND_OF_HOUR + sf * SECOND_OF_MINUTE + ss ;
}
/********************************************************************
  Return Value:  no
  Parameters: relative days   pointer of date
********************************************************************/
/*
void OTime::AbsToDate( long iabs )
{
  short a_month[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  short yy   = 1980 ;
  long nDays = 366;
  while ( iabs > nDays )
  {
    iabs -= nDays;
    yy   += 1;
    nDays = ( yy % 4 == 0 ) ? 366 : 365;
  }
  a_month[ 1 ] = ( yy % 4 == 0 ) ? 29 : 28;
  short mm = 1;
  for ( int i = 0; i < 12 && iabs > a_month[ i ]; i++ )
  {
    mm += 1;
    iabs -= a_month[ i ];
  }
  short dd = iabs ;
  sprintf( date, "%04hd%02hd%02hd", yy, mm, dd );
}
*/
void OTime::AbsToDate( long iabs )
{
  short a_month[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  long year4, year, day4, day, month ;

  year4 = iabs / 1461 ; //四年
  day4  = iabs % 1461 ;

  if(  day4 == 0 )
  {
    year4 -- ;
    day4  = 1461 ;
  }


/*
  day   = day4       - ( day4 / 365 ) * 365 ;
  year  = year4 * 4  -  day4 / 365  ;
*/
  if( day4 > 1096)
  {
    year = 4 * year4 + 3;
    day  = day4 - 1096;
  }
  else if(day4>731)
  {
    year = 4 * year4 + 2;
    day  = day4-731;
  }
  else if(day4 > 366)
  {
    year = 4 * year4 + 1;
    day  = day4 - 366;
  }
  else
  {
    year = 4 * year4;
    day  = day4;
  }


//  if( IsLeap( year ) == 1 )
  if(  year % 4  == 0 )
    a_month[ 1 ] = 29 ;
  else
    a_month[ 1 ] = 28 ;


  for( month = 0 ; month < 12 ; month ++ )
  {
     if( day - a_month[ month ] <= 0 )
     {
       break ;
     }
     day -= a_month[ month ] ;
  }
  month ++ ;
  sprintf( date,"%04ld%02ld%02ld",year,month,day ) ;
}

/********************************************************************
  Return Value:  no
  Parameters:relative seconds,  pointer of time
********************************************************************/
void OTime::AbsToTime( long iabs )
{
  short sh , sm , ss ;

  sh = iabs / SECOND_OF_HOUR ;

  sm = ( iabs % SECOND_OF_HOUR ) / SECOND_OF_MINUTE ;

  ss = iabs  % SECOND_OF_MINUTE ;

  sprintf( time,"%02hd%02hd%02hd", sh, sm, ss );

}
/********************************************************************
  Return Value: OTime instance
  Parameters:  otimespan instance to be added
********************************************************************/
OTime OTime::operator + ( OTimeSpan otimespan )
{
  long total_day,total_second;

  total_day    = AbsDay( )   + otimespan.GetDay( );
  total_second = AbsSecond() + otimespan.GetSecondinDay( );

  total_day    += total_second / SECOND_OF_DAY;
  total_second  = total_second % SECOND_OF_DAY;

  AbsToDate( total_day );
  AbsToTime( total_second );

  return *this;
}
/********************************************************************
  Return Value:OTime instance
  Parameters: otimespan instance to be minus
********************************************************************/
OTime OTime::operator -( OTimeSpan otimespan )
{
  long total_day,total_second;

  total_day    = AbsDay( )   - otimespan.GetDay( );
  total_second = AbsSecond() - otimespan.GetSecondinDay( );

  while( total_second < 0 )
  {
    total_day -- ;
    total_second += SECOND_OF_DAY ;
  }

  AbsToDate( total_day );
  AbsToTime( total_second );

  return *this;
}
/********************************************************************
  Return Value:otimespan instance
  Parameters: OTime instance to be minus
********************************************************************/
/*
OTimeSpan OTime::operator - ( OTime otime )
{
  static OTimeSpan otimespan ;

  long lday    = AbsDay( )    - otime.AbsDay( ) ;
  long lsecond = AbsSecond( ) - otime.AbsSecond( );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -