Example #1
0
def table_to_csvs(table: Table, start: datetime, end: datetime) -> list:
    """Take a feature of interest table and split it into a bunch of csv files
    that are grouped by node. Return the names of all the files it made.
    """
    files = {}
    writers = {}
    file_names = []

    query = redshift_session.query(table) \
        .filter(table.c.datetime >= start) \
        .filter(table.c.datetime < end) \
        .yield_per(1000)

    for row in query:
        node = row.node_id
        if node not in files:
            file = '{}.{}.{}.{}.csv'
            feature = table.name.split('__')[1]
            file = file.format(node, feature, start.date(), end.date())

            file_names.append(file)
            files[node] = open('/tmp/' + file, 'w')
            writers[node] = csv.writer(files[node])
            writers[node].writerow(row.keys())
        writers[node].writerow(row)

    for file in files.values():
        file.seek(0)
        file.close()

    return file_names
Example #2
0
async def query_hist_data(
        engine: object, sectype: str, symbol: str, datatype: str, barsize: str,
        start: datetime=None, end: datetime=None) -> MarketDataBlock:
    """Query database on conditions.
    """
    if start is None:
        start = pytz.UTC.localize(datetime(1, 1, 1))
    if end is None:
        end = pytz.UTC.localize(datetime(9999, 12, 31, 23, 59, 59))
    table = _gen_sa_table(sectype)
    stmt = table.select().where(
        and_(
            table.c.Symbol == symbol,
            table.c.DataType == datatype,
            table.c.BarSize == barsize,
            table.c.TickerTime.between(
                start.astimezone(pytz.UTC), end.astimezone(pytz.UTC))
        )
    )
    async with engine.acquire() as conn:
        result = await conn.execute(stmt)
    df = pd.DataFrame(list(result), columns=table.columns.keys())
    blk = MarketDataBlock(df, tz='UTC')
    blk.tz_convert(start.tzinfo)
    return blk
Example #3
0
def populate_full_water_temperature(request):
    obj = SoapCalls()
    DataFormat = 'json'
    sensors = obj.GetSensors(DataFormat)

    sDate = DT(2016, 1, 1)
    eDate = sDate + datetime.timedelta(days=9)
    stationNumbers = get_station_num(sensors, 'WT')

    temperature_data = None
    count = 0

    while eDate < DT.today():
        count = count + 1
        print ("**** Writing batch: %s" % count)
        sDate = eDate + datetime.timedelta(days=1)
        eDate = eDate + datetime.timedelta(days=10)
        for x in stationNumbers:
            temperature_x = obj.GetTimeSeriesData(x, ['WT'], sDate.isoformat(), eDate.isoformat(), DataFormat)
            for x in temperature_x['TimeSeriesData']:
                wt_datetime = DT.strptime(x['TimeStamp'], '%m/%d/%Y %I:%M:%S %p')
                try:
                    wt = WT(station_id=x['StationID'], station_name=x['StationName'],
                            timestamp=wt_datetime, value=x['Value'])
                    wt.save()
                except Exception:
                    continue

    return HttpResponse('Written to database FULL')
Example #4
0
    def list_transactions(self, account_id: str,
                          since: datetime.datetime = None,
                          before: datetime.datetime = None,
                          limit: int = None) -> List[Transaction]:
        """
        List recent transactions for the account

        :param account_id: account id
        :param before: only list transactions before that date
        :param since: only list transactions after that date
        :param limit: only show a number of transactions
        :return: A list of Transaction objects
        """

        params = {
            'account_id': account_id,
            'expand[]': 'merchant'
        }

        if since:
            params.update({'since': since.isoformat('T')+'Z'})
        if before:
            params.update({'before': before.isoformat('T')+'Z'})
        if limit:
            params.update({'limit': limit})

        response = self._make_request('/transactions', params)

        return [
            Transaction(client=self, **transaction)
            for transaction in response['transactions']
        ]
Example #5
0
def next_502(now: datetime=None):
    if now is None:
        now = datetime.utcnow()

    now = now.replace(tzinfo=timezone('UTC')) if now.utcoffset() is None else now
    now_replaced = now.replace(second=0, microsecond=0)

    candidates = []  # dt, tz, delta

    for tz in ['Europe/Berlin', 'US/Pacific']:
        for day_offset in [-1, 0, 1]:
            for hour in [5, 17]:
                candidate = now_replaced.astimezone(timezone(tz)).replace(hour=hour, minute=2) \
                            + timedelta(days=day_offset)

                if candidate >= now_replaced:
                    candidates.append((candidate, tz, candidate - now))

    dt, tz, delta = min(*candidates, key=lambda x: x[2])

    if now.hour in (5, 17) and now.minute == 2:
        return "Holy shit, it's 502."
    else:
        delta_str = str(delta)

        if time_start_regex.match(delta_str) is None:
            delta_str = '0' + delta_str

        return '{tz} 502: T-{delta}'.format(tz=tz, delta=delta_str)
Example #6
0
    def process(self, start_time:datetime, end_time:datetime, input:DataFrame):
        logger.debug('Start: %s  End: %s  Log: index=%s fields=%s' % (start_time.isoformat(), end_time.isoformat(), str(self.indices), str(self.fields)))

        search = Search(using=self.client, index=self.indices[0])
        search = search.filter(Range(** {'@timestamp': {'gte': start_time.isoformat(), 'lte': end_time.isoformat()}}))

        for k,v in self.fields.items():
            if isinstance(v, list):
                for sv in v:
                    search = search.query("match", **{k:sv})

            else:
                search = search.query("match", **{k:v})

        logger.debug('ES Query: %s' % str(search.to_dict()))
        response = search.execute()

        logger.debug('Results: success:%d failed:%d hits:%d' % (response._shards.successful, response._shards.failed, len(response.hits)))

        for hit in response:
            # filter out the meta key and flatten the values
            row = {k: str(hit[k]) for k in hit if k != 'meta'}

            logger.debug(row)
            input = input.append(row, ignore_index=True)

        return input
Example #7
0
def trading_days(
        time_end: datetime, time_dur: str=None, time_start: datetime=None):
    """determine start and end trading days covering time_dur or time_start.
       So far use NYSE trading days calendar for all exchanges.
    """
    xchg_tz = time_end.tzinfo
    end_idx = bisect.bisect_left(NYSE_CAL, time_end.replace(tzinfo=None))
    if time_start is not None:
        # ignore time_dur, use time_start, time_end as boundary.
        start_idx = bisect.bisect_left(
            NYSE_CAL, time_start.replace(tzinfo=None))
        trading_days = NYSE_CAL[start_idx:end_idx]
    else:
        tdur = timedur_to_timedelta(time_dur)
        # If tdur remainding h/m/s > 0, round it up to 1 more day.
        n_trading_days = tdur.days
        if xchg_tz.normalize(
                time_end - relativedelta(seconds=tdur.seconds)
        ) < tzmin(time_end, tz=xchg_tz):
            n_trading_days += 1
        # time_dur in days, and time_end is not beginning of day.
        if time_end.time() != datetime.min.time():
            n_trading_days += 1
        # Slicing from trading day calendar.
        trading_days = NYSE_CAL[end_idx-n_trading_days:end_idx]
    return trading_days
