示例#1
0
文件: atlas.py 项目: seoful/catsBot
 def __normalize_date(datetime):
     datetime = datetime.replace(year=2020)
     datetime = datetime.replace(month=1)
     datetime = datetime.replace(day=1)
     datetime = datetime.replace(second=0)
     datetime = datetime.replace(microsecond=0)
     return datetime
def g6barchart():
    data = {
        "returns": {
            "2020-10-19": 0,
            "2020-10-23": 0,
            "2020-10-26": 0,
            "2020-11-07": 0
        },
        "distr": {
            "2020-10-19": 0,
            "2020-10-23": 0,
            "2020-10-26": 0,
            "2020-11-07": 0
        }
    }
    distr = stall_distribution.find({"rasp_id": 1})
    returns = positivetrayreturn.find({"stall_id": 1})
    for x in distr:
        datetime = str(x["datetime"])
        d = datetime.replace(",", "-")
        date = d[:10]
        if date in data["distr"]:
            count = data["distr"][date]
            count += 1
            data["distr"][date] = count
    for x in returns:
        datetime = str(x["datetime"])
        d = datetime.replace(",", "-")
        date = d[:10]
        if date in data["returns"]:
            count = data["returns"][date]
            count += 1
            data["returns"][date] = count
    return json.dumps(data), 200
示例#3
0
def stat_page_view_users():
    # stat_page_view_users_weekly.remove({})

    startDate = date.today() - timedelta(days=6)
    endDate = date.today()+timedelta(days=1)
    start = datetime.now()
    end = datetime.now()
    start = datetime.replace(start, startDate.year, startDate.month, startDate.day, 0, 0, 0, 0)
    end = datetime.replace(start, endDate.year, endDate.month, endDate.day, 0, 0, 0, 0)
    # start = datetime.replace(start, 2018, 1, 8, 0, 0, 0, 0)
    # end = datetime.replace(start, 2018, 1, 14, 0, 0, 0, 0)
    start = start - timedelta(hours=8)
    end = end - timedelta(hours=8)

    pipeline = [
         { "$match":  {"time": {"$gte": start, "$lt": end}} },
 	     { "$group": {"_id":"$userId", "count":{"$sum":1}, "time": {"$max": "$time"}}}
    ]
    # print pipeline
    result = page_view.aggregate(pipeline)
    count_users = 0
    for item in result:
        # print item
        count_users += 1
        rule = {'userId': item['_id'], "time": {"$gte": start, "$lt": end}}
        insert_item = {'userId': item['_id'],
                       "count": item['count'],
                       "time": start,
                       "updateTime": datetime.now() - timedelta(hours=8)}

        stat_result = stat_page_view_users_weekly.find_one(rule)
        if stat_result is None:
            stat_page_view_users_weekly.insert(insert_item)
        else:
            stat_page_view_users_weekly.update_one({'_id': stat_result['_id']},  {'$set': insert_item})

    daily_result = stat_users_weekly.find_one({"time": {"$gte": start, "$lt": end}})
    insert_item = {"count": count_users,
                   "time": start,
                   "updateTime": datetime.now() - timedelta(hours=8)}
    if daily_result is None:
        stat_users_weekly.insert(insert_item)
    else:
        stat_users_weekly.update_one({'_id': daily_result['_id']},  {'$set': insert_item})

    conn = db.connect_torndb()
    result = stat_page_view_users_weekly.find().sort("count", pymongo.DESCENDING)
    id = 0
    for item in result:
        id += 1
        info = conn.get("select * from user u "
                        "left join user_organization_rel uor on u.id = uor.userId "
                        "left join organization o on o.id = uor.organizationId"
                        " where u.id =%s limit 1", item['userId'])
        if info is not None:
            print id, info['username'], info['name'], info['email'], item['count']
        info = None

    conn.close()
示例#4
0
    def dtround(self, datetime):
        '''Rounds seconds of datetime object up if microseconds > 500000
		else returns datetime object'''
        logger.info("Rounding %s" % str(datetime))
        if datetime.microsecond > 500000 and datetime.second == 59:
            datetime = datetime.replace(minute=datetime.minute + 1, second=0)
        elif datetime.microsecond > 500000:
            datetime = datetime.replace(second=datetime.second + 1)
        return datetime
示例#5
0
def stat_page_view_urls_daily_new():
    startDate = date.today()
    endDate = date.today() + timedelta(days=1)
    start = datetime.now()
    end = datetime.now()
    start = datetime.replace(start, startDate.year, startDate.month,
                             startDate.day, 0, 0, 0, 0)
    end = datetime.replace(start, endDate.year, endDate.month, endDate.day, 0,
                           0, 0, 0)
    start = start - timedelta(hours=8)
    end = end - timedelta(hours=8)

    rule_all = {"time": {"$gte": start, "$lt": end}}
    result = list(page_view.find(rule_all))

    url_set = set()
    for item in result:
        url_set.add(item['visitURL'])

    for url in url_set:
        count = 0
        count_investorY = 0
        count_investorN = 0
        count_ordinary = 0
        for item in result:
            if url == item['visitURL']:
                userId = item['userId']
                user = get_user(userId)
                if user is None:
                    continue
                count += 1
                userIdentify = user['userIdentify']
                verifiedInvestor = user['verifiedInvestor']
                if userIdentify == 61010:
                    if verifiedInvestor == 'Y':
                        count_investorY += 1
                    else:
                        count_investorN += 1
                else:
                    count_ordinary += 1
        rule = {'url': url, "time": {"$gte": start, "$lt": end}}
        stat_result = stat_page_view_urls.find_one(rule)
        insert_item = {
            'url': url,
            "time": start,
            "count": count,
            "count_investorY": count_investorY,
            "count_investorN": count_investorN,
            "count_ordinary": count_ordinary,
            "updateTime": datetime.now() - timedelta(hours=8)
        }
        if stat_result is None:
            stat_page_view_urls.insert(insert_item)
        else:
            stat_page_view_urls.update_one({'_id': stat_result['_id']},
                                           {'$set': insert_item})
示例#6
0
    def __init__(self, date):
        if isinstance(date, Date):
            datetime = date.datetime
        else:
            if isinstance(date, str):
                datetime = parser.parse(date, tzinfos={"CET": gettz("CET"), "CEST": gettz("CEST")})
            else:
                datetime = date

            if str(datetime.tzinfo) != self.timezone:
                datetime = self.get_timezone_obj().localize(datetime.replace(tzinfo=None))

            datetime = datetime.replace(second=0, microsecond=0)

        self.datetime = datetime
