PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/rctime/interp_time.py

https://bitbucket.org/pypy/pypy/
Python | 595 lines | 580 code | 4 blank | 11 comment | 8 complexity | 536419e8a77de9981e46eb63164cb917 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from pypy.rpython.tool import rffi_platform as platform
  2. from pypy.rpython.lltypesystem import rffi
  3. from pypy.interpreter.error import OperationError, operationerrfmt
  4. from pypy.interpreter.gateway import unwrap_spec
  5. from pypy.rpython.lltypesystem import lltype
  6. from pypy.rlib.rarithmetic import ovfcheck_float_to_int, intmask
  7. from pypy.rlib import rposix
  8. from pypy.translator.tool.cbuild import ExternalCompilationInfo
  9. import os
  10. import sys
  11. import time as pytime
  12. _POSIX = os.name == "posix"
  13. _WIN = os.name == "nt"
  14. if _WIN:
  15. # Interruptible sleeps on Windows:
  16. # We install a specific Console Ctrl Handler which sets an 'event'.
  17. # time.sleep() will actually call WaitForSingleObject with the desired
  18. # timeout. On Ctrl-C, the signal handler is called, the event is set,
  19. # and the wait function exits.
  20. from pypy.rlib import rwin32
  21. from pypy.interpreter.error import wrap_windowserror, wrap_oserror
  22. from pypy.module.thread import ll_thread as thread
  23. eci = ExternalCompilationInfo(
  24. separate_module_sources=['''
  25. #include <windows.h>
  26. static HANDLE interrupt_event;
  27. static BOOL WINAPI CtrlHandlerRoutine(
  28. DWORD dwCtrlType)
  29. {
  30. SetEvent(interrupt_event);
  31. /* allow other default handlers to be called.
  32. * Default Python handler will setup the
  33. * KeyboardInterrupt exception.
  34. */
  35. return 0;
  36. }
  37. BOOL pypy_timemodule_setCtrlHandler(HANDLE event)
  38. {
  39. interrupt_event = event;
  40. return SetConsoleCtrlHandler(CtrlHandlerRoutine, TRUE);
  41. }
  42. '''],
  43. export_symbols=['pypy_timemodule_setCtrlHandler'],
  44. )
  45. _setCtrlHandlerRoutine = rffi.llexternal(
  46. 'pypy_timemodule_setCtrlHandler',
  47. [rwin32.HANDLE], rwin32.BOOL,
  48. compilation_info=eci)
  49. class GlobalState:
  50. def __init__(self):
  51. self.init()
  52. def init(self):
  53. self.interrupt_event = rwin32.NULL_HANDLE
  54. def startup(self, space):
  55. # Initialize the event handle used to signal Ctrl-C
  56. try:
  57. globalState.interrupt_event = rwin32.CreateEvent(
  58. rffi.NULL, True, False, rffi.NULL)
  59. except WindowsError, e:
  60. raise wrap_windowserror(space, e)
  61. if not _setCtrlHandlerRoutine(globalState.interrupt_event):
  62. raise wrap_windowserror(
  63. space, rwin32.lastWindowsError("SetConsoleCtrlHandler"))
  64. globalState = GlobalState()
  65. class State:
  66. def __init__(self, space):
  67. self.main_thread = 0
  68. def _freeze_(self):
  69. self.main_thread = 0
  70. globalState.init()
  71. def startup(self, space):
  72. self.main_thread = thread.get_ident()
  73. globalState.startup(space)
  74. def get_interrupt_event(self):
  75. return globalState.interrupt_event
  76. _includes = ["time.h"]
  77. if _POSIX:
  78. _includes.append('sys/time.h')
  79. class CConfig:
  80. _compilation_info_ = ExternalCompilationInfo(
  81. includes = _includes
  82. )
  83. CLOCKS_PER_SEC = platform.ConstantInteger("CLOCKS_PER_SEC")
  84. clock_t = platform.SimpleType("clock_t", rffi.ULONG)
  85. has_gettimeofday = platform.Has('gettimeofday')
  86. if _POSIX:
  87. calling_conv = 'c'
  88. CConfig.timeval = platform.Struct("struct timeval",
  89. [("tv_sec", rffi.INT),
  90. ("tv_usec", rffi.INT)])
  91. CConfig.tm = platform.Struct("struct tm", [("tm_sec", rffi.INT),
  92. ("tm_min", rffi.INT), ("tm_hour", rffi.INT), ("tm_mday", rffi.INT),
  93. ("tm_mon", rffi.INT), ("tm_year", rffi.INT), ("tm_wday", rffi.INT),
  94. ("tm_yday", rffi.INT), ("tm_isdst", rffi.INT), ("tm_gmtoff", rffi.LONG),
  95. ("tm_zone", rffi.CCHARP)])
  96. elif _WIN:
  97. calling_conv = 'win'
  98. CConfig.tm = platform.Struct("struct tm", [("tm_sec", rffi.INT),
  99. ("tm_min", rffi.INT), ("tm_hour", rffi.INT), ("tm_mday", rffi.INT),
  100. ("tm_mon", rffi.INT), ("tm_year", rffi.INT), ("tm_wday", rffi.INT),
  101. ("tm_yday", rffi.INT), ("tm_isdst", rffi.INT)])
  102. class cConfig:
  103. pass
  104. for k, v in platform.configure(CConfig).items():
  105. setattr(cConfig, k, v)
  106. cConfig.tm.__name__ = "_tm"
  107. def external(name, args, result, eci=CConfig._compilation_info_):
  108. if _WIN and rffi.sizeof(rffi.TIME_T) == 8:
  109. # Recent Microsoft compilers use 64bit time_t and
  110. # the corresponding functions are named differently
  111. if (rffi.TIME_T in args or rffi.TIME_TP in args
  112. or result in (rffi.TIME_T, rffi.TIME_TP)):
  113. name = '_' + name + '64'
  114. return rffi.llexternal(name, args, result,
  115. compilation_info=eci,
  116. calling_conv=calling_conv,
  117. threadsafe=False)
  118. if _POSIX:
  119. cConfig.timeval.__name__ = "_timeval"
  120. timeval = cConfig.timeval
  121. CLOCKS_PER_SEC = cConfig.CLOCKS_PER_SEC
  122. clock_t = cConfig.clock_t
  123. tm = cConfig.tm
  124. glob_buf = lltype.malloc(tm, flavor='raw', zero=True, immortal=True)
  125. if cConfig.has_gettimeofday:
  126. c_gettimeofday = external('gettimeofday', [rffi.VOIDP, rffi.VOIDP], rffi.INT)
  127. TM_P = lltype.Ptr(tm)
  128. c_clock = external('clock', [rffi.TIME_TP], clock_t)
  129. c_time = external('time', [rffi.TIME_TP], rffi.TIME_T)
  130. c_ctime = external('ctime', [rffi.TIME_TP], rffi.CCHARP)
  131. c_gmtime = external('gmtime', [rffi.TIME_TP], TM_P)
  132. c_mktime = external('mktime', [TM_P], rffi.TIME_T)
  133. c_asctime = external('asctime', [TM_P], rffi.CCHARP)
  134. c_localtime = external('localtime', [rffi.TIME_TP], TM_P)
  135. if _POSIX:
  136. c_tzset = external('tzset', [], lltype.Void)
  137. if _WIN:
  138. win_eci = ExternalCompilationInfo(
  139. includes = ["time.h"],
  140. post_include_bits = ["long pypy_get_timezone();",
  141. "int pypy_get_daylight();",
  142. "char** pypy_get_tzname();"],
  143. separate_module_sources = ["""
  144. long pypy_get_timezone() { return timezone; }
  145. int pypy_get_daylight() { return daylight; }
  146. char** pypy_get_tzname() { return tzname; }
  147. """],
  148. export_symbols = [
  149. '_tzset', 'pypy_get_timezone', 'pypy_get_daylight', 'pypy_get_tzname'],
  150. )
  151. # Ensure sure that we use _tzset() and timezone from the same C Runtime.
  152. c_tzset = external('_tzset', [], lltype.Void, win_eci)
  153. c_get_timezone = external('pypy_get_timezone', [], rffi.LONG, win_eci)
  154. c_get_daylight = external('pypy_get_daylight', [], rffi.INT, win_eci)
  155. c_get_tzname = external('pypy_get_tzname', [], rffi.CCHARPP, win_eci)
  156. c_strftime = external('strftime', [rffi.CCHARP, rffi.SIZE_T, rffi.CCHARP, TM_P],
  157. rffi.SIZE_T)
  158. def _init_accept2dyear(space):
  159. if os.environ.get("PYTHONY2K"):
  160. accept2dyear = 0
  161. else:
  162. accept2dyear = 1
  163. _set_module_object(space, "accept2dyear", space.wrap(accept2dyear))
  164. def _init_timezone(space):
  165. timezone = daylight = altzone = 0
  166. tzname = ["", ""]
  167. if _WIN:
  168. c_tzset()
  169. timezone = c_get_timezone()
  170. altzone = timezone - 3600
  171. daylight = c_get_daylight()
  172. tzname_ptr = c_get_tzname()
  173. tzname = rffi.charp2str(tzname_ptr[0]), rffi.charp2str(tzname_ptr[1])
  174. if _POSIX:
  175. YEAR = (365 * 24 + 6) * 3600
  176. t = (((c_time(lltype.nullptr(rffi.TIME_TP.TO))) / YEAR) * YEAR)
  177. # we cannot have reference to stack variable, put it on the heap
  178. t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
  179. t_ref[0] = rffi.cast(rffi.TIME_T, t)
  180. p = c_localtime(t_ref)
  181. janzone = -p.c_tm_gmtoff
  182. tm_zone = rffi.charp2str(p.c_tm_zone)
  183. janname = [" ", tm_zone][bool(tm_zone)]
  184. tt = t + YEAR / 2
  185. t_ref[0] = rffi.cast(rffi.TIME_T, tt)
  186. p = c_localtime(t_ref)
  187. lltype.free(t_ref, flavor='raw')
  188. tm_zone = rffi.charp2str(p.c_tm_zone)
  189. julyzone = -p.c_tm_gmtoff
  190. julyname = [" ", tm_zone][bool(tm_zone)]
  191. if janzone < julyzone:
  192. # DST is reversed in the southern hemisphere
  193. timezone = julyzone
  194. altzone = janzone
  195. daylight = int(janzone != julyzone)
  196. tzname = [julyname, janname]
  197. else:
  198. timezone = janzone
  199. altzone = julyzone
  200. daylight = int(janzone != julyzone)
  201. tzname = [janname, julyname]
  202. _set_module_object(space, "timezone", space.wrap(timezone))
  203. _set_module_object(space, 'daylight', space.wrap(daylight))
  204. tzname_w = [space.wrap(tzname[0]), space.wrap(tzname[1])]
  205. _set_module_object(space, 'tzname', space.newtuple(tzname_w))
  206. _set_module_object(space, 'altzone', space.wrap(altzone))
  207. def _get_error_msg():
  208. errno = rposix.get_errno()
  209. return os.strerror(errno)
  210. if sys.platform != 'win32':
  211. @unwrap_spec(secs=float)
  212. def sleep(space, secs):
  213. if secs < 0:
  214. raise OperationError(space.w_IOError,
  215. space.wrap("Invalid argument: negative time in sleep"))
  216. pytime.sleep(secs)
  217. else:
  218. from pypy.rlib import rwin32
  219. from errno import EINTR
  220. def _simple_sleep(space, secs, interruptible):
  221. if secs == 0.0 or not interruptible:
  222. pytime.sleep(secs)
  223. else:
  224. millisecs = int(secs * 1000)
  225. interrupt_event = space.fromcache(State).get_interrupt_event()
  226. rwin32.ResetEvent(interrupt_event)
  227. rc = rwin32.WaitForSingleObject(interrupt_event, millisecs)
  228. if rc == rwin32.WAIT_OBJECT_0:
  229. # Yield to make sure real Python signal handler
  230. # called.
  231. pytime.sleep(0.001)
  232. raise wrap_oserror(space,
  233. OSError(EINTR, "sleep() interrupted"))
  234. @unwrap_spec(secs=float)
  235. def sleep(space, secs):
  236. if secs < 0:
  237. raise OperationError(space.w_IOError,
  238. space.wrap("Invalid argument: negative time in sleep"))
  239. # as decreed by Guido, only the main thread can be
  240. # interrupted.
  241. main_thread = space.fromcache(State).main_thread
  242. interruptible = (main_thread == thread.get_ident())
  243. MAX = sys.maxint / 1000.0 # > 24 days
  244. while secs > MAX:
  245. _simple_sleep(space, MAX, interruptible)
  246. secs -= MAX
  247. _simple_sleep(space, secs, interruptible)
  248. def _get_module_object(space, obj_name):
  249. w_module = space.getbuiltinmodule('time')
  250. w_obj = space.getattr(w_module, space.wrap(obj_name))
  251. return w_obj
  252. def _set_module_object(space, obj_name, w_obj_value):
  253. w_module = space.getbuiltinmodule('time')
  254. space.setattr(w_module, space.wrap(obj_name), w_obj_value)
  255. def _get_inttime(space, w_seconds):
  256. # w_seconds can be a wrapped None (it will be automatically wrapped
  257. # in the callers, so we never get a real None here).
  258. if space.is_w(w_seconds, space.w_None):
  259. seconds = pytime.time()
  260. else:
  261. seconds = space.float_w(w_seconds)
  262. try:
  263. seconds = ovfcheck_float_to_int(seconds)
  264. t = rffi.r_time_t(seconds)
  265. if rffi.cast(lltype.Signed, t) != seconds:
  266. raise OverflowError
  267. except OverflowError:
  268. raise OperationError(space.w_ValueError,
  269. space.wrap("time argument too large"))
  270. return t
  271. def _tm_to_tuple(space, t):
  272. time_tuple = [
  273. space.wrap(rffi.getintfield(t, 'c_tm_year') + 1900),
  274. space.wrap(rffi.getintfield(t, 'c_tm_mon') + 1), # want january == 1
  275. space.wrap(rffi.getintfield(t, 'c_tm_mday')),
  276. space.wrap(rffi.getintfield(t, 'c_tm_hour')),
  277. space.wrap(rffi.getintfield(t, 'c_tm_min')),
  278. space.wrap(rffi.getintfield(t, 'c_tm_sec')),
  279. space.wrap((rffi.getintfield(t, 'c_tm_wday') + 6) % 7), # want monday == 0
  280. space.wrap(rffi.getintfield(t, 'c_tm_yday') + 1), # want january, 1 == 1
  281. space.wrap(rffi.getintfield(t, 'c_tm_isdst'))]
  282. w_struct_time = _get_module_object(space, 'struct_time')
  283. w_time_tuple = space.newtuple(time_tuple)
  284. return space.call_function(w_struct_time, w_time_tuple)
  285. def _gettmarg(space, w_tup, allowNone=True):
  286. if allowNone and space.is_w(w_tup, space.w_None):
  287. # default to the current local time
  288. tt = rffi.r_time_t(int(pytime.time()))
  289. t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
  290. t_ref[0] = tt
  291. pbuf = c_localtime(t_ref)
  292. lltype.free(t_ref, flavor='raw')
  293. if not pbuf:
  294. raise OperationError(space.w_ValueError,
  295. space.wrap(_get_error_msg()))
  296. return pbuf
  297. tup_w = space.fixedview(w_tup)
  298. if len(tup_w) != 9:
  299. raise operationerrfmt(space.w_TypeError,
  300. "argument must be sequence of "
  301. "length 9, not %d", len(tup_w))
  302. y = space.int_w(tup_w[0])
  303. tm_mon = space.int_w(tup_w[1])
  304. if tm_mon == 0:
  305. tm_mon = 1
  306. tm_mday = space.int_w(tup_w[2])
  307. if tm_mday == 0:
  308. tm_mday = 1
  309. tm_yday = space.int_w(tup_w[7])
  310. if tm_yday == 0:
  311. tm_yday = 1
  312. rffi.setintfield(glob_buf, 'c_tm_mon', tm_mon)
  313. rffi.setintfield(glob_buf, 'c_tm_mday', tm_mday)
  314. rffi.setintfield(glob_buf, 'c_tm_hour', space.int_w(tup_w[3]))
  315. rffi.setintfield(glob_buf, 'c_tm_min', space.int_w(tup_w[4]))
  316. rffi.setintfield(glob_buf, 'c_tm_sec', space.int_w(tup_w[5]))
  317. rffi.setintfield(glob_buf, 'c_tm_wday', space.int_w(tup_w[6]))
  318. rffi.setintfield(glob_buf, 'c_tm_yday', tm_yday)
  319. rffi.setintfield(glob_buf, 'c_tm_isdst', space.int_w(tup_w[8]))
  320. if _POSIX:
  321. # actually never happens, but makes annotator happy
  322. glob_buf.c_tm_zone = lltype.nullptr(rffi.CCHARP.TO)
  323. rffi.setintfield(glob_buf, 'c_tm_gmtoff', 0)
  324. w_accept2dyear = _get_module_object(space, "accept2dyear")
  325. accept2dyear = space.int_w(w_accept2dyear)
  326. if y < 1900:
  327. if not accept2dyear:
  328. raise OperationError(space.w_ValueError,
  329. space.wrap("year >= 1900 required"))
  330. if 69 <= y <= 99:
  331. y += 1900
  332. elif 0 <= y <= 68:
  333. y += 2000
  334. else:
  335. raise OperationError(space.w_ValueError,
  336. space.wrap("year out of range"))
  337. if rffi.getintfield(glob_buf, 'c_tm_wday') < 0:
  338. raise OperationError(space.w_ValueError,
  339. space.wrap("day of week out of range"))
  340. rffi.setintfield(glob_buf, 'c_tm_year', y - 1900)
  341. rffi.setintfield(glob_buf, 'c_tm_mon',
  342. rffi.getintfield(glob_buf, 'c_tm_mon') - 1)
  343. rffi.setintfield(glob_buf, 'c_tm_wday',
  344. (rffi.getintfield(glob_buf, 'c_tm_wday') + 1) % 7)
  345. rffi.setintfield(glob_buf, 'c_tm_yday',
  346. rffi.getintfield(glob_buf, 'c_tm_yday') - 1)
  347. return glob_buf
  348. def time(space):
  349. """time() -> floating point number
  350. Return the current time in seconds since the Epoch.
  351. Fractions of a second may be present if the system clock provides them."""
  352. secs = pytime.time()
  353. return space.wrap(secs)
  354. if _WIN:
  355. class PCCache:
  356. pass
  357. pccache = PCCache()
  358. pccache.divisor = 0.0
  359. pccache.ctrStart = 0
  360. def clock(space):
  361. """clock() -> floating point number
  362. Return the CPU time or real time since the start of the process or since
  363. the first call to clock(). This has as much precision as the system
  364. records."""
  365. return space.wrap(pytime.clock())
  366. def ctime(space, w_seconds=None):
  367. """ctime([seconds]) -> string
  368. Convert a time in seconds since the Epoch to a string in local time.
  369. This is equivalent to asctime(localtime(seconds)). When the time tuple is
  370. not present, current time as returned by localtime() is used."""
  371. seconds = _get_inttime(space, w_seconds)
  372. t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
  373. t_ref[0] = seconds
  374. p = c_ctime(t_ref)
  375. lltype.free(t_ref, flavor='raw')
  376. if not p:
  377. raise OperationError(space.w_ValueError,
  378. space.wrap("unconvertible time"))
  379. return space.wrap(rffi.charp2str(p)[:-1]) # get rid of new line
  380. # by now w_tup is an optional argument (and not *args)
  381. # because of the ext. compiler bugs in handling such arguments (*args, **kwds)
  382. def asctime(space, w_tup=None):
  383. """asctime([tuple]) -> string
  384. Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
  385. When the time tuple is not present, current time as returned by localtime()
  386. is used."""
  387. buf_value = _gettmarg(space, w_tup)
  388. p = c_asctime(buf_value)
  389. if not p:
  390. raise OperationError(space.w_ValueError,
  391. space.wrap("unconvertible time"))
  392. return space.wrap(rffi.charp2str(p)[:-1]) # get rid of new line
  393. def gmtime(space, w_seconds=None):
  394. """gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,
  395. tm_sec, tm_wday, tm_yday, tm_isdst)
  396. Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
  397. GMT). When 'seconds' is not passed in, convert the current time instead.
  398. """
  399. # rpython does not support that a variable has two incompatible builtins
  400. # as value so we have to duplicate the code. NOT GOOD! see localtime() too
  401. seconds = _get_inttime(space, w_seconds)
  402. t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
  403. t_ref[0] = seconds
  404. p = c_gmtime(t_ref)
  405. lltype.free(t_ref, flavor='raw')
  406. if not p:
  407. raise OperationError(space.w_ValueError, space.wrap(_get_error_msg()))
  408. return _tm_to_tuple(space, p)
  409. def localtime(space, w_seconds=None):
  410. """localtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,
  411. tm_sec, tm_wday, tm_yday, tm_isdst)
  412. Convert seconds since the Epoch to a time tuple expressing local time.
  413. When 'seconds' is not passed in, convert the current time instead."""
  414. seconds = _get_inttime(space, w_seconds)
  415. t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
  416. t_ref[0] = seconds
  417. p = c_localtime(t_ref)
  418. lltype.free(t_ref, flavor='raw')
  419. if not p:
  420. raise OperationError(space.w_ValueError, space.wrap(_get_error_msg()))
  421. return _tm_to_tuple(space, p)
  422. def mktime(space, w_tup):
  423. """mktime(tuple) -> floating point number
  424. Convert a time tuple in local time to seconds since the Epoch."""
  425. buf = _gettmarg(space, w_tup, allowNone=False)
  426. rffi.setintfield(buf, "c_tm_wday", -1)
  427. tt = c_mktime(buf)
  428. # A return value of -1 does not necessarily mean an error, but tm_wday
  429. # cannot remain set to -1 if mktime succeeds.
  430. if tt == -1 and rffi.getintfield(buf, "c_tm_wday") == -1:
  431. raise OperationError(space.w_OverflowError,
  432. space.wrap("mktime argument out of range"))
  433. return space.wrap(float(tt))
  434. if _POSIX:
  435. def tzset(space):
  436. """tzset()
  437. Initialize, or reinitialize, the local timezone to the value stored in
  438. os.environ['TZ']. The TZ environment variable should be specified in
  439. standard Unix timezone format as documented in the tzset man page
  440. (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently
  441. fall back to UTC. If the TZ environment variable is not set, the local
  442. timezone is set to the systems best guess of wallclock time.
  443. Changing the TZ environment variable without calling tzset *may* change
  444. the local timezone used by methods such as localtime, but this behaviour
  445. should not be relied on"""
  446. c_tzset()
  447. # reset timezone, altzone, daylight and tzname
  448. _init_timezone(space)
  449. @unwrap_spec(format=str)
  450. def strftime(space, format, w_tup=None):
  451. """strftime(format[, tuple]) -> string
  452. Convert a time tuple to a string according to a format specification.
  453. See the library reference manual for formatting codes. When the time tuple
  454. is not present, current time as returned by localtime() is used."""
  455. buf_value = _gettmarg(space, w_tup)
  456. # Checks added to make sure strftime() does not crash Python by
  457. # indexing blindly into some array for a textual representation
  458. # by some bad index (fixes bug #897625).
  459. # No check for year since handled in gettmarg().
  460. if rffi.getintfield(buf_value, 'c_tm_mon') < 0 or rffi.getintfield(buf_value, 'c_tm_mon') > 11:
  461. raise OperationError(space.w_ValueError,
  462. space.wrap("month out of range"))
  463. if rffi.getintfield(buf_value, 'c_tm_mday') < 1 or rffi.getintfield(buf_value, 'c_tm_mday') > 31:
  464. raise OperationError(space.w_ValueError,
  465. space.wrap("day of month out of range"))
  466. if rffi.getintfield(buf_value, 'c_tm_hour') < 0 or rffi.getintfield(buf_value, 'c_tm_hour') > 23:
  467. raise OperationError(space.w_ValueError,
  468. space.wrap("hour out of range"))
  469. if rffi.getintfield(buf_value, 'c_tm_min') < 0 or rffi.getintfield(buf_value, 'c_tm_min') > 59:
  470. raise OperationError(space.w_ValueError,
  471. space.wrap("minute out of range"))
  472. if rffi.getintfield(buf_value, 'c_tm_sec') < 0 or rffi.getintfield(buf_value, 'c_tm_sec') > 61:
  473. raise OperationError(space.w_ValueError,
  474. space.wrap("seconds out of range"))
  475. if rffi.getintfield(buf_value, 'c_tm_yday') < 0 or rffi.getintfield(buf_value, 'c_tm_yday') > 365:
  476. raise OperationError(space.w_ValueError,
  477. space.wrap("day of year out of range"))
  478. if rffi.getintfield(buf_value, 'c_tm_isdst') < -1 or rffi.getintfield(buf_value, 'c_tm_isdst') > 1:
  479. raise OperationError(space.w_ValueError,
  480. space.wrap("daylight savings flag out of range"))
  481. if _WIN:
  482. # check that the format string contains only valid directives
  483. length = len(format)
  484. i = 0
  485. while i < length:
  486. if format[i] == '%':
  487. i += 1
  488. if i < length and format[i] == '#':
  489. # not documented by python
  490. i += 1
  491. if i >= length or format[i] not in "aAbBcdfHIjmMpSUwWxXyYzZ%":
  492. raise OperationError(space.w_ValueError,
  493. space.wrap("invalid format string"))
  494. i += 1
  495. i = 1024
  496. while True:
  497. outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor='raw')
  498. try:
  499. buflen = c_strftime(outbuf, i, format, buf_value)
  500. if buflen > 0 or i >= 256 * len(format):
  501. # if the buffer is 256 times as long as the format,
  502. # it's probably not failing for lack of room!
  503. # More likely, the format yields an empty result,
  504. # e.g. an empty format, or %Z when the timezone
  505. # is unknown.
  506. result = rffi.charp2strn(outbuf, intmask(buflen))
  507. return space.wrap(result)
  508. finally:
  509. lltype.free(outbuf, flavor='raw')
  510. i += i