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

📄 utc.c

📁 汉字在计算机内存放分为两种情况
💻 C
📖 第 1 页 / 共 2 页
字号:
  INT32  underflow;
  UINT32 result;

  underflow = 0;

  hours   = ((((lhs & 0xF000) >> 12) * 10) + ((lhs & 0x0F00) >> 8) - 
             (((rhs & 0xF000) >> 12) * 10) - ((rhs & 0x0F00) >> 8));
  minutes = ((((lhs & 0x00F0) >> 4) * 10) + (lhs & 0x000F) - 
             (((rhs & 0x00F0) >> 4) * 10) - (rhs & 0x000F));

  /* Check for underflows */
  if (minutes < 0)
  {
    hours--;
    minutes += 60;
  }
  if (hours < 0)
  {
    underflow--;
    hours += 24;
  }

  /*
  ** Hours digits.
  */
  result  = (UINT32)(underflow << 16);
  result |= (UINT32)(((hours / 10) & 0xF) << 12);
  result |= (UINT32)(((hours % 10) & 0xF) << 8);

  /*
  ** Minutes digits.
  */
  result |= (UINT32)(((minutes / 10) & 0xF) << 4);
  result |= (UINT32) ((minutes % 10) & 0xF);

  return result;
}

/****************************************************************************
** Function     UTCSetDateAndTime
**
** Description  Called whenever a new time is extracted from the TOT/TDT.
**              Stores the new time, applies any offset, updates the display 
**              and informs the application of the new time.
**              
** Input        UINT16 mjd - Modified Julian Date extracted from the TOT/TDT
**
**              UINT16 utc - BCD Time extracted from the TOT/TDT
**
** Modified     None
**
** Return       Nothing
**              
*****************************************************************************/
extern UINT32 MainTaskQueue;
void UTCSetDateAndTime( UINT16 mjd, UINT16 utc )
{
  UINT32     result;
  T_FTA_Event response;

#if (UTC_DEBUG==TRUE)
  Print( "UTCSetDateAndTime( %X, %X )\n", mjd, utc );
#endif

  if( utc != UTCCurrentTime )
    {
      /* Update the local storage of the current UTC */
      UTCCurrentTime = utc;
      UTCCurrentMJD  = mjd;

      /* Add or subtract the offset */
      result = UTCApplyOffset( utc );
      if( (result >> 16) != 0 )
        {
          mjd += (result >> 16);
        }
      
      /* Send message to the application */
      response.code   = FTA_TIME_UPDATE;
      response.param1 = utc;    
      response.param2 = FTA_TIME_UPDATE;    
      
      DTOS_DRV_SendMessage( MainTaskQueue, &response);
    }
}

/****************************************************************************
** Function     UTCSetCountryOffset
**
** Description  Called whenever a new offset is extracted from the TOT. If 
**              the country code matches one of the user requested countries
**              then the offset is stored, otherwise it is discarded.
**              
** Input        UINT16 countryCode - The country code that the offset applies
**                                   to. This is extracted from the TOT.
**
**              UINT8 regionID     - The regionID of the country (if any)
**
**              UINT8 offset       - The offset extracted from the TOT in BCD
**
**              UINT8 negative     - Whether the offset is positive or 
**                                   negative.
**
** Modified     None
**
** Return       Nothing
**              
*****************************************************************************/
void UTCSetCountryOffset( UINT16 countryCode, UINT8 regionID, 
                          UINT16 offset, UINT8 negative )
{
  T_UTCOffset *userOffset;
  UINT32       i;
  UINT16       currentUTC;

#if (UTC_DEBUG==TRUE)
  Print( "UTCSetCountryOffset(%d, %d, %X, %d)\n",
         countryCode, regionID, offset, negative );
  Print( "Offset Country codes: %d, %d, %d\n",
         UTCOffsets[0].countryCode,
         UTCOffsets[1].countryCode,
         UTCOffsets[2].countryCode );
#endif
  
  userOffset = UTCOffsets;
  i          = UTC_MAX_OFFSETS;  
  do
    {
      if( userOffset->countryCode == countryCode )
        {
          /* Store the offset */
          userOffset->offset   = offset;
          userOffset->negative = negative;
          
          /* Update the current offset to point to this offset. */
          UTCCurrentOffset = userOffset;
          break;
        }
      else if( userOffset->countryCode == 0xFFFF )
        {
          break;
        }
      userOffset++;
    } while( --i );

  currentUTC = UTCCurrentTime;

  /* In order to force the new offset to take effect we invalidate the current time */
  UTCCurrentTime = 0xFFFF;

  /* Update the current time with the new offset */
  UTCSetDateAndTime( UTCCurrentMJD, currentUTC );
}