示例#7
0
def stat_page_view_urls_daily():
    startDate = date.today()
    endDate = date.today() + timedelta(days=1)
    start = datetime.now()
    end = datetime.now()
    start = datetime.replace(start, startDate.year, startDate.month,
                             startDate.day, 0, 0, 0, 0)
    end = datetime.replace(start, endDate.year, endDate.month, endDate.day, 0,
                           0, 0, 0)
    start = start - timedelta(hours=8)
    end = end - timedelta(hours=8)

    pipeline = [{
        "$match": {
            "time": {
                "$gte": start,
                "$lt": end
            }
        }
    }, {
        "$group": {
            "_id": "$visitURL",
            "count": {
                "$sum": 1
            },
            "time": {
                "$max": "$time"
            }
        }
    }]
    result = page_view.aggregate(pipeline)
    count_urls = 0
    for item in result:
        count_urls += 1
        rule = {'url': item['_id'], "time": {"$gte": start, "$lt": end}}
        stat_result = stat_page_view_urls.find_one(rule)
        insert_item = {
            'url': item['_id'],
            "count": item['count'],
            "time": start,
            "updateTime": datetime.now() - timedelta(hours=8)
        }

        if stat_result is None:
            stat_page_view_urls.insert(insert_item)
        else:
            stat_page_view_urls.update_one({'_id': stat_result['_id']},
                                           {'$set': insert_item})
示例#8
0
def editAppointment(request, id):
    print("")
    if request.method == 'POST':
        user = request.user.person
        form = AppointmentForm(request.POST, prefix="delete")
        form.is_valid()
        cleanData = form.cleaned_data

        if not 'description' in cleanData:
            return redirect('/dashboard/')

        atime = cleanData['time']
        datetime = cleanData['date'] + "T" + atime
        print(atime)
        print(datetime)
        if int(atime[:2]) < 8 or int(atime[:2]) >= 17:
            print("time redirect")
            return redirect(request.META['HTTP_REFERER'].replace(
                "http://localhost:8000", ""))

        aptcompare = None
        if user.usertype == "doctor":
            aptcompare = Appointment.objects.filter(doctor=user.last_name,
                                                    date=datetime.replace(
                                                        "T",
                                                        " ")).exclude(pk=id)
        else:
            aptcompare = Appointment.objects.filter(
                doctor=cleanData['doctor'].replace(
                    "Dr. ", ""),  #Change doctor to Dr. for better viewing
                date=datetime.replace("T", " ")).exclude(pk=id)

        if len(aptcompare) >= 1:
            return redirect(request.META['HTTP_REFERER'].replace(
                "http://localhost:8000", ""))

        apt = Appointment.objects.get(pk=id)
        apt.doctor = cleanData['doctor'].replace("Dr. ", "")
        apt.date = datetime
        apt.description = cleanData['description']
        apt.save()
        activity = Activity(data=user.username + " edited an appointment",
                            date=time.strftime("%Y-%m-%d") + "T" +
                            time.strftime("%H:%M:%S"))
        activity.save()

    return redirect(request.META['HTTP_REFERER'].replace(
        "http://localhost:8000", ""))
示例#9
0
def count_days_to_virality(df):
    final_dict = {}
    df['publish_time'].apply(lambda x: datetime.replace(x, tzinfo=None))
    for index, row in df.iterrows():
        time_to_virality = (row["trending_date_parsed"].value -
                            row["publish_time"].value) / ((10**9) * 86400)
        if time_to_virality < 1:
            final_dict["< 1"] = final_dict.get("< 1", 0) + 1
        elif time_to_virality < 2:
            final_dict["1 - 2"] = final_dict.get("1 - 2", 0) + 1
        elif time_to_virality < 3:
            final_dict["2 - 3"] = final_dict.get("2 - 3", 0) + 1
        elif time_to_virality < 4:
            final_dict["3 - 4"] = final_dict.get("3 - 4", 0) + 1
        elif time_to_virality < 5:
            final_dict["3 - 4"] = final_dict.get("3 - 4", 0) + 1
        elif time_to_virality < 7:
            final_dict["5 - 7"] = final_dict.get("5 - 7", 0) + 1
        elif time_to_virality < 10:
            final_dict["7 - 10"] = final_dict.get("7 - 10", 0) + 1
        elif time_to_virality < 20:
            final_dict["10 - 20"] = final_dict.get("10 - 20", 0) + 1
        else:
            final_dict[">= 20"] = final_dict.get(">= 20", 0) + 1
    sorted_final_dict = sorted(final_dict.items(),
                               key=lambda x: x[1],
                               reverse=True)

    return sorted_final_dict
示例#10
0
def gen_all_posts(pageid: str, date_from: str, date_to: str,
                  post_filter: object = lambda x: True,
                  keys_to_delete: list = []) \
        -> list:
    """
    Generator of all post of a page since a date (most recent first).
    :param pageid:
    :param date_from: date as string
    :param date_to:
    :param post_filter:
    :param keys_to_delete:
    :return: posts (as generator)
    """
    # Posts are returned by the GraphAPI in paging mode
    if _graph is None:
        init()
    page_posts = _graph.get_object(id=pageid + '/posts')
    stop_date = dateutil.parser.parse(date_from)
    begin_date = dateutil.parser.parse(date_to)
    while True:
        for post in page_posts['data']:
            post_date = dateutil.parser.parse(post['created_time'])
            # Remove tzinfo for the comparison with stop_date
            post_date = datetime.replace(post_date, tzinfo=None)
            if post_date >= stop_date:
                if post_date < begin_date and post_filter(post):
                    yield distill_post(post, keys_to_delete)
            else:
                # No more posts to generate
                return
        if 'next' in page_posts['paging']:
            page_posts = requests.get(page_posts['paging']['next']).json()
        else:
            break
示例#11
0
    def convert_dates_to_rfc3339(cls, record: JsonResult, schema: Schema) -> JsonResult:
        """Appends UTC timezone information to all date-time fields.

        This ensures they are RFC 3339 compliant as per the JSON Schema spec.
        Returns a clone of record with updated date-time, does not modify original record.
        """
        result = record.copy()
        if schema.properties is None:
            return result

        for field_name, field in schema.properties.items():
            if field.type == "string" and field.format == "date-time":
                field_value = result.get(field_name)
                if field_value:
                    try:
                        dateval = isoparse(field_value)
                        if dateval.tzinfo is None:
                            dateval_utc = datetime.replace(dateval, tzinfo=timezone.utc)
                            result[field_name] = dateval_utc.isoformat()
                    except ValueError as error:
                        LOGGER.debug("Unable to convert datetime '%s' to RFC 3339 format: %s", field_name, error)

            elif field.properties is not None:
                field_value = result.get(field_name)
                if field_value:
                    result[field_name] = cls.convert_dates_to_rfc3339(field_value, field)

        return result
