Converting a DateTime to unix convention using %s%L (%s means seconds since Jan 1st 1970, %L means to append 3 digits to represent milliseconds) give this:
DateTime.now.strftime '%s%L'
=> "1656279075654"
How can I get a DateTime back from this?
DateTime.strptime('1656279075654', '%s%L') gives an error, as strptime doesn't seem to know %L strangely.
secs = '1656279075654'.to_f / 1000
DateTime.strptime(secs.to_s, '%s')
or
DateTime.strptime('1656279075654', '%Q')
(%Q - number of milliseconds since 1970-01-01 00:00:00 UTC)
Related
How should I interpret all aspects of the following timestamps? Where is the time based and how do timezones apply?
2015-11-15T14:45:28Z
2015-11-15T14:45:28.9694Z
2015-11-15T14:45:28.969412345Z
Below is my thoughts...
Date: 2015-11-15
???: T
Hours: 14
Minutes: 45
Seconds: 28 OR 28.9694 OR 28.969412345
???: Z
Most of your values are attributed correctly. The date portion (2015-11-15) is in the order YYYY-MM-DD, time in HH:MM:SS.ffff.
T indicates the start of the time portion of the date time.
Z indicates the time zone is UTC. Next to Z, you could have a format like Z+02:00, which indicates the time zone is UTC + 2 hours.
I have two dates a start date and an end date. I want to get a new time object which is the difference between the two. The differences I am concerned with are Hours, Minutes, Seconds and Milliseconds. I need to be able to create a new Time object from the result that includes the milliseconds difference
>> require 'time'
=> true
>> start_time = Time.parse '1970-01-01T00:00:00.200'
=> 1970-01-01 00:00:00 +0000
>> end_time = Time.parse '1970-01-01T01:01:01.400'
=> 1970-01-01 01:01:01 +0000
>> difference = Time.at(end_time - start_time)
=> 1970-01-01 01:01:01 +0000
my problem is that difference does not have the milliseconds
I can see that the Time has milliseconds by running
>> difference.strftime('%H:%M:%S.%L')
=> "01:01:01.199"
but how do I access the milliseconds that are in the Time difference object.
it is critical I have milliseconds as I am working in sub-second calculations?
UPDATE
I don't think my first attempt at this question was as descriptive as it should of been, my apologies for that.
require 'time'
a = Time.now
sleep(0.5)
b = Time.now
b - a
# => 0.505087
Milliseconds!
EDIT: Microseconds!
my problem is that difference does not have the milliseconds
It does have the milliseconds, Time#to_s / Time#inspect just doesn't show it. Its output is equivalent to: strftime "%Y-%m-%d %H:%M:%S %z"
how do I access the milliseconds that are in the Time difference object.
usec returns the microseconds and nsec returns the nanoseconds:
time = Time.at(0.2)
time.usec #=> 200000
time.nsec #=> 200000000
For milliseconds you could use
time.usec / 1000 #=> 200
Ruby's Time class has nanosecond precision: you can use Time#to_f to get a fractional number of seconds since the Unix epoch. If you subtract two Time objects, you'll get a fractional number of seconds between them. Thus, to get the number of milliseconds between two times, try:
((time2 - time1) * 1000).to_i
In particular, to add time (e.g. 11:40 + 00:30 = 12:10) and check whether a time belongs to a range (e.g. (11:00..12:00).include?(11:30)).
I understand that I can write a class, but maybe a solution already exists.
The built-in Time class is not entirely what I want, because I am not interested in date-related features, which are built-in.
You can use strftime to format the time any way you want. If you want only the hour and minutes you can use this:
Time.now.strftime("%H:%M")
=> "08:57"
time.strftime gives you all of these options to format
%a - The abbreviated weekday name (“Sun”)
%A - The full weekday name (“Sunday”)
%b - The abbreviated month name (“Jan”)
%B - The full month name (“January”)
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (“AM” or “PM”)
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the firstday of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal “%” character
Here is a link to the
strftime docs
To check if time belongs to a certain range you can use the cover
method
2.1.5 :003 > (Time.now..Time.now+10).cover?(Time.now)
=> true
2.1.5 :004 > (Time.now..Time.now+10).cover?(Time.now+20)
=> false
ActiveSupport's Numeric will help you.
You can do:
require 'date'
require 'active_support/all'
DateTime.parse("11:40") + 30.minutes
You will need the ActiveSupport gem in your Gemfile.
As for checking if a time is in a range you can use #cover:
irb(main):001:0> (DateTime.parse("11:40")..DateTime.parse("11:50")).cover?(DateTime.parse("11:45"))
=> true
irb(main):002:0> (DateTime.parse("11:40")..DateTime.parse("11:50")).cover?(DateTime.parse("12:00"))
=> false
When I hit a meetup group on Meetup.com to find upcoming events I end up with this fragment of JSON that I believe is when the next meetup is scheduled.
"utc_offset": -21600000,
"time": 1415752200000,
"waitlist_count": 0,
"updated": 1382991416000,
I'm trying to convert this to a humanized Date/Time in Ruby, but I'm not sure how to do this?
It seems that the time stamps here were in milliseconds. Divide them by 1000 and ruby can convert the seconds to Time.
You can use Time.at(i) in Ruby to convert integer to Time.
Time.at(1415752200000/1000) # => 2014-11-12 08:30:00 +0800
Time.at(1382991416000/1000) # => 2013-10-29 04:16:56 +0800
For more info, see http://www.ruby-doc.org/core-2.1.3/Time.html#method-c-at
As I understood from your time object its return only time(in miliseconds) not date.
For that you can try this using strftime method with Time class
seconds = 1415752200000/1000
Time.at(seconds).strftime("%H:%M:%S")
=> "06:00:00"
As per your requirement you can modify formatting of strftime() method like
Time.at(seconds).strftime("%Y-%B-%d %H:%M:%S")
=> "2014-November-12 06:00:00"
Available Formatting Options:
%a - The abbreviated weekday name (“Sun”)
%A - The full weekday name (“Sunday”)
%b - The abbreviated month name (“Jan”)
%B - The full month name (“January”)
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (“AM” or “PM”)
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the firstday of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal “%” character
I need to format subtract 2 long date time result to "hh.mm.ss"
open date closed date subtracted
01/01/2012 16:04 04/01/2012 17:07 3.01:02:58
02/01/2012 08:52 02/01/2012 17:03 08:10:27
using
closeddate.subtract(opendate)
return result in format D.hh:mm:ss
and using
DateDiff("h", OpenDate, closeDate)
return hours only (no minutes or seconds)
Also tried
closeddate.subtract(opendate).tostring("hh:mm:ss")
not working
as many Microsoft forums answered there is no way to do that
so i used
DateDiff("s", OpenDate, closeDate)
and passed result to helper function return calculated time in format hh:mm:ss