Ruby Time.now.strftime("%Y%m%d") equivalent in Crystal?

I want to convert a Type type column to a self-defined format which Mixed with Chinese.

But, after search forum and api, i could not found strftime equivalent.

Thank you.

It’s Time#to_s in Crystal:

Time.local.to_s("%Y%m%d")
1 Like

Is it possible in Crystal to do some kind of alias method that allows both Time#to_s and Time#strftime to invoke the same process?

Yes, but Crystal is usually against aliases for this kind of stuff.

Ref FAQ · crystal-lang/crystal Wiki · GitHub.

I have aliases in Ruby to make it more easily adapted to Crystal. I expected the more complicated programs I write I will also have to add a Crystal library that aliases methods to more easily allow Ruby code to be compiled in Crystal.

While I can see how that can make porting a program easier. Wouldn’t it ultimately be better in the long run to just switch to using the Crystal methods and drop the Ruby references all together? Crystal is not just compiled Ruby; they are separate languages. Can you run some Ruby programs directly with Crystal? Yes. Is it a goal to be able to run any Ruby program with Crystal? No.

I know you are right. So far my only experience with developing a Crystal-based project beyond experimental toys is my JUSTPREP utility that fronts Casey’s CLI task runner JUST. I developed JUSTPREP as a Ruby gem about a year ago then developed a Crystal implementation that uses a large amount of the existing Ruby code.

JUSTPREP is a simple file-based brain–dead m4 that combines included files into a bigger file that gets processed by JUST. It has no complexity. As a gem it only has about 5K downloads. As a Crystal binary installed via brew I have no idea. Nor do I know how many times the binary assets have been download from the github account madbomber/jstprep

My day job is Ruby. It pays the bills. What I’d like to do is take some of my simple AI code for clustering and classification and turn them into proper Crystal projects.

About 10 years ago I wrote a Ruby program that watches an input directory for new files. When a new file is added the program would compare its content against the signatures of several output directories. The program would then copy the new file into those output directories which had the closes matching signature. Then the new file was deleted from the input directory.

It worked pretty nicely but was slow.

BTW. i have to admit i have a bad memory, In fact, i saw the Time.local.to_s("%Y%m%d") usage when i first browse the Time Api,
When i need to use it again, i am not aware the correct to_s usage after a quick search and find following api explain.

 #to_s(io : IO, format : String) : Nil

Formats this Time according to the pattern in format to the given io.

Yes, it is my fault, what i want say is, maybe add new one line example into above instance method document is better than only explain it in Overview, user probably miss it anyway.

Like this:

#to_s(format : String) : String

Formats this Time according to the pattern in format.

Time.local.to_s("%Y-%m-%d %H:%M:%S %:z") # => "2015-10-12 10:30:00 +00:00"
1 Like

Yes, we should have such little usage examples for ideally every API method. It just needs someone to add it.

1 Like

I will, when i have a little free-time, i guess another issue is, even i done it, there is no core term member have time to check/merge it, e.g. this, a very small document fix for Fix.match? pattern usage.

I know all core-team member is very very busy, anyway, thank you very much for help answer question on community here.