PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/time/interp_time.py

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