Feature Request: time format for day "2nd"

782 views
Skip to first unread message

Erik Unger

unread,
Feb 14, 2012, 7:27:09 AM2/14/12
to golang-nuts
It would be good to have a day format pattern "2nd" that would
generate:

1st
2nd
3rd
4th
5th
....

Jan Mercl

unread,
Feb 14, 2012, 7:32:25 AM2/14/12
to golan...@googlegroups.com
In how many languages would like to have it? ;-)

David Symonds

unread,
Feb 14, 2012, 7:37:41 AM2/14/12
to Jan Mercl, golan...@googlegroups.com

I'm sure English would be sufficient, given the weekday and month
names are only in English.

It's pretty easy to write it yourself. Something like this:

// only works for 1-31.
func ordinal(x int) string {
suffix := "th"
switch x {
case 1, 21, 31: suffix = "st"
case 2, 22: suffix = "nd"
case 3, 23: suffix = "rd"
}
return strconv.Itoa(x) + suffix
}


Dave.

Dmytro Shteflyuk

unread,
Feb 14, 2012, 11:48:20 AM2/14/12
to David Symonds, Jan Mercl, golan...@googlegroups.com
Hi,

On Tuesday, 14 February, 2012 at 7:37 AM, David Symonds wrote:

It's pretty easy to write it yourself. Something like this:

// only works for 1-31.
func ordinal(x int) string {
suffix := "th"
switch x {
case 1, 21, 31: suffix = "st"
case 2, 22: suffix = "nd"
case 3, 23: suffix = "rd"
}
return strconv.Itoa(x) + suffix
}

More generic function would look like

func ordinal(x int) string {
  suffix := "th"
  switch x % 10 {
    case 1: suffix = "st"
    case 2: suffix = "nd"
    case 3: suffix = "rd"
  }
  return strconv.Itoa(x) + suffix
}

-- 
Best regards, Dmytro Shteflyuk

Kyle Lemons

unread,
Feb 14, 2012, 2:23:02 PM2/14/12
to Dmytro Shteflyuk, David Symonds, Jan Mercl, golan...@googlegroups.com
// only works for 1-31.
func ordinal(x int) string {
suffix := "th"
switch x {
case 1, 21, 31: suffix = "st"
case 2, 22: suffix = "nd"
case 3, 23: suffix = "rd"
}
return strconv.Itoa(x) + suffix
}

12nd? 

Kyle Lemons

unread,
Feb 14, 2012, 2:23:30 PM2/14/12
to Dmytro Shteflyuk, David Symonds, Jan Mercl, golan...@googlegroups.com
er, sorry, that was w.r.t the general one:

func ordinal(x int) string {
  suffix := "th"
  switch x % 10 {
    case 1: suffix = "st"
    case 2: suffix = "nd"
    case 3: suffix = "rd"
  }
  return strconv.Itoa(x) + suffix
}

12nd? 

Dmytro Shteflyuk

unread,
Feb 14, 2012, 2:42:05 PM2/14/12
to Kyle Lemons, David Symonds, Jan Mercl, golan...@googlegroups.com
Oh, right :-)

func ordinal(x int) string {
  suffix := "th"
  switch x % 10 {
    case 1: if (x % 100 != 11) { suffix = "st" }
    case 2: if (x % 100 != 12) { suffix = "nd" }
    case 3: if (x % 100 != 13) { suffix = "rd" }
  }
  return strconv.Itoa(x) + suffix
}

-- 
Best regards, Dmytro Shteflyuk

Dustin

unread,
Feb 14, 2012, 3:09:32 PM2/14/12
to golan...@googlegroups.com, Kyle Lemons, David Symonds, Jan Mercl

  Would you mind if I add this to github.com/dustin/go-humanize ?

Dmytro Shteflyuk

unread,
Feb 14, 2012, 3:49:57 PM2/14/12
to Dustin, golan...@googlegroups.com, Kyle Lemons, David Symonds, Jan Mercl
Of course no, go ahead.

-- 
Best regards, Dmytro Shteflyuk

Dustin

unread,
Feb 14, 2012, 4:15:13 PM2/14/12
to golan...@googlegroups.com, Dustin, Kyle Lemons, David Symonds, Jan Mercl

On Tuesday, February 14, 2012 12:49:57 PM UTC-8, Dmytro Shteflyuk wrote:
Of course no, go ahead.
 
  Great, thanks.  I added it with tests and a pointer back to this thread. 

Guillaume Lescure

unread,
Feb 14, 2012, 5:29:18 PM2/14/12
to golan...@googlegroups.com, Jan Mercl
Le mardi 14 février 2012 07:37:41 UTC-5, David Symonds a écrit :

I'm sure English would be sufficient, given the weekday and month

names are only in English.

It's pretty easy to write it yourself. Something like this:

// only works for 1-31.
func ordinal(x int) string {
  suffix := "th"
  switch x {
  case 1, 21, 31: suffix = "st"
  case 2, 22: suffix = "nd"
  case 3, 23: suffix = "rd"
  }
  return strconv.Itoa(x) + suffix
}


Dave.


