AcePerl

 view release on metacpan or  search on metacpan

acelib/timesubs.c  view on Meta::CPAN

----------------------------------------------------------------------
  written by  D.Wolf@dkfz-heidelberg.de  Thu May  5 17:39:54 MDT 1994 
  tested ok on 
  OSF1  V1.3 111 alpha
  IRIX  4.0.5F 08280217 IP12
  SunOS 5.3 Generic sun4c sparc (Solaris)
  SunOS 4.1.2 2 sun4c with gcc version 2.3.3
  -- does not compile with /usr/ucb/cc on SunOS 4.1.2
*/


static mytime_t aceTime(struct tm *tm, 
			BOOL wantMonth, BOOL wantDay, BOOL wantHours,
			BOOL wantMins, BOOL wantSecs)
{ 
  mytime_t t = 0;

  if (tm->tm_year < 91) /* use timeless format */
    { 
      t |= tm->tm_year << 9;
      if (wantMonth)
	t |= (tm->tm_mon + 1) << 5;
      if (wantDay)
	t |= tm->tm_mday;
    }
  else
    {
      if (wantSecs)
	t |= 1 + tm->tm_sec;

      if (wantMins)
	t |= (tm->tm_min + 1) << 6;

      if (wantHours)
	t |= (tm->tm_hour + 1) << 12;
      
      if (wantDay)
	t |= tm->tm_mday << 17;
      
      if (wantMonth)
	t |= (tm->tm_mon + 1) << 22;
      
      t |= (tm->tm_year - 90) << 26; 
    }
  return t;
}
  
