view sat/tools/common/date_utils.py @ 3922:0ff265725489

plugin XEP-0447: handle attachment and download: - plugin XEP-0447 can now be used in message attachments and to retrieve an attachment - plugin attach: `attachment` being processed is added to `extra` so the handler can inspect it - plugin attach: `size` is added to attachment - plugin download: a whole attachment dict is now used in `download` and `file_download`/`file_download_complete`. `download_uri` can be used as a shortcut when just a URI is used. In addition to URI scheme handler, whole attachment handlers can now be registered with `register_download_handler` - plugin XEP-0363: `file_http_upload` `XEP-0363_upload_size` triggers have been renamed to `XEP-0363_upload_pre_slot` and is now using a dict with arguments, allowing for the size but also the filename to be modified, which is necessary for encryption (filename may be hidden from URL this way). - plugin XEP-0446: fix wrong element name - plugin XEP-0447: source handler can now be registered (`url-data` is registered by default) - plugin XEP-0447: source parsing has been put in a separated `parse_sources_elt` method, as it may be useful to do it independently (notably with XEP-0448) - plugin XEP-0447: parse received message and complete attachments when suitable - plugin XEP-0447: can now be used with message attachments - plugin XEP-0447: can now be used with attachments download - renamed `options` arguments to `extra` for consistency - some style change (progressive move from legacy camelCase to PEP8 snake_case) - some typing rel 379
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 16:02:05 +0200
parents 92482cc80d0b
children 883db2790b11
line wrap: on
line source

#!/usr/bin/env python3


# SAT: a jabber client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""tools to help manipulating time and dates"""

from typing import Union
import calendar
import datetime
import re
import time

from babel import dates
from dateutil import parser, tz
from dateutil.parser import ParserError
from dateutil.relativedelta import relativedelta
from dateutil.utils import default_tzinfo

from sat.core import exceptions
from sat.core.constants import Const as C
from sat.core.i18n import _

RELATIVE_RE = re.compile(
    r"\s*(?P<in>\bin\b)?"
    r"(?P<date>[^+-].+[^\s+-])?\s*(?P<direction>[-+])?\s*"
    r"\s*(?P<quantity>\d+)\s*"
    r"(?P<unit>(second|sec|s|minute|min|month|mo|m|hour|hr|h|day|d|week|w|year|yr|y))s?"
    r"(?P<ago>\s+ago)?\s*",
    re.I
)
TIME_SYMBOL_MAP = {
    "s": "second",
    "sec": "second",
    "m": "minute",
    "min": "minute",
    "h": "hour",
    "hr": "hour",
    "d": "day",
    "w": "week",
    "mo": "month",
    "y": "year",
    "yr": "year",
}
YEAR_FIRST_RE = re.compile(r"\d{4}[^\d]+")
TZ_UTC = tz.tzutc()
TZ_LOCAL = tz.gettz()


def date_parse(value, default_tz=TZ_UTC):
    """Parse a date and return corresponding unix timestamp

    @param value(unicode): date to parse, in any format supported by parser
    @param default_tz(datetime.tzinfo): default timezone
    @return (int): timestamp
    """
    value = str(value).strip()
    dayfirst = False if YEAR_FIRST_RE.match(value) else True

    try:
        dt = default_tzinfo(
            parser.parse(value, dayfirst=dayfirst),
            default_tz)
    except ParserError as e:
        if value == "now":
            dt = datetime.datetime.now(tz.tzutc())
        else:
            try:
                # the date may already be a timestamp
                return int(value)
            except ValueError:
                raise e
    return calendar.timegm(dt.utctimetuple())

def date_parse_ext(value, default_tz=TZ_UTC):
    """Extended date parse which accept relative date

    @param value(unicode): date to parse, in any format supported by parser
        and with the hability to specify X days/weeks/months/years in the past or future.
        Relative date are specified either with something like `[main_date] +1 week`
        or with something like `3 days ago`, and it is case insensitive. [main_date] is
        a date parsable by parser, or empty to specify current date/time.
        "now" can also be used to specify current date/time.
    @param default_tz(datetime.tzinfo): same as for date_parse
    @return (int): timestamp
    """
    m = RELATIVE_RE.match(value)
    if m is None:
        return date_parse(value, default_tz=default_tz)

    if sum(1 for g in ("direction", "in", "ago") if m.group(g)) > 1:
        raise ValueError(
            _('You can use only one of direction (+ or -), "in" and "ago"'))

    if m.group("direction") == '-' or m.group("ago"):
        direction = -1
    else:
        direction = 1

    date = m.group("date")
    if date is not None:
        date = date.strip()
    if not date or date == "now":
        dt = datetime.datetime.now(tz.tzutc())
    else:
        try:
            dt = default_tzinfo(parser.parse(date, dayfirst=True), default_tz)
        except ParserError as e:
            try:
                timestamp = int(date)
            except ValueError:
                raise e
            else:
                dt = datetime.datetime.fromtimestamp(timestamp, tz.tzutc())

    quantity = int(m.group("quantity"))
    unit = m.group("unit").lower()
    try:
        unit = TIME_SYMBOL_MAP[unit]
    except KeyError:
        pass
    delta_kw = {f"{unit}s": direction * quantity}
    dt = dt + relativedelta(**delta_kw)
    return calendar.timegm(dt.utctimetuple())


