pike.git / src / builtin_functions.c

version» Context lines:

pike.git/src/builtin_functions.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: builtin_functions.c,v 1.524 2004/01/11 01:16:08 nilsson Exp $ + || $Id: builtin_functions.c,v 1.525 2004/01/30 16:17:57 grubba Exp $   */      #include "global.h" - RCSID("$Id: builtin_functions.c,v 1.524 2004/01/11 01:16:08 nilsson Exp $"); + RCSID("$Id: builtin_functions.c,v 1.525 2004/01/30 16:17:57 grubba Exp $");   #include "interpret.h"   #include "svalue.h"   #include "pike_macros.h"   #include "object.h"   #include "program.h"   #include "array.h"   #include "pike_error.h"   #include "constants.h"   #include "mapping.h"   #include "stralloc.h"
pike.git/src/builtin_functions.c:4362: Inside #if defined(HAVE_LOCALTIME) and #if defined(HAVE_EXTERNAL_TIMEZONE)
   push_int(timezone);    f_aggregate_mapping(20);   #else    f_aggregate_mapping(18);   #endif   #endif   #endif   }   #endif    + #ifdef HAVE_GMTIME + /* Returns the approximate difference in seconds between the +  * two struct tm's. +  * +  * NOTE: No checks for out of range are performed. +  */ + static time_t my_tm_diff(const struct tm *t1, const struct tm *t2) + { +  return (t1->tm_year - t2->tm_year) * 31557600 + +  (t1->tm_mon - t2->tm_mon) * 2629800 + +  (t1->tm_mday - t2->tm_mday) * 86400 + +  (t1->tm_hour - t2->tm_hour) * 3600 + +  (t1->tm_min - t2->tm_min) * 60 + +  (t1->tm_sec - t2->tm_sec); + } +  + /* Inverse operation of gmtime(). +  */ + static time_t my_timegm(struct tm *target_tm) + { +  time_t current_ts = 0; +  time_t diff_ts; +  struct tm *current_tm; +  int loop_cnt = 0; +  +  /* Assume dst is one hour. */ +  target_tm->tm_hour -= target_tm->tm_isdst; +  +  /* This loop seems stable, and usually converges in two passes. +  * The loop counter is for paranoia reasons. +  */ +  while((diff_ts = my_tm_diff(target_tm, current_tm = gmtime(&current_ts)))) { +  current_ts += diff_ts; +  loop_cnt++; +  if (loop_cnt > 20) { +  /* Infinite loop? */ +  return -1; +  } +  } +  /* Check that the result tm looks like what we expect... */ +  if ((current_tm->tm_sec == target_tm->tm_sec) && +  (current_tm->tm_min == target_tm->tm_min)) { +  /* Odds are that the rest of the fields are correct (1:3600). */ +  return current_ts; +  } +  return -1; + } + #endif /* HAVE_GMTIME */ +    #ifdef HAVE_MKTIME   /*! @decl int mktime(mapping(string:int) tm)    *! @decl int mktime(int sec, int min, int hour, int mday, int mon, int year, @    *! int|void isdst, int|void tz)    *!    *! This function converts information about date and time into an integer    *! which contains the number of seconds since 00:00:00 UTC, Jan 1, 1970.    *!    *! You can either call this function with a mapping containing the    *! following elements:
pike.git/src/builtin_functions.c:4463: Inside #if defined(HAVE_MKTIME)
   }    }       if ((args > 6) && (Pike_sp[6-args].subtype == NUMBER_NUMBER))    {    date.tm_isdst = Pike_sp[6-args].u.integer;    } else {    date.tm_isdst = -1;    }    +  date.tm_zone = NULL; +  + #ifdef HAVE_GMTIME +  if((args > 7) && (Pike_sp[7-args].subtype == NUMBER_NUMBER)) +  { +  /* UTC-relative time. Use my_timegm(). */ +  retval = my_timegm(&date); +  if (retval == -1) +  PIKE_ERROR("mktime", "Cannot convert.\n", Pike_sp, args); +  retval += Pike_sp[7-args].u.integer; +  } else { + #endif /* HAVE_GMTIME */ +    #ifdef STRUCT_TM_HAS_GMTOFF    /* BSD-style */    date.tm_gmtoff = 0;   #else   #ifdef STRUCT_TM_HAS___TM_GMTOFF    /* Linux-style */    date.__tm_gmtoff = 0;   #else    if((args > 7) && (Pike_sp[7-args].subtype == NUMBER_NUMBER))    {
pike.git/src/builtin_functions.c:4517:   #endif /* STRUCT_TM_HAS_GMTOFF */    }       if ((args > 6) && (Pike_sp[6-args].subtype == NUMBER_NUMBER) &&    (Pike_sp[6-args].u.integer != -1) &&    (Pike_sp[6-args].u.integer != date.tm_isdst)) {    /* Some stupid libc's (Hi Linux!) don't accept that we've set isdst... */    retval += 3600 * (Pike_sp[6-args].u.integer - date.tm_isdst);    }   #endif /* STRUCT_TM_HAS_GMTOFF || STRUCT_TM_HAS___TM_GMTOFF */ + #ifdef HAVE_GMTIME +  } + #endif /* HAVE_GMTIME */       pop_n_elems(args);    push_int(retval);   }      #endif      /* Parse a sprintf/sscanf-style format string */   static ptrdiff_t low_parse_format(p_wchar0 *s, ptrdiff_t slen)   {
pike.git/src/builtin_functions.c:8297: Inside #if defined(HAVE_GMTIME)
  #ifdef HAVE_GMTIME      /* function(int:mapping(string:int)) */    ADD_EFUN("gmtime",f_gmtime,tFunc(tInt,tMap(tStr,tInt)),OPT_TRY_OPTIMIZE);   #endif      #ifdef HAVE_MKTIME      /* function(int,int,int,int,int,int,int,void|int:int)|function(object|mapping:int) */    ADD_EFUN("mktime",f_mktime, -  tOr(tFunc(tInt tInt tInt tInt tInt tInt tInt tOr(tVoid,tInt),tInt), +  tOr(tFunc(tInt tInt tInt tInt tInt tInt +  tOr(tVoid,tInt) tOr(tVoid,tInt),tInt),    tFunc(tOr(tObj,tMapping),tInt)),OPT_TRY_OPTIMIZE);   #endif      /* function(:void) */    ADD_EFUN("_verify_internals",f__verify_internals,    tFunc(tNone,tVoid),OPT_SIDE_EFFECT|OPT_EXTERNAL_DEPEND);      #ifdef PIKE_DEBUG      /* function(int:int) */