【发布时间】:2017-01-15 21:56:16
【问题描述】:
我使用 datetime 从系统获取当前时间并将其存储为字符串(timenow),但是当我通过 sshclient_exec_command 将其发送到 linux 中设置时存在一些行为差异。
下面是我的代码:
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)
输出
timenow=2016-09-07 20:15:26 ip_time_now=2016-09-07 21:06:24
timenow 和 ip_time_now 在操作上应该是相同的
如果我将 timenow 行替换为
timenow = datetime.datetime.utcnow().strftime("%H:%M:%S") #passes, but without
setting the year and month
输出
timenow=20:25:49 ip_time_now=2016-09-07 20:25:50 #1 sec diff is ok
注意:执行命令时输出没有异常
strftime 语法有什么可能的解决方案?
【问题讨论】:
标签: python-3.x strftime