More elegant way to condense a date/time string?

J

jfry

Hey all,

I'm a relative ruby beginner, looking to create short-ish date/time
strings. I came up with:

Time.now.xmlschema.delete(':-').chop.chop.chop.chop #yields something
like 20071022T123910

Which works for me...but it seemed like I might be doing acrobatics to
solve a problem that ruby can solve more elegantly. I'm mostly just
looking for a unique alphanumeric string that's not too long. (I
chopped the timezone to make it a bit shorter.) The particular format
I ended up with works well for me, but is not a requirement - another
option I'd be happy with would be seconds (or smaller) since epoch,
for example.

I ask because I enjoy learning to make my ruby more elegant.

Thanks,
Jeff
 
B

Brian Adkins

Hey all,

I'm a relative ruby beginner, looking to create short-ish date/time
strings. I came up with:

Time.now.xmlschema.delete(':-').chop.chop.chop.chop #yields something
like 20071022T123910

Which works for me...but it seemed like I might be doing acrobatics to
solve a problem that ruby can solve more elegantly. I'm mostly just
looking for a unique alphanumeric string that's not too long. (I
chopped the timezone to make it a bit shorter.) The particular format
I ended up with works well for me, but is not a requirement - another
option I'd be happy with would be seconds (or smaller) since epoch,
for example.

Time.now.to_i will give the number of seconds since the epoch.
 
G

Gregory Seidman

Hey all,

I'm a relative ruby beginner, looking to create short-ish date/time
strings. I came up with:

Time.now.xmlschema.delete(':-').chop.chop.chop.chop #yields something
like 20071022T123910

Which works for me...but it seemed like I might be doing acrobatics to
solve a problem that ruby can solve more elegantly. I'm mostly just
looking for a unique alphanumeric string that's not too long. (I
chopped the timezone to make it a bit shorter.) The particular format
I ended up with works well for me, but is not a requirement - another
option I'd be happy with would be seconds (or smaller) since epoch,
for example.

I ask because I enjoy learning to make my ruby more elegant.

While #to_i will get you seconds since epoch, you should also be aware of
the #strftime method, which works (nearly?) exactly like the standard C
library call.
Thanks,
Jeff
--Greg
 
J

jfry

While #to_i will get you seconds since epoch, you should also be aware of
the #strftime method, which works (nearly?) exactly like the standard C
library call.

Thanks all! I'm glad I asked.
Jeff
 
R

Robert Klemme

2007/10/22 said:
Hey all,

I'm a relative ruby beginner, looking to create short-ish date/time
strings. I came up with:

Time.now.xmlschema.delete(':-').chop.chop.chop.chop #yields something
like 20071022T123910

Which works for me...but it seemed like I might be doing acrobatics to
solve a problem that ruby can solve more elegantly. I'm mostly just
looking for a unique alphanumeric string that's not too long. (I
chopped the timezone to make it a bit shorter.) The particular format
I ended up with works well for me, but is not a requirement - another
option I'd be happy with would be seconds (or smaller) since epoch,
for example.

I ask because I enjoy learning to make my ruby more elegant.

The proper way to achieve your format would probably be this:

irb(main):015:0> Time.now.strftime "%Y%m%dT%H%M%S"
=> "20071023T085747"

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top