ISO 8601 timestamp

Ideas for new features & functions

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 987
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

ISO 8601 timestamp

Post by Grovkillen » Thu Sep 14, 2023 6:12 am

GetTime and GetDate will return local format of date/time. When dealing with databases it's the ISO 8601 format that is used. I would like to have a GetIsoTimestamp command for this. UTC is always used except in format=2 where the offset is part of the output.

Example output(s):

Format=1 standard
2023‐09‐14T05:11:00Z

Format=2 UTC offset
2023‐09‐13T22:11:00−07:00 UTC−07:00
2023‐09‐14T05:11:00+00:00 UTC+00:00
2023‐09‐14T12:11:00+07:00 UTC+07:00

Format=3 week
2023‐W37

Format=4 week+day
2023‐W37‐4

Format=5 ordinal date
2023‐257

Format=6 date
2023‐09‐14

Format=7 time
05:11:00Z

Format=8 time without special characters
T051100Z

Format=9 timestamp without special characters
20230914T051100Z
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1342
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: ISO 8601 timestamp

Post by Dorian (MJT support) » Thu Sep 14, 2023 6:52 am

Thank you for this. I will make sure to pass it on.
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Grovkillen
Automation Wizard
Posts: 987
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: ISO 8601 timestamp

Post by Grovkillen » Thu Sep 14, 2023 7:25 am

For reference, if done in Python:

Code: Select all

//format=1
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")),ISO_8601_format_1
//format=2
PYExec>import datetime%CRLF%print(datetime.datetime.now().astimezone().replace(microsecond=0).isoformat()),ISO_8601_format_2
//format=3
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-W%V")),ISO_8601_format_3
//format=4
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-W%V-%u")),ISO_8601_format_4
//format=5
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-%j")),ISO_8601_format_5
//format=6
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-%m-%d")),ISO_8601_format_6
//format=7
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("T%H:%M:%SZ")),ISO_8601_format_7
//format=8
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("T%H%M%SZ")),ISO_8601_format_8
//format=9
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")),ISO_8601_format_9

//bonus formats
//epoch time
PYExec>import time%CRLF%print(int(time.time())),epoch
//epoch time as float
PYExec>import time%CRLF%print(time.time()),epoch_float
//epoch in nanoseconds
PYExec>import time%CRLF%print(time.time_ns()),epoch_ns
**BREAKPOINT**
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 987
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: ISO 8601 timestamp

Post by Grovkillen » Thu Sep 14, 2023 11:20 am

I found out that SQL uses a variant of the ISO format:

2023-09-14T13:18:54.357107+02:00

Code: Select all

//format=10
PYExec>import datetime%CRLF%print(datetime.datetime.now().astimezone().isoformat()),ISO_8601_format_10
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 987
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: ISO 8601 timestamp

Post by Grovkillen » Fri Sep 15, 2023 9:01 am

Just a clean up to show the output in a message:

Code: Select all

//format=1
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")),ISO_8601_format_1
//format=2
PYExec>import datetime%CRLF%print(datetime.datetime.now().astimezone().replace(microsecond=0).isoformat()),ISO_8601_format_2
//format=3
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-W%V")),ISO_8601_format_3
//format=4
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-W%V-%u")),ISO_8601_format_4
//format=5
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-%j")),ISO_8601_format_5
//format=6
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y-%m-%d")),ISO_8601_format_6
//format=7
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("T%H:%M:%SZ")),ISO_8601_format_7
//format=8
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("T%H%M%SZ")),ISO_8601_format_8
//format=9
PYExec>import datetime%CRLF%print(datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")),ISO_8601_format_9
//format=10
PYExec>import datetime%CRLF%print(datetime.datetime.now().astimezone().isoformat()),ISO_8601_format_10

//bonus formats
//epoch time
PYExec>import time%CRLF%print(int(time.time())),epoch
//epoch time as float
PYExec>import time%CRLF%print(time.time()),epoch_float
//epoch in nanoseconds
PYExec>import time%CRLF%print(time.time_ns()),epoch_ns

Let>OUTPUT_DATA=OUTPUT:
ConCat>OUTPUT_DATA,%CRLF%%ISO_8601_format_1%
ConCat>OUTPUT_DATA,ISO_8601_format_2
ConCat>OUTPUT_DATA,ISO_8601_format_3
ConCat>OUTPUT_DATA,ISO_8601_format_4
ConCat>OUTPUT_DATA,ISO_8601_format_5
ConCat>OUTPUT_DATA,ISO_8601_format_6
ConCat>OUTPUT_DATA,ISO_8601_format_7
ConCat>OUTPUT_DATA,ISO_8601_format_8
ConCat>OUTPUT_DATA,ISO_8601_format_9
ConCat>OUTPUT_DATA,ISO_8601_format_10
ConCat>OUTPUT_DATA,epoch
ConCat>OUTPUT_DATA,epoch_float
ConCat>OUTPUT_DATA,epoch_ns

MDL>OUTPUT_DATA
Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts