示例#1
0
    def get_context_data(self, **kwargs):
        context = super(ProjectEditView, self).get_context_data(**kwargs)

        project = self.get_object()
        project.url_detail = project.title.replace(" ", "_")
        comment_list = Comment.objects.filter(Q(project=project))
        selected_comment_list = comment_list.filter(Q(added_to_project=True))
        #process day of the comment
        for comment in comment_list:
            comment.day = humanize.naturalday(comment.publication_date)
        for comment in selected_comment_list:
            comment.day = humanize.naturalday(comment.publication_date)
            comment.form = UnSelectCommentForm(
                initial={'idComment': comment.id})

        context['project'] = project
        context['comment_list'] = comment_list
        context['num_comments'] = len(comment_list)
        context['select_comment_to_add_project'] = True

        selectedCommentForm = SelectCommentForm(project.title, project.user.id)
        if selectedCommentForm.has_comments:
            context['select_comment_form'] = selectedCommentForm
        if len(selected_comment_list) > 0:
            context['selected_comment_list'] = selected_comment_list
        return context
示例#2
0
    def respond(self, message):
        @with_pattern(r"in|at")
        def parse_type(text):
            return text

        # todo: should be handled in message class
        text = message.text.replace(message.get_own_mention(), '', 1)

        if text.startswith(":"):
            text = text.replace(":", "", 1)
        text = text.strip()

        pattern = "remind {whom} {type:type} {when} to {action}"
        res = parse(pattern, text, dict(type=parse_type))

        if not res:
            response = BotMessage(message.channel,
                                  "the pattern is: %s" % pattern)
            yield from self.publisher.send_response(response)
            return

        time_struct, parse_status = cal.parse(res['when'])
        run_at = datetime.fromtimestamp(mktime(time_struct))

        try:
            whom_list = self.parse_users(message, res['whom'])
        except InvalidUserException as e:
            response = BotMessage(message.channel,
                                  "Perdóname, mas: %s" % e.message)
            yield from self.publisher.send_response(response)
            return

        # todo: if not time

        if res['type'] == "in":
            when_readable = "%s, %s" % (humanize.naturalday(run_at),
                                        humanize.naturaltime(run_at))
        else:
            when_readable = "%s, %s" % (humanize.naturalday(run_at),
                                        run_at.time())

        # register the delayed task
        task = RemindTask(run_at, self.publisher, message, whom_list,
                          res['action'])
        self.scheduler.register_task(task)

        # immediate response
        response = BotMessage(
            message.channel, "Ok, will remind _{whom}_ *{when_readable}* "
            "to _{action}_".format(whom=", ".join(whom_list),
                                   when_readable=when_readable,
                                   action=res['action']))

        yield from self.publisher(response)
示例#3
0
    def respond(self, message):

        @with_pattern(r"in|at")
        def parse_type(text):
            return text

        # todo: should be handled in message class
        text = message.text.replace(message.get_own_mention(), '', 1)

        if text.startswith(":"):
            text = text.replace(":", "", 1)
        text = text.strip()

        pattern = "remind {whom} {type:type} {when} to {action}"
        res = parse(pattern, text, dict(type=parse_type))

        if not res:
            response = BotMessage(message.channel, "the pattern is: %s" % pattern)
            yield from self.publisher.send_response(response)
            return

        time_struct, parse_status = cal.parse(res['when'])
        run_at = datetime.fromtimestamp(mktime(time_struct))

        try:
            whom_list = self.parse_users(message, res['whom'])
        except InvalidUserException as e:
            response = BotMessage(message.channel, "Perdóname, mas: %s" % e.message)
            yield from self.publisher.send_response(response)
            return

        # todo: if not time

        if res['type'] == "in":
            when_readable = "%s, %s" % (humanize.naturalday(run_at), humanize.naturaltime(run_at))
        else:
            when_readable = "%s, %s" % (humanize.naturalday(run_at), run_at.time())

        # register the delayed task
        task = RemindTask(run_at, self.publisher, message, whom_list, res['action'])
        self.scheduler.register_task(task)

        # immediate response
        response = BotMessage(message.channel,
                               "Ok, will remind _{whom}_ *{when_readable}* "
                               "to _{action}_".format(whom=", ".join(whom_list),
                                                      when_readable=when_readable,
                                                      action=res['action']))

        yield from self.publisher(response)
示例#4
0
    def dt_filter(date):
        """Date time filter

        Returns a human readable string for a datetime object.
        """
        return '{time} {date}'.format(time=date.strftime('%H:%M'),
                                      date=humanize.naturalday(date))
