Python 3.6+ now provides tm_gmtoff and tm_zone on all platforms:
https://docs.python.org/3/library/time.html#time.struct_time
Previously:
6da6b774b03aa0e2629931788b901ca9f5e5eb0a (tag: 0.27-rc4)
Get TZ offset from C localtime, given tm_gmtoff
Signed-off-by: Rob Browning <
r...@defaultvalue.org>
Tested-by: Rob Browning <
r...@defaultvalue.org>
---
Proposed for main.
configure | 12 ------------
lib/bup/_helpers.c | 30 ------------------------------
lib/bup/helpers.py | 40 +++-------------------------------------
test/int/test_git.py | 1 +
4 files changed, 4 insertions(+), 79 deletions(-)
diff --git a/configure b/configure
index e3778b4c..f202e7a4 100755
--- a/configure
+++ b/configure
@@ -360,18 +360,6 @@ if test "${c_define[BUP_HAVE_READLINE]:-}"; then
fi
fi
-info -n 'checking for tm tm_gmtoff field'
-if try-c-code \
- -- \
- '#include <time.h>\n' \
- 'struct tm t;\n' \
- 'int main(int argc, char **argv) { return (int) sizeof(t.tm_gmtoff); }\n';
- then
- c_define[HAVE_TM_TM_GMTOFF]=1
- info ' (found)'
-else
- info ' (not found)'
-fi
info -n 'checking for libacl'
if pkg-config libacl; then
diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c
index cfa9af0b..224a2ebf 100644
--- a/lib/bup/_helpers.c
+++ b/lib/bup/_helpers.c
@@ -1092,32 +1092,6 @@ static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
}
#endif /* def BUP_HAVE_FILE_ATTRS */
-
-#ifdef HAVE_TM_TM_GMTOFF
-static PyObject *bup_localtime(PyObject *self, PyObject *args)
-{
- long long lltime;
- time_t ttime;
- if (!PyArg_ParseTuple(args, "L", &lltime))
- return NULL;
- if (!INTEGRAL_ASSIGNMENT_FITS(&ttime, lltime))
- return PyErr_Format(PyExc_OverflowError, "time value too large");
-
- struct tm tm;
- tzset();
- if(localtime_r(&ttime, &tm) == NULL)
- return PyErr_SetFromErrno(PyExc_OSError);
-
- // Match the Python struct_time values.
- return Py_BuildValue("[i,i,i,i,i,i,i,i,i,i,s]",
- 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
- tm.tm_hour, tm.tm_min, tm.tm_sec,
- tm.tm_wday, tm.tm_yday + 1,
- tm.tm_isdst, tm.tm_gmtoff, tm.tm_zone);
-}
-#endif /* def HAVE_TM_TM_GMTOFF */
-
-
static unsigned int vuint_encode(long long val, char *buf)
{
unsigned int len = 0;
@@ -1818,10 +1792,6 @@ static PyMethodDef helper_methods[] = {
#ifdef BUP_HAVE_FILE_ATTRS
{ "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
"Set the Linux attributes for the given file." },
-#endif
-#ifdef HAVE_TM_TM_GMTOFF
- { "localtime", bup_localtime, METH_VARARGS,
- "Return struct_time elements plus the timezone offset and name." },
#endif
{ "bytescmp", bup_bytescmp, METH_VARARGS,
"Return a negative value if x < y, zero if equal, positive otherwise."},
diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py
index 8edace9b..32bafeca 100644
--- a/lib/bup/helpers.py
+++ b/lib/bup/helpers.py
@@ -1,13 +1,12 @@
"""Helper functions and classes for bup."""
-from collections import namedtuple
from contextlib import ExitStack, nullcontext
from ctypes import sizeof, c_void_p
-from math import floor
from os import environ, fsencode
from random import SystemRandom
from subprocess import PIPE, Popen
from tempfile import mkdtemp
+from time import localtime
from shutil import rmtree
import sys, os, subprocess, errno, select, mmap, stat, re, struct
import hashlib, heapq, math, operator, time
@@ -1140,41 +1139,8 @@ def grafted_path_components(graft_points, path):
Sha1 = hashlib.sha1
-_localtime = getattr(_helpers, 'localtime', None)
-
-if _localtime:
- bup_time = namedtuple('bup_time', ['tm_year', 'tm_mon', 'tm_mday',
- 'tm_hour', 'tm_min', 'tm_sec',
- 'tm_wday', 'tm_yday',
- 'tm_isdst', 'tm_gmtoff', 'tm_zone'])
-
-# Define a localtime() that returns bup_time when possible. Note:
-# this means that any helpers.localtime() results may need to be
-# passed through to_py_time() before being passed to python's time
-# module, which doesn't appear willing to ignore the extra items.
-if _localtime:
- def localtime(time):
- return bup_time(*_helpers.localtime(int(floor(time))))
- def utc_offset_str(t):
- """Return the local offset from UTC as "+hhmm" or "-hhmm" for time t.
- If the current UTC offset does not represent an integer number
- of minutes, the fractional component will be truncated."""
- off = localtime(t).tm_gmtoff
- # Note: // doesn't truncate like C for negative values, it rounds down.
- offmin = abs(off) // 60
- m = offmin % 60
- h = (offmin - m) // 60
- return b'%+03d%02d' % (-h if off < 0 else h, m)
- def to_py_time(x):
- if isinstance(x, time.struct_time):
- return x
- return time.struct_time(x[:9])
-else:
- localtime = time.localtime
- def utc_offset_str(t):
- return time.strftime('%z', localtime(t)).encode('ascii')
- def to_py_time(x):
- return x
+def utc_offset_str(t):
+ return time.strftime('%z', localtime(t)).encode('ascii')
_some_invalid_save_parts_rx = re.compile(br'[\[ ~^:?*\\]|\.\.|//|@{')
diff --git a/test/int/test_git.py b/test/int/test_git.py
index 1230ce16..cf10e8fe 100644
--- a/test/int/test_git.py
+++ b/test/int/test_git.py
@@ -4,6 +4,7 @@ from binascii import hexlify, unhexlify
from contextlib import ExitStack
from subprocess import check_call
from functools import partial
+from time import localtime
import struct, os
import pytest
--
2.47.3