Is there an ISO 8601 format for representing week day and time? - iso8601

I'm trying to specify a format for week day and time combination. Since I've been using ISO 8601 for date time representation, I'm trying to fit weekday/time format as per 8601. It can be easily represented as
1T10:45
where 1 is the week day number (Monday as per 8601). But I'm not sure if this is a valid ISO 8601 representation. Any advice would be very helpful.
Thanks

ISO 8601 defines values for the days of the week but not a format representing only the day of the week (e.g. for unbounded recurring times). You should feel free to adopt the ISO 8601 notation for the day of the week but accept that whatever format you choose will not comply with ISO 8601 unless it specifies the year, the week number, and the day of the week within that week (as in the example 1985-W15-5 from ISO 8601).

Related

How to represent the duration of a calendar year in ISO 8601

I want to define a calendar year with the ISO 8601 standard. Not a specific year, but the general duration. This has to stand for something that has a duration of exactly one calendar year, that starts on the first of January and ends on December 31.
It is not P1Y, I think, because that could be the duration of a year starting anytime. I can only come up something like: P01/12, but that seems ambiguous or not according to the standard syntax.
You may try:
--01-01/P1Y
It’s not very clear to me whether it’s valid, but I think it follows the logic of the ISO 8601 standard. it’s a time interval that begins on January 1 with no year specified and lasts a year.
A time interval is pretty clearly what you are after. According to Wikipedia:
There are four ways to express a time interval:
Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M"
Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z"
Duration only, such as "P1Y2M10DT2H30M", with additional context information
So I have taken number 2. from the list.
Link: Wikipedia article ISO 8601, section Time intervals

Is it possible to specify an ISO Date with a timezone (i.e. GMT offset) but without a time?

I'm trying to see if you can specify a date with a timezone in ISO but without also specifying a time.
This may seem odd to ask about having a timezone without actually having a time, but technically a date represents a range between two times... the 24-hour period spanning from midnight to midnight, and that 'midnight' has to be in a timezone.
In our case, we have an API that wants to say 'Filter things on-or-before date X and on-or-after date Y' and we want the user to specify 'April 9th' (in their time zone) for both to get all things that happen on that day.
Of course we solve this by adding a day to the first date, then changing it to a pure 'before', but the front-end is required to do that math. We can't do it on the backend because having to send a date with a time means we would be sending April 9th at midnight, then on the backend adding a day to that, but what if someone passed in 4pm?
We could fail the date if it has a non-midnight time, but then we're back to why pass it in the first place.
So again, can you have a date with a timezone but not have a time component?
To decode ISO8601 dates only with year-month-day and time zone set the appropriate formatOptions of the ISO8601DateFormatter
let isoFormatter = ISO8601DateFormatter()
isoFormatter.formatOptions = [.withFullDate, .withTimeZone]
If by time zone you mean a UTC offset (as used with ISO 8601 dates with times), this is no problem. If by time zone you mean a true time zone with historic, present and known future offsets from UTC, including for example summer time/DST, like America/New_York or North American Eastern Time, then ISO 8601 does not support that, neither for dates with nor without time of day.
2020-04-25-04:00
This is perfectly valid ISO 8601 for April 25 this year at offset -04:00. So you may use it for representing the interval from 2020-04-25T00:00-04:00 (inclusive) to 2020-04-26T00:00-04:00 (exclusive). Which would then be equivalent to 2020-04-25T04:00Z to 2020-04-26T04:00Z (Z meaning UTC).
Java example code
I don’t know any Swift, so cannot tell you how to format or parse such a string in Swift. In Java formatting it is not bad. Example:
LocalDate date = LocalDate.of(2020, Month.APRIL, 25);
String isoOffsetDateString = date
.atStartOfDay(ZoneId.of("America/New_York"))
.format(DateTimeFormatter.ISO_OFFSET_DATE);
System.out.println(isoOffsetDateString);
Output:
2020-04-25-04:00
I am using Java’s built-in ISO_OFFSET_DATE formatter. The documentation informs us that this formnatter is:
The ISO date formatter that formats or parses a date with an offset,
such as '2011-12-03+01:00'.
Parsing the string and producing the start and end of the day takes a little more:
TemporalAccessor parsed
= DateTimeFormatter.ISO_OFFSET_DATE.parse(isoOffsetDateString);
Instant start = LocalDate.from(parsed)
.atStartOfDay(ZoneOffset.from(parsed))
.toInstant();
Instant end = start.plus(1, ChronoUnit.DAYS);
System.out.println("From " + start + " inclusive to " + end + " exclusive");
From 2020-04-25T04:00:00Z inclusive to 2020-04-26T04:00:00Z exclusive
I have opted to convert to Instant, the class for a moment in time independent of offset or time zone. Instants print in UTC, as the trailing Z on each says. In your Java code you may prefer not to do this conversion or to do a different conversion, all depending on circumstances.
Link
Documentation of DateTimeFormatter.ISO_OFFSET_DATE