Example #8
0
def get_start_end_date(time_range_selected, base: datetime = None):
    if not base:
        base = dt.now().date()
    if time_range_selected == TimeRanges.yesterday.value:
        start = end = base - timedelta(1)
    elif time_range_selected == TimeRanges.this_week.value:
        start = base - timedelta(days=base.weekday())
        end = start + timedelta(6)
    elif time_range_selected == TimeRanges.previous_week.value:
        start = base - timedelta(days=base.weekday() + 7)
        end = start + timedelta(6)
    elif time_range_selected == TimeRanges.this_2_week.value:
        start = base - timedelta(days=base.weekday() + 7)
        end = start + timedelta(13)
    elif time_range_selected == TimeRanges.previous_2_week.value:
        start = base - timedelta(days=base.weekday() + 14)
        end = start + timedelta(13)
    elif time_range_selected == TimeRanges.this_month.value:
        start = dt(base.year, base.month, 1).date()
        end = dt(base.year, base.month, monthrange(base.year, base.month)[1]).date()
    elif time_range_selected == TimeRanges.previous_month.value:
        prev_month = 12 if base.month == 1 else base.month - 1
        year = base.year - 1 if base.month == 1 else base.year
        start = dt(year, prev_month, 1).date()
        end = dt(year, prev_month, monthrange(year, prev_month)[1]).date()
    else:
        start = end = base

    end = base if end > base else end

    return start, end
Example #9
0
def str2dt(time: datetime) -> np.ndarray:
    """
    Converts times in string or list of strings to datetime(s)

    Parameters
    ----------

    time : str or datetime.datetime or numpy.datetime64

    Results
    -------

    t : datetime.datetime

    """
    if isinstance(time, datetime):
        return time
    elif isinstance(time, str):
        return parse(time)
    elif isinstance(time, np.datetime64):
        return time.astype(datetime)
    else:  # some sort of iterable
        try:
            if isinstance(time[0], datetime):
                return time
            elif isinstance(time[0], np.datetime64):
                return time.astype(datetime)
            elif isinstance(time[0], str):
                return [parse(t) for t in time]
        except (IndexError, TypeError):
            pass

        # last resort--assume pandas/xarray

        return time.values.astype('datetime64[us]').astype(datetime)
Example #10
0
def auction_results(date: datetime) -> dict:
    """
    get result of auctions.
    :param date:
    :return:
    """
    date = date.strftime('%d.%m.%Y')
    year = date.split('.')[2]
    # url = 'https://www.bank.gov.ua/control/uk/auction/details?date=' + date + '&year=' + year
    url = 'https://www.bank.gov.ua/control/uk/auction/details'
    payload = {'date': date, 'year': year}
    # if not proxy_is_used:
    #     responce_get = requests.get(url, headers=headers)
    # else:
    #     responce_get = requests.get(url, headers=headers, timeout = 3, proxies=proxies)
    responce_get = requests.get(url, headers=headers, params=payload)
    soup = BeautifulSoup(responce_get.text, "html.parser")
    # if date != soup.body.table.find('option', attrs={'selected': ''})['value']:
    #     return None
    document = {}
    get_float = lambda tag: float(tag.find('td', attrs={'class': 'cell_c'}).get_text(strip=True))
    document['time'] = datetime.strptime(date, '%d.%m.%Y')
    document['source'] = 'nbu_auction'
    for field in soup.body.table.find('table', attrs={'border': '0', 'width': '650px'}).find_all('tr'):
        if isinstance(field.td, type(None)):
            continue
        if field.td.string == 'Валюта аукціону':
            if field.td.next_sibling.next_sibling.get_text(strip=True) == 'Долар США':
                document['currency'] = 'USD'
            else:
                document['currency'] = None
        elif type(field.next_element) == Comment:
            if field.next_element in [' 1 # 1.0.1 || 1.0.2 ', ' 1.0.1 || 1.0.2 ']:
                if field.td.get_text(strip=True) == 'КУПІВЛЯ':
                    document['operation'] = 'buy'
                elif field.td.get_text(strip=True) == 'ПРОДАЖ':
                    document['operation'] = 'sell'
            elif field.next_element in [' 2 # 1.1 ', ' 1.1 ', ' 3 ']:
                # Загальний обсяг заявок суб'єктів ринку, прийнятих на аукціон відповідно до умов його проведення (млн. од. валюти)
                document['amount_requested'] = get_float(field)
            elif field.next_element in [' 6 # 1.2.1 ', ' 1.2.1 ']:
                # Курси гривні, заявлені учасниками аукціону (грн. за 1 од. валюти)
                document['rate_r_max'] = get_float(field)
            elif field.next_element in [' 7 # 1.2.2 ', '  1.2.2 ']:
                document['rate_r_min'] = get_float(field)
            elif field.next_element in [' 9 # 1.3.1 ', ' 1.3.1 ']:
                document['rate_acc_med'] = get_float(field)
            elif field.next_element in [' 10 # 1.3.2 ', ' 1.3.2 ']:
                document['rate_acc_max'] = get_float(field)
            elif field.next_element in [' 11 # 1.3.3 ', ' 1.3.3 ']:
                document['rate_acc_min'] = get_float(field)
            elif field.next_element in [' 12 # 1.4 ', ' 1.4 ', ' 7 ']:
                # Загальний обсяг задоволених заявок учасників аукціону (млн. од. валюти)
                document['amount_accepted_all'] = get_float(field)
            elif field.next_element == ' 13 - 1.5 || 1.6 ':
                # Частка задоволених заявок за максимальним курсом аукціону в загальному обсязі задоволених заявок (%)
                document['amount_accepted_p_min_max'] = get_float(field)

    return document
Example #11
0
 def __init__(self, id: str, name: str, created: datetime, modified: datetime, md5: str, size: int, status: Enum):
     self.id = id
     self.name = name
     self.created = created.replace(tzinfo=None)
     self.modified = modified.replace(tzinfo=None)
     self.md5 = md5
     self.size = size
     self.status = status
Example #12
0
 def datetime_to_utc(dt: datetime):
     """Convert a given datetime to UTC time for timezone compatibility"""
     if dt.strftime(DiscordClient.DATE_FORMAT) == "1900-01-01":
         dt = datetime.combine(datetime.now().date(), dt.time())
     to_zone = tz.tzutc()
     from_zone = tz.tzlocal()
     local = dt.replace(tzinfo=from_zone)
     return local.astimezone(to_zone)
    def get_usage(self, start: datetime.datetime, end: datetime.datetime):
        start_ts = start.timestamp() * 1000
        end_ts = end.timestamp() * 1000

        min_value = self.__value_item.db("min", start_ts, end_ts)
        max_value = self.__value_item.db("max", start_ts, end_ts)
        min_value = 0 if min_value is None else min_value
        max_value = 0 if max_value is None else max_value
        return max_value - min_value