I'm not so sure of that ... Go is the first UTF8-based language. That means it can be used by everyone with almost every characters. A chinese guy can use their own symbols to write code. And that's a so cool feature because that shows Go is opening up to the whole world. ASCII was enought for the beginning but now people have to considere that english is not the only spoken language. Everyone don't want to speak english and Go lets that choice :)

In my point of view , begin with english is obviously a pretty good choice but stay stuck on only one language is a terrible mistake.
So no 1st, 2nd, 3rd, etc ... is not sufficient for everyone because names are not only in English. But that's a good start ...

Regards

Dustin

unread,
Feb 14, 2012, 5:32:58 PM2/14/12
to golan...@googlegroups.com, Jan Mercl

On Tuesday, February 14, 2012 2:29:18 PM UTC-8, Guillaume Lescure wrote:
 
I'm not so sure of that ... Go is the first UTF8-based language.

  I don't believe this to be true.
 
In my point of view , begin with english is obviously a pretty good choice but stay stuck on only one language is a terrible mistake.
So no 1st, 2nd, 3rd, etc ... is not sufficient for everyone because names are not only in English. But that's a good start ...

  I'd welcome an internationalization effort of github.com/dustin/go-humanize.  :) 

David Symonds

unread,
Feb 14, 2012, 5:35:56 PM2/14/12
to Guillaume Lescure, golang-nuts
On Wed, Feb 15, 2012 at 9:29 AM, Guillaume Lescure
<guil.l...@gmail.com> wrote:

> So no 1st, 2nd, 3rd, etc ... is not sufficient for everyone because names
> are not only in English. But that's a good start ...

You missed my point. If you're trying to use this with the "time"
package in the standard library then you're already being stuck with
English names for weekdays and months, so just doing English ordinals
isn't making the situation worse.


Dave.

Rémy Oudompheng

unread,
Feb 14, 2012, 5:40:37 PM2/14/12
to Guillaume Lescure, golan...@googlegroups.com, Jan Mercl
On 2012/2/14 Guillaume Lescure <guil.l...@gmail.com> wrote:
> I'm not so sure of that ... Go is the first UTF8-based language. That means
> it can be used by everyone with almost every characters. A chinese guy can
> use their own symbols to write code. And that's a so cool feature because
> that shows Go is opening up to the whole world. ASCII was enought for the
> beginning but now people have to considere that english is not the only
> spoken language. Everyone don't want to speak english and Go lets that
> choice :)

Haskell supports Unicode too.

main = do
let ωmega = 3
print ωmega

is valid, whereas

main = do
let Ωmega = 3
print Ωmega

is not (variables/functions are lowercase, type names are uppercase).uppercase.

Rémy.

Guillaume Lescure

unread,
Feb 14, 2012, 5:41:53 PM2/14/12
to golan...@googlegroups.com, Jan Mercl


Le mardi 14 février 2012 17:32:58 UTC-5, Dustin a écrit :

On Tuesday, February 14, 2012 2:29:18 PM UTC-8, Guillaume Lescure wrote:
 
I'm not so sure of that ... Go is the first UTF8-based language.

  I don't believe this to be true.
 

Yeah, sorry I'm not so sure of that ... but it's the first I like ^^

 
In my point of view , begin with english is obviously a pretty good choice but stay stuck on only one language is a terrible mistake.
So no 1st, 2nd, 3rd, etc ... is not sufficient for everyone because names are not only in English. But that's a good start ...

  I'd welcome an internationalization effort of github.com/dustin/go-humanize.  :) 

I love open-mindedness. Unfortunatly I can't (studies and some other stuff) but I hope some other guys will ;) 

Guillaume Lescure

unread,
Feb 14, 2012, 5:43:53 PM2/14/12
to golan...@googlegroups.com, Guillaume Lescure, Jan Mercl
Le mardi 14 février 2012 17:40:37 UTC-5, Rémy Oudompheng a écrit :

Haskell supports Unicode too.

main = do
  let ωmega = 3
  print ωmega

is valid, whereas

main = do
  let Ωmega = 3
  print Ωmega

is not (variables/functions are lowercase, type names are uppercase).uppercase.

Rémy.


Yep I'm sorry I spoke too quick ^^
 

Guillaume Lescure

unread,
Feb 14, 2012, 5:59:39 PM2/14/12
to golan...@googlegroups.com, Guillaume Lescure
Le mardi 14 février 2012 17:35:56 UTC-5, David Symonds a écrit :

You missed my point. If you're trying to use this with the "time"
package in the standard library then you're already being stuck with
English names for weekdays and months, so just doing English ordinals
isn't making the situation worse.


Dave.


That's not fully true. Standard librairies are here like some tools. The output of a software have to be multilingual because it's for users. If the standard librairies allow that easily that's a great improvement. I agree, now it's not that yet but I think these improvements will come with time.

For exemple, C/C++ are ASCII-based languages so it's impossible to be multilingual. For that, you have to use, for exemple, Qt to translate and produce a UTF8 string. I think with Go, we can do that in the standard librairies themselft.

I repeat, it's not that for now, but I truly think that where we should lead to.
It's just a thought about leading development not criticism :)

Reply all
Reply to author
Forward
0 new messages