Working with ISO 8601 Date format

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Working with ISO 8601 Date format

Post by andyh1234 » Mon Oct 28, 2024 11:06 pm

Hi,

Is there an easy way yo work with the ISO 8601 date format.

The date is written as

2024-10-27T01:30:00Z or
2024-10-27T01:30:00+01:00

Where the date and time are separated by a letter T.

I am currently parsing the date into a comma separated string as used by the dateItems command, but am struggling with the UTC part, it is either Z for zulu (do nothing) or + or - a value for daylight savings time. I convert my string into the internet date format but it is adding an hour onto it before I even try to parse the UTC part at the end.

I would just like to convert it into seconds so I can work with it, and am getting the right time.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Working with ISO 8601 Date format

Post by jacque » Tue Oct 29, 2024 5:46 pm

The trailing number isn't daylight savings, it's the time zone. In the central US where I am, we're currently at - 5. When we go back to standard time we'll be at - 6. So if you want to translate my current time to universal time, add the last number to the hours in the date calculation.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10315
Joined: Wed May 06, 2009 2:28 pm

Re: Working with ISO 8601 Date format

Post by dunbarx » Tue Oct 29, 2024 6:42 pm

What Jacque said.

Know that the "Z" is "Zulu", or Universal Time Coordinated (UTC) It is what used to be called GMT, Greenwich Mean Time. It would essentially be "+0"

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10094
Joined: Fri Feb 19, 2010 10:17 am

Re: Working with ISO 8601 Date format

Post by richmond62 » Wed Oct 30, 2024 11:34 am

Screenshot 2024-10-30 at 12.32.57.png
-
Rude, Crude, and Competent. 8)

Code: Select all

on mouseUp
   ask "Enter a date in ISO 8601 format"
   put it into FULLD
   set the itemDelimiter to "T"
   put item 1 of FULLD into FIRSTD
   put item 2 of FULLD into SECONDD
   set the itemdelimiter to "-"
   put item 1 of FIRSTD into YEARX
   put item 2 of FIRSTD into MONTHX
   put item 3 of FIRSTD into DAYX
   put "US" && MONTHX & "/" & DAYX & "/" & YEARX into fld "USD"
   put "GB" && DAYX & "/" & MONTHX & "/" & YEARX into fld "GBD"
   if SECONDD contains "Z" then
      set the itemDelimiter to "Z"
   else
      set the itemDelimiter to "+"
   end if
   put "LOCAL:" && item 1 of SECONDD into fld "LT"
   if item 2 of SECONDD is not empty then
      put item 2 of SECONDD into ZONE
      put item 1 of SECONDD into HOMEX
      set the itemDelimiter to ":"
      put item 1 of ZONE into HOURY
      put item 2 of ZONE into MINY
      put item 1 of HOMEX into HOURX
      put item 2 of HOMEX into MINX
      put (HOURX + HOURY) into HOURZ
      put (MINX + MINY) into MINZ
      put "UNIVERSAL:" && HOURZ & ":" & MINZ into fld "UT"
   end if
end mouseUp
Attachments
ISO 8601 Cracker.livecode.zip
(1.4 KiB) Downloaded 1125 times

andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Re: Working with ISO 8601 Date format

Post by andyh1234 » Wed Oct 30, 2024 1:22 pm

Thanks everyone

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Working with ISO 8601 Date format

Post by stam » Wed Oct 30, 2024 1:26 pm

Except ISO 8601 is not a simple format. Many different string formats are possible under this.

Date formats allowed:

Code: Select all

YYYY-MM-DD	or	YYYYMMDD
YYYY-MM	(but not YYYYMM)
You can have ordinal dates (eg the 266th day of the year 2024)

Code: Select all

YYYY-DDD	or	YYYYDDD
Time is optional extra. Again lots of forms but most will be hh:mm:ss - however the "T" separator is optional if "unambiguous" context.
So valid expressions would be both "2024-10-30T11:30:00" and "20241030 11:30:00"

Then there is an optional time offset denoted by +, - or Z

Code: Select all

<time>Z // zulu time
<time>±hh:mm
<time>±hhmm
<time>±hh
Personally I would be looking at creating a small library to convert ISO 8601 to dateItems, which is LiveCode's date/time variable format. From there you can get US/UK dates, long/short/internet dates, or any part of this.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10094
Joined: Fri Feb 19, 2010 10:17 am

Re: Working with ISO 8601 Date format

Post by richmond62 » Wed Oct 30, 2024 1:50 pm

That is a 'real pain'.

I did indicate my stack was 'rude and crude', and for something more complex you will need something, err, more complex than my 10 minutes' worth. 8)

Post Reply