Example #14
0
def get_datetime_string(dt: datetime = None, with_timezone: bool = True) -> typing.Optional[str]:
    if not dt:
        dt = datetime.now()

    if with_timezone:
        dt = dt.astimezone()

    s = dt.isoformat()
    return s
Example #15
0
def w3c_datetime_str(dt: _datetime = None, date_only: bool = False) -> str:
    """Format date/time string according to W3C.
    """
    if not dt:
        dt = _datetime.now()

    if not dt.tzinfo:
        dt = _pytz.timezone(_tzname[0]).localize(dt)

    return dt.strftime('%Y-%m-%d') if date_only else dt.strftime('%Y-%m-%dT%H:%M:%S%z')
Example #16
0
    def cal_open(self, widget, event, model=None, window=None):
        if self.readonly:
            common.message(_('This widget is readonly !'))
            return True

        win = gtk.Dialog(_('OpenERP - Date selection'), window,
                gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                gtk.STOCK_OK, gtk.RESPONSE_OK))

        hbox = gtk.HBox()
        hbox.pack_start(gtk.Label(_('Hour:')), expand=False, fill=False)
        hour = gtk.SpinButton(gtk.Adjustment(0, 0, 23, 1, 5), 1, 0)
        hbox.pack_start(hour, expand=True, fill=True)
        hbox.pack_start(gtk.Label(_('Minute:')), expand=False, fill=False)
        minute = gtk.SpinButton(gtk.Adjustment(0, 0, 59, 1, 10), 1, 0)
        hbox.pack_start(minute, expand=True, fill=True)
        win.vbox.pack_start(hbox, expand=False, fill=True)

        cal = gtk.Calendar()
        cal.display_options(gtk.CALENDAR_SHOW_HEADING|gtk.CALENDAR_SHOW_DAY_NAMES|gtk.CALENDAR_SHOW_WEEK_NUMBERS)
        cal.connect('day-selected-double-click', lambda *x: win.response(gtk.RESPONSE_OK))
        win.vbox.pack_start(cal, expand=True, fill=True)
        win.show_all()

        try:
            val = self.get_value(model, timezone=False)
            if val:
                hour.set_value(int(val[11:13]))
                minute.set_value(int(val[-5:-3]))
                cal.select_month(int(val[5:7])-1, int(val[0:4]))
                cal.select_day(int(val[8:10]))
            else:
                hour.set_value(time.localtime()[3])
                minute.set_value(time.localtime()[4])
        except ValueError:
            pass
        response = win.run()
        if response == gtk.RESPONSE_OK:
            hr = int(hour.get_value())
            mi = int(minute.get_value())
            dt = cal.get_date()
            month = int(dt[1])+1
            day = int(dt[2])
            date = DT(dt[0], month, day, hr, mi)
            try:
                value = date.strftime(DHM_FORMAT)
            except ValueError:
                common.message(_('Invalid datetime value! Year must be greater than 1899 !'))        
            else:
                self.show(value, timezone=False)

        self._focus_out()
        win.destroy()
def format_time():
    drbd1 = DateTime(1941, 9, 9)
    now = time.localtime()

    print drbd1.strftime('%m/%d/%y')
    print drbd1.strftime('%B %d, %Y')
    print

    print time.strftime('%m/%d/%y', now)
    print time.strftime('%B %d, %Y', now)
    print
    def get_eventcount(self, bucket_id: str, limit: int=100, start: datetime=None, end: datetime=None) -> int:
        endpoint = "buckets/{}/events/count".format(bucket_id)

        params = dict()  # type: Dict[str, str]
        if start is not None:
            params["start"] = start.isoformat()
        if end is not None:
            params["end"] = end.isoformat()

        response = self._get(endpoint, params=params)
        return int(response.text)
    def get_events(self, bucket_id: str, limit: int=100, start: datetime=None, end: datetime=None) -> List[Event]:
        endpoint = "buckets/{}/events".format(bucket_id)

        params = dict()  # type: Dict[str, str]
        if limit is not None:
            params["limit"] = str(limit)
        if start is not None:
            params["start"] = start.isoformat()
        if end is not None:
            params["end"] = end.isoformat()

        events = self._get(endpoint, params=params).json()
        return [Event(**event) for event in events]
Example #20
0
def generate_chart_interval_datetimes(start_datetime:dt.datetime,end_datetime:dt.datetime,interval_seconds:int):
    values = []
    marketopen = get_market_opening_time(get_google_data('ZVZZT',interval_seconds,1))
    marketclosed = get_market_closing_time(get_google_data('ZVZZT',interval_seconds,1))

    start_datetime.combine(date=start_datetime.date(),time=marketopen)

    market_duration = dt.datetime.combine(dt.datetime.today(),marketclosed) - dt.datetime.combine(dt.datetime.today(),marketopen)
    input_days_difference = end_datetime.day - start_datetime.day
    market_trading_seconds_per_day = market_duration.total_seconds()
    input_trading_day_seconds = input_days_difference * market_trading_seconds_per_day
    input_hours_difference = end_datetime.hour - start_datetime.hour
    input_hours_seconds = input_hours_difference * 3600
    input_minutes_difference = end_datetime.minute - start_datetime.minute
    input_minutes_seconds = input_minutes_difference * 60
    input_seconds_difference = end_datetime.second - start_datetime.second
    total_seconds = input_trading_day_seconds + input_hours_seconds + input_minutes_seconds + input_seconds_difference

    total_intervals = total_seconds // interval_seconds
    print(total_intervals)
    for i in range(0,int(total_intervals)):

        amount_to_add = dt.timedelta(hours=marketopen.hour,minutes=marketopen.minute) + dt.timedelta(seconds=interval_seconds)*i
        basevalue = start_datetime + amount_to_add
        value = basevalue

        # advance to next day
        if value.time() > marketclosed:
            print("Advanced TO Next Day")
            value + dt.timedelta(days=1)
            value.combine(value.date(),marketopen)
        # in case its a saturday
        if value.weekday() == 5:
            value + dt.timedelta(days=2)
            value.combine(value.date(),marketopen)
        # in case its a sunday
        if value.weekday() == 6:
            value + dt.timedelta(days=1)
            value.combine(value.date(),marketopen)

        print(value.time())
        print(marketclosed)
        print(value.time() > marketclosed)
        print(value.date())
        print(value.date().weekday())

        values.append(value)

    print(marketclosed)
    return values
    def get(self, limit: int = -1,
            starttime: datetime=None, endtime: datetime=None) -> List[Event]:
        """Returns events sorted in descending order by timestamp"""
        # Resolution is rounded down since not all datastores like microsecond precision
        if starttime:
            starttime = starttime.replace(microsecond=1000 * int(starttime.microsecond / 1000))
        if endtime:
            # Rounding up here in order to ensure events aren't missed
            # second_offset and microseconds modulo required since replace() only takes microseconds up to 999999 (doesn't handle overflow)
            milliseconds = 1 + int(endtime.microsecond / 1000)
            second_offset = int(milliseconds / 1000)  # usually 0, rarely 1
            microseconds = (1000 * milliseconds) % 1000000  # will likely just be 1000 * milliseconds, if it overflows it would become zero
            endtime = endtime.replace(microsecond=microseconds) + timedelta(seconds=second_offset)

        return self.ds.storage_strategy.get_events(self.bucket_id, limit, starttime, endtime)