/****************************************************************************
** Function     UTCSetCountryCode
**
** Description  Specify which country codes to use in order of preference.
**              
** Input        UINT32 level        - Which level to replace (0 highest, 2 
**                                    lowest).
**
**              UINT16 countryCode  - The new country code to use.
**
**              UINT8 regionID      - The new region ID to use.
**
** Modified     None
**
** Return       Nothing
**              
*****************************************************************************/
void UTCSetCountryCode( UINT32 level, UINT16 countryCode, UINT8 regionID )
{
  if( level > UTC_MAX_OFFSETS )
    return;
  
  UTCOffsets[level].countryCode = countryCode;
  UTCOffsets[level].regionID    = regionID;
}

/****************************************************************************
** Function     UTCGetCountryCode
**
** Description  Get the current country code and offset for the specified 
**              user preference.
**              
** Input        UINT32 level - Which user perference to get
**
** Modified     UINT16 *countryCode - The current country code.
**
**              UINT8  *regionID    - The current region ID
**
**              UINT16 *offset      - The current offset in BCD (0xFFFF 
**                                    implies that the offset has not be 
**                                    obtained from the TOT)
**
**              UINT8  *negative    - Whether the offset is negative or 
**                                    positive
**
** Return       Nothing
**              
*****************************************************************************/
void UTCGetCountryCode( UINT32 level, UINT16 *countryCode, UINT8 *regionID,
                        UINT16 *offset, UINT8 *negative )
{
  if( level > UTC_MAX_OFFSETS )
    return;
  
  *countryCode = UTCOffsets[level].countryCode;
  *regionID    = UTCOffsets[level].regionID;
  *offset      = UTCOffsets[level].offset;
  *negative    = UTCOffsets[level].negative;
}

/****************************************************************************
** Function     UTCApplyOffset
**
** Description  Takes the current time and applies (adds or subtracts) the 
**              current offset.
**              
** Input        UINT16 utc - BCD time to apply the offset to
**
** Modified     None
**
** Return       UINT32 - The new time.
**              
*****************************************************************************/
UINT32 UTCApplyOffset( UINT16 utc )
{
  UINT32 result;

  result = utc;
  if( UTCCurrentOffset != NULL )
    {
      if( UTCCurrentOffset->offset != 0 )
        {
          if( UTCCurrentOffset->negative == TRUE )
            {
              result = UTCSubtract( utc, UTCCurrentOffset->offset );
            }
          else
            {
              result = UTCAdd( utc, UTCCurrentOffset->offset );
            }
        }
    }
  
  return result;
}

INT32 UTCGetCurrentOffset(void)
{
	INT32 result;
	if(UTCCurrentOffset->negative == TRUE)
		result = -UTCCurrentOffset->offset;
	else result = UTCCurrentOffset->offset;

	return result;
}

