Convert Julian time to sidereal time

    D. Vallado Ed. 4

    Parameters
    ----------

    Jdate: float
        Julian centuries from J2000.0

    Results
    -------

    tsr : float
        Sidereal time
551410
def julian2sidereal(Jdate: float) -> float:
    

    jdate = np.atleast_1d(Jdate)
    assert jdate.ndim == 1

    tsr = np.empty(jdate.size)
    for i, jd in enumerate(jdate):
        # %% Vallado Eq. 3-42 p. 184, Seidelmann 3.311-1
        tUT1 = (jd - 2451545.0) / 36525.

        # Eqn. 3-47 p. 188
        gmst_sec = (67310.54841 + (876600 * 3600 + 8640184.812866) *
                    tUT1 + 0.093104 * tUT1**2 - 6.2e-6 * tUT1**3)

        # 1/86400 and %(2*pi) implied by units of radians
        tsr[i] = gmst_sec * (2 * pi) / 86400. % (2 * pi)

    return tsr.squeeze()
1253498
def QA_util_future_to_tradedatetime(real_datetime):
    
    if len(str(real_datetime)) >= 19:
        dt = datetime.datetime.strptime(
            str(real_datetime)[0:19],
            '%Y-%m-%d %H:%M:%S'
        )
        return dt if dt.time(
        ) < datetime.time(21,
                          0) else QA_util_get_next_datetime(dt,
                                                            1)
    elif len(str(real_datetime)) == 16:
        dt = datetime.datetime.strptime(
            str(real_datetime)[0:16],
            '%Y-%m-%d %H:%M'
        )
        return dt if dt.time(
        ) < datetime.time(21,
                          0) else QA_util_get_next_datetime(dt,
                                                            1)
670171
def datetime_to_ns(then):
    
    if then == datetime(1970, 1, 1, 0, 0):
        return 'Antiquity'

    now = datetime.utcnow()
    delta = now - then
    seconds = delta.total_seconds()

    # There's gotta be a better way to do this...
    years,   seconds = divmod(seconds, 60*60*24*365)
    days,    seconds = divmod(seconds, 60*60*24)
    hours,   seconds = divmod(seconds, 60*60)
    minutes, seconds = divmod(seconds, 60)
    years   = int(years)
    days    = int(days)
    hours   = int(hours)
    minutes = int(minutes)
    seconds = round(seconds)

    if years > 1:
        if days > 1:
            return f'{years} years {days} days ago'
        elif days == 1:
            return '{years} years 1 day ago'
        return '{years} years ago'
    if years == 1:
        if days > 1:
            return f'1 year {days} days ago'
        elif days == 1:
            return '1 year 1 day ago'
        return '1 year ago'

    if days > 3:
        return f'{days} days ago'
    if days > 1:
        if hours > 1:
            return f'{days} days {hours} hours ago'
        elif hours == 1:
            return f'{days} days 1 hour ago'
        return f'{days} days ago'
    if days == 1:
        if hours > 1:
            return f'1 day {hours} hours ago'
        elif hours == 1:
            return '1 day 1 hour ago'
        return '1 day ago'

    if hours > 1:
        return f'{hours} hours ago'
    if hours == 1:
        return f'{minutes + 60} minutes ago'

    if minutes > 1:
        return f'{minutes} minutes ago'
    if minutes == 1:
        return '1 minute ago'

    return 'Seconds ago'
123388
def _adapt_WSDateTime(dt):
    
    try:
        ts = int(
            (dt.replace(tzinfo=pytz.utc)
            - datetime(1970,1,1,tzinfo=pytz.utc)
            ).total_seconds()
        )
    except (OverflowError,OSError):
        if dt < datetime.now():
            ts = 0
        else:
            ts = 2**63-1
    return ts
340428
def parse_date(sdate)
    # Assume this is UTC if no timezone is specified
    sdate = sdate + "Z" unless sdate.match(/Z|([+|-]\d{2}:\d{2})$/)

    # This is to handle older versions of Ruby (e.g. ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mingw32])
    # See http://makandra.com/notes/1017-maximum-representable-value-for-a-ruby-time-object
    # In recent versions of Ruby, Time has a much larger range
    begin
      result = Time.parse(sdate)
    rescue ArgumentError
      result = DateTime.parse(sdate)
    end

    return result
  end
239379
def sun_ra_dec(utc_time):
    
    jdate = jdays2000(utc_time) / 36525.0
    eps = np.deg2rad(23.0 + 26.0 / 60.0 + 21.448 / 3600.0 -
                     (46.8150 * jdate + 0.00059 * jdate * jdate -
                      0.001813 * jdate * jdate * jdate) / 3600)
    eclon = sun_ecliptic_longitude(utc_time)
    x__ = np.cos(eclon)
    y__ = np.cos(eps) * np.sin(eclon)
    z__ = np.sin(eps) * np.sin(eclon)
    r__ = np.sqrt(1.0 - z__ * z__)
    # sun declination
    declination = np.arctan2(z__, r__)
    # right ascension
    right_ascension = 2 * np.arctan2(y__, (x__ + r__))
    return right_ascension, declination
247039
def convert_timestamp(timestamp):
    
    datetime = dt.datetime.utcfromtimestamp(timestamp/1000.)
    return np.datetime64(datetime.replace(tzinfo=None))