def daily_stat(day: datetime, collection) -> dict:
    """
    create day statistics based on hourly data
    currently put in stat data for business hours
    :param day: datetime, date for aggregation hour statistic
    :param collection:
    :return: {'buy': 0.41, 'sell': 0.41, 'sell_rates': [0.41, 0.41], 'buy_rates': [0.41],
            'sell_val': 1030000, 'buy_val': 375000, 'source': 'd_int_stat',
            'time': datetime.datetime(2018, 6, 17, 0, 0),
            'currency': 'RUB'}
    """
    business_time = (day.replace(hour=9, minute=0), day.replace(hour=17, minute=59))
    full_time = (day.replace(hour=0, minute=0), day.replace(hour=23, minute=59))
    time = business_time
    source = 'd_int_stat'
    pipeline = [{'$match': {'source': 'h_int_stat',
                            '$and': [{'time': {'$gte': time[0]}}, {'time': {'$lt': time[1]}}]}},
                {'$group': {'_id': None, 'sell': {'$avg': '$sell'}, 'sell_rates': {'$push':  '$sell'},
                                         'buy': {'$avg': '$buy'}, 'buy_rates': {'$push':  '$buy'},
                            'sell_val': {'$sum': '$sell_val'}, 'buy_val': {'$sum': '$buy_val'}}},
                {'$project': {'_id': False, 'buy': '$buy', 'sell': '$sell', 'sell_rates': '$sell_rates',
                              'buy_rates': '$buy_rates', 'sell_val': '$sell_val', 'buy_val': '$buy_val'}}]
    command_cursor = collection.aggregate(pipeline)

    def form_output_doc(document):
        if collection.name == 'RUB':
            round_dig = 4
        else:
            round_dig = 2
        logger.debug('document= {}'.format(document))
        if not (any(document['sell_rates']) and any(document['buy_rates'])):
            logger.warning('rates missing in stat; len(document[\'sell_rates\'])= {},'
                           'len(document[\'buy_rates\']= {}'.format(document['sell_rates'],
                                                                    document['buy_rates']))
            return {}
        document['sell'] = round(document['sell'], round_dig)
        document['buy'] = round(document['buy'], round_dig)
        document['source'] = source
        document['time'] = time[1].replace(hour=0, minute=0)
        document['currency'] = collection.name
        return document
    # actually one document
    try:
        result_doc = [form_output_doc(doc) for doc in command_cursor][0]
    except IndexError or TypeError:
        return {}
    logger.debug('result_doc= {}'.format(result_doc))
    return result_doc
Example #23
0
    def build_url(cls, symbol: str, begin_datetime: datetime,
                  end_datetime: datetime, granularity: Granularity):

        query_string = json.dumps(dict(s=symbol + '+Interactive'))
        return 'http://finance.yahoo.com/_td_charts_api/resource/' \
               'charts;comparisonTickers=;events=div%7Csplit%7Cearn;' \
               'gmtz=9;indicators=quote;period1={};period2={};' \
               'queryString=%7B%22s%22%3A%22{}%2BInteractive%22%7D;' \
               'range={};rangeSelected=undefined;ticker={};' \
               'useMock=false'.format( \
            begin_datetime.strftime('%s'),
            end_datetime.strftime('%s'),
            urllib.parse.quote_plus(query_string),
            cls.GRANULARITY_RANGE_MAPPINGS[granularity]['str'],
            urllib.parse.quote_plus(symbol)
        )
Example #24
0
def apply_shift(start: datetime, end: datetime, delta: timedelta or None, abs_start: int or None) -> (
        datetime, datetime):

    if delta:
        pass  # nothing to be done

    elif abs_start:
        # delta must be calculated
        same_day_start = start.replace(hour=abs(abs_start), minute=0, second=0, microsecond=0)
        is_start_before_same_day_start = (start - same_day_start).total_seconds() <= 0
        shift_earlier = abs_start < 0

        if shift_earlier:
            if is_start_before_same_day_start:
                prev_day_start = same_day_start - timedelta(days=1)
                delta = prev_day_start - start
            else:
                delta = same_day_start - start

        else:  # shift to later point in time
            if is_start_before_same_day_start:
                delta = same_day_start - start
            else:
                next_day_start = same_day_start + timedelta(days=1)
                delta = start - next_day_start

    else:
        return None  # should not happen

    return start + delta, end + delta
 def timestamp(tn: datetime.datetime) -> str:
     """
     creates a properly formatted timestamp.
     :param tn: datetime to format to timestream timestamp string
     :return:
     """
     return tn.strftime('%Y_%m_%d_%H_%M_%S')
Example #26
0
 def name_file(lane: int, flowcell: str, sample: str, read: int,
               undetermined: bool=False, date: dt.datetime=None, index: str=None) -> str:
     """Name a FASTQ file following MIP conventions."""
     flowcell = f"{flowcell}-undetermined" if undetermined else flowcell
     date_str = date.strftime('%y%m%d') if date else '171015'
     index = index if index else 'XXXXXX'
     return f"{lane}_{date_str}_{flowcell}_{sample}_{index}_{read}.fastq.gz"
Example #27
0
    def __init__(self, id_client: Identifier, z_0: bytes, r: bytes, t_0: datetime, id_server: Identifier,
                 l: int=2 ** 16):
        """
        Create a ksi client's certificate.
        :param id_client: The Identifier of the client
        :type id_client: Identifier
        :param z_0: The last computed hash, it is _not_ part of the hash tree taken from ksi.Keys.keys[0].hash
        :type z_0: bytes
        :param r: The root of the hash tree taken from ksi.Keys.hash_tree_root.hash
        :type r: bytes
        :param t_0: The time at which the certificate becomes valid
        :type t_0: datetime
        :param id_server: The Identifier of the server
        :type id_server: Identifier
        :return:
        """
        assert isinstance(id_client, Identifier) and isinstance(id_server, Identifier)
        assert isinstance(z_0, bytes) and isinstance(r, bytes)
        assert isinstance(t_0, datetime)
        assert isinstance(l, int) and is_power_of_2(l)

        self.id_client = id_client
        self.z_0 = z_0
        self.r = r
        self.t_0 = t_0.replace(microsecond=0)
        self.id_server = id_server
        self.l = l
