ISO 8601 timestamp

Ideas for new features & functions

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1128
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.27
version history

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
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: 1128
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.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1128
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.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1128
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.27
version history

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

Re: ISO 8601 timestamp

Post by Grovkillen » Wed Sep 25, 2024 3:54 pm

Bump on a GetIsoTimestamp command :)
ISO 8601 defines several formats for representing date and time depending on the use case. Below are the nine main formats covered by the standard:

1. Calendar Date
Format: YYYY-MM-DD
Example: 2024-09-25
Describes a specific date in a calendar with year, month, and day.

2. Calendar Date Without Separators
Format: YYYYMMDD
Example: 20240925
A compact format for the same calendar date as above but without dashes.

3. Date with Week Number
Format: YYYY-Www-D
Example: 2024-W39-3
Describes a date using year, week number (ww), and weekday (D, where Monday = 1 and Sunday = 7). Often used in manufacturing and industry.

4. Date with Week Number Without Weekday
Format: YYYY-Www
Example: 2024-W39
Describes just the year and week number, without a specific weekday.

5. Ordinal Date (Day of the Year)
Format: YYYY-DDD
Example: 2024-268
Describes a date as the nth day of the year, where DDD is a day in the year (001–365, or 001–366 for leap years).

6. Combined Calendar Date and Time
Format: YYYY-MM-DDThh:mm:ss
Example: 2024-09-25T14:30:45
Combines a date and time, using "T" as a separator between the date and time. It may also include a time zone (e.g., Z for UTC or an offset).

7. Time of Day
Format: hh:mm:ss
Example: 14:30:45
A time of day in hours, minutes, and seconds in 24-hour format.

8. Time of Day Without Separators
Format: hhmmss
Example: 143045
A compact format for time without colons.

9. Time Zone Offset
Format: ±hh:mm or ±hhmm
Example: +02:00 or +0200
Specifies the time zone offset from UTC in hours and minutes. If the time is in UTC, Z is used instead of a numeric offset.
Example:
2024-09-25T14:30:45Z or
2024-09-25T14:30:45+02:00
And this :
Grovkillen wrote:
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
For reference, Android uses format 2 plus 8 when naming images and log files.
Let>ME=%Script%

Running: 15.0.27
version history

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