Example #1
0
def getTicketFrequency(location):
	from time import mktime
	from datetime import datetime
	try:
		P1 = Paths.objects.filter(path__dwithin=(location, 0.0001),	).aggregate(end_datetime=Max('end_datetime'), start_datetime=Min('start_datetime'), cnt=Count('id'))
		hours = (mktime(datetime.utctimetuple(P1['end_datetime'])) - mktime(datetime.utctimetuple(P1['start_datetime']))) / 3600
		tickets_per_hour = (P1['cnt'] / hours) * 100
		P1['hours'] = hours
		P1['tickets_per_hour'] = tickets_per_hour	
		return P1

	except TypeError:
		return "No"
Example #2
0
def datetime_to_utc_unixtime(dtobject):
    """
    Converts timezone-aware dtobject to unix timestamp.
    Adds tzinfo to unixtime.
    """
    naive = dtobject.replace(tzinfo=None)
    return calendar.timegm(datetime.utctimetuple(naive))
Example #3
0
    def utctimetuple(self):
        """Return UTC time tuple, compatible with time.gmtime().

        Notice: the original datetime documentation is misleading:
        Calling utctimetuple() on a timezone-aware datetime will 
        return the tuple in UTC, not in local time."""
        return _datetime.utctimetuple(self)
Example #4
0
def _new_discount(
    admin_id,
    amount,
    applicable_on,
    begins_on,
    container_id,
    description,
    discount_type,
    duration,
    ends_on,
    item_id,
    min_order,
    name,
):
    # create discount obj
    discount_obj = discounts.Discount()
    discount_obj.name = name
    discount_obj.description = description
    discount_obj.status = discounts.DiscountStatus.ENABLED
    discount_obj.admin_id = admin_id
    discount_obj.discount_scope = discounts.DiscountScope.ALL_ITEMS
    if applicable_on == "item":
        discount_obj.discount_scope = discounts.DiscountScope.ITEM_ONLY
        discount_obj.obj_id = coerce_bson_id(item_id)
        discount_obj.coll_name = items.Item.coll_name()
    elif applicable_on == "container":
        discount_obj.discount_scope = discounts.DiscountScope.CONTAINER_WIDE
        discount_obj.obj_id = coerce_bson_id(container_id)
        discount_obj.coll_name = items.Container.coll_name()
    if discount_type == "percentage":
        discount_obj.discount_percentage = float(amount)
    elif discount_type == "absolute":
        discount_obj.absolute_discounted_price = float(amount)
    discount_obj.order_minimum_spending = float(min_order)
    discount_obj.discount_lifetime_type = discounts.DiscountLifetime.FOREVER
    if duration == "duration":
        discount_obj.discount_lifetime_type = discounts.DiscountLifetime.LIMITED
        discount_obj.begins_utc_datetime = datetime.fromtimestamp(
            mktime(datetime.utctimetuple(datetime.strptime(begins_on, "%d/%m/%Y")))
        )
        discount_obj.expire_utc_datetime = datetime.fromtimestamp(
            mktime(datetime.utctimetuple(datetime.strptime(ends_on, "%d/%m/%Y")))
        )
    discount_obj._id = discount_obj.save()
    return discount_obj
Example #5
0
 def create(cls, badge, node, evidence=None, save=True, awarder=None):
     b = cls()
     b.badge = badge
     b.node = node
     b.evidence = evidence
     b.issued_on = calendar.timegm(datetime.utctimetuple(datetime.utcnow()))
     b._awarder = awarder
     if save:
         b.save()
     return b
Example #6
0
def render_card_creation_date(self, h, comp, model, *args):
    span_id = h.generate_id()
    h << h.span(id=span_id, class_="date")

    utcseconds = calendar.timegm(datetime.utctimetuple(self.data.creation_date))

    h << h.script(
        "YAHOO.kansha.app.utcToLocal('%s', %s, '%s', '%s');" % (span_id, utcseconds, _(u'at'), _(u'on'))
    )

    return h.root
