Syntax for aggrevate year: month: date with strftime command

I get the current time from the system with datetime and save it as a string (timenow), but there is some difference in behavior when I send it for installation on linux via sshclient_exec_command.

Below is my code:

timenow = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
command = 'date -s %s' %timenow
stdin, stdout, stderr = self._sshclient.exec_command(command, timeout=10)
try:
    command = 'date +"%Y-%m-%d %H:%M:%S"'
    stdin, stdout, stderr = self._sshclient.exec_command(command, timeout=10)
    ip_time_now = stdout.read().decode(encoding='UTF-8').rstrip('\n')
    self.logger.debug(" ip=%s timenow=%s ip_time_now=%s",ip, timenow,ip_time_now)

Output

 timenow=2016-09-07 20:15:26 ip_time_now=2016-09-07 21:06:24

Both timenow and ip_time_now should be the same of operations

Here, if I replace the timenow string with

timenow = datetime.datetime.utcnow().strftime("%H:%M:%S")  #passes, but without 
                                                      setting the year and month

Output

timenow=20:25:49 ip_time_now=2016-09-07 20:25:50 #1 sec diff is ok

Note. An exception is not output when executing commands

What could be possible for strftime syntax?

+4
source share
1 answer

Replacement

command = 'date -s %s' %timenow

with

command = 'date --set "%s"' %timenow

will decide.

+1
source

Source: https://habr.com/ru/post/1653968/


All Articles