429759
def hdate_to_jdn(date):
    
    day = date.day
    month = date.month
    if date.month == 13:
        month = 6
    if date.month == 14:
        month = 6
        day += 30

    # Calculate days since 1,1,3744
    day = _days_from_3744(date.year) + (59 * (month - 1) + 1) // 2 + day

    # length of year
    length_of_year = get_size_of_hebrew_year(date.year)
    # Special cases for this year
    if length_of_year % 10 > 4 and month > 2:  # long Heshvan
        day += 1
    if length_of_year % 10 < 4 and month > 3:  # short Kislev
        day -= 1
    if length_of_year > 365 and month > 6:  # leap year
        day += 30

    # adjust to julian
    return day + 1715118
508556
def julian_day(year, n):
    
    if n > 59 and isleap(year):
        n += 1
    return datetime(year, 1, 1) + timedelta(n - 1)
1125203
def from_jd(jd):
    ''''''

    jd = trunc(jd) + 0.5
    year = trunc(((30 * (jd - EPOCH)) + 10646) / 10631)
    month = min(12, ceil((jd - (29 + to_jd(year, 1, 1))) / 29.5) + 1)
    day = int(jd - to_jd(year, month, 1)) + 1
    return (year, month, day)
1309130
def _scale_tdb_minus_tt(self, mjd, eop):
        
        jd = mjd + Date.JD_MJD
        jj = Date._julian_century(jd)
        m = radians(357.5277233 + 35999.05034 * jj)
        delta_lambda = radians(246.11 + 0.90251792 * (jd - 2451545.))
        return 0.001657 * sin(m) + 0.000022 * sin(delta_lambda)
186636
def dt64_to_dt(dt64):
    
    ts = (dt64 - np.datetime64('1970-01-01T00:00:00')) / np.timedelta64(1, 's')
    return dt.datetime.utcfromtimestamp(ts)
24039
def to_jd(year, month, day):
    ""
    return day + (month - 1) * 30 + (year - 1) * 365 + floor(year / 4) + EPOCH - 1
759553
def from_jd(jd):
    ''''''
    year = gregorian.from_jd(jd)[0]
    day = jwday(jd) + 1

    dayofyear = ordinal.from_jd(jd)[1]
    week = trunc((dayofyear - day + 10) / 7)

    # Reset year
    if week < 1:
        week = weeks_per_year(year - 1)
        year = year - 1

    # Check that year actually has 53 weeks
    elif week == 53 and weeks_per_year(year) != 53:
        week = 1
        year = year + 1

    return year, week, day
1147665
def gmst(utc_time):
    
    ut1 = jdays2000(utc_time) / 36525.0
    theta = 67310.54841 + ut1 * (876600 * 3600 + 8640184.812866 + ut1 *
                                 (0.093104 - ut1 * 6.2 * 10e-6))
    return np.deg2rad(theta / 240.0) % (2 * np.pi)
303384
static double lunarLongitude(double jde) { // apparent moon longitude in degrees

        double jct = (jde - 2451545.0) / 36525; // julian centuries (J2000)

        // Meeus (47.1): L'
        double meanLongitude =
            normalize(
                218.3164477
                    + (481267.88123421 + (-0.0015786 + (1.0 / 538841 + (-1.0 / 65194000) * jct) * jct) * jct) * jct);

        // Meeus (47.2): D
        double meanElongation =
            normalize(
                297.8501921
                    + (445267.1114034 + (-0.0018819 + (1.0 / 545868 + (1.0 / 113065000) * jct) * jct) * jct) * jct);

        // Meeus (47.3): M
        double meanAnomalySun =
            normalize(
                357.5291092 + (35999.0502909 + (-0.0001536 + (1.0 / 24490000) * jct) * jct) * jct);

        // Meeus (47.4): M'
        double meanAnomalyMoon =
            normalize(
                134.9633964
                    + (477198.8675055 + (0.0087414 + (1.0 / 69699 + (1.0 / 14712000) * jct) * jct) * jct) * jct);

        // Meeus (47.5): F
        double meanDistance =
            normalize(
                93.272095
                    + (483202.0175233 + (-0.0036539 + (-1.0 / 3526000 + (1.0 / 863310000) * jct) * jct) * jct) * jct);

        // Meeus (47.6)
        double e = 1 - (0.002516 + 0.0000074 * jct) * jct;
        double ee = e * e;

        double sumL = 0.0;

        for (int i = A_D.length - 1; i >= 0; i--) {
            double eFactor;
            switch (A_M[i]) {
                case -1:
                case 1:
                    eFactor = e;
                    break;
                case -2:
                case 2:
                    eFactor = ee;
                    break;
                default:
                    eFactor = 1;
            }
            double arg = Math.toRadians(
                A_D[i] * meanElongation + A_M[i] * meanAnomalySun + A_M2[i] * meanAnomalyMoon + A_F[i] * meanDistance);
            sumL += (COEFF_L[i] * eFactor * Math.sin(arg));
        }

        double a1 = 119.75 + 131.849 * jct;
        double a2 = 53.09 + 479264.29 * jct;

        sumL += (
            3958 * Math.sin(Math.toRadians(a1))
                + 1962 * Math.sin(Math.toRadians(meanLongitude - meanDistance))
                + 318 * Math.sin(Math.toRadians(a2))
        );

        double[] result = new double[5];
        StdSolarCalculator.nutations(jct, result);
        return AstroUtils.toRange_0_360(meanLongitude + (sumL / MIO) + result[0]);

    }
1095971
def timedelta_to_httpdate(td):
    
    if isinstance(td, (int, float)):
        return format_date_time(time.time() + td)
    elif isinstance(td, timedelta):
        return format_date_time(time.time() + total_seconds(td))
    else:
        raise TypeError("expected datetime.timedelta or number of seconds"
                        "(int/float), got '%s'" % td)