Example #7
0
def new_coupon(admin_id, amount, applicable_on, begins_on, container_id, coupon_type, description, ends_on, item_id,
               lifetime_type, min_spending, name, user_applicable, user_groups, user_id, valid_times, coupon_code):
    c = Coupon()
    c.coupon_scope = coupons.CouponScope.ALL_ITEMS
    if applicable_on == "item":
        c.coupon_scope = coupons.CouponScope.ITEM_ONLY
        c.obj_id = coerce_bson_id(item_id)
        c.coll_name = items.Item.coll_name()
    elif applicable_on == "container":
        c.coupon_scope = coupons.CouponScope.CONTAINER_WIDE
        c.obj_id = coerce_bson_id(container_id)
        c.coll_name = items.Container.coll_name()
    if coupon_type == "percentage":
        c.coupon_percentage_off = amount
    elif coupon_type == "absolute":
        c.coupon_value = amount
    c.valid_times = valid_times
    c.order_minimum_spending = min_spending
    c.coupon_lifetime_type = coupons.CouponLifetime.FOREVER
    if lifetime_type == "duration":
        c.coupon_lifetime_type = coupons.CouponLifetime.LIMITED
        c.begins_utc_datetime = datetime.fromtimestamp(
            mktime(datetime.utctimetuple(datetime.strptime(begins_on, "%d/%m/%Y"))))
        c.expire_utc_datetime = c.begins_utc_datetime = datetime.fromtimestamp(
            mktime(datetime.utctimetuple(datetime.strptime(ends_on, "%d/%m/%Y"))))
    c.user_scope = coupons.CouponUserScope.ALL
    if user_applicable == "group":
        c.user_scope = coupons.CouponUserScope.GROUP
        c.user_group = user_groups
    elif user_applicable == "user_specific":
        c.user_id = coerce_bson_id(user_id)
        c.user_scope = coupons.CouponUserScope.SPECIFIC
    c.name = name
    c.description = description
    c.status = coupons.CouponStatus.AVAILABLE
    c.admin_id = admin_id
    c.coupon_code = coupon_code
    c.save()
