summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Georg <mail@jensge.org>2022-05-01 09:58:02 +0200
committerJens Georg <mail@jensge.org>2022-05-01 09:58:02 +0200
commit416009efb19e3247b295852d9d46a823559bcc34 (patch)
tree5565d92d1e644b10f35cf0c2c084730b4f3b89e0
parentd0fe08e70029fa27220026db548f6f1b7c974df4 (diff)
downloadgupnp-av-416009efb19e3247b295852d9d46a823559bcc34.tar.gz
Add helper function for iso8601
DLNA expects a certain subset of ISO8601 which this function provides
-rw-r--r--libgupnp-av/gupnp-didl-lite-object.c46
-rw-r--r--libgupnp-av/gupnp-didl-lite-object.h3
-rw-r--r--vala/GUPnPAV-1.0.metadata1
3 files changed, 50 insertions, 0 deletions
diff --git a/libgupnp-av/gupnp-didl-lite-object.c b/libgupnp-av/gupnp-didl-lite-object.c
index 67a683c..fd3090b 100644
--- a/libgupnp-av/gupnp-didl-lite-object.c
+++ b/libgupnp-av/gupnp-didl-lite-object.c
@@ -2645,3 +2645,49 @@ gupnp_didl_lite_object_get_xml_string (GUPnPDIDLLiteObject *object)
return ret;
}
+
+/**
+ * gupnp_format_date_time_for_didl_lite:
+ * @date_time: DateTime to format
+ *
+ * Get the representation of DateTime as an ISO8601 string.
+ *
+ * DLNA requires a specific subset of ISO8601
+ * Returns: (transfer full): @date_time formatted as an ISO8601 string
+ */
+char *
+gupnp_format_date_time_for_didl_lite (GDateTime *date_time, gboolean date_only)
+{
+ g_return_val_if_fail (date_time != NULL, NULL);
+
+ if (date_only) {
+ return g_date_time_format (date_time, "%F");
+ }
+
+ const char *format = "%FT%H:%M:%S";
+ char *base_string = g_date_time_format (date_time, format);
+ GString *iso_string = g_string_new (base_string);
+
+ // Check if we have sub-second precision. If so, we use that as well,
+ // but cannot use %f since that will use microsecond precision, but DLNA
+ // only allows for millisecond so we append the milliseconds manually
+ if (g_date_time_get_microsecond (date_time) % G_TIME_SPAN_SECOND != 0) {
+ g_string_append_printf (
+ iso_string,
+ ".%03d",
+ g_date_time_get_microsecond (date_time) / 1000);
+ }
+
+ GTimeSpan utc_offset = g_date_time_get_utc_offset (date_time);
+ if (utc_offset == 0) {
+ g_string_append (iso_string, "Z");
+ } else {
+ char *time_zone = g_date_time_format (date_time, "%:z");
+ g_string_append (iso_string, time_zone);
+ g_free (time_zone);
+ }
+
+ g_free (base_string);
+
+ return g_string_free (iso_string, FALSE);
+}
diff --git a/libgupnp-av/gupnp-didl-lite-object.h b/libgupnp-av/gupnp-didl-lite-object.h
index be250f0..e7719c2 100644
--- a/libgupnp-av/gupnp-didl-lite-object.h
+++ b/libgupnp-av/gupnp-didl-lite-object.h
@@ -277,6 +277,9 @@ gupnp_didl_lite_object_apply_fragments
char *
gupnp_didl_lite_object_get_xml_string (GUPnPDIDLLiteObject *object);
+char *
+gupnp_format_date_time_for_didl_lite (GDateTime *date_time, gboolean date_only);
+
G_END_DECLS
#endif /* __GUPNP_DIDL_LITE_OBJECT_H__ */
diff --git a/vala/GUPnPAV-1.0.metadata b/vala/GUPnPAV-1.0.metadata
index 0f6b360..82a19e7 100644
--- a/vala/GUPnPAV-1.0.metadata
+++ b/vala/GUPnPAV-1.0.metadata
@@ -12,3 +12,4 @@ SearchCriteriaParser
.expression skip
ProtocolError skip
protocol_error_quark skip
+format_date_time_for_didl_lite.date_only default=false