def date_fmt(timestamp, fmt="short", date_only=False, auto_limit=7, auto_old_fmt="short",
             auto_new_fmt="relative", locale_str=C.DEFAULT_LOCALE, tz_info=TZ_UTC):
    """format date according to locale

    @param timestamp(basestring, float): unix time
    @param fmt(str): one of:
        - short: e.g. u'31/12/17'
        - medium: e.g. u'Apr 1, 2007'
        - long: e.g. u'April 1, 2007'
        - full: e.g. u'Sunday, April 1, 2007'
        - relative: format in relative time
            e.g.: 3 hours
            note that this format is not precise
        - iso: ISO 8601 format
            e.g.: u'2007-04-01T19:53:23Z'
        - auto: use auto_old_fmt if date is older than auto_limit
            else use auto_new_fmt
        - auto_day: shorcut to set auto format with change on day
            old format will be short, and new format will be time only
        or a free value which is passed to babel.dates.format_datetime
        (see http://babel.pocoo.org/en/latest/dates.html?highlight=pattern#pattern-syntax)
    @param date_only(bool): if True, only display date (not datetime)
    @param auto_limit (int): limit in days before using auto_old_fmt
        use 0 to have a limit at last midnight (day change)
    @param auto_old_fmt(unicode): format to use when date is older than limit
    @param auto_new_fmt(unicode): format to use when date is equal to or more recent
        than limit
    @param locale_str(unicode): locale to use (as understood by babel)
    @param tz_info(datetime.tzinfo): time zone to use

    """
    timestamp = float(timestamp)
    if fmt == "auto_day":
        fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm"
    if fmt == "auto":
        if auto_limit == 0:
            now = datetime.datetime.now(tz_info)
            # we want to use given tz_info, so we don't use date() or today()
            today = datetime.datetime(year=now.year, month=now.month, day=now.day,
                                      tzinfo=now.tzinfo)
            today = calendar.timegm(today.utctimetuple())
            if timestamp < today:
                fmt = auto_old_fmt
            else:
                fmt = auto_new_fmt
        else:
            days_delta = (time.time() - timestamp) / 3600
            if days_delta > (auto_limit or 7):
                fmt = auto_old_fmt
            else:
                fmt = auto_new_fmt

    if fmt == "relative":
        delta = timestamp - time.time()
        return dates.format_timedelta(
            delta, granularity="minute", add_direction=True, locale=locale_str
        )
    elif fmt in ("short", "long", "full"):
        if date_only:
            dt = datetime.datetime.fromtimestamp(timestamp, tz_info)
            return dates.format_date(dt, format=fmt, locale=locale_str)
        else:
            return dates.format_datetime(timestamp, format=fmt, locale=locale_str,
                                        tzinfo=tz_info)
    elif fmt == "iso":
        if date_only:
            fmt = "yyyy-MM-dd"
        else:
            fmt = "yyyy-MM-ddTHH:mm:ss'Z'"
        return dates.format_datetime(timestamp, format=fmt)
    else:
        return dates.format_datetime(timestamp, format=fmt, locale=locale_str,
                                     tzinfo=tz_info)


def delta2human(start_ts: Union[float, int], end_ts: Union[float, int]) -> str:
    """Convert delta of 2 unix times to human readable text

    @param start_ts: timestamp of starting time
    @param end_ts: timestamp of ending time
    """
    if end_ts < start_ts:
        raise exceptions.InternalError(
            "end timestamp must be bigger or equal to start timestamp !"
        )
    rd = relativedelta(
        datetime.datetime.fromtimestamp(end_ts),
        datetime.datetime.fromtimestamp(start_ts)
    )
    text_elems = []
    for unit in ("years", "months", "days", "hours", "minutes"):
        value = getattr(rd, unit)
        if value == 1:
            # we remove final "s" when there is only 1
            text_elems.append(f"1 {unit[:-1]}")
        elif value > 1:
            text_elems.append(f"{value} {unit}")

    return ", ".join(text_elems)