lestrrat-go / strftime

Fast strftime for Go
MIT License
117 stars 22 forks source link

%I and %r should pad zero #18

Closed itchyny closed 4 years ago

itchyny commented 4 years ago

Description

As title, %I and %r should pad zero for 1-9.

Sample code

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/lestrrat-go/strftime"
)

func main() {
    t := time.Date(2020, 07, 24, 9, 8, 7, 0, time.UTC)
    str, err := strftime.Format("%I:%M:%S %p", t)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(str) // 9:08:07 AM but should be 09:08:07 AM

    str, err = strftime.Format("%r", t)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(str) // 9:08:07 AM but should be 09:08:07 AM
}

For reference,

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

int main(void)
{
  struct tm tm;
  char buf[255];

  memset(&tm, 0, sizeof(struct tm));
  strptime("09:08:07", "%T", &tm);
  strftime(buf, sizeof(buf), "%I %r", &tm);
  puts(buf); // 09 09:08:07 AM
}
lestrrat commented 4 years ago

Thank you!