Moduł Pythona do zmiany daty i czasu systemu
Jak mogę zmienić systemową datę, godzinę, strefę czasową w Pythonie? Czy jest do tego jakiś moduł?
- nie chcę wykonywać żadnych poleceń systemowych
- chcę jednego wspólnego rozwiązania, które powinno działać zarówno na Unix i Windows.
18
3 answers
import sys
import datetime
time_tuple = ( 2012, # Year
9, # Month
6, # Day
0, # Hour
38, # Minute
0, # Second
0, # Millisecond
)
def _win_set_time(time_tuple):
import pywin32
# http://timgolden.me.uk/pywin32-docs/win32api__SetSystemTime_meth.html
# pywin32.SetSystemTime(year, month , dayOfWeek , day , hour , minute , second , millseconds )
dayOfWeek = datetime.datetime(time_tuple).isocalendar()[2]
pywin32.SetSystemTime( time_tuple[:2] + (dayOfWeek,) + time_tuple[2:])
def _linux_set_time(time_tuple):
import ctypes
import ctypes.util
import time
# /usr/include/linux/time.h:
#
# define CLOCK_REALTIME 0
CLOCK_REALTIME = 0
# /usr/include/time.h
#
# struct timespec
# {
# __time_t tv_sec; /* Seconds. */
# long int tv_nsec; /* Nanoseconds. */
# };
class timespec(ctypes.Structure):
_fields_ = [("tv_sec", ctypes.c_long),
("tv_nsec", ctypes.c_long)]
librt = ctypes.CDLL(ctypes.util.find_library("rt"))
ts = timespec()
ts.tv_sec = int( time.mktime( datetime.datetime( *time_tuple[:6]).timetuple() ) )
ts.tv_nsec = time_tuple[6] * 1000000 # Millisecond to nanosecond
# http://linux.die.net/man/3/clock_settime
librt.clock_settime(CLOCK_REALTIME, ctypes.byref(ts))
if sys.platform=='linux2':
_linux_set_time(time_tuple)
elif sys.platform=='win32':
_win_set_time(time_tuple)
Nie mam komputera z systemem windows, więc nie testowałem go na windows... Ale masz pomysł.
27
Author: tMC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-06 15:24:26
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-06 15:24:26
Musiałem trochę zmodyfikować wersję win32 odpowiedzi tMC:
def _win_set_time(time_tuple):
import win32api
dayOfWeek = datetime(*time_tuple).isocalendar()[2]
t = time_tuple[:2] + (dayOfWeek,) + time_tuple[2:]
win32api.SetSystemTime(*t)
Np. kiedy używam go do ustawiania czasu według starego serwera czasu (protokół czasu, RFC868) robię to aproximately w ten sposób:
data = s.recv(4)
remote_time = (ord(data[0])<<24) + (ord(data[1])<<16) + (ord(data[2])<<8) + ord(data[3])
remote_time -= 2208988800
_win_set_time(time.gmtime(remote_time)[0:6] + (0,))
4
Author: jk987,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-27 07:45:21
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-27 07:45:21
Cóż na razie myślę, że jest to najlepsze rozwiązanie
import sys,os
def change(s):
if s == 1:os.system('date -s "2 OCT 2006 18:00:00"')#don't forget to change it , i've used date command for linux
elif s == 2:
try:
import pywin32
except ImportError:
print 'pywin32 module is missing'
sys.exit(1)
pywin32.SetSystemTime(year, month , dayOfWeek , day , hour , minute , second , millseconds )# fill all Parameters with int numbers
else:print 'wrong param'
def check_os():
if sys.platform=='linux2':change(1)
elif sys.platform=='win32':change(2)
else:print 'unknown system'
Na razie jest to tymczasowe rozwiązanie, mam nadzieję, że będzie pomocne, a także zajrzyj
Http://timgolden.me.uk/pywin32-docs/win32api__SetSystemTime_meth.html
Http://timgolden.me.uk/pywin32-docs/win32api__SetLocalTime_meth.html
-1
Author: Hamoudaq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 22:30:10
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 22:30:10