Merge branch 'master' into 'master'

gdatetime: Add %f format specifier and microsecond precision to g_date_time_format_iso8601

See merge request GNOME/glib!1605
This commit is contained in:
Philip Withnall 2020-08-12 13:37:06 +00:00
commit 056aaf36f8
2 changed files with 28 additions and 6 deletions

View File

@ -3132,6 +3132,10 @@ g_date_time_format_utf8 (GDateTime *datetime,
format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
g_date_time_get_day_of_month (datetime));
break;
case 'f':
g_string_append_printf (outstr, "%06" G_GUINT64_FORMAT,
datetime->usec % G_TIME_SPAN_SECOND);
break;
case 'F':
g_string_append_printf (outstr, "%d-%02d-%02d",
g_date_time_get_year (datetime),
@ -3180,9 +3184,6 @@ g_date_time_format_utf8 (GDateTime *datetime,
format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
(g_date_time_get_hour (datetime) + 11) % 12 + 1);
break;
case 'n':
g_string_append_c (outstr, '\n');
break;
case 'm':
format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
g_date_time_get_month (datetime));
@ -3191,6 +3192,9 @@ g_date_time_format_utf8 (GDateTime *datetime,
format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
g_date_time_get_minute (datetime));
break;
case 'n':
g_string_append_c (outstr, '\n');
break;
case 'O':
alt_digits = TRUE;
goto next_mod;
@ -3323,7 +3327,7 @@ g_date_time_format_utf8 (GDateTime *datetime,
* strftime() format language as specified by C99. The \%D, \%U and \%W
* conversions are not supported, nor is the 'E' modifier. The GNU
* extensions \%k, \%l, \%s and \%P are supported, however, as are the
* '0', '_' and '-' modifiers.
* '0', '_' and '-' modifiers. The Python extension \%f is also supported.
*
* In contrast to strftime(), this function always produces a UTF-8
* string, regardless of the current locale. Note that the rendering of
@ -3355,6 +3359,7 @@ g_date_time_format_utf8 (GDateTime *datetime,
* single digits are preceded by a blank
* - \%m: the month as a decimal number (range 01 to 12)
* - \%M: the minute as a decimal number (range 00 to 59)
* - \%f: the microsecond as a decimal number (range 000000 to 999999)
* - \%p: either "AM" or "PM" according to the given time value, or the
* corresponding strings for the current locale. Noon is treated as
* "PM" and midnight as "AM". Use of this format specifier is discouraged, as
@ -3399,7 +3404,7 @@ g_date_time_format_utf8 (GDateTime *datetime,
* conversion specifier by one or more modifier characters. The
* following modifiers are supported for many of the numeric
* conversions:
*
*
* - O: Use alternative numeric symbols, if the current locale supports those.
* - _: Pad a numeric result with spaces. This overrides the default padding
* for the specifier.
@ -3460,6 +3465,8 @@ g_date_time_format (GDateTime *datetime,
* including the date, time and time zone, and return that as a UTF-8 encoded
* string.
*
* Since GLib 2.66, this will output to sub-second precision if needed.
*
* Returns: (transfer full) (nullable): a newly allocated string formatted in
* ISO 8601 format or %NULL in the case that there was an error. The string
* should be freed with g_free().
@ -3472,9 +3479,15 @@ g_date_time_format_iso8601 (GDateTime *datetime)
GString *outstr = NULL;
gchar *main_date = NULL;
gint64 offset;
gchar *format = "%Y-%m-%dT%H:%M:%S";
/* if datetime has sub-second non-zero values below the second precision we
* should print them as well */
if (datetime->usec % G_TIME_SPAN_SECOND != 0)
format = "%Y-%m-%dT%H:%M:%S.%f";
/* Main date and time. */
main_date = g_date_time_format (datetime, "%Y-%m-%dT%H:%M:%S");
main_date = g_date_time_format (datetime, format);
outstr = g_string_new (main_date);
g_free (main_date);

View File

@ -1530,6 +1530,7 @@ GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
TEST_PRINTF ("%d", "24");
TEST_PRINTF_DATE (2009, 1, 1, "%d", "01");
TEST_PRINTF ("%e", "24"); // fixme
TEST_PRINTF_TIME (10, 10, 1.001, "%f", "001000");
TEST_PRINTF ("%h", "Oct");
TEST_PRINTF ("%H", "00");
TEST_PRINTF_TIME (15, 0, 0, "%H", "15");
@ -2207,6 +2208,14 @@ test_format_iso8601 (void)
g_free (p);
g_date_time_unref (dt);
g_time_zone_unref (tz);
tz = g_time_zone_new_utc ();
dt = g_date_time_new (tz, 2020, 8, 5, 12, 30, 55.000001);
p = g_date_time_format_iso8601 (dt);
g_assert_cmpstr (p, ==, "2020-08-05T12:30:55.000001Z");
g_free (p);
g_date_time_unref (dt);
g_time_zone_unref (tz);
}
#pragma GCC diagnostic push