Class: Time
- Defined in:
- lib/openc3/core_ext/time.rb,
lib/openc3/io/json_rpc.rb
Overview
This file contains the OpenC3 specific additions to the Ruby Time class
Time is expressed in many different ways and with many different epochs. This file supports the following formats:
Julian Date (jd or julian)
Modified Julian Date (mjd)
yds (year, day, and seconds of day)
mdy (year, month, day, hour, minute, second, us of second)
ccsds (day, ms, us from Jan 1, 1958 midnight)
time (a ruby time object with unix epoch Jan 1, 1970 midnight)
sec (seconds since an arbitrary epoch)
Constant Summary collapse
- JULIAN_DAYS_PER_CENTURY =
There are 365.25 days per year because of leap years. In the Gregorian calendar some centuries have 36524 days because of the divide by 400 rule while others have 36525 days. The Julian century is DEFINED as having 36525 days.
36525.0- JULIAN_DATE_OF_JULIAN_EPOCH =
-4713/01/01 Noon
0.0- JULIAN_DATE_OF_MJD_EPOCH =
1858/11/17 Midnight
2400000.5- JULIAN_DATE_OF_GPS_EPOCH =
1980/01/06 Midnight
2444244.5- JULIAN_DATE_OF_J2000_EPOCH =
2000/01/01 Noon
2451545.0- JULIAN_DATE_OF_CCSDS_EPOCH =
1958/01/01 Midnight
2436204.5- DATE_TIME_MJD_EPOCH =
DateTime.new(1858, 11, 17)
- USEC_PER_MSEC =
1000- MSEC_PER_SECOND =
1000- SEC_PER_MINUTE =
60- MINUTES_PER_HOUR =
60- HOURS_PER_DAY =
24- NSEC_PER_SECOND =
1_000_000_000- NSEC_PER_MSEC =
1_000_000- USEC_PER_SECOND =
USEC_PER_MSEC * MSEC_PER_SECOND
- MSEC_PER_MINUTE =
60 * MSEC_PER_SECOND
- MSEC_PER_HOUR =
60 * MSEC_PER_MINUTE
- MSEC_PER_DAY =
HOURS_PER_DAY * MSEC_PER_HOUR
- SEC_PER_HOUR =
SEC_PER_MINUTE * MINUTES_PER_HOUR
- SEC_PER_DAY =
HOURS_PER_DAY * SEC_PER_HOUR
- USEC_PER_DAY =
USEC_PER_SECOND * SEC_PER_DAY
- MINUTES_PER_DAY =
MINUTES_PER_HOUR * HOURS_PER_DAY
- USEC_PER_MSEC_FLOAT =
USEC_PER_MSEC.to_f
- MSEC_PER_SECOND_FLOAT =
MSEC_PER_SECOND.to_f
- SEC_PER_MINUTE_FLOAT =
SEC_PER_MINUTE.to_f
- MINUTES_PER_HOUR_FLOAT =
MINUTES_PER_HOUR.to_f
- HOURS_PER_DAY_FLOAT =
HOURS_PER_DAY.to_f
- USEC_PER_SECOND_FLOAT =
USEC_PER_SECOND.to_f
- MSEC_PER_MINUTE_FLOAT =
MSEC_PER_MINUTE.to_f
- MSEC_PER_HOUR_FLOAT =
MSEC_PER_HOUR.to_f
- MSEC_PER_DAY_FLOAT =
MSEC_PER_DAY.to_f
- SEC_PER_HOUR_FLOAT =
SEC_PER_HOUR.to_f
- SEC_PER_DAY_FLOAT =
SEC_PER_DAY.to_f
- USEC_PER_DAY_FLOAT =
USEC_PER_DAY.to_f
- MINUTES_PER_DAY_FLOAT =
MINUTES_PER_DAY.to_f
- LeapYearMonthDays =
The number of days in each month during a leap year
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- CommonYearMonthDays =
The number of days in each month during a year (not a leap year)
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- @@use_utc =
Class variable that allows us to globally select whether to use UTC or local time.
false
Class Method Summary collapse
-
.ccsds2julian(day, ms, us) ⇒ Float
The CCSDS date converted to a julian date.
-
.ccsds2mdy(day, ms, us) ⇒ Array<Year, Month, Day, Hour, Minute, Second, Microsecond>
Convert a CCSDS Date to mdy format Note that an array is returned rather than a Time object because Time objects cannot represent all possible CCSDS dates.
-
.ccsds2sec(day, ms, us, sec_epoch_jd = JULIAN_DATE_OF_CCSDS_EPOCH) ⇒ Float
The number of seconds from the given epoch to the given CCSDS day, milliseconds, and microseconds.
-
.days_from_j2000(time) ⇒ Float
Number of julian days since Jan 1, 2000 at noon.
-
.format_seconds(seconds) ⇒ String
Seconds formatted as a human readable string with days, hours, minutes, and seconds.
- .from_nsec_from_epoch(nsec_from_epoch) ⇒ Object
-
.init_epoch_delta(epoch) ⇒ Float
Ruby time objects cannot handle times before the Unix Epoch.
-
.julian2ccsds(jdate) ⇒ Array<day, ms, us>
Julian converted to CCSDS.
-
.julian2mdy(jdate) ⇒ Array<Year, Month, Day, Hour, Minute, Second, Microsecond>
Convert a Julian Date to mdy format Note that an array is returned rather than a Time object because Time objects cannot represent all possible Julian dates.
-
.julian_centuries_since_j2000(time) ⇒ Float
Number of julian centuries since Jan 1, 2000 at noon.
-
.leap_year?(year) ⇒ Boolean
Whether the year is a leap year.
-
.mdy2ccsds(year, month, day, hour, minute, second, us) ⇒ Array<day, ms, us>
Convert from mdy format to CCSDS Date Note that an array is used rather than a Time object because Time objects cannot represent all possible CCSDS dates.
-
.mdy2julian(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, us = 0) ⇒ Float
Convert the given year, month, day, hour, minute, second, and us into a Julian date.
-
.mdy2mjd(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, us = 0) ⇒ Time
Convert the given year, month, day, hour, minute, second, and us into a Modified Julian date.
-
.sec2ccsds(sec, sec_epoch_jd = JULIAN_DATE_OF_CCSDS_EPOCH) ⇒ Array<day, ms, us>
CCSDS date.
-
.total_seconds(hour, minute, second, us) ⇒ Float
The number of seconds represented by the hours, minutes, seconds and microseconds.
-
.use_local ⇒ Object
Set up the Time class so that a call to the sys method will set the Time object being operated upon to be a local time.
-
.use_utc ⇒ Object
Set up the Time class so that a call to the sys method will set the Time object being operated upon to be a UTC time.
-
.yds(year, day_of_year, sec_of_day) ⇒ Object
Create a new time object given year, day of year (1-366), and seconds of day.
-
.yds2julian(year, day, sec) ⇒ Float
Year, day, seconds converted to the Julian date.
-
.yds2mdy(year, day, sec) ⇒ Array
[year, month, day, hour, minute, second, usec].
Instance Method Summary collapse
-
#as_json(_options = nil) ⇒ Object
:nodoc:.
-
#formatted(include_year = true, fractional_digits = 3) ⇒ String
Date formatted as YYYY/MM/DD HH:MM:SS.US.
-
#leap_year? ⇒ Boolean
Whether the year is a leap year.
-
#seconds_of_day ⇒ Float
The number of seconds in the day (0-86399.99).
-
#sys ⇒ Object
Set the Time object to be either a UTC or local time depending on the use_utc flag.
-
#to_julian ⇒ Float
(also: #to_jd)
The Time converted to a julian date.
-
#to_mjd ⇒ Object
Convert a time object to the modified julian date.
- #to_msec_from_epoch ⇒ Object
- #to_nsec_from_epoch ⇒ Object
-
#to_timestamp ⇒ String
Date formatted as YYYYMMDDHHmmSSNNNNNNNNN.
Class Method Details
.ccsds2julian(day, ms, us) ⇒ Float
Returns The CCSDS date converted to a julian date.
374 375 376 |
# File 'lib/openc3/core_ext/time.rb', line 374 def self.ccsds2julian(day, ms, us) (day + JULIAN_DATE_OF_CCSDS_EPOCH) + ((ms.to_f + (us / 1000.0)) / MSEC_PER_DAY_FLOAT) end |
.ccsds2mdy(day, ms, us) ⇒ Array<Year, Month, Day, Hour, Minute, Second, Microsecond>
Convert a CCSDS Date to mdy format Note that an array is returned rather than a Time object because Time objects cannot represent all possible CCSDS dates
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/openc3/core_ext/time.rb', line 337 def self.ccsds2mdy(day, ms, us) jdate = day + JULIAN_DATE_OF_CCSDS_EPOCH year, month, day, hour, minute, second, _ = julian2mdy(jdate) hour = (ms / MSEC_PER_HOUR).to_i temp = ms - (hour * MSEC_PER_HOUR) minute = (temp / MSEC_PER_MINUTE).to_i temp -= minute * MSEC_PER_MINUTE second = temp / MSEC_PER_SECOND temp -= second * MSEC_PER_SECOND us = us + (temp * USEC_PER_MSEC) return [year, month, day, hour, minute, second, us] end |
.ccsds2sec(day, ms, us, sec_epoch_jd = JULIAN_DATE_OF_CCSDS_EPOCH) ⇒ Float
Returns The number of seconds from the given epoch to the given CCSDS day, milliseconds, and microseconds.
398 399 400 |
# File 'lib/openc3/core_ext/time.rb', line 398 def self.ccsds2sec(day, ms, us, sec_epoch_jd = JULIAN_DATE_OF_CCSDS_EPOCH) (self.ccsds2julian(day, ms, us) - sec_epoch_jd) * SEC_PER_DAY_FLOAT end |
.days_from_j2000(time) ⇒ Float
Returns Number of julian days since Jan 1, 2000 at noon.
263 264 265 |
# File 'lib/openc3/core_ext/time.rb', line 263 def self.days_from_j2000(time) time.to_julian - JULIAN_DATE_OF_J2000_EPOCH end |
.format_seconds(seconds) ⇒ String
Returns Seconds formatted as a human readable string with days, hours, minutes, and seconds.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/openc3/core_ext/time.rb', line 109 def self.format_seconds(seconds) result = "" mm, ss = seconds.divmod(60) hh, mm = mm.divmod(60) dd, hh = hh.divmod(24) if dd != 0 if dd == 1 result << "%d day, " % dd else result << "%d days, " % dd end end if hh != 0 if hh == 1 result << "%d hour, " % hh else result << "%d hours, " % hh end end if mm != 0 if mm == 1 result << "%d minute, " % mm else result << "%d minutes, " % mm end end if ss > 0 result << "%.2f seconds" % ss else result = result[0..-3] end result end |
.from_nsec_from_epoch(nsec_from_epoch) ⇒ Object
493 494 495 496 497 498 499 |
# File 'lib/openc3/core_ext/time.rb', line 493 def self.from_nsec_from_epoch(nsec_from_epoch) return nil if nsec_from_epoch.nil? seconds = nsec_from_epoch / NSEC_PER_SECOND nanoseconds = nsec_from_epoch % NSEC_PER_SECOND Time.at(seconds, nanoseconds, :nsec) end |
.init_epoch_delta(epoch) ⇒ Float
Ruby time objects cannot handle times before the Unix Epoch. Calculate a delta (in seconds) to be used when real epochs are before the Unix Epoch. Each received timestamp will be adjusted by this delta so a ruby time object can be used to parse the time.
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/openc3/core_ext/time.rb', line 463 def self.init_epoch_delta(epoch) # UnixEpoch - Jan 1, 1970 00:00:00 unix_epoch = DateTime.new(1970, 1, 1, 0, 0, 0) split_epoch = epoch.split epoch_date = split_epoch[0].split("/") epoch_time = split_epoch[1].split(":") if epoch_date[0].to_i < 1970 then # Calculate delta between epoch and unix epoch real_epoch = DateTime.new(epoch_date[0].to_i, epoch_date[1].to_i, epoch_date[2].to_i, epoch_time[0].to_i, epoch_time[1].to_i, epoch_time[2].to_i) day_delta = (unix_epoch - real_epoch).to_i unix_epoch_delta = day_delta * 86400 if real_epoch.hour != 0 or real_epoch.min != 0 or real_epoch.sec != 0 hour_delta = 23 - real_epoch.hour min_delta = 59 - real_epoch.min sec_delta = 60 - real_epoch.sec unix_epoch_delta += ((hour_delta * 3600) + (min_delta * 60) + sec_delta) end else unix_epoch_delta = 0 end unix_epoch_delta end |
.julian2ccsds(jdate) ⇒ Array<day, ms, us>
Returns Julian converted to CCSDS.
380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/openc3/core_ext/time.rb', line 380 def self.julian2ccsds(jdate) day = jdate - JULIAN_DATE_OF_CCSDS_EPOCH fraction = day % 1.0 day = day.to_i ms = fraction * MSEC_PER_DAY_FLOAT fraction = ms % 1.0 ms = ms.to_i us = fraction * USEC_PER_MSEC us = us.to_i return [day, ms, us] end |
.julian2mdy(jdate) ⇒ Array<Year, Month, Day, Hour, Minute, Second, Microsecond>
Convert a Julian Date to mdy format Note that an array is returned rather than a Time object because Time objects cannot represent all possible Julian dates
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/openc3/core_ext/time.rb', line 279 def self.julian2mdy(jdate) z = (jdate + 0.5).to_i w = ((z - 1867216.25) / 36524.25).to_i x = w / 4 a = z + 1 + w - x b = a + 1524 c = ((b - 122.1) / 365.25).to_i d = (365.25 * c).to_i e = ((b - d) / 30.6001).to_i f = (30.6001 * e).to_i day = b - d - f if e > 13 month = e - 13 else month = e - 1 end if month > 2 year = c - 4716 else year = c - 4715 end fraction = jdate - jdate.to_i if fraction >= 0.5 hour = (fraction - 0.5) * 24.0 else hour = (fraction * 24.0) + 12.0 end fraction = hour - hour.to_i hour = hour.to_i minute = fraction * 60.0 fraction = minute - minute.to_i minute = minute.to_i second = fraction * 60.0 fraction = second - second.to_i second = second.to_i us = fraction * 1000000.0 us = us.to_i return [year, month, day, hour, minute, second, us] end |
.julian_centuries_since_j2000(time) ⇒ Float
Returns Number of julian centuries since Jan 1, 2000 at noon.
269 270 271 |
# File 'lib/openc3/core_ext/time.rb', line 269 def self.julian_centuries_since_j2000(time) self.days_from_j2000(time) / JULIAN_DAYS_PER_CENTURY end |
.leap_year?(year) ⇒ Boolean
Returns Whether the year is a leap year.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/openc3/core_ext/time.rb', line 209 def self.leap_year?(year) return_value = false if (year % 4) == 0 return_value = true if (year % 100) == 0 return_value = false if (year % 400) == 0 return_value = true end end end return return_value end |
.mdy2ccsds(year, month, day, hour, minute, second, us) ⇒ Array<day, ms, us>
Convert from mdy format to CCSDS Date Note that an array is used rather than a Time object because Time objects cannot represent all possible CCSDS dates
362 363 364 365 366 367 368 |
# File 'lib/openc3/core_ext/time.rb', line 362 def self.mdy2ccsds(year, month, day, hour, minute, second, us) ms = (hour * MSEC_PER_HOUR) + (minute * MSEC_PER_MINUTE) + (second * MSEC_PER_SECOND) + (us / USEC_PER_MSEC) us = us % USEC_PER_MSEC jd = Time.mdy2julian(year, month, day, 0, 0, 0, 0) day = (jd - JULIAN_DATE_OF_CCSDS_EPOCH).round return [day, ms, us] end |
.mdy2julian(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, us = 0) ⇒ Float
Convert the given year, month, day, hour, minute, second, and us into a Julian date. Julian dates are the number of days (plus fractional days) since Jan 1, 4713 BC at noon.
165 166 167 168 169 |
# File 'lib/openc3/core_ext/time.rb', line 165 def self.mdy2julian(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, us = 0) # Note DateTime does not support fractions of seconds date_time = DateTime.new(year, month, day, hour, minute, second) (date_time - DATE_TIME_MJD_EPOCH).to_f + JULIAN_DATE_OF_MJD_EPOCH + us / USEC_PER_DAY_FLOAT end |
.mdy2mjd(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, us = 0) ⇒ Time
Convert the given year, month, day, hour, minute, second, and us into a Modified Julian date. Modified Julian dates have an Epoch of Nov 17, 1858 at midnight.
189 190 191 |
# File 'lib/openc3/core_ext/time.rb', line 189 def self.mdy2mjd(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, us = 0) return Time.mdy2julian(year, month, day, hour, minute, second, us) - JULIAN_DATE_OF_MJD_EPOCH end |
.sec2ccsds(sec, sec_epoch_jd = JULIAN_DATE_OF_CCSDS_EPOCH) ⇒ Array<day, ms, us>
Returns CCSDS date.
405 406 407 |
# File 'lib/openc3/core_ext/time.rb', line 405 def self.sec2ccsds(sec, sec_epoch_jd = JULIAN_DATE_OF_CCSDS_EPOCH) self.julian2ccsds((sec / SEC_PER_DAY_FLOAT) + sec_epoch_jd) end |
.total_seconds(hour, minute, second, us) ⇒ Float
Returns The number of seconds represented by the hours, minutes, seconds and microseconds.
238 239 240 |
# File 'lib/openc3/core_ext/time.rb', line 238 def self.total_seconds(hour, minute, second, us) (hour * SEC_PER_HOUR_FLOAT) + (minute * SEC_PER_MINUTE_FLOAT) + second + (us / USEC_PER_SECOND_FLOAT) end |
.use_local ⇒ Object
Set up the Time class so that a call to the sys method will set the Time object being operated upon to be a local time.
92 93 94 |
# File 'lib/openc3/core_ext/time.rb', line 92 def self.use_local @@use_utc = false end |
.use_utc ⇒ Object
Set up the Time class so that a call to the sys method will set the Time object being operated upon to be a UTC time.
86 87 88 |
# File 'lib/openc3/core_ext/time.rb', line 86 def self.use_utc @@use_utc = true end |
.yds(year, day_of_year, sec_of_day) ⇒ Object
Create a new time object given year, day of year (1-366), and seconds of day
203 204 205 |
# File 'lib/openc3/core_ext/time.rb', line 203 def self.yds(year, day_of_year, sec_of_day) return Time.utc(*yds2mdy(year, day_of_year, sec_of_day)) end |
.yds2julian(year, day, sec) ⇒ Float
Returns Year, day, seconds converted to the Julian date.
453 454 455 456 |
# File 'lib/openc3/core_ext/time.rb', line 453 def self.yds2julian(year, day, sec) year, month, day, hour, min, seconds, usec = self.yds2mdy(year, day, sec) Time.mdy2julian(year, month, day, hour, min, seconds, usec) end |
.yds2mdy(year, day, sec) ⇒ Array
Returns [year, month, day, hour, minute, second, usec].
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/openc3/core_ext/time.rb', line 413 def self.yds2mdy(year, day, sec) # Convert day of year (1-366) to day of month (1-31) if self.leap_year?(year) array = Time::LeapYearMonthDays else array = Time::CommonYearMonthDays end month = 1 array.each do |days| if (day - days) >= 1 day -= days month += 1 else break end end # Calculate hour of day (0-23) hour = (sec / SEC_PER_HOUR).to_i sec -= (hour * SEC_PER_HOUR).to_f # Calculate minute of hour (0-59) min = (sec / SEC_PER_MINUTE).to_i sec -= (min * SEC_PER_MINUTE).to_f # Calculate second of minute (0-60) seconds = sec.to_i sec -= seconds.to_f # Calculate useconds of second (0-999999) usec = (sec * 1000000.0).to_i return [year, month, day, hour, min, seconds, usec] end |
Instance Method Details
#as_json(_options = nil) ⇒ Object
:nodoc:
138 139 140 |
# File 'lib/openc3/io/json_rpc.rb', line 138 def as_json( = nil) #:nodoc: self.to_s end |
#formatted(include_year = true, fractional_digits = 3) ⇒ String
Returns Date formatted as YYYY/MM/DD HH:MM:SS.US.
248 249 250 251 252 253 254 |
# File 'lib/openc3/core_ext/time.rb', line 248 def formatted(include_year = true, fractional_digits = 3) str = "" str << "%Y/%m/%d " if include_year str << "%H:%M:%S" str << ".%#{fractional_digits}N" if fractional_digits > 0 self.strftime(str) end |
#leap_year? ⇒ Boolean
Returns Whether the year is a leap year.
228 229 230 |
# File 'lib/openc3/core_ext/time.rb', line 228 def leap_year? Time.leap_year?(self.year) end |
#seconds_of_day ⇒ Float
Returns The number of seconds in the day (0-86399.99).
243 244 245 |
# File 'lib/openc3/core_ext/time.rb', line 243 def seconds_of_day Time.total_seconds(self.hour, self.min, self.sec, self.usec) end |
#sys ⇒ Object
Set the Time object to be either a UTC or local time depending on the use_utc flag.
98 99 100 101 102 103 104 |
# File 'lib/openc3/core_ext/time.rb', line 98 def sys if @@use_utc self.utc else self.localtime end end |
#to_julian ⇒ Float Also known as: to_jd
Returns The Time converted to a julian date.
172 173 174 |
# File 'lib/openc3/core_ext/time.rb', line 172 def to_julian return Time.mdy2julian(self.year, self.month, self.day, self.hour, self.min, self.sec, self.usec) end |
#to_mjd ⇒ Object
Convert a time object to the modified julian date
194 195 196 |
# File 'lib/openc3/core_ext/time.rb', line 194 def to_mjd return Time.mdy2mjd(self.year, self.month, self.day, self.hour, self.min, self.sec, self.usec) end |
#to_msec_from_epoch ⇒ Object
501 502 503 |
# File 'lib/openc3/core_ext/time.rb', line 501 def to_msec_from_epoch (self.tv_sec * MSEC_PER_SECOND) + (self.tv_nsec / NSEC_PER_MSEC) end |
#to_nsec_from_epoch ⇒ Object
489 490 491 |
# File 'lib/openc3/core_ext/time.rb', line 489 def to_nsec_from_epoch (self.tv_sec * NSEC_PER_SECOND) + self.tv_nsec end |
#to_timestamp ⇒ String
Returns Date formatted as YYYYMMDDHHmmSSNNNNNNNNN.
257 258 259 |
# File 'lib/openc3/core_ext/time.rb', line 257 def self.strftime("%Y%m%d%H%M%S%N") end |