2016-09-08 2 views
4

システムから現在の時刻を取得して文字列(timenow)として保存していますが、sshclient_exec_commandを使用してlinuxにセットするときに動作に違いがあります。aggrevateとの構文:年月:strftimeコマンドの日付

以下

私のコードです:出力

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

両方timenow

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ラインを交換する場合ip_time_nowが操作ここで

から同じことになっています

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の構文のための可能な解決策になる何のコマンド

を実行中に例外が出力にありませんか?

答えて

1

解決します

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

command = 'date -s %s' %timenow 

を交換します。

関連する問題