Ignore:
Timestamp:
Sep 25, 2007, 7:57:57 AM (18 years ago)
Author:
bird
Message:

Added $(date ) and $(date-utc ) function as simple wrappers around strftime.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/function.c

    r991 r1121  
    26732673#endif
    26742674
     2675#ifdef CONFIG_WITH_DATE
     2676static char *
     2677func_date (char *o, char **argv, const char *funcname)
     2678{
     2679  char *buf;
     2680  size_t buf_size;
     2681  time_t tval;
     2682  const char *format = !strcmp (funcname, "date-utc")
     2683                     ? "%Y-%m-%dT%H:%M:%SZ"
     2684                     : "%Y-%m-%dT%H:%M:%S";
     2685  if (argv[0])
     2686    {
     2687      buf = argv[0];
     2688      while (isspace ((unsigned char)*buf))
     2689        buf++;
     2690      if (*buf)
     2691        format = buf;
     2692    }
     2693
     2694  if (argv[1])
     2695    {
     2696      /* FIXME */
     2697      fatal (NILF, _("The reverse strftime aspect of the $(date*) functions isn't implemented yet.\n"));
     2698    }
     2699  else
     2700    time(&tval);
     2701
     2702  buf_size = 64;
     2703  buf = xmalloc (buf_size);
     2704  while (strftime (buf, buf_size, format,
     2705                   !strcmp (funcname, "date-utc")
     2706                   ? gmtime (&tval) : localtime (&tval)) == 0)
     2707    buf = xrealloc (buf, buf_size <<= 1);
     2708  o = variable_buffer_output (o, buf, strlen (buf));
     2709  free (buf);
     2710  return o;
     2711}
     2712#endif
     2713
    26752714
    26762715#ifdef CONFIG_WITH_STACK
     
    31533192  { STRING_SIZE_TUPLE("comp-cmds"),     3,  3,  1,  func_comp_vars},
    31543193#endif
     3194#ifdef CONFIG_WITH_DATE
     3195  { STRING_SIZE_TUPLE("date"),          0,  1,  1,  func_date},
     3196  { STRING_SIZE_TUPLE("date-utc"),      0,  1,  1,  func_date},
     3197#endif
    31553198#ifdef CONFIG_WITH_STACK
    31563199  { STRING_SIZE_TUPLE("stack-push"),    2,  2,  1,  func_stack_push},
Note: See TracChangeset for help on using the changeset viewer.