示例#12
0
def datetime_to_timestamp(datetime, utc=True):
    '''
    Convert datetime object to timestamp,
    eg. datetime(2016, 2, 16, 17, 38, 53) to 1455651533.
    '''
    if utc: return int(datetime.replace(tzinfo=timezone.utc).timestamp())
    else: return int(datetime.timestamp())
示例#13
0
def mainapp(request):

    # check member login
    user = None
    if request.session.get('user_login'):
        user = User.objects.get(id=request.session.get('user_login'))

    context = {
        'Title': "Teaching Machine",
        'SubTitle':
        "Define your class, add dataset, make machine learn your data",
        'RoomCode': uuid.uuid4().hex[:6].upper(),
        "User": user
    }

    tomorrow = datetime.now() + timedelta(days=1)
    tomorrow = datetime.replace(tomorrow, hour=0, minute=0, second=0)
    expires = datetime.strftime(tomorrow, "%a, %d-%b-%Y %H:%M:%S GMT")

    response = render(request, 'index.html', context)
    response.set_cookie("RoomCode",
                        "teachapp_" + context['RoomCode'],
                        expires=expires)

    return response
示例#14
0
 def addRecord(self):
     DB = DataBase.DataBase()
     self.MR_ID = DB.AddRecord(
         Patient_ID=self.Patient_ID,
         Doctor_ID=self.Doctor_ID,
         MR_Time=self.MR_Time,
         Description=self.Description,
         Advice=self.Advice,
         FU_Time=self.FU_Time
     )
     delta = timedelta(days=1)
     starttime = datetime.now()+delta
     starttime = datetime.replace(starttime,hour=8,minute=0,second=0)
     for i in range(len(self.Prescriptions)):
         DB.AddPrescription(
             MR_ID=self.MR_ID,
             Medicine=self.Prescriptions[i].Medicine,
             Frequency_D=self.Prescriptions[i].Frequency_D,
             Frequency_T=self.Prescriptions[i].Frequency_T,
             Dose=self.Prescriptions[i].Dose,
             Notes=self.Prescriptions[i].Notes
         )
         #name
         Notes:str = self.Prescriptions[i].Medicine
         Notes += '&'
         #info1:dose
         Notes += self.Prescriptions[i].Dose
         Notes += '&'
         #info2:frequency
         Notes += str(self.Prescriptions[i].Frequency_D)+'天'+str(self.Prescriptions[i].Frequency_T)+'次'
         Notes += '&'
         #info3:notes
         Notes += self.Prescriptions[i].Notes
         endtime = datetime.strptime(self.Prescriptions[i].Endtime,"%Y-%m-%d")
         for day in range((endtime.date()-starttime.date()).days+1):
             if day % self.Prescriptions[i].Frequency_D == 0:
                 Event_Time = (starttime+timedelta(days=day)).strftime("%Y-%m-%d %H:%M:%S")
                 DB.AddEvent(
                     U_ID=self.Patient_ID,
                     Event_Type='M',
                     Event_Time=Event_Time,
                     Note=Notes
                 )
     Notes = '&'
     #info1:doctorname
     Notes += DB.GetDoctorInformation(self.Doctor_ID)['U_Name'].encode('latin1').decode('gbk')
     Notes += '&'
     #info2:patientname
     Notes += DB.GetPatientInformation(self.Patient_ID)['U_Name'].encode('latin1').decode('gbk')
     Notes += '&'
     #info3:appointnote
     Notes += self.Description
     if self.FU_Time != '':
         DB.AddEvent(
             U_ID=self.Patient_ID,
             Event_Type='F',
             Event_Time=self.FU_Time,
             Note=Notes
         )
     DB.close()
    def __init__(self, listOfNormalBars=list, listOfDatedLabels=list):
        """creates a series of LabeledBars by tagging bars with their closest labels. datetimes are automatically rounded down to the nearest day."""
        super().__init__()
        self.UnderlyingBars = BarDataSeries()
        self.__combined = list()
        self.__log = Logger(str(self.__class__))
        self.__log.info("constructing.")

        for currentBar in listOfNormalBars:
            self.UnderlyingBars.append(currentBar)
            labeledBarWip = LabeledBar(currentBar, [])
            barsDate = labeledBarWip.Bar.getDateTime()

            for datedLabel in listOfDatedLabels:
                #datetimes are immutable
                datedLabel.Date = datetime.replace(
                    datedLabel.Date, hour=0, minute=0, second=0, microsecond=0
                )  #im using a class method version in order to get at design time intellisense of a boxed datetime

                if datedLabel.Date <= barsDate:  #assuming bar dates are ordered
                    self.__removePreviousOccurrencesOfLabel(datedLabel.Label)
                    labeledBarWip.Labels.append(datedLabel.Label)

            self.__combined.append(labeledBarWip)

        self.__log.info(str(self.__class__) + "constructed. ")
示例#16
0
def view(request, page=1, id=0):
    if 'authuser' not in request.session:
        return HttpResponseRedirect('/board')
    else:
        # 키워드 넘겨주기 (글 목록 갈 때 쓰려고)
        kwd = request.GET.get('kwd', '')
        # id로 글 가져오기
        board = Board.objects.get(id=id)

        # render로 넘길 context 사전형 객체
        data = {'board': board, 'page': page, 'kwd': kwd}

        # set_cookie
        response = render(request, 'board/view.html', data)

        if request.COOKIES.get('alreadyseenpost') is not None:
            viewer = request.COOKIES.get('alreadyseenpost')
            viewerlist = viewer.split('+')

            # id 없을 경우 조회수 업데이트 & id 쿠키에 넣어주기
            if str(id) not in viewerlist:
                Board.objects.filter(id=id).update(hit=F('hit') + 1)
                viewer = viewer + str(id) + '+'
        else:
            viewer = str(id) + '+'

        # cookie 만료시간 (오늘 자정까지)
        tomorrow = datetime.today() + timedelta(days=1)
        tomorrow = datetime.replace(tomorrow, hour=0, minute=0, second=0)
        response.set_cookie('alreadyseenpost', viewer, expires=tomorrow)

        return response
示例#17
0
文件: util.py 项目: tinyms/TanYue
    def list_modules(path_):
        """
        得到path_的模块名,模块名包括完整的包名
        :param path_: 扫描的路径
        :return: 模块名列表
        """
        packages = list()
        modules = list()
        iter_files = os.walk(path_)
        for root, dirs, files in iter_files:
            for f in files:
                if f.find('__init__.py') != -1:
                    packages.append(root)

        iter_files = os.walk(path_)
        for root, dirs, files in iter_files:
            for f in files:
                if packages.count(root) == 1 and f.endswith(".py") and f.find('__init__.py') == -1:
                    modules.append(os.path.join(root, f).replace(".py", ""))

        last_modules = list()
        for module in modules:
            tmp = module.replace(path_, "")
            tmp = tmp.replace(os.path.sep, ".")
            last_modules.append(tmp[1:])

        return last_modules