示例#5
0
文件: main.py 项目: brvier/DownDN
    def load_notes(self):
        if not exists(self.notes_fn):
            makedirs(self.notes_fn)

        self.notes = []
        for path, folders, files in walk(self.notes_fn):
            if os.path.relpath(path, self.notes_fn) != '.':
                continue
            for afile in files:
                if (splitext(basename(afile))[1]
                        in ('.txt')) and (afile[0] != '.'):
                    mtime = stat(join(path, afile))[ST_MTIME]
                    self.notes.append({
                        'title':
                        splitext(basename(afile))[0],
                        'category':
                        dirname(relpath(join(afile, path), self.notes_fn)),
                        'last_modification':
                        time.asctime(time.localtime(mtime)),
                        'natural_last_modification':
                        humanize.naturalday(
                            datetime.datetime.fromtimestamp(mtime)),
                        'mtime':
                        mtime,
                        'content':
                        '',
                        'filepath':
                        join(path, afile)
                    })
        self.sort_notes()
def human_date(date):
    def style_day(n):
        n = int(n)
        return str(n) + ("th" if 4 <= n % 100 <= 20 else {
            1: "st",
            2: "nd",
            3: "rd"
        }.get(n % 10, "th"))

    # apply humanization fx
    h = humanize.naturalday(datetime.datetime.strptime(date,
                                                       "%Y-%m-%d")).title()

    # remove zeros
    m0 = re.search(r"([A-Za-z]+) 0([0-9])", h)
    if m0: h = "%s %s" % (m0.group(1), m0.group(2))

    # style day
    m_day = re.search(r"([A-Za-z]+) (\d+)", h)
    if m_day: h = "%s %s" % (m_day.group(1), style_day(m_day.group(2)))

    # lowercase yesterday and today
    if h in ['Yesterday', 'Today']: h = h.lower()

    return h
示例#7
0
文件: main.py 项目: brvier/DownDN
 def load_todos(self):
     self.todos = []
     try:
         for path in os.listdir(self.notes_fn):
             if 'todo' in path.lower():
                 print(path)
                 with open(os.path.join(self.notes_fn, path), 'rb') as fh:
                     content = fh.read().decode('utf-8')
                 for line in content.split('\n'):
                     if line.startswith('- [ ]') or line.startswith(
                             '- [x]'):
                         try:
                             due = parse(
                                 re.search('due:(\S*)', line).group(1))
                         except AttributeError:
                             due = ''
                         text = re.sub('( due:\S*)', '', line[5:]).strip()
                         self.todos.append({
                             'line': line[5:].strip(),
                             'text': text,
                             'due': humanize.naturalday(due),
                             'datetime': due,
                             'done': line.startswith('- [x]'),
                             'filename': path
                         })
     except OSError as err:
         print(err)
示例#8
0
文件: main.py 项目: Thuruv/kekday
def get_cake_day(username):
    redditor = reddit_client.get_redditor(username)
    try:
        created_on = datetime.utcfromtimestamp(redditor.created_utc)
    except praw.errors.NotFound:
        return False
    return(humanize.naturalday(created_on))
示例#9
0
    def get_context_data(self, **kwargs):
        context = super(ProjectDetailView, self).get_context_data(**kwargs)

        project = self.get_object()
        comment_list = Comment.objects.filter(project=project)
        #process day of the comment
        for comment in comment_list:
            comment.day = humanize.naturalday(comment.publication_date)

        context['comment_list'] = comment_list
        context['num_comments'] = len(comment_list)
        # display comment form if:
        #   * user is log in
        #   * the user dont have a comment in the project.
        #   * the user is not the project owner
        if self.request.user.is_authenticated\
                and not self.is_second_comment(self.request.user,project)\
                and project.user.username != self.request.user.username:
            context['new_comment_form'] = self.get_form()

        project.url_detail = project.title.replace(" ", "_")
        context['project'] = project
        if project.user.username == self.request.user.username:
            context['display_edit_delete_button'] = True

        return context
示例#10
0
 def last_sync_time_natural(self):
     if self.last_sync_time is None:
         return '-'
     else:
         delta = datetime.now(
             tz=get_current_timezone()) - self.last_sync_time
         return humanize.naturaldelta(delta) + ' ago' if delta < timedelta(
             days=2) else humanize.naturalday(self.last_sync_time)
