Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

strftime subtracting time

Hey HF, long time no see.

Got a script which i need to subtract 25 minutes from, essentially at the moment data is being gathered from a SQL database when being input/plotted into an Excel spreadsheet. The time on the spreadsheet needs to be 25 minutes sooner than what the SQL Database outputs - What i've got at the moment is below.

PHP Code

<?php 
plotsheet
.write(ssrow,1,sector['std'].strftime("%H:%M"), boldformat) 

Can't seem to find any documentation on strftime which explains what i want, i assume it needs to look something like the below, although its not working:
PHP Code

<?php 
plotsheet
.write(ssrow,1,sector['std'].strftime("%H:%M - 15M"), boldformat) 

Any suggestions?


TIA 🤝
(Jan 14, 2019 06:10 PM)Lockheed Wrote:  Hey HF, long time no see.

Got a script which i need to subtract 25 minutes from, essentially at the moment data is being gathered from a SQL database when being input/plotted into an Excel spreadsheet. The time on the spreadsheet needs to be 25 minutes sooner than what the SQL Database outputs - What i've got at the moment is below.

PHP Code

<?php 
plotsheet
.write(ssrow,1,sector['std'].strftime("%H:%M"), boldformat) 

Can't seem to find any documentation on strftime which explains what i want, i assume it needs to look something like the below, although its not working:
PHP Code

<?php 
plotsheet
.write(ssrow,1,sector['std'].strftime("%H:%M - 15M"), boldformat) 

Any suggestions?


TIA  🤝

First of all nice name 🤝
You can find info on strftime here:
https://docs.python.org/2/library/dateti...e-behavior
http://strftime.org/

But I'm not entirely sure what you're trying to do. Do you want to take the current time (the time the script is being executed) and subtract 25 minutes from it, then write
it into a spreadsheet? Or get a time from a SQL DB then sub 25 mins from that and write it into a spreadsheet?

Either way strftime() won't really help you there, it only gives you a string representation of a date object in a specified format. And "-15M" isn't a format specifier.
I think what you need is the timedelta() function (also documented in the link above).

Code
from datetime import datetime
from datetime import timedelta

now = datetime.today() #gives you current time
earlier = now - timedelta(minutes=25) # subtract 25 mins from now
strtime = earlier.strftime("%H:%M") #format the datetime object into a string
[Image: 0DvFxnA.gif]
Donate: 3ABKRghau93ziRaDfVwiiwtjky3i1gLrKn
(Jan 15, 2019 03:29 AM)lockhead Wrote:  First of all nice name  🤝
You can find info on strftime here:
https://docs.python.org/2/library/dateti...e-behavior
http://strftime.org/

But I'm not entirely sure what you're trying to do. Do you want to take the current time (the time the script is being executed) and subtract 25 minutes from it, then write
it into a spreadsheet? Or get a time from a SQL DB then sub 25 mins from that and write it into a spreadsheet?

Either way strftime() won't really help you there, it only gives you a string representation of a date object in a specified format. And "-15M" isn't a format specifier.
I think what you need is the timedelta() function (also documented in the link above).

Code
from datetime import datetime
from datetime import timedelta

now = datetime.today() #gives you current time
earlier = now - timedelta(minutes=25) # subtract 25 mins from now
strtime = earlier.strftime("%H:%M") #format the datetime object into a string

Ha, thanks.

Will look into it - thanks for the code you included :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by kplishereJan 10, 2019 07:59 PM 1 201 Jan 10, 2019 07:59 PM
Last Post: Europa
[Java] by CorkNov 22, 2015 02:07 AM 5 201 Nov 22, 2015 02:07 AM
Last Post: rusia
[.NET] by SapientiaJul 16, 2011 04:37 PM 3 69 Jul 16, 2011 04:37 PM
Last Post: Cody8295