/****************************************************************************
** Function     UTCConvertDateToString
**
** Description  Converts the requested MJD into a string of the specified 
**              format. Currently supported formats are:
**     
**              UTC_DDD_DD_MMM  - "Mon, 2 Aug"
**              UTC_DDDD        - "Monday"
**              
** Input        UINT16 MJD    - The date to convert
**
**              UINT32 format - The output format
**
** Modified     UINT8 *string - Pointer to the output string. Note there must 
**                              be enough space available in the string.
**
** Return       UINT32 - The number of characters written to string
**              
*****************************************************************************/
static const UINT8 dddString[8][7] = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天" };
static const UINT8 MMDD[2][3] = {"月","日"};
UINT32 UTCConvertDateToString( UINT16 MJD, UINT32 format, UINT8 *string )
{
  UINT32 weekDay, day, month;
  UINT32 length;
  UINT32 i,j;
  const UINT8 *srcPtr;


  /*
  ** The following algorithm was developed by Peter Baum.
  ** See http://www.capecod.net/~pbaum/date/date0.htm for more details.
  */
  UINT32 Z    = MJD - 1721118 + 2400000;
  UINT32 H    = (100 * Z) - 25;
  UINT32 A    = H / 3652425;
  UINT32 B    = A - (A >> 2);
  UINT32 year = (100 * B + H) / 36525;
  UINT32 C    = B + Z - (365 * year) - (year >> 2);
  month       = ((535 * C) + 48950) >> 14;
  day         = C - (153 * month - 457) / 5;
  
  if( month > 12 )
    {
      /* Year not used      year++; */
      month -= 12;
    }

  weekDay = ((MJD + 2) % 7) + 1;

  length = 0;  

  /* Validate results before accessing array */
  if( weekDay > 7 || 
      month > 12 )
  {
    *string = '\0';      
    return length;
  }

  switch( format )
  {
    case UTC_DDD_DD_MMM:   
      /* Month */
      if ( (i = (month / 10)) != 0)
      {
        *string++ = (UINT8)(i + '0');
        month      -= (i * 10);
        length++;
      }
      *string++ = (UINT8)(month + '0');
	  *string++ = MMDD[0][0]; 
	  *string++ = MMDD[0][1]; 

      /* Date */
      if ( (i = (day / 10)) != 0)
      {
        *string++ = (UINT8)(i + '0');
        day      -= (i * 10);
        length++;
      }
      *string++ = (UINT8)(day + '0');
	  *string++ = MMDD[1][0]; 
	  *string++ = MMDD[1][1]; 
      
	  *string++ = ' ';
      /* Day */
	  for(i = 0;i < 6;i++)
		  *string++ = dddString[weekDay][i];

      length += 13;
      break;

    case UTC_DDDD:
      i      = 6;
      srcPtr = dddString[weekDay];
      length = i;
      do
      {
        *string++ = *srcPtr++;
      } while( --i );
      break;
  }

  *string = '\0';  

  return(length);
}  

INT32 epgConvertUTCTimeToMinutes(UINT32 time)
{
  INT16  days;
  INT32  hours;
  INT32  minutes;

  days = (INT16)((time & 0xFFFF0000) >> 16);
  
  hours = (((time & 0xF000) >> 12) * 10) + 
           ((time & 0x0F00) >> 8);

  minutes = ( (((time & 0x00F0) >> 4) * 10) + (time & 0x000F) +
              (hours * 60) +
              (days  * EPG_NUMBER_MINUTES_PER_DAY) );

  return (minutes);
}  

void epgConvertMinutesToEPGTime( INT32 time, UINT16 MJD, T_EPGTime *newTime )
{
  newTime->MJD  = MJD;
  if( time < 0 )
  {
    newTime->min = (UINT16)(time + EPG_NUMBER_MINUTES_PER_DAY);
    newTime->MJD--;
  }
  else if( time >= EPG_NUMBER_MINUTES_PER_DAY )
  {
    newTime->min = (UINT16)(time - EPG_NUMBER_MINUTES_PER_DAY);
    newTime->MJD++;
  }
  else
  {
    newTime->min = (UINT16)time;
  }
}  
void epgConvertMinutesToString( INT32 min, UINT8 *buffer )
{
  UINT32 hours = (min / 60);
  
  hours %= 24;
  min = (min % 60);

  *buffer++ = (UINT8)((hours / 10) + '0');
  *buffer++ = (UINT8)((hours % 10) + '0');
  *buffer++ = ':';
  *buffer++ = (UINT8)((min / 10)   + '0');
  *buffer++ = (UINT8)((min % 10)   + '0');
  *buffer   = '\0';  
  
}  

⌨️ 快捷键说明

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