示例#11
0
 def get_change(self, change_id):
     Change = Query()
     res = self.changes.search(Change.id == change_id)
     res = len(res) == 1 and res[0] or None
     if res:
         d = datetime.datetime.strptime(res["date"], "%Y-%m-%dT%H:%M:%SZ")
         res["date_str"] = humanize.naturalday(d)
     return res
示例#12
0
def get_cake_day(username):
    redditor = reddit_client.get_redditor(username)
    try:
        created_on = datetime.utcfromtimestamp(redditor.created_utc)
    except praw.errors.NotFound:
        return False
    return humanize.naturalday(
        created_on), redditor.comment_karma, redditor.link_karma
示例#13
0
    def dt_filter(date):
        """Date time filter

        Returns a human readable string for a datetime object.
        """
        return '{time} {date}'.format(
            time=date.strftime('%H:%M'),
            date=humanize.naturalday(date)
        )
示例#14
0
文件: Posts.py 项目: Kesi-P/learn-py
    def get_all_posts(self):
        all_posts = self.posts.find().sort('date_added', -1)
        new_posts = []
        for post in all_posts:
            post['user'] = self.users.find_one({'username':post['username']}) #find just one
            post['timestamp'] = humanize.naturalday(datetime.datetime.now() - post['date_added']) #if it is key error, mean some table doens't have that data
            post['old_comment'] = self.comments.find({'post_id': str(post['_id'])}) #find all

            post['comment'] =[]

            for comment in post['old_comment']:
                comment['user'] = self.users.find_one({'username': comment['username']})
                comment['time'] = humanize.naturalday(datetime.datetime.now() - comment['date_added'])
                post['comment'].append(comment)

            new_posts.append(post)

        return new_posts
示例#15
0
def ls(ctx, show_all):
    db = ctx.obj['database']
    table = [['Name', 'Tags', 'Last Modified', 'Phrase']]
    phrases = db.all()
    for phrase in phrases:
        tags = ', '.join([x for x in phrase.get('tags')])
        v = phrase.get('value')
        value = (v[:40] + '...' if len(v) > 40  and not show_all else v)
        table.append([phrase.get('key'), tags, humanize.naturalday(phrase.get('updated')), value])
    output = AsciiTable(table)
    click.echo(output.table)
示例#16
0
 def format_time(d: datetime) -> str:
     """Format time relatively if necessary"""
     diff: timedelta = datetime.now() - d
     if diff.days >= 7:
         return d.strftime("%-d %B, %Y")
     elif 0 < diff.days < 7:
         return humanize.naturalday(d).capitalize()
     elif diff.seconds > 60:
         return humanize.naturaltime(d).capitalize()
     else:
         return "A few seconds ago"
示例#17
0
def getStats(bot, msg, match):
    stats = bot.stats
    response = "I was born "
    response += naturalday(datetime.datetime.fromtimestamp(stats['bornOn']))
    response += ' and named CrashBot version '+stats['version']
    response += ".\n"
    response += "I woke up "
    response += naturaltime(time.time() - stats['startTime'])
    response += ".\n"
    response += "I've answered " + str(stats['count']+1) + " commands.\n"
    response += "I've seen " + str(bot.users.count()) + " users."
    bot.respond(msg, response)
示例#18
0
def videolink(v):
    detail_url = base + "videos/" + v.key + "/"
    revision = None
    try:
        revision = v.draft
    except:
        return None
    return [
        "<a href ='" + detail_url + "'>" + smart_truncate(revision.title, 50) + "</a>",
        v.team.owner.username,
        humanize.naturalday(revision.modified),
        str(v.total_plays) 
    ] 
示例#19
0
def search_calendar(keyword_list):
    """Search for listed keywords in calendar entries."""
    cal_items = char.calendar_events()
    items = []
    for _, event in cal_items[0].iteritems():
        for keyword in keyword_list:
            if keyword in event['title'].lower():
                start = humanize.naturalday(
                    datetime.datetime.fromtimestamp(event['start_ts']))

                items.append('%s - %s' % (start, event['title']))

    return items
示例#20
0
def search_calendar(keyword_list):
    """Search for listed keywords in calendar entries."""
    cal_items = char.calendar_events()
    items = []
    for _, event in cal_items[0].iteritems():
        for keyword in keyword_list:
            if keyword in event['title'].lower():
                start = humanize.naturalday(
                    datetime.datetime.fromtimestamp(event['start_ts']))

                items.append('%s - %s' % (start, event['title']))

    return items