Example #28
0
def get_with_timezone(
        datetime_object: datetime.datetime,
        to_timezone: str='',
        default_from_timezone: str='UTC',
) -> datetime.datetime:
    """
    Change timezone of a date
    :param datetime_object: datetime to update
    :param to_timezone: timezone name, if equal to '',
    try to grap current user timezone. If no given timezone name and no
    current user timezone, return original date time
    :param default_from_timezone: datetime original timezone if datetime
    object is naive
    :return: datetime updated
    """
    # If no to_timezone, try to grab from current user
    if not to_timezone and tmpl_context.current_user:
        to_timezone = tmpl_context.current_user.timezone

    # If no to_timezone, return original datetime
    if not to_timezone:
        return datetime_object

    # If datetime_object have not timezone, set new from default_from_timezone
    if not datetime_object.tzinfo:
        from_tzinfo = pytz.timezone(default_from_timezone)
        datetime_object = from_tzinfo.localize(datetime_object)

    new_tzinfo = pytz.timezone(to_timezone)
    return datetime_object.astimezone(new_tzinfo)
    def fetch_posts(ch_monitor_id: int, day: datetime.datetime) -> dict:
        """
        Return a mock ch response to the posts end point.

        Generate the mock response by sending back data from a consistent but semirandom selection of
        ch-posts-2016-01-0[12345].json.
        """
        assert MOCK_TWEETS_PER_DAY <= MAX_MOCK_TWEETS_PER_DAY

        test_path = mediawords.util.paths.mc_root_path() + '/mediacloud/test-data/ch/'
        filename = test_path + "ch-posts-" + day.strftime('%Y-%m-%d') + '.json'
        with open(filename, 'r', encoding='utf-8') as fh:
            json = fh.read()

        data = dict(decode_json(json))

        assert 'posts' in data
        assert len(data['posts']) >= MOCK_TWEETS_PER_DAY

        data['posts'] = data['posts'][0:MOCK_TWEETS_PER_DAY]

        # replace tweets with the epoch of the start date so that we can infer the date of each tweet in
        # tweet_urler_lookup below
        i = 0
        for ch_post in data['posts']:
            ch_post['url'] = re.sub(r'status/(\d+)/', '/status/' + str(i), ch_post['url'])
            i += 1

        return data
Example #30
0
    def agregators_per_month(self, date: datetime) -> json:
        # Грошові агрегати та їх компоненти (залишки коштів на кінець періоду, млн. грн.):
        # https://bank.gov.ua/NBUStatService/v1/statdirectory/monetary?date=YYYYMM
        # http https://bank.gov.ua/NBUStatService/v1/statdirectory/monetary  date==201609 json==

        # Складовими Г.а. є фінансові активи у формі готівкових коштів у національній валюті,
        # переказних депозитів, інших депозитів, коштів за цінними паперами, крім акцій, що емітовані
        # депозитними корпораціями та належать на правах власності іншим фінансовим корпораціям,
        # нефінансовим корпораціям, домашнім господарствам та некомерційним організаціям, що
        # обслуговують домашні господарства. Залежно від зниження ступеня ліквідності фінансові активи
        # групують у різні грошові агрегати М0, М1, М2 та М3.
        # Грошовий агрегат М0 включає готівкові кошти в обігу поза депозитними корпораціями.
        # Грошовий агрегат М1 – грошовий агрегат М0 та переказні депозити в національній валюті.
        # Грошовий агрегат М2 – грошовий агрегат М1 і переказні депозити в іноземній валюті та інші депозити.
        # Грошовий агрегат М3 (грошова маса) – грошовий агрегат М2 та цінні папери, крім акцій.
        def _date_object(obj: dict) -> dict:
            try:
                obj['date'] = datetime.strptime(obj['dt'], '%Y%m%d')
            except:
                pass
            del obj['dt']
            return obj
        params = {'date': date.strftime('%Y%m01'), 'json': ''}
        url = self.url + 'monetary'
        logger.debug(f'requested url= {url}, params= {params}')
        resp = requests.get(url, params=params)
        if resp.status_code != 200:
            logger.error(f'server error= {resp.status_code}')
            return {'error': resp.status_code}
        logger.debug(f'resp.json(object_hook=_date_object)= {resp.json(object_hook=_date_object)}')
        return resp.json(object_hook=_date_object)
Example #31
0
def get_date_str(date: datetime):
    return date.strftime("%Y%m%d")
Example #32
0
def datetime_to_epoch_us(date_time: datetime) -> int:
    """Convert a datetime object to an epoch integer (microseconds)."""
    return int(date_time.timestamp() * 1_000_000)
Example #33
0
def find_next_time_expression_time(
    now: dt.datetime,  # pylint: disable=redefined-outer-name
    seconds: list[int],
    minutes: list[int],
    hours: list[int],
) -> dt.datetime:
    """Find the next datetime from now for which the time expression matches.

    The algorithm looks at each time unit separately and tries to find the
    next one that matches for each. If any of them would roll over, all
    time units below that are reset to the first matching value.

    Timezones are also handled (the tzinfo of the now object is used),
    including daylight saving time.
    """
    if not seconds or not minutes or not hours:
        raise ValueError(
            "Cannot find a next time: Time expression never matches!")

    while True:
        # Reset microseconds and fold; fold (for ambiguous DST times) will be handled later
        result = now.replace(microsecond=0, fold=0)

        # Match next second
        if (next_second := _lower_bound(seconds, result.second)) is None:
            # No second to match in this minute. Roll-over to next minute.
            next_second = seconds[0]
            result += dt.timedelta(minutes=1)

        result = result.replace(second=next_second)

        # Match next minute
        next_minute = _lower_bound(minutes, result.minute)
        if next_minute != result.minute:
            # We're in the next minute. Seconds needs to be reset.
            result = result.replace(second=seconds[0])

        if next_minute is None:
            # No minute to match in this hour. Roll-over to next hour.
            next_minute = minutes[0]
            result += dt.timedelta(hours=1)

        result = result.replace(minute=next_minute)

        # Match next hour
        next_hour = _lower_bound(hours, result.hour)
        if next_hour != result.hour:
            # We're in the next hour. Seconds+minutes needs to be reset.
            result = result.replace(second=seconds[0], minute=minutes[0])

        if next_hour is None:
            # No minute to match in this day. Roll-over to next day.
            next_hour = hours[0]
            result += dt.timedelta(days=1)

        result = result.replace(hour=next_hour)

        if result.tzinfo in (None, UTC):
            # Using UTC, no DST checking needed
            return result

        if not _datetime_exists(result):
            # When entering DST and clocks are turned forward.
            # There are wall clock times that don't "exist" (an hour is skipped).

            # -> trigger on the next time that 1. matches the pattern and 2. does exist
            # for example:
            #   on 2021.03.28 02:00:00 in CET timezone clocks are turned forward an hour
            #   with pattern "02:30", don't run on 28 mar (such a wall time does not exist on this day)
            #   instead run at 02:30 the next day

            # We solve this edge case by just iterating one second until the result exists
            # (max. 3600 operations, which should be fine for an edge case that happens once a year)
            now += dt.timedelta(seconds=1)
            continue

        now_is_ambiguous = _datetime_ambiguous(now)
        result_is_ambiguous = _datetime_ambiguous(result)

        # When leaving DST and clocks are turned backward.
        # Then there are wall clock times that are ambiguous i.e. exist with DST and without DST
        # The logic above does not take into account if a given pattern matches _twice_
        # in a day.
        # Example: on 2021.10.31 02:00:00 in CET timezone clocks are turned backward an hour

        if now_is_ambiguous and result_is_ambiguous:
            # `now` and `result` are both ambiguous, so the next match happens
            # _within_ the current fold.

            # Examples:
            #  1. 2021.10.31 02:00:00+02:00 with pattern 02:30 -> 2021.10.31 02:30:00+02:00
            #  2. 2021.10.31 02:00:00+01:00 with pattern 02:30 -> 2021.10.31 02:30:00+01:00
            return result.replace(fold=now.fold)

        if now_is_ambiguous and now.fold == 0 and not result_is_ambiguous:
            # `now` is in the first fold, but result is not ambiguous (meaning it no longer matches
            # within the fold).
            # -> Check if result matches in the next fold. If so, emit that match

            # Turn back the time by the DST offset, effectively run the algorithm on the first fold
            # If it matches on the first fold, that means it will also match on the second one.

            # Example: 2021.10.31 02:45:00+02:00 with pattern 02:30 -> 2021.10.31 02:30:00+01:00

            check_result = find_next_time_expression_time(
                now + _dst_offset_diff(now), seconds, minutes, hours)
            if _datetime_ambiguous(check_result):
                return check_result.replace(fold=1)

        return result
