Pyspark adding few seconds to time
<p>I am trying to add few seconds to time but I haven't been successful. Here is my example</p>

<pre><code>import datetime
str1 = sc.parallelize(["170745","140840"])
aa = str1.map(lambda l: datetime.datetime.strptime(l, '%H%M%S').strftime('%H:%M:%S'))
</code></pre>

<p>yields</p>

<pre><code>['17:07:45', '14:08:40'] 
</code></pre>

<p>but I want is</p>

<pre><code>['17:07:52', '14:08:47']
</code></pre>

<p>How could I add 7 seconds to each converted time. I know <code>timedelta</code> is there but not sure about that.</p>

<p>You can use "+datetime.timedelta(seconds=7)" to solve this problem.</p>

<pre><code>(datetime.datetime.strptime(l, '%H%M%S') + datetime.timedelta(seconds=7)).strftime('%H:%M:%S')
</code></pre>

<p>If you want to calculates the number of seconds between two times, minus two datetime.datetime object directly.</p>

<pre><code>import datetime
starttime = datetime.datetime.now()
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
</code></pre>

python datetime pyspark