示例#18
0
def to_msk_datetime(datetime):
    if is_naive(datetime):
        return datetime.replace(tzinfo=msk_tz)
    elif datetime.tzinfo == msk_tz:
        return datetime
    else:
        return tz.localtime(datetime, msk_tz)
示例#19
0
def humanize_time(time):
    """
    Get a datetime object and return a relative time string like
    "one hour ago", "yesterday", "3 months ago", "just now", etc.
    """
    now = datetime.now(timezone.utc)
    naive = datetime.replace(now, tzinfo=None)
    rd = relativedelta.relativedelta(naive, time)

    def line(number, unit):
        if abs(number) < 10 and unit == "seconds":
            return "just now"
        if number == 1 and unit == "days":
            return 'yesterday'
        if number == -1 and unit == "days":
            return "tomorrow"

        prefix, suffix = '', ''
        unit = unit if abs(number) > 1 else unit[:-1]  # Unpluralizing.

        if number > 0:
            suffix = " ago"
        else:
            prefix = "in "

        return "%s%d %s%s" % (prefix, abs(number), unit, suffix)

    for attr in ['years', 'months', 'days', 'hours', 'minutes', 'seconds']:
        value = getattr(rd, attr)
        if value != 0:
            return line(value, attr)

    return "just now"
示例#20
0
 def changeDateTime(self, datetime, time):
     hours = int(time[0:2])
     minutes = int(time[2:])
     new_datetime = datetime.replace(hour=hours,
                                     minute=minutes,
                                     second=0,
                                     microsecond=0)
示例#21
0
def reformat_time(scan_time):
    """
    Transform scan_time to readable time.

    Parameters
    ----------
    scan_time : str
        Time string.

    Returns
    -------
    datetime
        Datetime object with the string translated.
    """
    hour_format = '%H'
    colon = ''
    locale = ''
    if ':' in scan_time:
        colon = ':%M'
    if re.search('[a-zA-Z]', scan_time):
        locale = '%p'
        hour_format = '%I'
    cd = datetime.now()
    return datetime.replace(datetime.strptime(scan_time, hour_format + colon + locale),
                            year=cd.year, month=cd.month, day=cd.day)
示例#22
0
def detail(request, pk):
    codepost = get_object_or_404(CodePost, pk=pk)
    codecommentform = CodeCommentForm()
    comments = codepost.codecomment_set.all()
    if request.session.get('_auth_user_id'):
        cookie_name = f'hit:{request.session["_auth_user_id"]}'
    else:
        cookie_name = 'hit'
    tomorrow = datetime.replace(datetime.now(), hour=23, minute=59, second=0)
    expires = datetime.strftime(tomorrow, "%a, %d-%b-%Y %H:%M:%S GMT")
    context = {
        'codepost': codepost,
        'codecommentform': codecommentform,
        'comments': comments,
    }
    response = render(request, 'codeboard/detail.html', context)
    if request.COOKIES.get(cookie_name) is not None:
        cookies = request.COOKIES.get(cookie_name)
        cookies_list = cookies.split('|')
        if str(codepost.id) not in cookies_list:
            response.set_cookie(cookie_name,
                                cookies + f'|{codepost.id}',
                                expires=expires)
            codepost.hit = F('hit') + 1
            codepost.save()
    else:
        response.set_cookie(cookie_name, codepost.id, expires=expires)
        codepost.hit = F('hit') + 1
        codepost.save()
    return response
 def wait_for_tomorrow(self):
     try:
         tomorrow = datetime.replace(datetime.now()+timedelta(days=1), hour=15, minute=11, second=0)
         delta = tomorrow-datetime.now()
         return delta.seconds
     except Exception as e:
         my_logger.logging_operation(e.__str__())
示例#24
0
def to_local_time(datetime):
    """Converts to the request's timezone."""
    request = getattr(local, 'request', None)
    tzinfo = getattr(request, 'tzinfo', None)
    if tzinfo is None:
        return datetime
    return datetime.replace(tzinfo=UTC).astimezone(tzinfo).replace(tzinfo=None)
示例#25
0
def cancel_unused_reservations(request):
	# Exit early if the missed reservation email template has not been customized for the organization yet.
	if not get_media_file_contents('missed_reservation_email.html'):
		return HttpResponseNotFound('The missed reservation email template has not been customized for your organization yet. Please visit the NEMO customizable_key_values page to upload a template, then missed email notifications can be sent.')

	tools = Tool.objects.filter(visible=True, _operational=True, _missed_reservation_threshold__isnull=False)
	missed_reservations = []
	for tool in tools:
		# If a tool is in use then there's no need to look for unused reservation time.
		if tool.in_use() or tool.required_resource_is_unavailable() or tool.scheduled_outage_in_progress():
			continue
		# Calculate the timestamp of how long a user can be late for a reservation.
		threshold = (timezone.now() - timedelta(minutes=tool.missed_reservation_threshold))
		threshold = datetime.replace(threshold, second=0, microsecond=0)  # Round down to the nearest minute.
		# Find the reservations that began exactly at the threshold.
		reservation = Reservation.objects.filter(cancelled=False, missed=False, shortened=False, tool=tool, user__is_staff=False, start=threshold, end__gt=timezone.now())
		for r in reservation:
			# Staff may abandon reservations.
			if r.user.is_staff:
				continue
			# If there was no tool enable or disable event since the threshold timestamp then we assume the reservation has been missed.
			if not (UsageEvent.objects.filter(tool_id__in=tool.get_family_tool_ids(), start__gte=threshold).exists() or UsageEvent.objects.filter(tool_id__in=tool.get_family_tool_ids(), end__gte=threshold).exists()):
				# Mark the reservation as missed and notify the user & NanoFab staff.
				r.missed = True
				r.save()
				missed_reservations.append(r)

	for r in missed_reservations:
		send_missed_reservation_notification(r)

	return HttpResponse()