Example #34
0
def _datetime_ambiguous(dattim: dt.datetime) -> bool:
    """Check whether a datetime is ambiguous."""
    assert dattim.tzinfo is not None
    opposite_fold = dattim.replace(fold=not dattim.fold)
    return _datetime_exists(
        dattim) and dattim.utcoffset() != opposite_fold.utcoffset()
Example #35
0
def convert_datetime_to_realworld(dt: datetime) -> str:
    return dt.replace(tzinfo=timezone.utc).isoformat().replace("+00:00", "Z")
Example #36
0
 def __GetEventsOfDay(self, day: datetime.datetime):
     result = list()
     for event in self.Calendar:
         if event.beginTime.date() == day.date():
             result.append(event)
     return result
Example #37
0
def update_answer(
    self,
    user_id: int,
    task_id: int,
    files: List[File],
    description: str,
    files_to_delete=None,
    reveal: datetime.datetime = pytz.utc.localize(datetime.datetime.utcnow())
) -> int:
    """Update the database answer to match. If no answer was submitted, a new one is created. If one is found, it is replaced
        Doesn't check if the user has rights to update the answer
    Arguments:
        user_id {int} -- [description]
        task_id {int} -- [description]
        files {List[File]} -- [description]
        description {str} -- [description]

    Keyword Arguments:
        reveal {datetime.datetime} -- [if this arguments is None, set reveal to be the assignment deadline] (default: {datetime.datetime.utcnow()}) 

    Returns:
        int -- [inserted (or exsisting answer id)]
    """

    self.logger.info("Updating answer for task %s for user %s", task_id,
                     str(user_id))

    with self.engine.connect() as conn:
        if reveal:
            reveal = reveal.astimezone(pytz.utc)
        else:

            j = self.task.join(self.assignment)
            sql = select([self.assignment.c.deadline
                          ]).select_from(j).where(self.task.c.id == task_id)
            rs = conn.execute(sql)
            row = rs.first()
            if row is None:
                self.logger.error("Invalid task id! %s", task_id)
                raise ValueError("Invalid task id!")
            reveal = row[self.assignment.c.deadline]
            self.logger.info(
                "Setting answer reveal same as assignment deadline %s", reveal)

        sql = select([self.answer.c.id
                      ]).where(self.answer.c.task_id == task_id)
        rs = conn.execute(sql)
        row = rs.first()

        if row is None:
            new_answer = True
            self.logger.info("no old answer found, creating new entry")
            sql = self.answer.insert().values(reveal=reveal,
                                              task_id=task_id,
                                              description=description)

            rs = conn.execute(sql)
            id = rs.inserted_primary_key[0]
            self.logger.info("Insert success! id: %s", id)
        else:
            new_answer = False
            id = row[self.answer.c.id]
            self.logger.info("old answer with id " + str(id) +
                             " found, updating values")
            sql = update(self.answer).values(
                reveal=reveal, task_id=task_id).where(self.answer.c.id == id)
            if description is not None:
                sql = sql.values(description=description)
            conn.execute(sql)
            self.logger.info("Update success!")

        if new_answer:
            self.update_file(user_id, files, answer_id=id)
        else:
            self.update_file(user_id,
                             files,
                             answer_id=id,
                             files_to_delete=files_to_delete)
    return id
Example #38
0
 def backup_sim(self, from_date: datetime.datetime, is_move: bool = False):
     return self._connection.post(
         'sms/backup-sim',
         OrderedDict((('IsMove', int(is_move)),
                      ('Date', from_date.strftime("%Y-%m-%d %H:%M:%S")))))
Example #39
0
def _datetime_as_utctimestamp(dt: datetime) -> int:
    return int(dt.astimezone(timezone.utc).timestamp())
Example #40
0
def week(date: datetime = datetime.now()) -> int:
    return date.isocalendar()[1]
Example #41
0
    def _rollup_and_del(
            self,
            start_time: datetime,
            roll_period: int,
            ent_id: int,
            key_id: int,
            end_time: datetime,
            dry_run: bool):
        sel_query = dedent(
            '''
            SELECT t.id, t.added, t.value
            FROM tsd t
            WHERE
                entity_id = %s
                AND key_id = %s
                AND added > %s
                AND added < %s
            ORDER BY added
            '''
        )

        ins_query = dedent(
            '''
            INSERT INTO tsd (entity_id, key_id, added, value)
            VALUES (%s, %s, %s, %s)
            '''
        )

        del_query = 'DELETE FROM tsd WHERE id in ({})'

        stime = start_time.strftime('%Y-%m-%d %H:%M:%S')
        etime = end_time.strftime('%Y-%m-%d %H:%M:%S')

        # First, get all the items we need to work on
        with self.conn.cursor() as curs:
            curs.execute(sel_query, (ent_id, key_id, etime, stime))
            to_compress = curs.fetchall()
        self.conn.commit()

        if not to_compress:
            # If we have no metrics for the period, return
            return

        new_vals = self._compress_vals(to_compress, roll_period)
        # modify new vals for db insertion
        new_vals = [(ent_id, key_id, d, v) for d, v in new_vals]

        # We'll do all this in a transaction
        try:
            with self.conn.cursor() as curs:
                # First we'll delete
                curs.execute(del_query.format(
                    ', '.join([str(d[0]) for d in to_compress])))
                # Now we add the new items
                curs.executemany(ins_query, new_vals)
                if dry_run:
                    self.conn.rollback()
        except psycopg2.errors.AdminShutdown:
            logging.error('The connection has been terminated, reconnecting')
            self.conn = self._get_conn()
        except Exception as e:
            # Log the exception and roll back
            logging.exception('Failed to insert metrics into the db')
            self.conn.rollback()
        else:
            self.conn.commit()
