Addressing a question where a REST API is used to schedule an automation plan, where a literal start time is not accepted. Per the schema, only TimeOffsets are allowed.
<xsd:complexType name="PlanSchedule" mixed="true">
<xsd:sequence>
<xsd:element name="HasStartTime" type="xsd:boolean"/>
<xsd:element name="StartDateTimeOffset" type="TimeInterval" minOccurs="0" maxOccurs="1" />
<xsd:element name="HasEndTime" type="xsd:boolean"/>
<xsd:element name="EndDateTimeOffset" type="TimeInterval" minOccurs="0" maxOccurs="1" />
<xsd:element name="UseUTCTime" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
where TimeOffset is an ISO8601 Duration â actually a subset of the ISO8601 Duration, as we donât accept Years or Months, only Days,Hours,Minutes,Seconds
<xsd:simpleType name="TimeInterval">
<xsd:restriction base="xsd:duration">
<!--
Duration Data Type:
The duration data type is used to specify a time interval.
The time interval is specified in the following form "PnYnMnDTnHnMnS" where:
* P indicates the period (required)
* nY indicates the number of years (not used here)
* nM indicates the number of months (not used here)
* nD indicates the number of days
* T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds)
* nH indicates the number of hours
* nM indicates the number of minutes
* nS indicates the number of seconds
-->
<xsd:pattern value="\-?P([0-9]+D)?(T([0-9]+H)?([0-9]+M)?([0-9]+(\.[0-9]{1,6})?S)?)?" />
</xsd:restriction>
</xsd:simpleType>
This Python snippet, given Date/Time String, should calculate the time duration between the given time and the current time, and output an ISO8601-formatted string that we can accept:
import datetime
# The time we want the action to start, and the format in which we supply the time string
# see https://docs.python.org/3/library/datetime.html
schedule_time = datetime.datetime.strptime(
"2022-09-30 23:30:00 -0600", "%Y-%m-%d %H:%M:%S %z"
)
# The current time now, with local machine time zone
time_now = datetime.datetime.astimezone(datetime.datetime.now())
# the python datetime duration between the schedule and now
time_delta = schedule_time - time_now
# the total number of seconds in that interval, cast as Integer to ignore milliseconds and microseconds
seconds = int(time_delta.total_seconds())
# split minutes and seconds, then hours and minutes, then days and hours
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
iso_duration = f"P{days}DT{hours}H{minutes}M{seconds}S"
print(iso_duration)
In my case, given that I executed this shortly after 2022-09-27 at 4pm CST, the result printed is
P3DT8H22M36S
representing a Period of 3 Days, with Time of 8 Hours, 22 Minutes, and 36 Seconds