題 關於在查找選項-amin,-atime,-cmin,-ctime中使用小數+ n,-n或n的困惑


我不確定我是否理解這種方式 find 使用其中一個謂詞查找時忽略小數部分。對於例如我用了

$ find . -ctime .2 # files status changed exactly 4.8 hours ago ? 
$ find . -ctime +.2 # files status changed more than 4.8 hours ago ?
$ find . -ctime -.2 # files status changed less than 4.8 hours ago ?

總是有不同的輸出。如何計算小時數?


3
2018-05-18 15:02


起源


不是一個編程問題。 - Konerak


答案:


你可能會更好 -cmin 和 -mmin 謂詞


是的,它將執行插值子日和子時間:

parser.c,第3243行

/* Get a timestamp and comparison type.

   STR is the ASCII representation.
   Set *NUM_DAYS to the number of days/minutes/whatever, taken as being
   relative to ORIGIN (usually the current moment or midnight).
   Thus the sense of the comparison type appears to be reversed.
   Set *COMP_TYPE to the kind of comparison that is requested.
   Issue OVERFLOWMESSAGE if overflow occurs.
   Return true if all okay, false if input error.

   Used by -atime, -ctime and -mtime (parsers) to
   get the appropriate information for a time predicate processor. */

static boolean
get_relative_timestamp (const char *str,
            struct time_val *result,
            struct timespec origin,
            double sec_per_unit,
            const char *overflowmessage)
{
  double offset, seconds, nanosec;
  static const long nanosec_per_sec = 1000000000;

  if (get_comp_type(&str, &result->kind))
    {
      /* Invert the sense of the comparison */
      switch (result->kind)
    {
    case COMP_LT: result->kind = COMP_GT; break;
    case COMP_GT: result->kind = COMP_LT; break;
    default: break;
    }

      /* Convert the ASCII number into floating-point. */
      if (xstrtod(str, NULL, &offset, strtod))
    {
      /* Separate the floating point number the user specified
       * (which is a number of days, or minutes, etc) into an
       * integral number of seconds (SECONDS) and a fraction (NANOSEC).
       */
      nanosec = modf(offset * sec_per_unit, &seconds);
      nanosec *= 1.0e9; /* convert from fractional seconds to ns. */
      assert (nanosec < nanosec_per_sec);

      /* Perform the subtraction, and then check for overflow.
       * On systems where signed aritmetic overflow does not
       * wrap, this check may be unreliable.   The C standard
       * does not require this approach to work, but I am aware
       * of no platforms where it fails.
       */
      result->ts.tv_sec  = origin.tv_sec - seconds;
      if ((origin.tv_sec < result->ts.tv_sec) != (seconds < 0))
        {
          /* an overflow has occurred. */
          error (1, 0, overflowmessage, str);
        }

      result->ts.tv_nsec = origin.tv_nsec - nanosec;
      if (origin.tv_nsec < nanosec)
        {
          /* Perform a carry operation */
          result->ts.tv_nsec += nanosec_per_sec;
          result->ts.tv_sec  -= 1;
        }
      return true;
    }
      else
    {
      /* Conversion from ASCII to double failed. */
      return false;
    }
    }
  else
    {
      return false;
    }
}

pred.c,第237行:

/* Returns ts1 - ts2 */
static double ts_difference(struct timespec ts1,
                struct timespec ts2)
{
  double d =  difftime(ts1.tv_sec, ts2.tv_sec)
    + (1.0e-9 * (ts1.tv_nsec - ts2.tv_nsec));
  return d;
}

1
2018-05-18 15:22



“這不是一個編程問題,”但我們得到了一個編程答案...... - Felipe Alvarez