Example #42
0
def _utctoday(now: datetime.datetime) -> datetime.date:
    return now.date()
Example #43
0
def datetime_encoder(date: datetime.datetime):
    representation = date.isoformat()
    if representation.endswith('+00:00'):
        representation = representation[:-6] + 'Z'
    return representation
Example #44
0
def _format_datetime(dt: datetime.datetime) -> str:
    return dt.strftime(settings.DT_TEMPLATE)
Example #45
0
def _format_time(date_time: datetime.datetime) -> str:
    return date_time.astimezone(tzlocal()).strftime("%c %z")
Example #46
0
    def spawnpoints(self, origin: str, map_proto: dict, proto_dt: datetime):
        origin_logger = get_origin_logger(logger, origin=origin)
        origin_logger.debug3("DbPogoProtoSubmit::spawnpoints called with data received")
        cells = map_proto.get("cells", None)
        if cells is None:
            return False
        spawnpoint_args, spawnpoint_args_unseen = [], []
        spawn_ids = []

        query_spawnpoints = (
            "INSERT INTO trs_spawn (spawnpoint, latitude, longitude, earliest_unseen, "
            "last_scanned, spawndef, calc_endminsec, eventid) "
            "VALUES (%s, %s, %s, %s, %s, %s, %s, "
            "(select id from trs_event where now() between event_start and "
            "event_end order by event_start desc limit 1)) "
            "ON DUPLICATE KEY UPDATE "
            "last_scanned=VALUES(last_scanned), "
            "earliest_unseen=LEAST(earliest_unseen, VALUES(earliest_unseen)), "
            "spawndef=if(((select id from trs_event where now() between event_start and event_end order "
            "by event_start desc limit 1)=1 and eventid=1) or (select id from trs_event where now() between "
            "event_start and event_end order by event_start desc limit 1)<>1 and eventid<>1, VALUES(spawndef), "
            "spawndef), "
            "calc_endminsec=VALUES(calc_endminsec)"
        )

        query_spawnpoints_unseen = (
            "INSERT INTO trs_spawn (spawnpoint, latitude, longitude, earliest_unseen, last_non_scanned, spawndef, "
            "eventid) VALUES (%s, %s, %s, %s, %s, %s, "
            "(select id from trs_event where now() between event_start and "
            "event_end order by event_start desc limit 1)) "
            "ON DUPLICATE KEY UPDATE "
            "spawndef=if(((select id from trs_event where now() between event_start and event_end order "
            "by event_start desc limit 1)=1 and eventid=1) or (select id from trs_event where now() between "
            "event_start and event_end order by event_start desc limit 1)<>1 and eventid<>1, VALUES(spawndef), "
            "spawndef), "
            "last_non_scanned=VALUES(last_non_scanned)"
        )

        now = proto_dt.strftime("%Y-%m-%d %H:%M:%S")
        dt = proto_dt

        for cell in cells:
            for wild_mon in cell["wild_pokemon"]:
                spawn_ids.append(int(str(wild_mon['spawnpoint_id']), 16))

        spawndef = self._get_spawndef(spawn_ids)

        for cell in cells:
            for wild_mon in cell["wild_pokemon"]:
                spawnid = int(str(wild_mon["spawnpoint_id"]), 16)
                lat, lng, _ = S2Helper.get_position_from_cell(
                    int(str(wild_mon["spawnpoint_id"]) + "00000", 16))
                despawntime = wild_mon["time_till_hidden"]

                minpos = self._get_current_spawndef_pos()
                # TODO: retrieve the spawndefs by a single executemany and pass that...

                spawndef_ = spawndef.get(spawnid, False)
                if spawndef_:
                    newspawndef = self._set_spawn_see_minutesgroup(spawndef_, minpos)
                else:
                    newspawndef = self._set_spawn_see_minutesgroup(self.default_spawndef, minpos)

                last_scanned = None
                last_non_scanned = None

                if 0 <= int(despawntime) <= 90000:
                    fulldate = dt + timedelta(milliseconds=despawntime)
                    earliest_unseen = int(despawntime)
                    last_scanned = now
                    calcendtime = fulldate.strftime("%M:%S")

                    spawnpoint_args.append(
                        (spawnid, lat, lng, earliest_unseen, last_scanned, newspawndef, calcendtime)
                    )
                else:
                    earliest_unseen = 99999999
                    last_non_scanned = now

                    spawnpoint_args_unseen.append(
                        (spawnid, lat, lng, earliest_unseen, last_non_scanned, newspawndef)
                    )

        self._db_exec.executemany(query_spawnpoints, spawnpoint_args, commit=True)
        self._db_exec.executemany(query_spawnpoints_unseen,
                                  spawnpoint_args_unseen, commit=True)
Example #47
0
def create(task_name: str, exe_path: str, arguments: Union[str, List[str]]=None, remote_host: str=None,
           remote_user: str=None, remote_password: str=None, user: str=None, user_domain: str=None,
           password: str=None, start_time: datetime_mod.datetime=None, schedule_type: str=None) \
        -> Tuple[CommandLine, Callable[[str], None]]:
    """Create a scheduled task using "schtasks".

    Args:
        task_name: The name of the task (the "/TN" flag)
        exe_path: The path to the executable for the task. If the task is being scheduled on another computer this is
            the path relative to that computer. (combined with the arguments to form the "/TR" flag)
        arguments: Any arguments to be passed to the executable when it is run within the task (combined with the
            exe_path to form the "/TR" flag)
        remote_host: The host that the task will be scheduled on (the "/RU" flag)
        remote_user: The user account that the task will execute under. Often this is "System" (the "/RP" flag)
        remote_password: The password for the remote_user account. Leave blank if the remote user is "System"
            (the "/RU" flag)
        user: The user account to authenticate with when creating this scheduled tasks (if different from the current
            user context).
        user_domain: The domain for the ``user`` (combined with ``user`` to form the "/U" flag)
        password: The password for the ``user`` (the "/P" flag)
        start_time: The time that the task is scheduled to run at (the "/ST" and "/SD" flags)
        schedule_type: The schedule type (the "/SC" flag). One of (MINUTE, HOURLY, DAILY, WEEKLY,
                       MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE, ONEVENT). If ``None`` is passed, will use "ONCE".

    Returns:
        The CommandLine and a parser for the command

    """
    args = ['/Create']

    if remote_host is not None:
        args.append('/S ' + remote_host)
        if user is not None:
            if user_domain is not None:
                args.append('/U ' + user_domain + '\\' + user)
            else:
                args.append('/U ' + user)
            if password is not None:
                args.append('/P ' + password)

    if schedule_type:
        args.append('/SC ' + schedule_type)
    else:
        args.append('/SC ONCE')

    args.append('/TN ' + task_name)

    if arguments is not None and isinstance(arguments, list):
        arguments = ' '.join(
            ["'{}'".format(x) if ' ' in x else x for x in arguments])

    double_quote = False
    if ' ' in exe_path:
        exe_path = "'{}'".format(exe_path)
        double_quote = True

    if arguments:
        exe_path = "{} {}".format(exe_path, arguments)
        double_quote = True

    if double_quote:
        exe_path = '"{}"'.format(exe_path)

    args.append('/TR ' + exe_path)

    if start_time is not None:
        args.append('/ST ' + start_time.strftime('%H:%M'))
        args.append('/SD ' + start_time.strftime('%m/%d/%Y'))

    args.append("/F")

    if remote_user is not None:
        args.append('/RU ' + remote_user)
        if remote_password is not None:
            args.append('/RP ' + remote_password)

    return schtasks(args=args), parsers.schtasks.create