示例#26
0
def get_ical_for_the_week(week, datetime):
    """Returns the ics representation of the week"""
    ical_content = ""
    for day in week:
        all_day_slots = week[day]
        for slot in all_day_slots:
            slot_content = all_day_slots[slot]
            #print slot_content
            day_value = ICAL_BYDAY[CAL_FR_LABELS[day]]
            slot_date = slot.split(":")
            dtstart_obj = datetime.replace(day=day_value,
                                                   hour=int(slot_date[0]),
                                                   minute=int(slot_date[1]),
                                                   second=0)
            dtstart = dtstart_obj.strftime("%Y%m%dT%H%M%S")
            h,m = delta_time_duration(slot_content['duration'])
            dtend_obj = dtstart_obj + timedelta(hours=h, minutes=m)
            dtend = dtend_obj.strftime("%Y%m%dT%H%M%S")
            ical_content += "BEGIN:VEVENT\n"
            ical_content += "DTSTART;TZID=Europe/Paris:" + dtstart + "\n"
            ical_content += "DTEND;TZID=Europe/Paris:" + dtend + "\n"
            ical_content += "RRULE:FREQ=WEEKLY;BYDAY="
            ical_content += CAL_FR_LABELS[day] + "\n"
            ical_content += "SUMMARY:" + slot_content['name'] + "\n"
            ical_content += "END:VEVENT\n"
    return ical_content
示例#27
0
 def validate(self, postData):
     errors = []
     if len(postData['destination']) == 0:
         errors.append("Enter a destination!")
     if len(postData['description']) == 0:
         errors.append("Enter a description!")
     if not re.match(date_regex, postData['start_date']):
         errors.append("Start date is invalid!")
     if not re.match(date_regex, postData['end_date']):
         errors.append("End date is invalid!")
     else:
         start_date = datetime.strptime(postData['start_date'], '%m/%d/%Y')
         end_date = datetime.strptime(postData['end_date'], '%m/%d/%Y')
         today = datetime.now()
         today = datetime.replace(today,
                                  hour=0,
                                  minute=0,
                                  second=0,
                                  microsecond=0)
         if start_date == end_date:
             errors.append("Start date and end date cannot be the same!")
         if start_date <= today:
             errors.append("Start date cannot be on or before today!")
         if end_date <= today:
             errors.append("End date cannot be on or before today!")
     return errors
示例#28
0
文件: darksky.py 项目: charlesfu4/MT
    def get_pandas(self):
        if len(self.requests_list) == 0:
            raise Exception("user should do the request() first")
        else:

            for js_obj in self.requests_list:
                self.json_objs += js_obj.json()['hourly']['data']
            with open('temp.json', 'w') as outfile:
                json.dump(self.json_objs, outfile)
            jspd = pd.read_json('temp.json', orient='columns')
            ## data frame cleaning
            jspd['time'] = pd.to_datetime(jspd['time'], unit='s')
            jspd['time'] = jspd['time'].dt.tz_localize("UTC").dt.tz_convert(
                self.timezone)
            jspd['time'] = jspd['time'].apply(
                lambda x: datetime.replace(x, tzinfo=None))
            jspd.set_index(jspd['time'], inplace=True)
            jspd.drop(columns=['time'], inplace=True)
            # get the max occurance of year as name of csv
            lst = [year for year in jspd.index.year]
            jspd.to_csv("weather_{}_{}_{}.csv".format(max(lst, key=lst.count),
                                                      self.lat, self.long),
                        index=True)
            return jspd
        return
示例#29
0
def testing(request, machineid):

    if Machine.objects.filter(id=machineid).count() == 0:
        raise Http404

    # check member login
    user = None
    if request.session.get('user_login'):
        user = User.objects.get(id=request.session.get('user_login'))

    machine = Machine.objects.get(id=machineid)
    randomid = str("testing_") + uuid.uuid4().hex[:6].upper() + "_" + str(
        machineid)

    context = {
        'Title': "Your machine are ready",
        'SubTitle': "Start testing, Our Machine Has been learn your data",
        'Machine': machine,
        'RoomCode': randomid,
        'User': user
    }

    response = render(request, 'testing.html', context)

    tomorrow = datetime.now() + timedelta(days=1)
    tomorrow = datetime.replace(tomorrow, hour=0, minute=0, second=0)
    expires = datetime.strftime(tomorrow, "%a, %d-%b-%Y %H:%M:%S GMT")
    response.set_cookie("RoomCode",
                        "teachapp_" + context['RoomCode'],
                        expires=expires)

    return response
示例#30
0
def to_local_time(datetime):
    """Converts to the request's timezone."""
    request = getattr(local, 'request', None)
    tzinfo = getattr(request, 'tzinfo', None)
    if tzinfo is None:
        return datetime
    return datetime.replace(tzinfo=UTC).astimezone(tzinfo).replace(tzinfo=None)
示例#31
0
    def run(self):

        counter = 0
        start_time = time.time()
        sleep_time = 0.0

        # wait for next midnight
        self.log_and_print('waiting for next midnight')
        tomorrow = datetime.replace(datetime.now() + timedelta(days=1),
                                    hour=0,
                                    minute=0,
                                    second=0)
        delta = tomorrow - datetime.now()
        time.sleep(delta.seconds)

        try:
            while True:
                counter += 1
                execution_target = start_time + counter * self.time_step

                if self.s.login():
                    t = datetime.now().isoformat()
                    p = self.s.getActiveParticipants()
                    self.D.store_count_data(p, t)
                    self.s.logout()
                    self.log_and_print(p)
                else:
                    self.log_and_print('could not get / store data')

                sleep_time = execution_target - time.time()
                time.sleep(sleep_time)

        except KeyboardInterrupt:
            print('shutting down')
示例#32
0
 def validate(self, postData):
     errors = []
     try:
         user = User.objects.get(email=postData['email'])
         errors.append("User email already exists! Please log in!")
     except:
         if not re.match(name_regex, postData['first_name']):
             errors.append("First name must be more than 2 characters!")
         if not re.match(name_regex, postData['last_name']):
             errors.append("Last name must be more than 2 characters!")
         if not re.match(email_regex, postData['email']):
             errors.append("Email address is invalid!")
         if not re.match(password_regex, postData['password']):
             errors.append("Password is invalid!")
         elif postData['password'] != postData['confirm_password']:
             errors.append("Passwords do not match!")
         if not re.match(date_regex, postData['birthdate']):
             errors.append("Birthdate must be in MM/DD/YYYY format!")
         else:
             birthdate = datetime.strptime(postData['birthdate'],
                                           '%m/%d/%Y')
             today = datetime.now()
             today = datetime.replace(today,
                                      hour=0,
                                      minute=0,
                                      second=0,
                                      microsecond=0)
             if birthdate >= today:
                 errors.append("Birthdate cannot be on or after today!")
     return errors
def replace_date(date, days):
    """
    Add a number of days to the given date and calculates if it should change the month as well.

    Parameters
    ----------
    date : datetime
        Source date to be modified
    days : int
        Number of days that will be added to `date`

    Returns
    -------
    datetime
        `date` + `days` resulting datetime
    """
    today = datetime.now()
    max_days_in_month = monthrange(today.year, today.month)[1]
    if today.day + days > max_days_in_month:
        new_day = (today.day + days) % max_days_in_month
        new_month = today.month + 1
    else:
        new_day = today.day + days
        new_month = today.month

    return datetime.replace(date, day=new_day, month=new_month)