示例#21
0
def convert_date(date_to_convert):
    """Convert date to human readable format: Month Day Year
    If date is less than a day return: today or yesterday
    Format of date to convert: 2019-01-12T16:48:41.821037+00:00
    Output: Jan 12 2019
    :param date_to_convert: Date to convert
    :returns: Readable date
    """
    date_parsed = parser.parse(date_to_convert).replace(tzinfo=None)
    delta = datetime.datetime.now() - datetime.timedelta(days=1)
    if delta < date_parsed:
        return humanize.naturalday(date_parsed).title()
    else:
        return date_parsed.strftime("%-d %B %Y")
示例#22
0
def ls(ctx, show_all):
    db = ctx.obj["database"]
    table = [["Name", "Tags", "Last Modified", "Phrase"]]
    phrases = db.all()
    for phrase in phrases:
        tags = ", ".join([x for x in phrase.get("tags")])
        v = phrase.get("value")
        value = v[:40] + "..." if len(v) > 40 and not show_all else v
        table.append([
            phrase.get("key"), tags,
            humanize.naturalday(phrase.get("updated")), value
        ])
    output = AsciiTable(table)
    click.echo(output.table)
示例#23
0
def natural_date(date):
    """
    A template filter for rendering
    dates in human-readable form,
    e.g. "5 days ago", etc.

    Example usage (in `pyjade`)::

        div= event.updated_at.date()|natural_date
    """
    dt = datetime.combine(date, datetime.min.time())
    diff = datetime.utcnow() - dt
    if diff <= timedelta(days=2):
        return humanize.naturalday(diff)
    return humanize.naturaltime(diff)
示例#24
0
def natural_date(date):
    """
    A template filter for rendering
    dates in human-readable form,
    e.g. "5 days ago", etc.

    Example usage (in `pyjade`)::

        div= event.updated_at.date()|natural_date
    """
    dt = datetime.combine(date, datetime.min.time())
    diff = datetime.utcnow() - dt
    if diff <= timedelta(days=2):
        return humanize.naturalday(diff)
    return humanize.naturaltime(diff)
示例#25
0
def upload_movies(wk_movies: Worksheet) -> None:
    title = 'Filmer'
    if wk_movies.title == title:
        rows: List[Row] = []
        movies: List[Movie] = db.movies()
        for movie in movies:
            rows.append(get_row_rating(movie))

        row_end, col_end = len(movies) + 1, ALPHABETH[len(USERS) + 4]
        wk_movies.update_values(f'A2:{col_end}{row_end}', rows)
        latest_ratings = [
            [a, humanize.naturalday(b), to_link(IMDB_LINK.format(e), c), d] for (a, b, c, d, e) in db.latest_ratings(20)
        ]
        wk_movies.update_values('M12:P31', latest_ratings)
    else:
        print(f'Wrong title on worksheet. Got {wk_movies.title} ,expected {title}')
示例#26
0
def convert_date(date_to_convert):
    """Convert date to human readable format: Month Day Year

    If date is less than a day return: today or yesterday

    Format of date to convert: 2019-01-12T16:48:41.821037+00:00
    Output: Jan 12 2019

    :param date_to_convert: Date to convert
    :returns: Readable date
    """
    date_parsed = parser.parse(date_to_convert).replace(tzinfo=None)
    delta = datetime.datetime.now() - datetime.timedelta(days=1)
    if delta < date_parsed:
        return humanize.naturalday(date_parsed).title()
    else:
        return date_parsed.strftime("%-d %B %Y")
示例#27
0
def parse_date(string):
    # Parse schedule string
    pdt_obj, status = pdt.Calendar().parse(
        string)  # Turn any human-written string into a pdt object
    datetime_obj = datetime(
        *pdt_obj[:6])  # Turn the object into a datetime object
    day = humanize.naturalday(datetime_obj,
                              "%A").capitalize()  # 'Tomorrow' or 'Wednesday'
    if day not in ["Today", "Tomorrow"]:
        # Set day to be of the format "Wednesday 24th Feb"
        # For full months, swap "%b" for "%B"
        day = " ".join([
            day,
            humanize.ordinal(datetime_obj.day),
            datetime_obj.strftime("%b")
        ])
    time = datetime_obj.strftime(" at %H%M")
    return datetime_obj, day + time
示例#28
0
def get_jinja_env():
    global JENV
    
    if not JENV is None:
        return JENV

    JENV = Environment(loader=FileSystemLoader(
        os.path.join(os.path.dirname(__file__), 'templates')))

    def _naturalsize(v):
        try:
            return humanize.naturalsize(v)
        except:
            return "n.a."
    JENV.filters['humanfilesize'] = lambda v: _naturalsize(v)
