Function: current-time-string

current-time-string is a function defined in timefns.c.

Signature

(current-time-string &optional SPECIFIED-TIME ZONE)

Documentation

Return the current local time, as a human-readable string.

Programs can use this function to decode a time, since the number of columns in each field is fixed if the year is in the range 1000-9999. The format is Sun Sep 16 01:03:52 1973. However, see also the functions decode-time and format-time-string which provide a much more powerful and general facility.

If SPECIFIED-TIME is given, it is the time value to format instead of the current time. See format-time-string for the various forms of a time value.

The optional ZONE is omitted or nil for Emacs local time, t for Universal Time, wall for system wall clock time, or a string as in the TZ environment variable. It can also be a list (as from current-time-zone) or an integer (as from decode-time) applied without consideration for daylight saving time.

View in manual

Probably introduced at or before Emacs version 24.3.

Aliases

eshell/date

Source Code

// Defined in /usr/src/emacs/src/timefns.c
{
  time_t value = lisp_seconds_argument (specified_time);
  timezone_t tz = tzlookup (zone, false);

  /* Convert to a string in ctime format, except without the trailing
     newline, and without the 4-digit year limit.  Don't use asctime
     or ctime, as they might dump core if the year is outside the
     range -999 .. 9999.  */
  struct tm tm;
  struct tm *tmp = emacs_localtime_rz (tz, &value, &tm);
  xtzfree (tz);
  if (! tmp)
    time_error (errno);

  static char const wday_name[][4] =
    { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  static char const mon_name[][4] =
    { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  intmax_t year_base = TM_YEAR_BASE;
  char buf[sizeof "Mon Apr 30 12:49:17 " + INT_STRLEN_BOUND (int) + 1];
  int len = sprintf (buf, "%s %s%3d %02d:%02d:%02d %"PRIdMAX,
		     wday_name[tm.tm_wday], mon_name[tm.tm_mon], tm.tm_mday,
		     tm.tm_hour, tm.tm_min, tm.tm_sec,
		     tm.tm_year + year_base);

  return make_unibyte_string (buf, len);
}