示例#34
0
def email_out_of_time_reservation_notification(request):
	"""
	Out of time reservation notification for areas is when a user is still logged in a area but his reservation expired.
	"""
	# Exit early if the missed reservation email template has not been customized for the organization yet.
	if not get_media_file_contents('out_of_time_reservation_email.html'):
		return HttpResponseNotFound('The out of time reservation email template has not been customized for your organization yet. Please visit the customization page to upload a template, then out of time email notifications can be sent.')

	out_of_time_user_area = []

	# Find all logged users
	access_records:List[AreaAccessRecord] = AreaAccessRecord.objects.filter(end=None, staff_charge=None).prefetch_related('customer', 'area').only('customer', 'area')
	for access_record in access_records:
		# staff are exempt from out of time notification
		customer = access_record.customer
		area = access_record.area
		if customer.is_staff:
			continue

		if area.requires_reservation:
			# Calculate the timestamp of how late a user can be logged in after a reservation ended.
			threshold = timezone.now() if not area.logout_grace_period else timezone.now() - timedelta(minutes=area.logout_grace_period)
			threshold = datetime.replace(threshold, second=0, microsecond=0)  # Round down to the nearest minute.
			reservations = Reservation.objects.filter(cancelled=False, missed=False, shortened=False, area=area, user=customer, start__lte=timezone.now(), end=threshold)
			if reservations.exists():
				out_of_time_user_area.append(reservations[0])

	for reservation in out_of_time_user_area:
		send_out_of_time_reservation_notification(reservation)

	return HttpResponse()
示例#35
0
def fix_past_date(datetime):
    now = datetime.now()
    now = date_parser.parse("%s-%s-%s" % (now.year, now.month, now.day))
    while datetime < now:
        try:
            return datetime.replace(year=datetime.year + 1)
        except ValueError:
            return datetime + (date(datetime.year + 1, 1, 1) - date(datetime.year, 1, 1))
示例#36
0
def get_latest_week():
    """
    最近一周的时间点
    """
    date = datetime.now()
    ret_date = datetime.replace(date, hour=0, minute=0, second=0, microsecond=0) + timedelta(days=-7)

    return ret_date
示例#37
0
def get_latest_month():
    """
    最近一月的时间点
    """
    date = datetime.now()
    ret_date = datetime.replace(date, hour=0, minute=0, second=0, microsecond=0) + timedelta(days=-30)

    return ret_date
示例#38
0
def get_next_n_days(n):
    """
    接下来的第n天
    """
    date = datetime.now()
    ret_date = datetime.replace(date, hour=0, minute=0, second=0, microsecond=0) + timedelta(days=n)

    return ret_date
示例#39
0
def get_latest_n_days(n):
    """
    最近n天的时间点
    """
    date = datetime.now()
    ret_date = datetime.replace(date, hour=0, minute=0, second=0, microsecond=0) + timedelta(days=-n)

    return ret_date
示例#40
0
def to_user_timezone(tzinfo, datetime):
    """Convert a datetime object to the user's timezone.  This automatically
    happens on all date formatting unless rebasing is disabled.  If you need
    to convert a :class:`datetime.datetime` object at any time to the user's
    timezone (as returned by :func:`get_timezone` this function can be used).
    """
    if datetime.tzinfo is None:
        datetime = datetime.replace(tzinfo=UTC)
    return tzinfo.normalize(datetime.astimezone(tzinfo))
    def _get_new_datetime(self, datetime, relative_mod, time_mod):
        """Build new datetime from relative and time modifiers."""
        if relative_mod:
            datetime += relative_mod

        if time_mod:
            return datetime.replace(hour=time_mod.hour, minute=time_mod.minute)

        return datetime
示例#42
0
def extract_date(str_date):
    """    Find the first %Y-%m-%d string
          and return any legitimate datetime
          with the remainder of the string
    """
    rgx = re.compile(r'((\d{4})-(\d{2})-(\d{2}))')
    o_match = rgx.search(str_date)
    if o_match is not None:

        lng_day = int(o_match.group(4))
        lng_month = int(o_match.group(3))
        lng_year = int(o_match.group(2))

        # These digits may not give a legitimate combination of Y M D
        try:
            dte = datetime(lng_year, lng_month, lng_day)
        except ValueError:
            # Use today's values as defaults, and use any part that does work
            dte = datetime.now()
            # Start with day=1 in case the month is feb and the day 30 etc
            dte = datetime.replace(dte, day=1, hour=0, minute=0, \
              second=0, microsecond=0)
            try:
                dte = datetime.replace(dte, year=lng_year)
            except ValueError:
                pass
            try:
                dte = datetime.replace(dte, month=lng_month)
            except ValueError:
                pass
            try:
                dte = datetime.replace(dte, day=lng_day)
            except ValueError:
                pass

        i_start = o_match.start()
        tpl_date_rest = (dte, str_date[0:i_start] + ' ' + \
          str_date[i_start + 10:])

    else:
        tpl_date_rest = (None, str_date)

    return tpl_date_rest
示例#43
0
def localize_date(
    datetime, long_format=False, time_only=False, local_tz=False
):
    """ Like plone.api.get_localized_time, but with the ability to ignore
    the timezone (which is considered by get_localized_time if present).

    """

    if not local_tz:
        datetime = datetime.replace(tzinfo=None)

    return api.portal.get_localized_time(datetime, long_format, time_only)
示例#44
0
def format_relative_date(date, tz=pytz.utc):
    #todo cleanup
    now = datetime.now(tz=tz)
    time = datetime.replace(date, tzinfo=tz)
    dtime = now - time
    if dtime.days < 1:
        dtext = "Today"
    elif dtime.days < 2:
        dtext = "Yesterday"
    else:
        dtext = "%s days ago" % dtime.days
    return format_datatables_data(dtext, dtime.days)
示例#45
0
 def maintain_thread(self, firstCallResult=None):
     ''' thread to update token and
     register next update event into event loop '''
     r = firstCallResult or self._syncTokenFunction()
     if not r:
         self.core.ioLoop.call_later(
             (datetime.replace(datetime.now() + timedelta(days=1), 
             hour=0, minute=5, second=0) - datetime.now()).seconds,
             self.maintain_access_token, None)
     else:
         self.core.ioLoop.call_later(r['expires_in'] - 30,
             self.maintain_access_token, None)