#    JENV.filters['humanfilesize'] = lambda v: type(v)
    JENV.filters['humanday'] = lambda v: humanize.naturalday(v)
    JENV.filters['shacol'] = shasum_to_colors
    return JENV
示例#29
0
def human_date(date):

    def style_day(n):
        n = int(n)
        return str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))

    # apply humanization fx
    h = humanize.naturalday(datetime.datetime.strptime(date, "%Y-%m-%d")).title()

    # remove zeros
    m0 = re.search(r"([A-Za-z]+) 0([0-9])", h)
    if m0: h = "%s %s" % ( m0.group(1), m0.group(2) )

    # style day
    m_day = re.search(r"([A-Za-z]+) (\d+)", h)
    if m_day: h = "%s %s" % ( m_day.group(1), style_day(m_day.group(2)) )

    # lowercase yesterday and today
    if h in ['Yesterday', 'Today']: h = h.lower()

    return h
示例#30
0
    def get_changelog(self, **filters):
        # limited set of supported filters
        def has_tag(tags, tag):
            return tag in tags

        def _since(date, since):
            return date >= since

        def _until(date, until):
            return date <= until

        Change = Query()
        query = []
        if "tag" in filters:
            query.append(Change.tags.test(has_tag, filters["tag"]))
        if "since" in filters:
            query.append(Change.date.test(_since, filters["since"]))
        if "until" in filters:
            query.append(Change.date.test(_until, filters["until"]))

        if query == []:
            res = self.changes
        else:
            fquery = query[0]
            for q in query[1:]:
                fquery = fquery & q
            res = self.changes.search(fquery)

        changes = []
        for line in res:
            d = datetime.datetime.strptime(line["date"], "%Y-%m-%dT%H:%M:%SZ")
            line["date_str"] = humanize.naturalday(d)
            changes.append((d, line))

        changes.sort()
        return reversed([change for _, change in changes])
示例#31
0
 async def upcoming(self, ctx):
     now = datetime.datetime.utcnow()
     two_days = now + datetime.timedelta(days=2)
     events_result = await asyncify(lambda: self.service.events().list(
         calendarId=config['google_api_auth']['calendar_id'],
         timeMin=time_format(now),
         timeMax=time_format(two_days),
         singleEvents=True,
         orderBy='startTime').execute())
     events = events_result.get('items', [])
     if len(events) == 0:
         await timed_send(ctx, 'not much is happening')
     else:
         await timed_send(ctx, 'starting soon (24-hour times, Pacific):')
         now = datetime.datetime.now(datetime.timezone.utc)
         for event in events:
             if 'date' not in event['start']:
                 start = parse_datetime(event['start']['dateTime'])
                 end = parse_datetime(event['end']['dateTime'])
                 if start.date() == end.date():
                     await ctx.send('{} from {} to {}: {}'.format(
                         naturalday(start.date()),
                         start.time().strftime('%H:%M'),
                         end.time().strftime('%H:%M'), event['summary']))
示例#32
0
 def format_humanize_naturalday(tmp):
     if not tmp:
         return 'n/a'
     return humanize.naturalday(tmp)
示例#33
0
    exit(0)
print("your stats for " + sys.argv[1])
print()
whole_time = 0

for key in all_times.keys():
    # print(all_times[key])
    start_time = datetime.datetime.fromtimestamp(float(key))
    start_time_readable = start_time.strftime("%Y-%m-%d %H:%M")
    end_time = datetime.datetime.fromtimestamp(float(all_times[key].get("end_time", None)))
    end_time_readable = end_time.strftime("%Y-%m-%d %H:%M")

    diff = float(all_times[key].get("end_time", None)) - float(key)
    diff_datetime = datetime.timedelta(seconds=diff)
    whole_time += float(all_times[key].get("end_time", None)) - float(key)
    print("Session: (" + humanize.naturalday(start_time) + ") " + " duration: " + str(diff_datetime) + "s")

    print(str(start_time_readable) + " - " + end_time_readable)

    if all_times[key].get("description", None):
        print(" description: " + all_times[key].get("description", None))

    perc_act = percentage_activity(all_times[key].get("focus_times"))

    for wmname in perc_act.keys():
        print("  " + str(round(perc_act[wmname], 2)) + "% - " + wmname[:25])

    print()

print("whole time spent on this project: " + str(datetime.timedelta(seconds=whole_time)))
示例#34
0
文件: human.py 项目: q210/vkblind
def human_date(date):
    return humanize.naturalday(datetime.datetime.fromtimestamp(date))
