# HG changeset patch # User jwe # Date 846989307 0 # Node ID fd838110e6cad1af765e14b7779afd29117938f9 # Parent 22af83aceafb88a55ffbff4485820aec5baa8f27 [project @ 1996-11-03 02:47:15 by jwe] diff --git a/src/getpwent.cc b/src/getpwent.cc new file mode 100644 --- /dev/null +++ b/src/getpwent.cc @@ -0,0 +1,195 @@ +/* + +Copyright (C) 1996 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +#ifdef HAVE_PWD_H +#include +#endif + +#include "defun-dld.h" +#include "error.h" +#include "help.h" +#include "oct-map.h" +#include "ov.h" +#include "oct-obj.h" +#include "utils.h" + +// Password file functions. (Why not?) + +static octave_value +mk_pw_map (struct passwd *pw) +{ + octave_value retval; + + if (pw) + { + Octave_map m; + + m ["name"] = pw->pw_name; + m ["passwd"] = pw->pw_passwd; + m ["uid"] = STATIC_CAST (double, pw->pw_uid); + m ["gid"] = STATIC_CAST (double, pw->pw_gid); + m ["gecos"] = pw->pw_gecos; + m ["dir"] = pw->pw_dir; + m ["shell"] = pw->pw_shell; + + retval = m; + } + else + retval = 0.0; + + return retval; +} + +#if ! (defined (HAVE_GETPWENT) || defined (HAVE_GETPWUID) \ + || defined (HAVE_GETPWNAM) || defined (HAVE_SETPWENT) \ + || defined (HAVE_ENDPWENT)) +static void +gripe_not_implemented (const char *nm) +{ + error ("%s: not implemented on this system", nm); +} +#endif + +DEFUN_DLD (getpwent, , , + "getpwent ()\n\ +\n\ + Read an entry from the password-file stream, opening it if necessary.") +{ + octave_value retval; + +#ifdef HAVE_GETPWENT + retval = mk_pw_map (getpwent ()); +#else + gripe_not_implemented ("getpwent"); +#endif + + return retval; +} + +DEFUN_DLD (getpwuid, args, , + "getpwuid (UID)\n\ +\n\ + Search for a password entry with a matching user ID.") +{ + octave_value retval; + +#ifdef HAVE_GETPWUID + int nargin = args.length (); + + if (nargin == 1) + { + double dval = args(0).double_value (); + + if (! error_state) + { + if (D_NINT (dval) == dval) + { + uid_t uid = STATIC_CAST (uid_t, dval); + + retval = mk_pw_map (getpwuid (uid)); + } + else + error ("getpwuid: argument must be an integer"); + } + } + else + print_usage ("getpwuid"); +#else + gripe_not_implemented ("getpwuid"); +#endif + + return retval; +} + +DEFUN_DLD (getpwnam, args, , + "getpwnam (NAME)\n\ +\n\ + Search for password entry with a matching username.") +{ + octave_value retval; + +#ifdef HAVE_GETPWNAM + int nargin = args.length (); + + if (nargin == 1) + { + string s = args(0).string_value (); + + if (! error_state) + retval = mk_pw_map (getpwnam (s.c_str ())); + } + else + print_usage ("getpwnam"); +#else + gripe_not_implemented ("getpwnam"); +#endif + + return retval; +} + +DEFUN_DLD (setpwent, , , + "setpwent ()\n\ +\n\ + Rewind the password-file stream.") +{ + octave_value retval; + +#ifdef HAVE_SETPWENT + setpwent (); +#else + gripe_not_implemented ("setpwent"); +#endif + + return retval; +} + +DEFUN_DLD (endpwent, , , + "endpwent ()\n\ +\n\ + Close the password-file stream.") +{ + octave_value retval; + +#ifdef HAVE_ENDPWENT + endpwent (); +#else + gripe_not_implemented ("endpwent"); +#endif + + return retval; +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/src/getrusage.cc b/src/getrusage.cc new file mode 100644 --- /dev/null +++ b/src/getrusage.cc @@ -0,0 +1,168 @@ +/* + +Copyright (C) 1996 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "systime.h" + +#ifdef HAVE_SYS_RESOURCE_H +#include +#endif + +#if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) + +#if defined (HAVE_SYS_PARAM_H) +#include +#endif +#include + +#if !defined (HZ) +#if defined (CLK_TCK) +#define HZ CLK_TCK +#elif defined (USG) +#define HZ 100 +#else +#define HZ 60 +#endif +#endif + +#endif + +#include "defun-dld.h" +#include "help.h" +#include "oct-map.h" +#include "sysdep.h" +#include "ov.h" +#include "oct-obj.h" +#include "utils.h" + +#ifndef RUSAGE_SELF +#define RUSAGE_SELF 0 +#endif + +// System resource functions. + +DEFUN_DLD (getrusage, , , + "getrusage ()\n\ +\n\ +Return system resource statistics.") +{ + Octave_map m; + Octave_map tv_tmp; + +#if defined (HAVE_GETRUSAGE) + + struct rusage ru; + + getrusage (RUSAGE_SELF, &ru); + + tv_tmp ["sec"] = (double) ru.ru_utime.tv_sec; + tv_tmp ["usec"] = (double) ru.ru_utime.tv_usec; + m ["utime"] = octave_value (tv_tmp); + + tv_tmp ["sec"] = (double) ru.ru_stime.tv_sec; + tv_tmp ["usec"] = (double) ru.ru_stime.tv_usec; + m ["stime"] = octave_value (tv_tmp); + +#if ! defined (RUSAGE_TIMES_ONLY) + m ["maxrss"] = (double) ru.ru_maxrss; + m ["ixrss"] = (double) ru.ru_ixrss; + m ["idrss"] = (double) ru.ru_idrss; + m ["isrss"] = (double) ru.ru_isrss; + m ["minflt"] = (double) ru.ru_minflt; + m ["majflt"] = (double) ru.ru_majflt; + m ["nswap"] = (double) ru.ru_nswap; + m ["inblock"] = (double) ru.ru_inblock; + m ["oublock"] = (double) ru.ru_oublock; + m ["msgsnd"] = (double) ru.ru_msgsnd; + m ["msgrcv"] = (double) ru.ru_msgrcv; + m ["nsignals"] = (double) ru.ru_nsignals; + m ["nvcsw"] = (double) ru.ru_nvcsw; + m ["nivcsw"] = (double) ru.ru_nivcsw; +#endif + +#else +#if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) + + struct tms t; + + times (&t); + + unsigned long ticks; + unsigned long seconds; + unsigned long fraction; + + ticks = t.tms_utime + t.tms_cutime; + fraction = ticks % HZ; + seconds = ticks / HZ; + + tv_tmp ["sec"] = (double) seconds; + tv_tmp ["usec"] = (double) (fraction * 1e6 / HZ); + m ["utime"] = octave_value (tv_tmp); + + ticks = t.tms_stime + t.tms_cstime; + fraction = ticks % HZ; + seconds = ticks / HZ; + + tv_tmp ["sec"] = (double) seconds; + tv_tmp ["usec"] = (double) (fraction * 1e6 / HZ); + m ["stime"] = octave_value (tv_tmp); + +#else + + tv_tmp ["sec"] = 0.0; + tv_tmp ["usec"] = 0.0; + m ["utime"] = octave_value (tv_tmp); + + tv_tmp ["sec"] = 0.0; + tv_tmp ["usec"] = 0.0; + m ["stime"] = octave_value (tv_tmp); + +#endif + + m ["maxrss"] = octave_NaN; + m ["ixrss"] = octave_NaN; + m ["idrss"] = octave_NaN; + m ["isrss"] = octave_NaN; + m ["minflt"] = octave_NaN; + m ["majflt"] = octave_NaN; + m ["nswap"] = octave_NaN; + m ["inblock"] = octave_NaN; + m ["oublock"] = octave_NaN; + m ["msgsnd"] = octave_NaN; + m ["msgrcv"] = octave_NaN; + m ["nsignals"] = octave_NaN; + m ["nvcsw"] = octave_NaN; + m ["nivcsw"] = octave_NaN; + +#endif + + return octave_value (m); +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/src/resource.cc b/src/resource.cc deleted file mode 100644 --- a/src/resource.cc +++ /dev/null @@ -1,168 +0,0 @@ -/* - -Copyright (C) 1996 John W. Eaton - -This file is part of Octave. - -Octave is free software; you can redistribute it and/or modify it -under the terms of the GNU General Public License as published by the -Free Software Foundation; either version 2, or (at your option) any -later version. - -Octave is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. - -You should have received a copy of the GNU General Public License -along with Octave; see the file COPYING. If not, write to the Free -Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include "systime.h" - -#ifdef HAVE_SYS_RESOURCE_H -#include -#endif - -#if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) - -#if defined (HAVE_SYS_PARAM_H) -#include -#endif -#include - -#if !defined (HZ) -#if defined (CLK_TCK) -#define HZ CLK_TCK -#elif defined (USG) -#define HZ 100 -#else -#define HZ 60 -#endif -#endif - -#endif - -#include "defun-dld.h" -#include "help.h" -#include "oct-map.h" -#include "sysdep.h" -#include "ov.h" -#include "oct-obj.h" -#include "utils.h" - -#ifndef RUSAGE_SELF -#define RUSAGE_SELF 0 -#endif - -// System resource functions. - -DEFUN_DLD (getrusage, , , - "getrusage ()\n\ -\n\ -Return system resource statistics.") -{ - Octave_map m; - Octave_map tv_tmp; - -#if defined (HAVE_GETRUSAGE) - - struct rusage ru; - - getrusage (RUSAGE_SELF, &ru); - - tv_tmp ["sec"] = (double) ru.ru_utime.tv_sec; - tv_tmp ["usec"] = (double) ru.ru_utime.tv_usec; - m ["utime"] = octave_value (tv_tmp); - - tv_tmp ["sec"] = (double) ru.ru_stime.tv_sec; - tv_tmp ["usec"] = (double) ru.ru_stime.tv_usec; - m ["stime"] = octave_value (tv_tmp); - -#if ! defined (RUSAGE_TIMES_ONLY) - m ["maxrss"] = (double) ru.ru_maxrss; - m ["ixrss"] = (double) ru.ru_ixrss; - m ["idrss"] = (double) ru.ru_idrss; - m ["isrss"] = (double) ru.ru_isrss; - m ["minflt"] = (double) ru.ru_minflt; - m ["majflt"] = (double) ru.ru_majflt; - m ["nswap"] = (double) ru.ru_nswap; - m ["inblock"] = (double) ru.ru_inblock; - m ["oublock"] = (double) ru.ru_oublock; - m ["msgsnd"] = (double) ru.ru_msgsnd; - m ["msgrcv"] = (double) ru.ru_msgrcv; - m ["nsignals"] = (double) ru.ru_nsignals; - m ["nvcsw"] = (double) ru.ru_nvcsw; - m ["nivcsw"] = (double) ru.ru_nivcsw; -#endif - -#else -#if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) - - struct tms t; - - times (&t); - - unsigned long ticks; - unsigned long seconds; - unsigned long fraction; - - ticks = t.tms_utime + t.tms_cutime; - fraction = ticks % HZ; - seconds = ticks / HZ; - - tv_tmp ["sec"] = (double) seconds; - tv_tmp ["usec"] = (double) (fraction * 1e6 / HZ); - m ["utime"] = octave_value (tv_tmp); - - ticks = t.tms_stime + t.tms_cstime; - fraction = ticks % HZ; - seconds = ticks / HZ; - - tv_tmp ["sec"] = (double) seconds; - tv_tmp ["usec"] = (double) (fraction * 1e6 / HZ); - m ["stime"] = octave_value (tv_tmp); - -#else - - tv_tmp ["sec"] = 0.0; - tv_tmp ["usec"] = 0.0; - m ["utime"] = octave_value (tv_tmp); - - tv_tmp ["sec"] = 0.0; - tv_tmp ["usec"] = 0.0; - m ["stime"] = octave_value (tv_tmp); - -#endif - - m ["maxrss"] = octave_NaN; - m ["ixrss"] = octave_NaN; - m ["idrss"] = octave_NaN; - m ["isrss"] = octave_NaN; - m ["minflt"] = octave_NaN; - m ["majflt"] = octave_NaN; - m ["nswap"] = octave_NaN; - m ["inblock"] = octave_NaN; - m ["oublock"] = octave_NaN; - m ["msgsnd"] = octave_NaN; - m ["msgrcv"] = octave_NaN; - m ["nsignals"] = octave_NaN; - m ["nvcsw"] = octave_NaN; - m ["nivcsw"] = octave_NaN; - -#endif - - return octave_value (m); -} - -/* -;;; Local Variables: *** -;;; mode: C++ *** -;;; End: *** -*/ diff --git a/src/time.cc b/src/time.cc new file mode 100644 --- /dev/null +++ b/src/time.cc @@ -0,0 +1,313 @@ +/* + +Copyright (C) 1996 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "defun.h" +#include "error.h" +#include "help.h" +#include "oct-map.h" +#include "systime.h" +#include "ov.h" +#include "oct-obj.h" +#include "utils.h" + +// Date and time functions. + +static Octave_map +mk_tm_map (struct tm *tm, double fraction) +{ + Octave_map m; + + m ["usec"] = fraction * 1e6; + m ["sec"] = (double) tm->tm_sec; + m ["min"] = (double) tm->tm_min; + m ["hour"] = (double) tm->tm_hour; + m ["mday"] = (double) tm->tm_mday; + m ["mon"] = (double) tm->tm_mon; + m ["year"] = (double) tm->tm_year; + m ["wday"] = (double) tm->tm_wday; + m ["yday"] = (double) tm->tm_yday; + m ["isdst"] = (double) tm->tm_isdst; +#if defined (HAVE_TM_ZONE) + m ["zone"] = tm->tm_zone; +#elif defined (HAVE_TZNAME) + if (tm->tm_isdst && tzname[1] && *tzname[1]) + m ["zone"] = tzname[1]; + else + m ["zone"] = tzname[0]; +#else + m ["zone"] = zone_name (tm); +#endif + + return m; +} + +static struct tm* +extract_tm (Octave_map &m, double& fraction) +{ + static struct tm tm; + + fraction = (m ["usec"] . double_value ()) / 1e6; + tm.tm_sec = NINT (m ["sec"] . double_value ()); + tm.tm_min = NINT (m ["min"] . double_value ()); + tm.tm_hour = NINT (m ["hour"] . double_value ()); + tm.tm_mday = NINT (m ["mday"] . double_value ()); + tm.tm_mon = NINT (m ["mon"] . double_value ()); + tm.tm_year = NINT (m ["year"] . double_value ()); + tm.tm_wday = NINT (m ["wday"] . double_value ()); + tm.tm_yday = NINT (m ["yday"] . double_value ()); + tm.tm_isdst = NINT (m ["isdst"] . double_value ()); +#ifdef HAVE_TMZONE + string tstr = m ["zone"] . string_value (); + tm.tm_zone = tstr.c_str (); +#endif + + return &tm; +} + +DEFUN (time, , , + "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; + +#ifdef GETTIMEOFDAY_NO_TZ + gettimeofday (&tp); +#else + gettimeofday (&tp, 0); +#endif + + now = tp.tv_sec; + + fraction = tp.tv_usec / 1e6; + +#else + + now = time (0); + +#endif + + return (double) now + fraction; +} + +DEFUN (gmtime, args, , + "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_value_list 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 = octave_value (mk_tm_map (gmtime (&timeval), fraction)); + } + } + else + print_usage ("gmtime"); + + return retval; +} + +DEFUN (localtime, args, , + "localtime (TIME)\n\ +\n\ + Given a value returned from time(), return a structure with\n\ + the following elements:\n\ +\n\ + usec : microseconds after the second (0, 999999)\n\ + sec : seconds after the minute (0, 61)\n\ + min : minutes after the hour (0, 59)\n\ + hour : hours since midnight (0, 23)\n\ + mday : day of the month (1, 31)\n\ + mon : months since January (0, 11)\n\ + year : years since 1900\n\ + wday : days since Sunday (0, 6)\n\ + yday : days since January 1 (0, 365)\n\ + isdst : Daylight Savings Time flag\n\ + zone : Time zone") +{ + octave_value_list 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 = octave_value (mk_tm_map (localtime (&timeval), fraction)); + } + } + else + print_usage ("localtime"); + + return retval; +} + +DEFUN (mktime, args, , + "mktime (TMSTRUCT)") +{ + octave_value_list retval; + + if (args.length () == 1 && args(0).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; +} + +DEFUN (strftime, args, , + "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_value_list retval; + + if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) + { + string 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.c_str (), tm)) + { + delete [] buf; + bufsize *= 2; + buf = new char [bufsize]; + } + + retval = buf; + + delete [] buf; + } + } + else + print_usage ("strftime"); + + return retval; +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/src/timefns.cc b/src/timefns.cc deleted file mode 100644 --- a/src/timefns.cc +++ /dev/null @@ -1,313 +0,0 @@ -/* - -Copyright (C) 1996 John W. Eaton - -This file is part of Octave. - -Octave is free software; you can redistribute it and/or modify it -under the terms of the GNU General Public License as published by the -Free Software Foundation; either version 2, or (at your option) any -later version. - -Octave is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. - -You should have received a copy of the GNU General Public License -along with Octave; see the file COPYING. If not, write to the Free -Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include - -#include "defun.h" -#include "error.h" -#include "help.h" -#include "oct-map.h" -#include "systime.h" -#include "ov.h" -#include "oct-obj.h" -#include "utils.h" - -// Date and time functions. - -static Octave_map -mk_tm_map (struct tm *tm, double fraction) -{ - Octave_map m; - - m ["usec"] = fraction * 1e6; - m ["sec"] = (double) tm->tm_sec; - m ["min"] = (double) tm->tm_min; - m ["hour"] = (double) tm->tm_hour; - m ["mday"] = (double) tm->tm_mday; - m ["mon"] = (double) tm->tm_mon; - m ["year"] = (double) tm->tm_year; - m ["wday"] = (double) tm->tm_wday; - m ["yday"] = (double) tm->tm_yday; - m ["isdst"] = (double) tm->tm_isdst; -#if defined (HAVE_TM_ZONE) - m ["zone"] = tm->tm_zone; -#elif defined (HAVE_TZNAME) - if (tm->tm_isdst && tzname[1] && *tzname[1]) - m ["zone"] = tzname[1]; - else - m ["zone"] = tzname[0]; -#else - m ["zone"] = zone_name (tm); -#endif - - return m; -} - -static struct tm* -extract_tm (Octave_map &m, double& fraction) -{ - static struct tm tm; - - fraction = (m ["usec"] . double_value ()) / 1e6; - tm.tm_sec = NINT (m ["sec"] . double_value ()); - tm.tm_min = NINT (m ["min"] . double_value ()); - tm.tm_hour = NINT (m ["hour"] . double_value ()); - tm.tm_mday = NINT (m ["mday"] . double_value ()); - tm.tm_mon = NINT (m ["mon"] . double_value ()); - tm.tm_year = NINT (m ["year"] . double_value ()); - tm.tm_wday = NINT (m ["wday"] . double_value ()); - tm.tm_yday = NINT (m ["yday"] . double_value ()); - tm.tm_isdst = NINT (m ["isdst"] . double_value ()); -#ifdef HAVE_TMZONE - string tstr = m ["zone"] . string_value (); - tm.tm_zone = tstr.c_str (); -#endif - - return &tm; -} - -DEFUN (time, , , - "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; - -#ifdef GETTIMEOFDAY_NO_TZ - gettimeofday (&tp); -#else - gettimeofday (&tp, 0); -#endif - - now = tp.tv_sec; - - fraction = tp.tv_usec / 1e6; - -#else - - now = time (0); - -#endif - - return (double) now + fraction; -} - -DEFUN (gmtime, args, , - "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_value_list 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 = octave_value (mk_tm_map (gmtime (&timeval), fraction)); - } - } - else - print_usage ("gmtime"); - - return retval; -} - -DEFUN (localtime, args, , - "localtime (TIME)\n\ -\n\ - Given a value returned from time(), return a structure with\n\ - the following elements:\n\ -\n\ - usec : microseconds after the second (0, 999999)\n\ - sec : seconds after the minute (0, 61)\n\ - min : minutes after the hour (0, 59)\n\ - hour : hours since midnight (0, 23)\n\ - mday : day of the month (1, 31)\n\ - mon : months since January (0, 11)\n\ - year : years since 1900\n\ - wday : days since Sunday (0, 6)\n\ - yday : days since January 1 (0, 365)\n\ - isdst : Daylight Savings Time flag\n\ - zone : Time zone") -{ - octave_value_list 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 = octave_value (mk_tm_map (localtime (&timeval), fraction)); - } - } - else - print_usage ("localtime"); - - return retval; -} - -DEFUN (mktime, args, , - "mktime (TMSTRUCT)") -{ - octave_value_list retval; - - if (args.length () == 1 && args(0).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; -} - -DEFUN (strftime, args, , - "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_value_list retval; - - if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) - { - string 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.c_str (), tm)) - { - delete [] buf; - bufsize *= 2; - buf = new char [bufsize]; - } - - retval = buf; - - delete [] buf; - } - } - else - print_usage ("strftime"); - - return retval; -} - -/* -;;; Local Variables: *** -;;; mode: C++ *** -;;; End: *** -*/