Example #8
0
    def strDateFormat(self,date = datetime.utcnow()):
        strWeekDay = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
        strMonth   = ["NOT", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

        time    = datetime.utctimetuple(date)
        
        weekday = strWeekDay[time.tm_wday]
        day     = self.__strTwoDigitNumber(time.tm_mday)
        month   = strMonth[time.tm_mon]
        year    = time.tm_year
        hour    = self.__strTwoDigitNumber(time.tm_hour)
        minute  = self.__strTwoDigitNumber(time.tm_min)
        second  = self.__strTwoDigitNumber(time.tm_sec)
        
        return "Date: {0}, {1} {2} {3} {4}:{5}:{6} GMT".format(weekday,day,month,year,
                                                               hour,minute,second)
Example #9
0
	if custom_day>0:
		day = custom_day

	hour = date[1][0:2]
	minute = date[1][2:4]
	second = date[1][4:6]
	milisec = date[1][6:9]
	
	bid = cols[1]
	ask = cols[2]

	timestring = year + month + day + hour + minute + second + milisec + "00"	

	timezone_blind = datetime.strptime(timestring,"%Y%m%d%H%M%S%f")
	timezone_aware = est.localize(timezone_blind)
	utc_ts         = datetime.utctimetuple(timezone_aware)
	#utc_t          = mktime(utc_ts) + 1.0 * int(milisec) / 1000
	utc_t          = calendar.timegm(utc_ts) + 1.0 * int(milisec) / 1000
	utc_s1         = strftime('%Y-%m-%d %H:%M:%S.', utc_ts)
	utc_s1         = utc_s1 + milisec + "+0000"
	utc_k          = forex_pair + ":" + strftime('%Y-%m-%d', utc_ts)

	#print utc_ts
	#print utc_k

	q = "INSERT INTO ticks (pair_day,issued_at,bid,ask) VALUES ("
	q = q + "'" + utc_k + "',"
	q = q + "'" + utc_s1 + "',"
	q = q + "" + bid + ","
	q = q + "" + ask + ") USING TTL 10800;"
Example #10
0
def timestamp(datetime, local=False):
    if not local:
        return long(time.mktime(datetime.utctimetuple()))
    else:
        return long(time.mktime(datetime.timetuple()))
Example #11
0
def toTimestamp(datetime):
    return timegm(datetime.utctimetuple())
Example #12
0
def datetime_to_unixtime(dtobject):
    """
    Convert datetime object to unix timestamp. Drops tzinfo.
    """
    return calendar.timegm(datetime.utctimetuple(dtobject))
Example #13
0
def timestamp_from_datetime(datetime):
    return str(int(time.mktime(datetime.utctimetuple())))
Example #14
0
def parse_erinyes_output_per_site(site_address):
    root = "Output\\"
    file_path = root + "erinyes.txt"

    # parse availability data for site from output
    data = read_file_into_list(file_path)['list']
    avail_data = []
    for line in data:
        if site_address in line:
            str_data = line.split(' ')
            date_time = str_data[0] + ' ' + str_data[1]
            try:
                timestamp = int(
                    mktime(datetime.utctimetuple(datetime.strptime(
                        date_time, "%Y-%m-%d %H:%M:%S.%f")))
                )
            except ValueError:
                timestamp = int(
                    mktime(datetime.utctimetuple(datetime.strptime(
                        date_time, "%Y-%m-%d %H:%M:%S")))
                )
            if 'no longer available' in line:
                available = False
            else:
                available = True
            avail_data.append({
                'date/time': date_time, 'timestamp': timestamp, 'available': available
            })

    # determine availability sessions
    sessions = []
    start_date = None
    start_time = None
    last_date = None
    last_time = None
    for entry in avail_data:
        if entry['available']:
            start_date = entry['date/time']
            start_time = entry['timestamp']

        if not entry['available'] and start_time is not None:
            duration = entry['timestamp'] - start_time
            if last_time is not None:
                time_to_reconnect = start_time - last_time
            else:
                time_to_reconnect = 'Unknown'
            sessions.append(
                {'date/time': start_date, 'duration': duration,
                 'last date/time': last_date, 'last time': last_time,
                 'time to reconnect': time_to_reconnect})
            start_date = None
            start_time = None
            last_date = entry['date/time']
            last_time = entry['timestamp']

    # summarize
    total_sessions = len(sessions)
    total_duration = 0
    longest_time = 0
    total_lapse = 0
    longest_lapse = 0
    for session in sessions:
        total_duration += session['duration']
        if session['duration'] > longest_time: longest_time = session['duration']
        if session['time to reconnect'] is not 'Unknown':
            total_lapse += session['time to reconnect']
            if session['time to reconnect'] > longest_lapse:
                longest_lapse = session['time to reconnect']
    avg_session = total_duration/total_sessions
    avg_lapse = total_lapse/(total_sessions-1)

    display_data = sessions
    print
    for entry in display_data:
        print entry
    print "Total Sessions:\t%d" % total_sessions
    print "Average Time Connected:\t%d min %d s" % (avg_session/60, avg_session%60)
    print "Longest Time Connected:\t%d min %d s" % (longest_time/60, longest_time%60)
    print "Average Time Between Connections:\t%d min %d s" % (avg_lapse/60, avg_lapse%60)
    print "Longest Time Between Connections:\t%d min %d s" % (longest_lapse/60, longest_lapse%60)

    # export
    output = open("Output\\output.txt", 'w')
    header = 'Date/Time\tTime Connected\t Time Since Previous Connect\n'
    lines = [header]
    for session in sessions:
        line = '\n%(date/time)s\t%(duration)s\t%(time to reconnect)s' % session
        lines.append(line)
    output.writelines(lines)
    output.close()
Example #15
0
 def _to_naive_utc_datetime(datetime):
     # naive datetime is regarded as UTC datetime
     timestamp = calendar.timegm(datetime.utctimetuple())
     return datetime.utcfromtimestamp(timestamp)
Example #16
0
def get_data_from_table(existing_games, page):

    tables = page.find_all('table', {'class': 'searchResultTable'})
    if not tables: return existing_games

    for table in tables:

        datestr = table.find('tbody').find('td').text.strip()

        for row in table.find_all('tr')[1:]:

            cols = row.find_all('td')

            try:
                if 'font-weight:bold' in cols[0].get('style'):
                    compname = cols[0].text.replace(u'\xa0', ' ').replace(
                        '  ', ' ').strip(' -')
                    continue
            except:
                pass

            try:
                gmtime = cols[1].text
            except:
                continue

            gametimestr = datestr + ' ' + gmtime
            utctime_struct = datetime.utctimetuple(
                dateparser.parse(gametimestr))
            time_stamp = time.mktime(utctime_struct)

            hometeam, awayteam, fix_names = get_names(cols[0].text)
            homescore, awayscore = [int(x) for x in cols[2].text.split(':')]
            gameid = str(time_stamp) + hometeam + awayteam

            gamedic = {
                'DATE': [
                    datetime.fromtimestamp(time_stamp).strftime(
                        '%Y-%m-%d %H:%M')
                ],
                'TIMESTAMP': [time_stamp],
                'COMPETITION': [compname],
                'HOME': [hometeam],
                'AWAY': [awayteam],
                'VENUE': [np.nan],
                'HOME SCORE': [homescore],
                'AWAY SCORE': [awayscore],
                'TOTAL': [homescore + awayscore],
                'SEASON': [utctime_struct[0]],
                'GAME ID': [gameid],
                'FIX NAMES': [fix_names],
                'RAW NAMES STRING': [cols[0].text],
                'STAGE': ['Group Stage'],
            }

            #This turns the new game data into a pandas dataframe
            new_game = pd.DataFrame(gamedic)
            #This adds the new dataframe to the complete list of games
            existing_games = pd.concat([existing_games, new_game],
                                       ignore_index=True,
                                       sort=False)
    return existing_games
Example #17
0
    if int(custom_day) > 0:
        day = custom_day

    hour = date[1][0:2]
    minute = date[1][2:4]
    second = date[1][4:6]
    milisec = date[1][6:9]

    bid = cols[1]
    ask = cols[2]

    timestring = year + month + day + hour + minute + second + milisec + "00"

    timezone_blind = datetime.strptime(timestring, "%Y%m%d%H%M%S%f")
    timezone_aware = est.localize(timezone_blind)
    utc_ts = datetime.utctimetuple(timezone_aware)
    utc_t = calendar.timegm(utc_ts) + 1.0 * int(milisec) / 1000
    utc_s1 = strftime('%Y-%m-%d %H:%M:%S.', utc_ts)
    utc_s1 = utc_s1 + milisec + "+0000"
    utc_k = forex_pair + ":" + strftime('%Y-%m-%d', utc_ts)

    #print utc_ts
    #print utc_k

    q = "INSERT INTO ticks (pair_day,issued_at,bid,ask) VALUES ("
    q = q + "'" + utc_k + "',"
    q = q + "'" + utc_s1 + "',"
    q = q + "" + bid + ","
    q = q + "" + ask + ") USING TTL 10800;"

    m = "pushing " + forex_pair + " "
Example #18
0
def timestamp_format(datetime):
    return time.mktime(datetime.utctimetuple()) + datetime.microsecond / 1e6
Example #19
0
def get_timestamp(datetime):
    return calendar.timegm(datetime.utctimetuple())
Example #20
0
def timestamp_from_datetime(dt):
    """
    Given a datetime in utc, create the corresponding timestamp
    """
    return calendar.timegm(dt.utctimetuple()) * __SUBSECOND_RESOLUTION__ + dt.microsecond
Example #21
0
def totimestamp(datetime):
    from calendar import timegm

    return int(timegm(datetime.utctimetuple()))
Example #22
0
def to_timestamp(datetime):
    """
    Convert *datetime* to a UTC unix timestamp.
    """
    return timegm(datetime.utctimetuple())
Example #23
0
def datetime2utctime(datetime):
    return long(
        calendar.timegm(datetime.utctimetuple()) * 1000.0 +
        datetime.microsecond / 1000.0)
Example #24
0
 def delayed_push(self, datetime, item):
     key = int(calendar.timegm(datetime.utctimetuple()))
     self.redis.rpush('resque:delayed:%s' % key, ResQ.encode(item))
     self.redis.zadd('resque:delayed_queue_schedule', key, key)