How to Get the Current Time as 13-Digit Integer in Ruby

get milliseconds since 1970 from string

Try

require 'date'
DateTime.parse("2016-08-12 06:13:24 UTC").strftime('%Q')

Get first 3 integer

in rails console
something like

=> 92774.to_s.first(3).to_i
=> 927

in irb

 => 92774.to_s.chars.first(3).join.to_i
=> 927

How do i print the date and time of specific lines that contain a certain key word using Ruby?

One way to do it would be like so (within a block that's iterating over the lines in the file, obviously):

if line.include?('MAY_DAY')
puts line[0..14]
end

Since the date information (which is what you want output) appears in the same position and is the same length in every line, we don't bother doing any parsing of the text for the output - just spit out the first 15 characters.

I'm tempted to try to compress all of this into a single regular expression, but this ought to work. Obviously, you could do something other than print out the date within the conditional, and if you wanted to work with it as a date, you could pass it to DateTime.parse() (just remember to require 'date' first).

Convert times created by Javascript's getTime() method to ruby Time objects

You can use the Time.at method, and divide the JavaScript timestamp value by 1000, since the at method, uses seconds instead of milliseconds, for example:

jsTime = 1252268867928
print Time.at(jsTime/1000)
# Sun Sep 06 20:27:47 +0000 2009

Ensuring a unique number to identify something

If you need unique identifier, you should make such. For example, SecureRandom.uuid:

p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"

(I assume that PayPal accepts arbitrary strings and doesn't require actual integers)



Related Topics



Leave a reply



Submit