示例#46
0
def time_for_tomorrow():
    """
    This function return the seconds for tomorrow from now.
    :return: millis for tomorrow from now
    :rtype: integer
    """
    now = datetime.now()
    tomorrow = datetime.replace(now + timedelta(days=1), hour=0, minute=0, second=0)
    delta = tomorrow - now
    seconds = ((delta.days * 24 * 60 * 60 + delta.seconds) * 1000 + delta.microseconds / 1000.0) / 1000
    logging.info("Seconds for tomorrow: " + str(seconds))
    return seconds
示例#47
0
文件: i18n.py 项目: ksachdeva/tipfy
    def to_local_timezone(self, datetime):
        """Returns a datetime object converted to the local timezone.

        :param datetime:
            A ``datetime`` object.
        :returns:
            A ``datetime`` object normalized to a timezone.
        """
        if datetime.tzinfo is None:
            datetime = datetime.replace(tzinfo=pytz.UTC)

        return self.tzinfo.normalize(datetime.astimezone(self.tzinfo))
示例#48
0
文件: base.py 项目: UpOut/UpOutDF
    def _set_start_time(self,datetime):
        #This may be required to handle DST
        datetime = self.timezone.normalize(datetime.astimezone(self.timezone))
        starting_time = self.timezone.normalize(self.starting_time.astimezone(self.timezone))

        datetime = datetime.replace(
            hour=starting_time.hour,
            minute=starting_time.minute,
            second=starting_time.second
        )

        return pytz.utc.normalize(datetime.astimezone(pytz.utc))
示例#49
0
def up_date(dte, r_quant, str_unit, bln_post_colon):
    """    Adjust a date in the light of a (quantity, unit) tuple,
          taking account of any recent colon
    """
    if str_unit == 'w':
        dte += timedelta(weeks=r_quant)
    elif str_unit == 'd':
        dte += timedelta(days=r_quant)
    elif str_unit == 'h':
        dte += timedelta(hours=r_quant)
    elif str_unit == 'm':
        dte += timedelta(minutes=r_quant)
    elif str_unit in ('Y', 'y'):
        if r_quant > 500: # jul 2019 vs jul 17
            r_year = r_quant
        else:
            r_year = datetime.now().year + r_quant
        try:
            dte = datetime.replace(dte, year=int(r_year))
        except ValueError:
            dte = datetime.replace(dte, day=28, month=2,
                year=int(datetime.now().year + r_quant))
    elif str_unit == 'H':
        dte = datetime.replace(dte, hour=int(r_quant), second=0, microsecond=0)
    elif str_unit == 'M':
        dte = datetime.replace(dte, minute=int(r_quant),
            second=0, microsecond=0)
    elif str_unit == 'a':
        if not bln_post_colon:
            dte = datetime.replace(dte, hour=int(r_quant), minute=0,
                second=0, microsecond=0)
    elif str_unit == 'p':
        if bln_post_colon: # adjust by 12 hours if necessary
            if dte.hour < 12:
                dte = datetime.replace(dte, hour=dte.hour+12)
        else:
            p_quant = r_quant
            if p_quant < 12:
                p_quant += 12
            dte = datetime.replace(dte, hour=int(p_quant), minute=0,
                second=0, microsecond=0)
    elif (len(str_unit) >= 3) and (STR_MONTHS.find(str_unit) != -1):
        dte = datetime.replace(dte, month=(STR_MONTHS.index(str_unit) + 3)/3,
            day=int(r_quant), second=0, microsecond=0)
        # refers to this year or next year ? (assume not past)
        dte_today = datetime.today().replace(hour=0, minute=0, \
          second=0, microsecond=0)
        if dte < dte_today:
            dte = dte.replace(year=(dte_today.year+1))
    return dte
示例#50
0
def get_next_of_one_month(date):
    u"""
    获取某个月下月第一天的开始时间
    """
    if is_last_month_in_year(date):
        year = date.year + 1
        month = 1
    else:
        year = date.year
        month = date.month + 1

    ret_date = datetime.replace(date, year=year, month=month, day=1, hour=0, minute=0, second=0, microsecond=0)
    return ret_date
示例#51
0
 def set_server_list(self):
     self._serverList = []
     serverList, fetchTime = self.core.atStorage.get_server_list()
     if fetchTime < time.mktime(datetime.replace(datetime.now(),
         hour=0, minute=0, second=0).timetuple()):
         r = self._serverIpFn()
         if not r:
             logger.debug(r)
         else:
             self._serverList = r.get('ip_list', [])
             self.core.atStorage.store_server_list(self._serverList, time.time())
     else:
         self._serverList = serverList
示例#52
0
def get_previous_month():
    u"""
    获取上月第一天开始时间
    """

    date = datetime.now()
    if is_first_day_in_month(date):
        year = date.year - 1
        month = 12
    else:
        year = date.year
        month = date.month - 1
    ret_date = datetime.replace(date, year=year, month=month, day=1, hour=0, minute=0, second=0, microsecond=0)
    return ret_date
示例#53
0
文件: time_util.py 项目: cedadev/cis
def set_year(datetime, new_year):
    """
    Change the year of a datetime to some new year. If datetime is a leapday then return None
    :param datetime.datetime datetime: Datetime object to change
    :param int new_year: The new year
    :return: A datetime with the same date as the original except the changed year
    """
    # Once we have the data as datetimes we can just use replace to change the year...
    try:
        new_dt = datetime.replace(year=new_year)
    except ValueError:
        # ...Unless the date is 29th of Feb!
        new_dt = None
    return new_dt
    def handle_member(self, member):

        start = datetime.replace(datetime.now(), day=1).date()
        end = start + relativedelta(months=1, days=-1)

        Membership.objects.create(
            member=member,
            protected=True,  # Otherwise it will attempt to reset member automatically
            membership_type=Membership.MT_COMPLIMENTARY,
            sale_price=0.0,
            start_date=start,
            end_date=end,
        )
        self.logger.info("Comp membership created for %s", str(member))
