Mercurial > hg > octave-lojdl
changeset 1184:1def0d0014bd
[project @ 1995-03-30 04:55:12 by jwe]
author | jwe |
---|---|
date | Thu, 30 Mar 1995 04:55:12 +0000 |
parents | 8d45d63ecbc8 |
children | 669750cfad84 |
files | src/timefns.cc |
diffstat | 1 files changed, 259 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/src/timefns.cc +++ b/src/timefns.cc @@ -30,7 +30,10 @@ #include "tree-const.h" #include "oct-obj.h" #include "systime.h" +#include "oct-map.h" #include "defun.h" +#include "utils.h" +#include "help.h" #ifdef HAVE_SYS_RESOURCE_H extern "C" @@ -43,43 +46,7 @@ #define RUSAGE_SELF 0 #endif - -DEFUN ("clock", Fclock, Sclock, 1, 0, - "clock (): return current date and time in vector with elements\n\ -\n\ - [ year, month, day-of-month, hour, minute, second ]") -{ - time_t now; - double fraction = 0.0; - -#ifdef HAVE_GETTIMEOFDAY - - struct timeval tp; - - gettimeofday (&tp, 0); - - now = tp.tv_sec; - - fraction = tp.tv_usec / 1e6; - -#else - - time (&now); - -#endif - - struct tm *tm = localtime (&now); - - Matrix m (1, 6); - m.elem (0, 0) = tm->tm_year + 1900; - m.elem (0, 1) = tm->tm_mon + 1; - m.elem (0, 2) = tm->tm_mday; - m.elem (0, 3) = tm->tm_hour; - m.elem (0, 4) = tm->tm_min; - m.elem (0, 5) = tm->tm_sec + fraction; - - return m; -} +// CPU time functions. DEFUN ("cputime", Fcputime, Scputime, 0, 0, "[total, user, system] = cputime ()\n\ @@ -109,20 +76,265 @@ return retval; } -DEFUN ("date", Fdate, Sdate, 1, 0, - "date (): return current date in a string, in the form `18-Jul-94'") +// Date and time functions. + +static Octave_map +mk_tm_map (struct tm *tm, double fraction) +{ + Octave_map m; + + m ["tm_usec"] = fraction * 1e6; + m ["tm_sec"] = (double) tm->tm_sec; + m ["tm_min"] = (double) tm->tm_min; + m ["tm_hour"] = (double) tm->tm_hour; + m ["tm_mday"] = (double) tm->tm_mday; + m ["tm_mon"] = (double) tm->tm_mon; + m ["tm_year"] = (double) tm->tm_year; + m ["tm_wday"] = (double) tm->tm_wday; + m ["tm_yday"] = (double) tm->tm_yday; + m ["tm_isdst"] = (double) tm->tm_isdst; +#if defined (HAVE_TM_ZONE) + m ["tm_zone"] = tm->tm_zone; +#elif defined (HAVE_TZNAME) + if (tm->tm_isdst && tzname[1] && *tzname[1]) + m ["tm_zone"] = tzname[1]; + else + m ["tm_zone"] = tzname[0]; +#else + m ["tm_zone"] = zone_name (tm); +#endif + + return m; +} + +static struct tm* +extract_tm (Octave_map &m, double &fraction) +{ + static struct tm tm; + + fraction = NINT (m ["tm_usec"] . double_value ()); + tm.tm_sec = NINT (m ["tm_sec"] . double_value ()); + tm.tm_min = NINT (m ["tm_min"] . double_value ()); + tm.tm_hour = NINT (m ["tm_hour"] . double_value ()); + tm.tm_mday = NINT (m ["tm_mday"] . double_value ()); + tm.tm_mon = NINT (m ["tm_mon"] . double_value ()); + tm.tm_year = NINT (m ["tm_year"] . double_value ()); + tm.tm_wday = NINT (m ["tm_wday"] . double_value ()); + tm.tm_yday = NINT (m ["tm_yday"] . double_value ()); + tm.tm_isdst = NINT (m ["tm_isdst"] . double_value ()); + tm.tm_zone = (m ["tm_zone"] . string_value ()); + + return &tm; +} + +DEFUN ("time", Ftime, Stime, 1, 0, + "time ()\n\ +\n\ + Return current time. On Unix systems, this is the number of\n\ + seconds since the epoch.") +{ + time_t now; + double fraction = 0.0; + +#ifdef HAVE_GETTIMEOFDAY + + struct timeval tp; + + gettimeofday (&tp, 0); + + now = tp.tv_sec; + + fraction = tp.tv_usec / 1e6; + +#else + + now = time (0); + +#endif + + return (double) now + fraction; +} + +DEFUN ("gmtime", Fgmtime, Sgmtime, 1, 1, + "gmtime (TIME)\n\ +\n\ + Given a value returned from time(), return a structure like that\n\ + returned from localtime() but with values corresponding to\n\ + Coordinated Universal Time (UTC).") +{ + Octave_object retval; + + if (args.length () == 1) + { + double tmp = args(0).double_value (); + + if (! error_state) + { + time_t timeval = NINT (tmp); + double ip; + double fraction = modf (tmp, &ip); + + retval = tree_constant (mk_tm_map (gmtime (&timeval), fraction)); + } + } + else + print_usage ("gmtime"); + + return retval; +} + +DEFUN ("localtime", Flocaltime, Slocaltime, 1, 1, + "localtime (TIME)\n\ +\n\ + Given a value returned from time(), return a structure with\n\ + the following elements:\n\ +\n\ + tm_usec : microseconds after the second (0, 999999)\n\ + tm_sec : seconds after the minute (0, 61)\n\ + tm_min : minutes after the hour (0, 59)\n\ + tm_hour : hours since midnight (0, 23)\n\ + tm_mday : day of the month (1, 31)\n\ + tm_mon : months since January (0, 11)\n\ + tm_year : years since 1900\n\ + tm_wday : days since Sunday (0, 6)\n\ + tm_yday : days since January 1 (0, 365)\n\ + tm_isdst : Daylight Savings Time flag\n\ + tm_zone : Time zone") { Octave_object retval; - time_t now; - struct tm *tm; + if (args.length () == 1) + { + double tmp = args(0).double_value (); + + if (! error_state) + { + time_t timeval = NINT (tmp); + double ip; + double fraction = modf (tmp, &ip); + + retval = tree_constant (mk_tm_map (localtime (&timeval), fraction)); + } + } + else + print_usage ("localtime"); + + return retval; +} + +DEFUN ("mktime", Fmktime, Smktime, 1, 2, + "mktime (TMSTRUCT)") +{ + Octave_object retval; + + if (args.length () == 1 && args(1).is_map ()) + { + Octave_map map = args(0).map_value (); + + double fraction; + + struct tm *tm = extract_tm (map, fraction); + + if (! error_state) + retval = (double) mktime (tm) + fraction; + } + else + print_usage ("mktime"); + + return retval; +} - time (&now); - tm = localtime (&now); - char date[32]; - int len = strftime (date, 31, "%d-%b-%y", tm); - if (len > 0) - retval = date; +DEFUN ("strftime", Fstrftime, Sstrftime, 1, 2, + "strftime (FMT, TMSTRUCT)\n\ +\n\ + Performs `%' substitutions similar to those in printf. Except where\n\ + noted, substituted fields have a fixed size; numeric fields are\n\ + padded if necessary. Padding is with zeros by default; for fields\n\ + that display a single number, padding can be changed or inhibited by\n\ + following the `%' with one of the modifiers described below.\n\ + Unknown field specifiers are copied as normal characters. All other\n\ + characters are copied to the output without change.\n\ +\n\ + Supports a superset of the ANSI C field specifiers.\n\ +\n\ + Literal character fields:\n\ +\n\ + % %\n\ + n newline\n\ + t tab\n\ +\n\ + Numeric modifiers (a nonstandard extension):\n\ +\n\ + - do not pad the field\n\ + _ pad the field with spaces\n\ +\n\ + Time fields:\n\ +\n\ + %H hour (00..23)\n\ + %I hour (01..12)\n\ + %k hour ( 0..23)\n\ + %l hour ( 1..12)\n\ + %M minute (00..59)\n\ + %p locale's AM or PM\n\ + %r time, 12-hour (hh:mm:ss [AP]M)\n\ + %R time, 24-hour (hh:mm)\n\ + %s time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension)\n\ + %S second (00..61)\n\ + %T time, 24-hour (hh:mm:ss)\n\ + %X locale's time representation (%H:%M:%S)\n\ + %Z time zone (EDT), or nothing if no time zone is determinable\n\ +\n\ + Date fields:\n\ +\n\ + %a locale's abbreviated weekday name (Sun..Sat)\n\ + %A locale's full weekday name, variable length (Sunday..Saturday)\n\ + %b locale's abbreviated month name (Jan..Dec)\n\ + %B locale's full month name, variable length (January..December)\n\ + %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\ + %C century (00..99)\n\ + %d day of month (01..31)\n\ + %e day of month ( 1..31)\n\ + %D date (mm/dd/yy)\n\ + %h same as %b\n\ + %j day of year (001..366)\n\ + %m month (01..12)\n\ + %U week number of year with Sunday as first day of week (00..53)\n\ + %w day of week (0..6)\n\ + %W week number of year with Monday as first day of week (00..53)\n\ + %x locale's date representation (mm/dd/yy)\n\ + %y last two digits of year (00..99)\n\ + %Y year (1970...)") +{ + Octave_object retval; + + if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) + { + char *fmt = args(0).string_value (); + Octave_map map = args(1).map_value (); + + double fraction; + + struct tm *tm = extract_tm (map, fraction); + + if (! error_state) + { + int bufsize = 128; + char *buf = new char [bufsize]; + + while (! strftime (buf, bufsize, fmt, tm)) + { + delete [] buf; + bufsize *= 2; + buf = new char [bufsize]; + } + + retval = buf; + + delete [] buf; + } + } + else + print_usage ("strftime"); return retval; }