I want to calculate the number of seconds elapsed since the start of the current year. A straightforward approach would be to get the current date and the date on the start of the year and subtract the two but I was wondering if there was a library method that could do that for me.
This would help my year to date calculations look prettier.
current_time = Time.new
current_time - Time.new(current_time.year)
This will return a Float of the number of seconds since the start of the current year. See Time for more information.
Related
I need to calculate the time in hours since a specific input.
I tried some different codes and tools but couldn't get any result yet ...
So to summarize the requirement here I just need to time gone from now since specific input?
eg, in a simple way, i need the time consumed since 6/12 4:40 PM?
If your script is running at time t0 and it's still running at time t1 and you want to know how many hours have elapsed between t0 and t1, I think the easiest way to do that is to use the UNIX epoch.
So at time t0 store the epoch time:
t0="$(date '+%s')"
and then at time t1 get the epoch time again:
t1="$(date '+%s')"
then you can just query the difference between the two times to get the seconds, and divide by 3600 if you want that in hours:
(( elapsed = t1 - t0 ))
(( hours = elapsed / 3600 ))
Korn shell isn't the best tool for division though. You might prefer to stick with the number of seconds elapsed.
I want to calculate month between two date and output how month and any additional days by using google sheet
I use this formula to calculate month but i don't know how to calculate any additional days after a complete month.
DATEDIF(AG2,TODAY(),"M")
Quoting this reference of DATEDIF (emphasis mine): https://sheetshelp.com/datedif/
Syntax
=DATEDIF(start_date,end_date,unit)
start_date Date at which to start the calculation
end_date Date at which to end the calculation
unit Type of output. Choices are “Y”, “M”, “D”, “YM”, “YD”, or “MD”.
...
"M" – Number of whole months elapsed between start and end dates
"MD" – Number of days elapsed after the number of months shown with the “M” or “YM” unit. Can’t go higher than 30.
...
What is the extra data included in a date object? Given the following example:
time = Time.at(1392328830)
# => 2014-02-13 15:00:30 -0700
date = time.to_date
# => #<Date: 2014-02-13 ((2456702j,0s,0n),+0s,2299161j)>
What does all this represent? It's not clear from looking at the Ruby Date documentation.
((2456702j,0s,0n),+0s,2299161j)
What you're seeing is the output from Object.inspect which is a human-readable representation of an object. In the case of the Date class:
From date.rb:
# Return internal object state as a programmer-readable string.
def inspect() format('#<%s: %s,%s,%s>', self.class, #ajd, #of, #sg) end
# Return the date as a human-readable string.
#
# The format used is YYYY-MM-DD.
def to_s() strftime end
The instance variables are:
#ajd is an Astronomical Julian Day Number
#of is an offset or fraction of a day from UTC
#sg is the Day of Calendar Reform
But what do these terms mean?
1. What is an Astronomical Julian Day Number? (#ajd)
For scientific purposes, it is convenient to refer to a date simply as a day count, counting from an arbitrary initial day. The date first chosen for this was January 1, 4713 BCE. A count of days from this date is the Julian *Day* Number or Julian *Date*. This is in local time, and counts from midnight on the initial day. The stricter usage is in UTC, and counts from midday on the initial day. This is referred to in the Date class as the Astronomical *Julian* Day *Number*. In the Date class, the Astronomical Julian Day Number includes fractional days.
2. Offset from what? (#offset)
Time zones are represented as an offset from UTC, as a fraction of a day. This offset is the how much local time is later (or earlier) than UTC. UTC offset 0 is centered on England (also known as GMT). As you travel east, the offset increases until you reach the dateline in the middle of the Pacific Ocean; as you travel west, the offset decreases.
3. What is the Day of Calendar Reform? (#sg)
The Gregorian Calendar was introduced at different times in different regions. The day on which it was introduced for a particular region is the Day *of* Calendar *Reform* for that region. This is abbreviated as sg (for Start of Gregorian calendar) in the Date class.
From what I can tell, the Gregorian Calendar is calendar that self-corrects via leap years.
Alright I was looking at this in python but I just like ruby better. What I'm trying to do is get a date and time from this number - 12988822998637849 - which is the number of microseconds since January 1, 1601 UTC. This is how Chrome stores it's timestamps and i've seen a number of methods to do this in python, but I am just more comfortable with ruby and I have no idea on how to even start going about doing this. (My Google-Fu didn't help me this time)
Note this example number is from a few days ago. I'll take any help I can get. Thank you!
look at Time.at.
A Windows file time is "a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC)." Ref.
In contrast, Ruby stores times like Unix: "Time is stored internally as the number of seconds and microseconds since the epoch, January 1, 1970 00:00 UTC" Ref.
# This return a Time
Time.at(12988822998637849/1000000-11644473600) # Epoch Diff is 11644473600
# => 2012-08-07 11:23:18 -0300
# This returns a String
Time.at(12988822998637849/1000000-11644473600).strftime("%Y-%m-%d %H:%M.%S")
# => "2012-08-07 11:23.18"
All you need to do is to create a Ruby date using the Chrome time origin, then increment by the requisite number of microseconds:
Time.gm(1601,1,1) + 12988822998637849 / 1000000
# => 2012-08-07 14:23:18 UTC
I have time in UTC seconds format. Could any one assist on how to convert such numbers to GPS
time in normal timestamp (dd-mm-yyyy hh:mm:ss)? I need a C code or, perhaps, algorithm.
Update (June 2017): Currently 18 leap seconds.
GPS time is simply UTC time, but without leap seconds. As of this writing, there have been 15 leap seconds since the GPS epoch (January 6, 1980 # 00:00:00), so if it's 2012/02/13 # 12:00:00 (UTC), then it's 2012/02/13 # 12:00:15 in GPS time. If you want to do correct conversions for other times, you'll have to take into account when each leap second went into effect.
Here's how you can compute the current offset, from a couple different "authoritative" sources:
http://www.ietf.org/timezones/data/leap-seconds.list -- Count the number of lines starting from the 2571782400 20 # 1 Jul 1981 line. Or just subtract 19 from the last number in the list (e.g., 37-19 = 18) as of May 2017.
https://www.nist.gov/pml/time-and-frequency-division/atomic-standards/leap-second-and-ut1-utc-information -- Count the number of leap seconds inserted (from the Leap Seconds Inserted into the UTC Time Scale section), starting with (and including) the 1981-06-30 entry.
There is a Javascript library that can convert to and from unixtime. The library is available at
http://www.lsc-group.phys.uwm.edu/~kline/gpstime/
Whatever algorithm you use, you must update it when new leap seconds are announced.
for an algorithm check this site source code: UTC Converter
for built-in functions in c++ check here - especially ctime()