示例#55
0
  def write_boundary_grid_kml(self, boundary, datetime, boundary_grids):
    if self.logger:
      self.logger.info("Start write_boundary_grid_kml for boundary: %s Date: %s" % (boundary, datetime))
    kml_doc = KML.kml(KML.Document(
                        KML.Name("Boundary: %s" % (boundary)),
                        KML.Style(
                          KML.LineStyle(
                            KML.color('ffff0000'),
                            KML.width(3),
                          ),
                          KML.PolyStyle(
                            KML.color('800080ff'),
                          ),
                          id='grid_style'
                        )
                      )
    )

    #doc = etree.SubElement(kml_doc, 'Document')
    try:
      for polygon, val in boundary_grids:
        coords = " ".join("%s,%s,0" % (tup[0],tup[1]) for tup in polygon.exterior.coords[:])
        kml_doc.Document.append(KML.Placemark(KML.name('%f' % val),
                                              KML.styleUrl('grid_style'),
                                               KML.Polygon(
                                                 KML.outerBoundaryIs(
                                                   KML.LinearRing(
                                                    KML.coordinates(coords)
                                                   )
                                                 )
                                               ))
        )
    except (TypeError,Exception) as e:
      if self.logger:
        self.logger.exception(e)
    else:
      try:
        kml_outfile = "%s%s_%s.kml" % (self.KMLDir, boundary, datetime.replace(':', '_'))
        if self.logger:
          self.logger.debug("write_boundary_grid_kml KML outfile: %s" % (kml_outfile))
        kml_file = open(kml_outfile, "w")
        kml_file.write(etree.tostring(kml_doc, pretty_print=True))
        kml_file.close()
      except (IOError,Exception) as e:
        if self.logger:
          self.logger.exception(e)

    if self.logger:
      self.logger.info("End write_boundary_grid_kml for boundary: %s Date: %s" % (boundary, datetime))
    return
示例#56
0
 def filter_request(self, request):
     if self._serverList is None:
         t = threading.Thread(target=self.set_server_list)
         t.setDaemon = True
         t.start()
         def clear_server_list():
             self._serverList = None
         self.core.ioLoop.call_later(
             (datetime.replace(datetime.now() + timedelta(days=1), 
             hour=0, minute=5, second=0) - datetime.now()).seconds,
             clear_server_list)
     if not self._serverList:
         logger.debug('Server list is loading, so ignore verifying once.')
         return True
     return request.remote_ip in self._serverList
示例#57
0
def to_local_timezone(datetime):
    """Returns a datetime object converted to the local timezone.

    This function derives from `Kay <http://code.google.com/p/kay-framework/>`_.

    :param datetime:
        A ``datetime`` object.
    :returns:
        A ``datetime`` object normalized to a timezone.
    """
    tzinfo = get_timezone()
    if datetime.tzinfo is None:
        datetime = datetime.replace(tzinfo=pytz.UTC)

    return tzinfo.normalize(datetime.astimezone(tzinfo))
示例#58
0
文件: dates.py 项目: 10sr/hue
def format_datetime(datetime=None, format='medium', tzinfo=None,
                    locale=LC_TIME):
    """Return a date formatted according to the given pattern.
    
    >>> dt = datetime(2007, 04, 01, 15, 30)
    >>> format_datetime(dt, locale='en_US')
    u'Apr 1, 2007 3:30:00 PM'
    
    For any pattern requiring the display of the time-zone, the third-party
    ``pytz`` package is needed to explicitly specify the time-zone:
    
    >>> from pytz import timezone
    >>> format_datetime(dt, 'full', tzinfo=timezone('Europe/Paris'),
    ...                 locale='fr_FR')
    u'dimanche 1 avril 2007 17:30:00 HEC'
    >>> format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz",
    ...                 tzinfo=timezone('US/Eastern'), locale='en')
    u'2007.04.01 AD at 11:30:00 EDT'
    
    :param datetime: the `datetime` object; if `None`, the current date and
                     time is used
    :param format: one of "full", "long", "medium", or "short", or a custom
                   date/time pattern
    :param tzinfo: the timezone to apply to the time for display
    :param locale: a `Locale` object or a locale identifier
    :rtype: `unicode`
    """
    if datetime is None:
        datetime = datetime_.utcnow()
    elif isinstance(datetime, (int, long)):
        datetime = datetime_.utcfromtimestamp(datetime)
    elif isinstance(datetime, time):
        datetime = datetime_.combine(date.today(), datetime)
    if datetime.tzinfo is None:
        datetime = datetime.replace(tzinfo=UTC)
    if tzinfo is not None:
        datetime = datetime.astimezone(tzinfo)
        if hasattr(tzinfo, 'normalize'): # pytz
            datetime = tzinfo.normalize(datetime)

    locale = Locale.parse(locale)
    if format in ('full', 'long', 'medium', 'short'):
        return get_datetime_format(format, locale=locale) \
            .replace('{0}', format_time(datetime, format, tzinfo=None,
                                        locale=locale)) \
            .replace('{1}', format_date(datetime, format, locale=locale))
    else:
        return parse_pattern(format).apply(datetime, locale)
示例#59
0
    def __init__(self, vevent):
        self.dtstart = vevent.get('dtstart').dt
        self.dtend = vevent.get('dtend').dt
        self.summary = vevent.get('summary')
        self.description = vevent.get('description')

        # Normalize start/end to be datetime objects
        for att in ('dtstart', 'dtend'):
            fixed = getattr(self, att)
            if type(fixed) == date:
                fixed = datetime.combine(fixed, time(0, tzinfo=DEFAULT_TZINFO))

            if fixed.tzinfo is None:
                fixed = datetime.replace(fixed, tzinfo=DEFAULT_TZINFO)

            setattr(self, att, fixed)
示例#60
0
文件: dates.py 项目: 10sr/hue
def get_timezone_gmt(datetime=None, width='long', locale=LC_TIME):
    """Return the timezone associated with the given `datetime` object formatted
    as string indicating the offset from GMT.
    
    >>> dt = datetime(2007, 4, 1, 15, 30)
    >>> get_timezone_gmt(dt, locale='en')
    u'GMT+00:00'
    
    >>> from pytz import timezone
    >>> tz = timezone('America/Los_Angeles')
    >>> dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
    >>> get_timezone_gmt(dt, locale='en')
    u'GMT-08:00'
    >>> get_timezone_gmt(dt, 'short', locale='en')
    u'-0800'
    
    The long format depends on the locale, for example in France the acronym
    UTC string is used instead of GMT:
    
    >>> get_timezone_gmt(dt, 'long', locale='fr_FR')
    u'UTC-08:00'
    
    :param datetime: the ``datetime`` object; if `None`, the current date and
                     time in UTC is used
    :param width: either "long" or "short"
    :param locale: the `Locale` object, or a locale string
    :return: the GMT offset representation of the timezone
    :rtype: `unicode`
    :since: version 0.9
    """
    if datetime is None:
        datetime = datetime_.utcnow()
    elif isinstance(datetime, (int, long)):
        datetime = datetime_.utcfromtimestamp(datetime).time()
    if datetime.tzinfo is None:
        datetime = datetime.replace(tzinfo=UTC)
    locale = Locale.parse(locale)

    offset = datetime.tzinfo.utcoffset(datetime)
    seconds = offset.days * 24 * 60 * 60 + offset.seconds
    hours, seconds = divmod(seconds, 3600)
    if width == 'short':
        pattern = u'%+03d%02d'
    else:
        pattern = locale.zone_formats['gmt'] % '%+03d:%02d'
    return pattern % (hours, seconds // 60)