Example #48
0
 def _get_time(self, now: datetime.datetime = None):
     if not now:
         now = datetime.datetime.now().utcnow()
     return now.strftime("%Y%m%d%H%M")
Example #49
0
def _from_date(obj: datetime):
    return obj.isoformat()
Example #50
0
 def _epoch_to_string(epoch: datetime) -> str:
     return epoch.strftime('%Y-%m-%d_%H-%M-%S')
Example #51
0
def _datetime_exists(dattim: dt.datetime) -> bool:
    """Check if a datetime exists."""
    assert dattim.tzinfo is not None
    original_tzinfo = dattim.tzinfo
    # Check if we can round trip to UTC
    return dattim == dattim.astimezone(UTC).astimezone(original_tzinfo)
Example #52
0
def get_em_for_date(date: datetime.datetime):
    return date.replace(hour=17, minute=0, second=0, microsecond=0)
Example #53
0
def format_datetime(dt: datetime, with_tz=True, timespec="microseconds") -> str:
    dt = normalise_dt(dt)
    dt = dt.isoformat(timespec=timespec)
    if with_tz:
        dt = dt + "Z"
    return dt
Example #54
0
 def parsed_output_datetime(cls, value: datetime.datetime) -> str:
     display_datetime_format = '%Y-%m-%dT%H:%M:%S'
     server_timezone = '+00:00'
     return value.strftime(display_datetime_format) + server_timezone
Example #55
0
def datetime_to_epoch(date_time: datetime) -> int:
    """Convert a datetime object to an epoch integer (seconds)."""
    return int(date_time.timestamp())
Example #56
0
 async def get_next_stop_passage(
         self, stop_id: str, time: datetime.datetime) -> datetime.datetime:
     stop_schedule = await self.get_stop_schedule(stop_id)
     return _recur_find_next_passage(time, time.strftime("%H:%M:%S"),
                                     stop_schedule, 0,
                                     len(stop_schedule) - 1)
Example #57
0
def display_date_with_date_and_year(dt: datetime.datetime, tz: Any = WOTC_TZ) -> str:
    s = '{:%b _%d_}'.format(dt.astimezone(tz))
    return replace_day_with_ordinal(s)
Example #58
0
def datetime_to_epoch_milliseconds(dt: datetime) -> int:
    """Convert datetime object to epoch milliseconds."""
    if not dt.tzinfo:
        dt = dt.replace(tzinfo=timezone.utc)
    return int(dt.timestamp() * 1000)
    def search(self,
               channel_id: str = "",
               search_query: str = "",
               order: Order = Order.DATE,
               max_results: int = 50,
               published_after: datetime = None,
               published_before: datetime = None,
               relevance_language: str = "",
               result_type: List[ResultType] = (ResultType.VIDEO,),
               part='snippet',
               page_token: str = "",
               # video only
               event_type: EventType = None,
               duration: VideoDuration = None,
               caption: VideoCaption = None,
               embeddable: VideoEmbeddable = None,
               definition: VideoDefinition = None,
               related_to_video_id: str = "") -> Dict[str, Any]:
        """ make /search request to www.googleapis.com/youtube/v3.
            Args:
                channel_id (str): channel to search in.
                search_query (str): search term.
                order (Order): ordering of the search result.
                max_results: results per page.
                published_before (datetime): utc datetime.
                    (ie: datetime.datetime.utcnow())
                published_after (datetime): utc datetime.
                    (ie: datetime.datetime.utcnow())
                relevance_language (str): The relevanceLanguage parameter
                    instructs the API to return search results that are most
                    relevant to the specified language. The parameter value is
                    typically an ISO 639-1 two-letter language code.
                result_type (ResultType): what resource you searching for.
                part (str)
                page_token (str)
                event_type (EventType): broadcast or not?
                    (only for result_type: video)
                duration (VideoDuration): (only for result_type: video)
                caption (VideoCaption)
                embeddable (VideoEmbeddable)
                definition (VideoDefinition)
                related_to_video_id (str): The relatedToVideoId parameter
                    retrieves a list of videos that are related to the video
                    that the parameter value identifies.
            Returns:
                dict:
                    youtube response, see
                    https://developers.google.com/youtube/v3/docs/search/list#response
        """

        params = {
                'part': part,
                'order': order.value,
                'maxResults': max_results,
                'type': ','.join(map(lambda e: e.value, result_type)),
        }

        if search_query:
            params.update({'q': search_query})
        if channel_id:
            params.update({'channelId': channel_id})
        if published_before:
            published_before_tz = published_before.astimezone()
            before_rfc3339 = published_before_tz.isoformat()
            params.update({'publishedBefore': before_rfc3339})
        if published_after:
            published_after_tz = published_after.astimezone()
            after_rfc3339 = published_after_tz.isoformat()
            params.update({'publishedAfter': after_rfc3339})
        if relevance_language:
            params.update({'relevanceLanguage': relevance_language})
        if page_token:
            params.update({'pageToken': page_token})
        if event_type:
            params.update({'eventType': event_type.value})
        if duration:
            params.update({'videoDuration': duration.value})
        if caption:
            params.update({'videoCaption': caption.value})
        if embeddable:
            params.update({'videoEmbeddable': embeddable.value})
        if definition:
            params.update({'videoDefinition': definition.value})
        if related_to_video_id:
            params.update({'relatedToVideoId': related_to_video_id})

        return self._request("search", params)
Example #60
0
 def dump(self, value: datetime) -> str:
     return value.isoformat()