Print current date/time in email

Posted on
Sun Oct 03, 2021 5:49 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Print current date/time in email

I would like to include the current time of an email in the email text. Is there an indigo function I could use?
Since I was unable to find one, I created a variable and have written a python script to assign the date/tiime to the indigo variable.
It's not working:

Code: Select all
import datetime;
currentTime = indigo.variables[524935544].getValue(int)   #current time - indigo variable
ct = datetime.datetime.now() #ct - python variable
indigo.variable.updateValue(524935544, ct)


The script returns the text "now".
Can I do this inside indigo? If not, is there a fix to the script above?
Thank you,
Bob

Posted on
Mon Oct 04, 2021 10:18 am
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Print current date/time in email

Indigo variable values are always strings, so just convert the datetime object into a string when storing it:

Code: Select all
import datetime
currentTime = indigo.variables[524935544].getValue(int)   #current time - indigo variable
ct = datetime.datetime.now() #ct - python variable
indigo.variable.updateValue(524935544, str(ct))

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Oct 04, 2021 3:13 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Print current date/time in email

I pasted your reply with the 'str' added, and still get the same result, "now".

Posted on
Mon Oct 04, 2021 4:07 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Print current date/time in email

At the very least, you need to remove that semi-colon on the first line. Python doesn't use semi-colons.

And the second line isn't being used at all. Delete it.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 04, 2021 4:24 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Print current date/time in email

Code: Select all
import datetime
ct = datetime.datetime.now() #ct - python variable
indigo.variable.updateValue(524935544, str(ct))


still returns 'now'

Posted on
Mon Oct 04, 2021 4:34 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Print current date/time in email

Are you sure you have the right identifier for the variable? Did you try clearing it out and then running the script?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 04, 2021 4:35 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Print current date/time in email

Because the rest of the code works correctly:

Code: Select all
Python 2.7.16 (default, Aug 30 2021, 14:43:11)
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> ct = datetime.datetime.now()
>>> print ct
2021-10-04 18:35:08.049862

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 04, 2021 4:40 pm
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Print current date/time in email

Works for me (substituted a variable ID from my variables):

Code: Select all
>>> import datetime
>>> ct = datetime.datetime.now() #ct - python variable
>>> indigo.variable.updateValue(534204190, str(ct))
>>> print(indigo.variables[534204190].value)
2021-10-04 17:37:48.647020

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Oct 04, 2021 4:47 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Print current date/time in email

works for me too.

Posted on
Mon Oct 04, 2021 4:49 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Print current date/time in email

SMUSEBY wrote:
works for me too.


What works? Or still doesn't work?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 04, 2021 4:54 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Print current date/time in email

I thought I had sent an explanatory reply - I must have inadvertently deleted it .
Your suggestion to check the variable (it is OK) prompted me to check the basics, What I found was that I had created a second version of the file with BBE and I was editing one and running the other. No surprise - the edits weren't effective.
As usual, thank you for your patience and assistance.
Bob

Posted on
Wed Oct 06, 2021 2:09 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Print current date/time in email

I am able to use the current time in email, but the precision is unnecessary.
I have attempted to truncate the string with the additional code at line 3 where I want to display the first 19 characters in the string:
Code: Select all
import datetime
ct = datetime.datetime.now() #ct - python variable
HMS = ct[0:19]
indigo.variable.updateValue(524935544, str(HMS))


Indigo does not like my 'improvement':
Script Error currentTime.py: 'datetime.datetime' object has no attribute '__getitem__'
Script Error Exception Traceback (most recent call shown last):

currentTime.py, line 3, at top level
TypeError: 'datetime.datetime' object has no attribute '__getitem__'


I also tried using "ct" in both lines 3 & 4.

Posted on
Wed Oct 06, 2021 2:21 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Print current date/time in email

'ct' is a datetime object, not a string. You need to convert it to a string before you truncate.

Or you could use the datatime formating options to get exactly the string you want from the ct object, and not do the truncation hack.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Oct 06, 2021 4:02 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Print current date/time in email

This works - is there a better way? - cleaned up?
Code: Select all
import datetime
ct = datetime.datetime.now() #ct - python object
s1 = str(ct) #converts ct to string
HMS = s1[5:16] #drops year, and sets precision to minutes ([:19] for seconds)
indigo.variable.updateValue(524935544, str(HMS))

Posted on
Wed Oct 06, 2021 4:18 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Print current date/time in email

Code: Select all
import datetime
HMS = str(datetime.datetime.now())[5:16] #drops year, and sets precision to minutes ([:19] for seconds)
indigo.variable.updateValue(524935544, HMS)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Who is online

Users browsing this forum: No registered users and 1 guest