How do I express coming Monday in iso8601?

While answering Regexp matching ISO8601 date/time format I wondered how you express first coming Monday or repeat every monday in ISO8601.
My guess would be something like:
W-1
R/W-1
But I cannot find a confirmation of that.
ISO 8601 has the concept of week dates which allow you to specify a week of the year and the day of the year. In the following example 2014-W01-1 means the first week of 2014 on Monday. Then repeat that every week using P1W
R/2014-W01-1T00:00:00/P1W
Seen in this answer

TSQL ISO Month Week Number for ISO Year Week Number

Using TSQL I need to get the ISO Week Number in a Month for a give ISO Year Week Number.
For example: The following code will give me Week #1 for 12/31/2001, which is correct. It is the first Monday in 2002 and the first day of the ISO Year 2002.
select DATEPART(ISO_WEEK, '12-31-2001'); --Week 1 January 2002
My question is how do I...
(1) Take the ISO Week Number Example: ISO Year Week Number: 14 for April 4, 2016 (April Week #1).
(2) Now Take ISO Year Week Number 14 and return April Month Week Number = 1 for the example above.
There seems to be nothing in SQL Server to get the ISO Month Week# from the ISO Year Week Number. I have a function I wrote but it is has some hacks to get it to work, but not 100%.
I think you want something like this... but am not sure why you need the ISO_WEEK. Just replace getdate() with your column.
select datepart(wk, getdate()) - datepart(wk,dateadd(m, DATEDIFF(M, 0, getdate()), 0)) + 1
After attempting to handle getting ISO Month Week Number in functions I decided an easier solution was to create an ISO_Calendar table in SQL Sever.
We know in TSQL you can get the ISO Year Week Number and some a bit of work the ISO Year Number. The ISO Month Week Number is another story.
I build the table shown below with data from 2000 to 2040 by populating all the columns other than the ISO Month number. Then on a second pass I looped through the table and set the ISO Month number based on the Month# in the Monday Date.
Now if I want to get the ISO Month number for a date I check #Date between Monday and Sunday. In my case I am only concerned with dates between Monday and Friday since this is for a stock analysis site.
select #ISOMonthWeekNo = c.ISOMonthWeekNo
from ISO_Calendar c
where #TransDate between c.Monday and c.Sunday;
The advantage is the table is a one time build and easier to verify the accuracy of the data.

Html5 time tag's datetime attribute - day month order

What is the correct format for a html5 <time datetime="" /> attribute? Do days come before month or vice versa:
<time datetime="yyyy-dd-mm"></time>
or
<time datetime="yyyy-mm-dd"></time>
where
mm = Month (ie 01)
dd = Day (ie 20)
It's yyyy-mm-dd, in order to comply with ISO-8601 and general sanity.
From the W3C proposed recommendation:
Note: While the formats described here are intended to be subsets of the corresponding ISO8601 formats, this specification defines parsing rules in much more detail than ISO8601. Implementors are therefore encouraged to carefully examine any date parsing libraries before using them to implement the parsing rules described below; ISO8601 libraries might not parse dates and times in exactly the same manner. [ISO8601]
That's the simplest indication I could find in the recommendation, although there's also the "dates" section which indicates that a date is a valid-month-string followed by a - and then a day, and valid-month-string is defined as:
A string is a valid month string representing a year year and month month if it consists of the following components in the given order:
Four or more ASCII digits, representing year, where year > 0
A "-" (U+002D) character
Two ASCII digits, representing the month month, in the range 1 ≤ month ≤ 12
You should use the format of yyyy-mm-dd
the second one is correct :
<time datetime="yyyy-mm-dd" />