示例#35
0
secrets = open('graphapi.secret', 'r')
at = util.get_fb_token(secrets.readline().strip(), secrets.readline().strip())
graph = facebook.GraphAPI(access_token = at, version = '2.2')

results = open('EventChart.result', 'w+')
results.write("------------------- Upcoming Events -------------------\n---------------------------------------------------------------\n\n")

csv = open('EventChart.csv', 'w+')
csv.write("Name,Date,Addresses,Venue,Facebook Page\n")

for event_id in event_ids:
    event = graph.get_object( id = event_id)
    
    name = re.sub("\(.*\)","",event['name'])
    print event['place']
    
    date = datetime.datetime.strptime(event['start_time'][:10], "%Y-%m-%d")
    date = humanize.naturalday(date).title()
    
    city = event['place']['location']['city']
    state = event['place']['location']['state']
    address = event['place']['location']['street']
    location = event['place']['name']
    
    url = "facebook.com/events/"+event_id
    
    results.write(date + " - " + name + " [" + city + ", " + state + "]\n"+url+"\n----------------------------------------------------------------\n")
    csv.write(name+","+date+","+address+","+location+","+url+"\n")
    
results.close()
csv.close()
示例#36
0
import datetime

import humanize
import time

start_day = datetime.datetime.fromtimestamp(time.time() - 3600)
print(humanize.naturaltime(start_day))
print(start_day)


if __name__ == "__main__":
    print(humanize.naturalday(datetime.datetime.now() + datetime.timedelta(seconds=3600*24)))

    print(humanize.naturalday(1516446617))

示例#37
0
 def date_fmt(self):
     return humanize.naturalday(self.date)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'ipetrash'

# SOURCE: https://github.com/jmoiron/humanize

# pip install humanize
import humanize

# Date & time humanization:
import datetime as DT
print(humanize.naturalday(DT.datetime.now()))  # 'today'
print(humanize.naturalday(DT.datetime.now() -
                          DT.timedelta(days=1)))  # 'yesterday'
print(humanize.naturalday(DT.date(2007, 6, 5)))  # 'Jun 05'
print()

print(DT.timedelta(seconds=1001))  # '0:16:41'
print(humanize.naturaldelta(DT.timedelta(seconds=1001)))  # '16 minutes'
print(humanize.naturaldelta(DT.timedelta(seconds=5)))  # '5 seconds'
print(humanize.naturaldelta(DT.timedelta(hours=30)))  # 'a day'
print(humanize.naturaldelta(DT.timedelta(hours=60)))  # '2 days'
print()

print(humanize.naturaldate(DT.date(2007, 6, 5)))  # 'Jun 05 2007'
print(humanize.naturaldate(DT.date(2007, 6, 5)))  # 'Jun 05 2007'
print()

print(humanize.naturaltime(DT.datetime.now() -
                           DT.timedelta(seconds=1)))  # 'a second ago'
示例#39
0
def naturalday(context, value, format='%b %d'):
    return humanize.naturalday(value, format)
示例#40
0
文件: human.py 项目: q210/vkblind
def human_datetime(date):
    date = datetime.datetime.fromtimestamp(date)
    return '{0}, {1}'.format(humanize.naturalday(date),
                             humanize.naturaltime(date))
示例#41
0
 def naturalTime(self, date_time):
     if not isinstance(date_time, datetime.datetime):
         raise TypeError(date_time + ' is not an instance of type datetime.datetime')
     return humanize.naturalday(date_time)