static void timeStruct(struct tm *tm, mytime_t t,
		BOOL *wantMonth, BOOL *wantDay, BOOL *wantHours,
		BOOL *wantMins, BOOL *wantSecs)
{
  unsigned int secs;
  unsigned int mins;
  unsigned int hours;
  unsigned int day;
  unsigned int month;
  unsigned int year;

  if (!t)
    {
      /* fprintf (stderr, "timeStruct() warning: received null t\n"); */
      tm->tm_year = 0;
      tm->tm_mon = 0;
      tm->tm_mday = 0;
      tm->tm_hour = 0;
      tm->tm_min = 0;
      tm->tm_sec = 0;
      tm->tm_wday = 0;
      tm->tm_yday = 0;
      tm->tm_isdst = -1;
      return;
    }

  secs = t & 0x3f;
  mins = (t >> 6) & 0x3f;
  hours = (t >> 12) & 0x1f;
  day = (t >> 17) & 0x1f;
  month = ( t >> 22) & 0xf;
  year = ( t >> 26) &0x3f;
  
  if (year == 0) /* before 1990, use time-less format. */
    { 
      secs = mins = hours = 0;
      day = t & 0x1f;
      month = (t >> 5) & 0x0f;
      year = (t >> 9) & 0x7f;
    }
  else 
    year += 90; 
  
  tm->tm_year = year;
  
  if (month == 0)
    { *wantMonth  = FALSE;
      tm->tm_mon = 0;
    }
  else
    { *wantMonth = TRUE;
      tm->tm_mon = month - 1;
    }
  
  if (day == 0)
    { *wantDay = FALSE;
      tm->tm_mday = 1;
    }
  else
    { *wantDay = TRUE;
      tm->tm_mday = day;
    }
  
  if (hours == 0)
    { *wantHours = FALSE;
      tm->tm_hour = 0;
    }
  else 
    { *wantHours = TRUE;
      tm->tm_hour = hours - 1;
    }

  if (mins == 0)
    { *wantMins = FALSE;

acelib/timesubs.c  view on Meta::CPAN

  cp += n ;
  if ((v = sscanf (cp, "-%d%n", &ts.tm_mday, &n)) != 1)
    goto done ;
  if (ts.tm_mday > 31)
    return 0;
  wantDay = TRUE;
  cp += n ;
  if (*cp == 0)
    goto done ;
  if (*cp != '_' && *cp != ' ') /* separator */
    return 0;
  ++cp ;
  if ((v = sscanf (cp, "%d%n", &ts.tm_hour, &n)) != 1)
    goto done ;
  if (ts.tm_hour > 23)
    return 0;
  wantHours = TRUE;
  ts.tm_min = 0;
  ts.tm_sec = 0;
  cp += n ;
  if ((v = sscanf (cp, ":%d%n", &ts.tm_min, &n)) != 1)
    goto done ;
  if (ts.tm_min > 59)
    return 0;
  wantMins = TRUE;
  cp += n ;
  if ((v = sscanf (cp, ":%d%n", &ts.tm_sec, &n)) != 1)
    goto done ;
  if (ts.tm_sec > 59)
    return 0;
  wantSecs = TRUE;
  cp += n ;

 done:
  if (*cp) return 0;	/* incomplete */
   
  if (ts.tm_year < 1900)	/* convert into 4 digit-year */
    { if (ts.tm_year > 50) 
	ts.tm_year += 1900 ;
      else               
	ts.tm_year += 2000 ;
    } 
  
  ts.tm_year -= 1900 ;
  ts.tm_mon-- ;			/* January is 0 */
  
  return aceTime(&ts, wantMonth, wantDay, wantHours, wantMins, wantSecs) ;
}

/**********************************************/

char *timeShowJava (mytime_t t) 
{
  static char ace_time[25] ;
  struct tm ts;
  BOOL wantMonth, wantDay, wantHours, wantMins, wantSecs;


  if (!t)
    {
      /*   fprintf(stderr, "timeShowJava() warning: received NULL value\n"); */
      return "" ;
    }

  timeStruct(&ts, t, &wantMonth, &wantDay, &wantHours, &wantMins, &wantSecs);
  if (!wantMonth)
    strftime (ace_time, 25, "01 JAN %Y 00:00:00", &ts) ;
  else if (!wantDay)
    strftime (ace_time, 25, "01 %b %Y 00:00:00", &ts) ;
  else if (!wantHours)
    strftime (ace_time, 25, "%d %b %Y 00:00:00", &ts) ;
  else if (!wantMins)
    strftime(ace_time, 25, "%d %b %Y %H:00:00", &ts);
  else if (!wantSecs)
    strftime (ace_time, 25, "%d %b %Y %R:00", &ts);
  else
    strftime (ace_time, 25, "%d %b %Y %T", &ts) ;
    
  return ace_time ;
}

/**********************************************/

char *timeShow (mytime_t t) 
{
  static char ace_time[25] ;
  struct tm ts;
  BOOL wantMonth, wantDay, wantHours, wantMins, wantSecs;

  if (!t)
    {
      /*   fprintf(stderr, "timeShow() warning: received NULL value\n"); */
      return "" ;
    }

  timeStruct(&ts, t, &wantMonth, &wantDay, &wantHours, &wantMins, &wantSecs);
  if (!wantMonth)
    strftime (ace_time, 25, "%Y", &ts) ;
  else if (!wantDay)
    strftime (ace_time, 25, "%Y-%m", &ts) ;
  else if (!wantHours)
    strftime (ace_time, 25, "%Y-%m-%d", &ts) ;
  else if (!wantMins)
    strftime(ace_time, 25, "%Y-%m-%d_%H", &ts);
  else if (!wantSecs)
    strftime (ace_time, 25, "%Y-%m-%d_%R", &ts);
  else
    strftime (ace_time, 25, "%Y-%m-%d_%T", &ts) ;
    
  return ace_time ;
}

/**********************************************/

BOOL timeDiffSecs (mytime_t t1, mytime_t t2, int *diff) 
{
  struct tm ts1, ts2;
  BOOL wantMonth1, wantDay1, wantHours1, wantMins1, wantSecs1;
  BOOL wantMonth2, wantDay2, wantHours2, wantMins2, wantSecs2;
  double d ;
  time_t tt1, tt2 ;
  timeStruct (&ts1, t1, &wantMonth1, &wantDay1, &wantHours1, &wantMins1, &wantSecs1) ;
  timeStruct (&ts2, t2, &wantMonth2, &wantDay2, &wantHours2, &wantMins2, &wantSecs2) ;

  if (!wantSecs1 || !wantSecs2)
    return FALSE ;
  tt1 = mktime (&ts1) ;
  tt2 = mktime (&ts2) ;
  d = difftime (tt2, tt1) ;
  /*  d = difftime (mktime (&ts2), mktime (&ts1)) ;*/
  *diff = (int)d ;
  return TRUE ;
}

/**********************************************/

BOOL timeDiffMins (mytime_t t1, mytime_t t2, int *diff) 
{
  struct tm ts1, ts2;
  BOOL wantMonth1, wantDay1, wantHours1, wantMins1, wantSecs1;
  BOOL wantMonth2, wantDay2, wantHours2, wantMins2, wantSecs2;
  double d;

  timeStruct (&ts1, t1, &wantMonth1, &wantDay1, &wantHours1, &wantMins1, &wantSecs1) ;
  timeStruct (&ts2, t2, &wantMonth2, &wantDay2, &wantHours2, &wantMins2, &wantSecs2) ;

  if (!wantMins1 || !wantMins2)
    return FALSE ;

  ts1.tm_sec = ts2.tm_sec = 0 ;

  d = difftime (mktime (&ts2), mktime (&ts1)) ;



( run in 4.368 seconds using v1.01-cache-2.11-cpan-81fc1098f69 )