Project

General

Profile

Actions

Misc #19098

open

Time#strftime: %z and width

Added by andrykonchin (Andrew Konchin) about 2 years ago. Updated about 2 years ago.

Status:
Open
Assignee:
-
[ruby-core:110574]

Description

It seems %z behaves in some surprising way when it is combined with a width - sign + is placed at the beginning of the result string:

Time.now.strftime("%10z")
=> "+000000200"

Time.now.strftime("%_10z")
=> "      +200"

It seems a time zone offset is treated as a number. It probably makes sense with default format but it looks strange with : separators:

Time.now.strftime("%10::z")
# => "+002:00:00"

So I would expect that %z directive output to be treated as a non-numerical and padded by default with spaces.

Actions #1

Updated by andrykonchin (Andrew Konchin) about 2 years ago

  • Tracker changed from Bug to Misc
  • Description updated (diff)
  • ruby -v deleted (3.1.2p20)
  • Backport deleted (2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN)
Actions #2

Updated by andrykonchin (Andrew Konchin) about 2 years ago

  • Description updated (diff)
Actions #3

Updated by andrykonchin (Andrew Konchin) about 2 years ago

  • Description updated (diff)
Actions #4

Updated by andrykonchin (Andrew Konchin) about 2 years ago

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

#include <stdio.h>
#include <time.h>

void
test_strftime(const char *fmt, const struct tm *t)
{
    char buf[1024];
    int n = strftime(buf, sizeof(buf), fmt, t);
    printf("%-10s=>\t[%s]\n", fmt, buf);
}

int
main(void)
{
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    test_strftime("%z", tm);
    test_strftime("%_z", tm);
    test_strftime("%10z", tm);
    test_strftime("%10::z", tm);
    test_strftime("%_10z", tm);
    test_strftime("%_10::z", tm);
    return 0;
}

This program shows

Ubuntu 22.04:

%z        =>	[+0900]
%_z       =>	[+ 900]
%10z      =>	[         +0000000900]
%10::z    =>	[      %10::z]
%_10z     =>	[         +       900]
%_10::z   =>	[     %_10::z]

macOS Monterey:

%z        =>	[+0900]
%_z       =>	[+ 900]
%10z      =>	[10z]
%10::z    =>	[10::z]
%_10z     =>	[10z]
%_10::z   =>	[10::z]

%z with width seems not standardized.

Updated by andrykonchin (Andrew Konchin) about 2 years ago

I have checked on MacOS Monterey and Debian 11 (bullseye) (in Docker, if it matters) and received the same results like mentioned in the description.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0