示例#42
0
def getnextevent():
    """ Loops through all calendars and prints the next even in each one """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build("calendar", "v3", http=http)

    # 'Z' needed for calendar API
    now = datetime.datetime.utcnow().isoformat(
    ) + 'Z'  # 'Z' indicates UTC time
    #now = datetime.datetime.now().isoformat() + 'Z' # 'Z' indicates UTC time
    today = datetime.date.today().isoformat()
    today = datetime.datetime.strptime(today, "%Y-%m-%d")

    location = 'Australia/Perth'
    tz = timezone(location)
    # Need to change times to non-naive
    event_title_low = None
    page_token = None

    calendar_list = service.calendarList().list(pageToken=page_token).execute()
    # Get next event in each calendar
    timed_events = []
    allday_events = []
    for calendar_list_entry in calendar_list['items']:
        eventsResult = (service.events().list(
            calendarId=calendar_list_entry['id'],
            timeMin=now,
            singleEvents=True,
            orderBy="startTime",
            maxResults=2,
        ).execute())
        event = eventsResult.get("items", [])
        if event != []:
            #Determine start time
            try:
                event_time = {event[0]["start"]["dateTime"]}
                for start_time in event_time:
                    if start_time[-1] == 'Z':
                        start_time = datetime.datetime.strptime(
                            start_time, "%Y-%m-%dT%H:%M:%SZ")
                        start_time = start_time + datetime.timedelta(hours=8)
                        start_time = tz.localize(start_time)
                    else:
                        start_time = datetime.datetime.strptime(
                            start_time, '%Y-%m-%dT%H:%M:%S%z')
                #calendar_entry = [start_time, end_time, event[0]["summary"]]
                #timed_events.append(calendar_entry)
            except:
                event_time = {event[0]["start"]["date"]}
                for start_time in event_time:
                    start_time = start_time
                #calendar_entry = [start_time, end_time, event[0]["summary"]]
                #allday_events.append(calendar_entry)

            #Determine end time
            try:
                event_time = {event[0]["end"]["dateTime"]}
                for end_time in event_time:
                    if end_time[-1] == 'Z':
                        end_time = datetime.datetime.strptime(
                            end_time, "%Y-%m-%dT%H:%M:%SZ")
                        end_time = end_time + datetime.timedelta(hours=8)
                        end_time = tz.localize(end_time)
                    else:
                        end_time = datetime.datetime.strptime(
                            end_time, '%Y-%m-%dT%H:%M:%S%z')
                duration = end_time - start_time
                duration = duration.seconds
                calendar_entry = [
                    start_time, end_time, event[0]["summary"], duration
                ]
                timed_events.append(calendar_entry)
            except:
                event_time = {event[0]["end"]["date"]}
                for end_time in event_time:
                    end_time = end_time
                calendar_entry = [start_time, end_time, event[0]["summary"]]
                allday_events.append(calendar_entry)

    event_start_low = allday_events[0][0]
    for i in allday_events:
        #print(f'{i[0]} - {i[1]}: {i[2]}')
        if i[0] <= event_start_low:
            event_start_low = i[0]
            lowest_allday = [i[0], i[1], i[2]]
    event_start_low = timed_events[0][0]
    for i in timed_events:
        #Only want events shorter than 11hrs
        #print(f'{i[0]} - {i[1]}: {i[2]}')
        if i[0] <= event_start_low and 1 < i[3] < 39601:
            event_start_low = i[0]
            lowest_timed = [i[0], i[1], i[2], i[3]]

    #Determine which is lower
    #If event occurs less than 24hrs after allday event starts its in the same day
    if (lowest_timed[0] - tz.localize(
            datetime.datetime.strptime(lowest_allday[0],
                                       "%Y-%m-%d"))).seconds < 86400:
        start_time_print = humanize.naturalday(
            lowest_timed[0]).title() + "-" + datetime.datetime.strftime(
                lowest_timed[0], "%H:%M")
        #start_time_print = datetime.datetime.strftime(lowest_timed[0], "%H:%M")
        print(f'{start_time_print}-{lowest_timed[2]}')
        #print(lowest_timed)
    elif str(
            datetime.datetime.strftime(datetime.date.today(),
                                       "%Y-%m-%d %H:%M:%S")) == str(
                                           datetime.datetime.strptime(
                                               lowest_allday[0], "%Y-%m-%d")):
        print(f'Today: {lowest_allday[2]}')
示例#43
0
def step_impl(context):
    start_time = context.post_email['start_time']
    response = context.post_email['response']
    assert humanize.naturalday(start_time) in response.data.decode('utf8')
示例#44
0
 def humanize_date(time):
     return humanize.naturalday(time)
示例#45
0
    if FLAGS.print_report:
        print '\n\n==== %s ====' % char_name

    categories, tx_details = get_journal_tx()

    add_market_tx(categories)

    if FLAGS.print_report:
        if categories:
            print '\n%10s  %12s  %12s  %12s  %12s' % (
                'Summary', 'Bounties', 'Duty', 'Sales', 'Purchases')
            for date in sorted(categories.keys()):
                cats = categories[date]
                print '%10s: %12s  %12s  %12s  %12s' % (
                    humanize.naturalday(date),
                    humanize.intcomma(int(cats['bounties'])),
                    humanize.intcomma(int(cats['duty'])),
                    humanize.intcomma(int(cats['sales'])),
                    humanize.intcomma(int(cats['purchases'])),
                )

        info, _, _ = char.wallet_info()
        print '\nBalance: %s' % humanize.intcomma(info['balance'])

    get_contracts(2 * 24 * 60 * 60)

    events = []
    if keywords:
        events.extend(search_calendar(keywords))
