PageRenderTime 72ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/rctime/interp_time.py

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