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- 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, include_utc_offset = false) ⇒ String
Date formatted as YYYY/MM/DD HH:MM:SS.US UTC_OFFSET.
-
#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_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.
385 386 387 |
# File 'lib/openc3/core_ext/time.rb', line 385 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
348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/openc3/core_ext/time.rb', line 348 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.
409 410 411 |
# File 'lib/openc3/core_ext/time.rb', line 409 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.
274 275 276 |
# File 'lib/openc3/core_ext/time.rb', line 274 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.
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 142 143 144 145 |
# File 'lib/openc3/core_ext/time.rb', line 113 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
504 505 506 507 508 509 510 |
# File 'lib/openc3/core_ext/time.rb', line 504 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.
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/openc3/core_ext/time.rb', line 474 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.
391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/openc3/core_ext/time.rb', line 391 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
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 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/openc3/core_ext/time.rb', line 290 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.
280 281 282 |
# File 'lib/openc3/core_ext/time.rb', line 280 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.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/openc3/core_ext/time.rb', line 213 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
373 374 375 376 377 378 379 |
# File 'lib/openc3/core_ext/time.rb', line 373 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.
169 170 171 172 173 |
# File 'lib/openc3/core_ext/time.rb', line 169 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.
193 194 195 |
# File 'lib/openc3/core_ext/time.rb', line 193 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.
416 417 418 |
# File 'lib/openc3/core_ext/time.rb', line 416 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.
242 243 244 |
# File 'lib/openc3/core_ext/time.rb', line 242 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.
96 97 98 |
# File 'lib/openc3/core_ext/time.rb', line 96 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.
90 91 92 |
# File 'lib/openc3/core_ext/time.rb', line 90 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
207 208 209 |
# File 'lib/openc3/core_ext/time.rb', line 207 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.
464 465 466 467 |
# File 'lib/openc3/core_ext/time.rb', line 464 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].
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/openc3/core_ext/time.rb', line 424 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:
121 122 123 |
# File 'lib/openc3/io/json_rpc.rb', line 121 def as_json( = nil) #:nodoc: self.to_s end |
#formatted(include_year = true, fractional_digits = 3, include_utc_offset = false) ⇒ String
Returns Date formatted as YYYY/MM/DD HH:MM:SS.US UTC_OFFSET.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/openc3/core_ext/time.rb', line 252 def formatted(include_year = true, fractional_digits = 3, include_utc_offset = false) str = "" str << "%Y/%m/%d " if include_year str << "%H:%M:%S" str << ".%#{fractional_digits}N" if fractional_digits > 0 if include_utc_offset if self.utc? str << " UTC" else str << " %z" end end self.strftime(str) end |
#leap_year? ⇒ Boolean
Returns Whether the year is a leap year.
232 233 234 |
# File 'lib/openc3/core_ext/time.rb', line 232 def leap_year? Time.leap_year?(self.year) end |
#seconds_of_day ⇒ Float
Returns The number of seconds in the day (0-86399.99).
247 248 249 |
# File 'lib/openc3/core_ext/time.rb', line 247 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.
102 103 104 105 106 107 108 |
# File 'lib/openc3/core_ext/time.rb', line 102 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.
176 177 178 |
# File 'lib/openc3/core_ext/time.rb', line 176 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
198 199 200 |
# File 'lib/openc3/core_ext/time.rb', line 198 def to_mjd return Time.mdy2mjd(self.year, self.month, self.day, self.hour, self.min, self.sec, self.usec) end |
#to_nsec_from_epoch ⇒ Object
500 501 502 |
# File 'lib/openc3/core_ext/time.rb', line 500 def to_nsec_from_epoch (self.tv_sec * NSEC_PER_SECOND) + self.tv_nsec end |
#to_timestamp ⇒ String
Returns Date formatted as YYYYMMDDHHmmSSNNNNNNNNN.
268 269 270 |
# File 'lib/openc3/core_ext/time.rb', line 268 def self.strftime("%Y%m%d%H%M%S%N") end |