示例#46
0
    async def updateCard(self, trainer):
        dailyDiff = await self.getDiff(trainer, 1)
        level = trainer.level
        embed = discord.Embed(timestamp=dailyDiff.new_date,
                              colour=int(
                                  trainer.team().colour.replace("#", ""), 16))
        try:
            embed.set_author(name=trainer.username,
                             icon_url=trainer.account().discord().avatar_url)
        except:
            embed.set_author(name=trainer.username)
        embed.add_field(name='Level', value=level.level)
        if level.level != 40:
            embed.add_field(name='XP',
                            value='{:,} / {:,}'.format(
                                trainer.update.xp - level.total_xp,
                                level.xp_required))
        else:
            embed.add_field(name='Total XP',
                            value='{}'.format(humanize.intword(
                                level.total_xp)))
        if dailyDiff.change_xp and dailyDiff.change_time:
            gain = '{:,} since {}. '.format(
                dailyDiff.change_xp, humanize.naturalday(dailyDiff.old_date))
            if dailyDiff.change_time.days > 1:
                gain += "That's {:,} xp/day.".format(
                    round(dailyDiff.change_xp / dailyDiff.change_time.days))
            embed.add_field(name='Gain', value=gain)
            if trainer.goal_daily and dailyDiff.change_time.days > 0:
                dailyGoal = trainer.goal_daily
                embed.add_field(name='Daily completion',
                                value='{}% towards {:,}'.format(
                                    pycent.percentage(
                                        dailyDiff.change_xp /
                                        max(1, dailyDiff.change_time.days),
                                        dailyGoal), dailyGoal))
        if trainer.goal_total and trainer.goal_total != 0:
            totalGoal = trainer.goal_total
        elif level.level < 40:
            totalGoal = trainerdex.Level.from_level(level.level + 1).total_xp
        else:
            totalGoal = None
        if totalGoal:
            totalDiff = await self.getDiff(trainer, 7)
            embed.add_field(name='Goal remaining',
                            value='{:,} out of {}'.format(
                                totalGoal - totalDiff.new_xp,
                                humanize.intword(totalGoal)))
            if totalDiff.change_time.seconds >= 1:
                eta = lambda x, y, z: round(x / (y / z))
                eta = eta(totalGoal - totalDiff.new_xp, totalDiff.change_xp,
                          totalDiff.change_time.total_seconds())
                eta = totalDiff.new_date + datetime.timedelta(seconds=eta)
                embed.add_field(name='Goal ETA',
                                value=humanize.naturaltime(
                                    eta.replace(tzinfo=None)))
            if totalDiff.change_time.total_seconds() < 583200:
                embed.description = "ETA may be inaccurate. Using {} of data.".format(
                    humanize.naturaldelta(totalDiff.change_time))
        embed.set_footer(text="Total XP: {:,}".format(dailyDiff.new_xp))

        return embed
示例#47
0
def test_naturalday(test_args, expected):
    assert humanize.naturalday(*test_args) == expected
示例#48
0
    if FLAGS.print_report:
        print '\n\n==== %s ====' % char_name

    categories, tx_details = get_journal_tx()

    add_market_tx(categories)

    if FLAGS.print_report:
        if categories:
            print '\n%10s  %12s  %12s  %12s  %12s' % (
                'Summary', 'Bounties', 'Duty', 'Sales', 'Purchases')
            for date in sorted(categories.keys()):
                cats = categories[date]
                print '%10s: %12s  %12s  %12s  %12s' % (
                    humanize.naturalday(date),
                    humanize.intcomma(int(cats['bounties'])),
                    humanize.intcomma(int(cats['duty'])),
                    humanize.intcomma(int(cats['sales'])),
                    humanize.intcomma(int(cats['purchases'])),
                )

        info, _, _ = char.wallet_info()
        print '\nBalance: %s' % humanize.intcomma(info['balance'])

    get_contracts(2*24*60*60)

    events = []
    if keywords:
        events.extend(search_calendar(keywords))
示例#49
0
def _humanize_date_filter(obj):
    return naturalday(obj)
示例#50
0
def datetime_to_text(dt: datetime.datetime) -> str:
    date_str = humanize.naturalday(dt, format="%A %B %-d")
    time_str = dt.strftime("%-I %-M %p")
    return f"{date_str} at {time_str}"