mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-09 18:54:04 +02:00
gdatetime: Extend the '%z' timezone format
Implement gnulib strftime extensions for the '%z' numeric timezone format. These are also supported and documented by GNU date(1): %z +hhmm numeric time zone (e.g., -0400) %:z +hh:mm numeric time zone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) https://bugzilla.gnome.org/show_bug.cgi?id=707151
This commit is contained in:
@@ -2090,6 +2090,59 @@ g_date_time_to_utc (GDateTime *datetime)
|
|||||||
|
|
||||||
/* Format {{{1 */
|
/* Format {{{1 */
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
format_z (GString *outstr,
|
||||||
|
gint offset,
|
||||||
|
guint colons)
|
||||||
|
{
|
||||||
|
gint hours;
|
||||||
|
gint minutes;
|
||||||
|
gint seconds;
|
||||||
|
|
||||||
|
hours = offset / 3600;
|
||||||
|
minutes = ABS (offset) / 60 % 60;
|
||||||
|
seconds = ABS (offset) % 60;
|
||||||
|
|
||||||
|
switch (colons)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
g_string_append_printf (outstr, "%+03d%02d",
|
||||||
|
hours,
|
||||||
|
minutes);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
g_string_append_printf (outstr, "%+03d:%02d",
|
||||||
|
hours,
|
||||||
|
minutes);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
g_string_append_printf (outstr, "%+03d:%02d:%02d",
|
||||||
|
hours,
|
||||||
|
minutes,
|
||||||
|
seconds);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
g_string_append_printf (outstr, "%+03d", hours);
|
||||||
|
|
||||||
|
if (minutes != 0 || seconds != 0)
|
||||||
|
{
|
||||||
|
g_string_append_printf (outstr, ":%02d", minutes);
|
||||||
|
|
||||||
|
if (seconds != 0)
|
||||||
|
g_string_append_printf (outstr, ":%02d", seconds);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
format_number (GString *str,
|
format_number (GString *str,
|
||||||
gboolean use_alt_digits,
|
gboolean use_alt_digits,
|
||||||
@@ -2185,6 +2238,7 @@ g_date_time_format_locale (GDateTime *datetime,
|
|||||||
gboolean locale_is_utf8)
|
gboolean locale_is_utf8)
|
||||||
{
|
{
|
||||||
guint len;
|
guint len;
|
||||||
|
guint colons;
|
||||||
gchar *tmp;
|
gchar *tmp;
|
||||||
gunichar c;
|
gunichar c;
|
||||||
gboolean alt_digits = FALSE;
|
gboolean alt_digits = FALSE;
|
||||||
@@ -2219,6 +2273,7 @@ g_date_time_format_locale (GDateTime *datetime,
|
|||||||
if (!*format)
|
if (!*format)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
colons = 0;
|
||||||
alt_digits = FALSE;
|
alt_digits = FALSE;
|
||||||
pad_set = FALSE;
|
pad_set = FALSE;
|
||||||
|
|
||||||
@@ -2414,17 +2469,15 @@ g_date_time_format_locale (GDateTime *datetime,
|
|||||||
g_date_time_get_year (datetime));
|
g_date_time_get_year (datetime));
|
||||||
break;
|
break;
|
||||||
case 'z':
|
case 'z':
|
||||||
if (datetime->tz != NULL)
|
{
|
||||||
{
|
gint64 offset;
|
||||||
gint64 offset = g_date_time_get_utc_offset (datetime)
|
if (datetime->tz != NULL)
|
||||||
/ USEC_PER_SECOND;
|
offset = g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND;
|
||||||
|
else
|
||||||
g_string_append_printf (outstr, "%+03d%02d",
|
offset = 0;
|
||||||
(int) offset / 3600,
|
if (!format_z (outstr, (int) offset, colons))
|
||||||
(int) abs(offset) / 60 % 60);
|
return FALSE;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
g_string_append (outstr, "+0000");
|
|
||||||
break;
|
break;
|
||||||
case 'Z':
|
case 'Z':
|
||||||
tz = g_date_time_get_timezone_abbreviation (datetime);
|
tz = g_date_time_get_timezone_abbreviation (datetime);
|
||||||
@@ -2453,6 +2506,12 @@ g_date_time_format_locale (GDateTime *datetime,
|
|||||||
pad_set = TRUE;
|
pad_set = TRUE;
|
||||||
pad = "0";
|
pad = "0";
|
||||||
goto next_mod;
|
goto next_mod;
|
||||||
|
case ':':
|
||||||
|
/* Colons are only allowed before 'z' */
|
||||||
|
if (*format && *format != 'z' && *format != ':')
|
||||||
|
return FALSE;
|
||||||
|
colons++;
|
||||||
|
goto next_mod;
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
@@ -2674,7 +2733,23 @@ g_date_time_format_locale (GDateTime *datetime,
|
|||||||
* <varlistentry><term>
|
* <varlistentry><term>
|
||||||
* <literal>\%z</literal>:
|
* <literal>\%z</literal>:
|
||||||
* </term><listitem><simpara>
|
* </term><listitem><simpara>
|
||||||
* the time-zone as hour offset from UTC
|
* the time zone as an offset from UTC (+hhmm)
|
||||||
|
* </simpara></listitem></varlistentry>
|
||||||
|
* <varlistentry><term>
|
||||||
|
* <literal>\%:z</literal>:
|
||||||
|
* </term><listitem><simpara>
|
||||||
|
* the time zone as an offset from UTC (+hh:mm). This is a gnulib strftime extension. Since: 2.38
|
||||||
|
* </simpara></listitem></varlistentry>
|
||||||
|
* <varlistentry><term>
|
||||||
|
* <literal>\%::z</literal>:
|
||||||
|
* </term><listitem><simpara>
|
||||||
|
* the time zone as an offset from UTC (+hh:mm:ss). This is a gnulib strftime extension. Since: 2.38
|
||||||
|
* </simpara></listitem></varlistentry>
|
||||||
|
* <varlistentry><term>
|
||||||
|
* <literal>\%:::z</literal>:
|
||||||
|
* </term><listitem><simpara>
|
||||||
|
* the time zone as an offset from UTC, with : to necessary precision
|
||||||
|
* (e.g., -04, +05:30). This is a gnulib strftime extension. Since: 2.38
|
||||||
* </simpara></listitem></varlistentry>
|
* </simpara></listitem></varlistentry>
|
||||||
* <varlistentry><term>
|
* <varlistentry><term>
|
||||||
* <literal>\%Z</literal>:
|
* <literal>\%Z</literal>:
|
||||||
|
Reference in New Issue
Block a user