<commit_before>from ..base import *
from .time_helpers import *

TZ_OFFSET = (time.altzone // 3600)
ABS_OFFSET = abs(TZ_OFFSET)
TZ_NAME = time.tzname[1]
ISO_FORMAT = '%s-%s-%sT%s:%s:%s.%sZ'


@Js
def Date(year, month, date, hours, minutes, seconds, ms):
    return now().to_string()


Date.Class = 'Date'


def now():
    return PyJsDate(int(time.time() * 1000), prototype=DatePrototype)


@Js
def UTC(year, month, date, hours, minutes, seconds, ms):  # todo complete this
    args = arguments
    y = args[0].to_number()
    m = args[1].to_number()
    l = len(args)
    dt = args[2].to_number() if l > 2 else Js(1)
    h = args[3].to_number() if l > 3 else Js(0)
    mi = args[4].to_number() if l > 4 else Js(0)
    sec = args[5].to_number() if l > 5 else Js(0)
    mili = args[6].to_number() if l > 6 else Js(0)
    if not y.is_nan() and 0 <= y.value <= 99:
        y = y + Js(1900)
    return TimeClip(MakeDate(MakeDay(y, m, dt), MakeTime(h, mi, sec, mili)))


@Js
def parse(string):
    return PyJsDate(
        TimeClip(parse_date(string.to_string().value)),
        prototype=DatePrototype)


Date.define_own_property('now', {
    'value': Js(now),
    'enumerable': False,
    'writable': True,
    'configurable': True
})

Date.define_own_property('parse', {
    'value': parse,
    'enumerable': False,
    'writable': True,
    'configurable': True
})

Date.define_own_property('UTC', {
    'value': UTC,
    'enumerable': False,
    'writable': True,
    'configurable': True
})


class PyJsDate(PyJs):
    Class = 'Date'
    extensible = True

    def __init__(self, value, prototype=None):
        self.value = value
        self.own = {}
        self.prototype = prototype

    # todo fix this problematic datetime part
    def to_local_dt(self):
        return datetime.datetime(1970, 1, 1) + datetime.timedelta(
            seconds=UTCToLocal(self.value) // 1000)

    def to_utc_dt(self):
        return datetime.datetime(1970, 1, 1) + datetime.timedelta(
            seconds=self.value // 1000)

    def local_strftime(self, pattern):
        if self.value is NaN:
            return 'Invalid Date'
        try:
            dt = self.to_local_dt()
        except:
            raise MakeError(
                'TypeError',
                'unsupported date range. Will fix in future versions')
        try:
            return dt.strftime(pattern)
        except:
            raise MakeError(
                'TypeError',
                'Could not generate date string from this date (limitations of python.datetime)'
            )

    def utc_strftime(self, pattern):
        if self.value is NaN:
            return 'Invalid Date'
        try:
            dt = self.to_utc_dt()
        except:
            raise MakeError(
                'TypeError',
                'unsupported date range. Will fix in future versions')
        try:
            return dt.strftime(pattern)
        except:
            raise MakeError(
                'TypeError',
                'Could not generate date string from this date (limitations of python.datetime)'
            )


def parse_date(py_string):  # todo support all date string formats
    try:
        try:
            dt = datetime.datetime.strptime(py_string, "%Y-%m-%dT%H:%M:%S.%fZ")
        except:
            dt = datetime.datetime.strptime(py_string, "%Y-%m-%dT%H:%M:%SZ")
        return MakeDate(
            MakeDay(Js(dt.year), Js(dt.month - 1), Js(dt.day)),
            MakeTime(
                Js(dt.hour), Js(dt.minute), Js(dt.second),
                Js(dt.microsecond // 1000)))
    except:
        raise MakeError(
            'TypeError',
            'Could not parse date %s - unsupported date format. Currently only supported format is RFC3339 utc. Sorry!'
            % py_string)


def date_constructor(*args):
    if len(args) >= 2:
        return date_constructor2(*args)
    elif len(args) == 1:
        return date_constructor1(args[0])
    else:
        return date_constructor0()


def date_constructor0():
    return now()


def date_constructor1(value):
    v = value.to_primitive()
    if v._type() == 'String':<commit_msg>Adds basic support for JS input date formats<commit_after>from ..base import *
from .time_helpers import *

TZ_OFFSET = (time.altzone // 3600)
ABS_OFFSET = abs(TZ_OFFSET)
TZ_NAME = time.tzname[1]
ISO_FORMAT = '%s-%s-%sT%s:%s:%s.%sZ'


@Js
def Date(year, month, date, hours, minutes, seconds, ms):
    return now().to_string()


Date.Class = 'Date'


def now():
    return PyJsDate(int(time.time() * 1000), prototype=DatePrototype)


@Js
def UTC(year, month, date, hours, minutes, seconds, ms):  # todo complete this
    args = arguments
    y = args[0].to_number()
    m = args[1].to_number()
    l = len(args)
    dt = args[2].to_number() if l > 2 else Js(1)
    h = args[3].to_number() if l > 3 else Js(0)
    mi = args[4].to_number() if l > 4 else Js(0)
    sec = args[5].to_number() if l > 5 else Js(0)
    mili = args[6].to_number() if l > 6 else Js(0)
    if not y.is_nan() and 0 <= y.value <= 99:
        y = y + Js(1900)
    return TimeClip(MakeDate(MakeDay(y, m, dt), MakeTime(h, mi, sec, mili)))


@Js
def parse(string):
    return PyJsDate(
        TimeClip(parse_date(string.to_string().value)),
        prototype=DatePrototype)


Date.define_own_property('now', {
    'value': Js(now),
    'enumerable': False,
    'writable': True,
    'configurable': True
})

Date.define_own_property('parse', {
    'value': parse,
    'enumerable': False,
    'writable': True,
    'configurable': True
})

Date.define_own_property('UTC', {
    'value': UTC,
    'enumerable': False,
    'writable': True,
    'configurable': True
})


class PyJsDate(PyJs):
    Class = 'Date'
    extensible = True

    def __init__(self, value, prototype=None):
        self.value = value
        self.own = {}
        self.prototype = prototype

    # todo fix this problematic datetime part
    def to_local_dt(self):
        return datetime.datetime(1970, 1, 1) + datetime.timedelta(
            seconds=UTCToLocal(self.value) // 1000)

    def to_utc_dt(self):
        return datetime.datetime(1970, 1, 1) + datetime.timedelta(
            seconds=self.value // 1000)

    def local_strftime(self, pattern):
        if self.value is NaN:
            return 'Invalid Date'
        try:
            dt = self.to_local_dt()
        except:
            raise MakeError(
                'TypeError',
                'unsupported date range. Will fix in future versions')
        try:
            return dt.strftime(pattern)
        except:
            raise MakeError(
                'TypeError',
                'Could not generate date string from this date (limitations of python.datetime)'
            )

    def utc_strftime(self, pattern):
        if self.value is NaN:
            return 'Invalid Date'
        try:
            dt = self.to_utc_dt()
        except:
            raise MakeError(
                'TypeError',
                'unsupported date range. Will fix in future versions')
        try:
            return dt.strftime(pattern)
        except:
            raise MakeError(
                'TypeError',
                'Could not generate date string from this date (limitations of python.datetime)'
            )


def parse_date(py_string):  # todo support all date string formats
    supported_formats = (
        "%Y-%m-%dT%H:%M:%S.%fZ",
        "%Y-%m-%dT%H:%M:%SZ",
        "%Y-%m-%d",
        "%m/%d/%Y",
        "%b %d %Y",
    )
    for date_format in supported_formats:
        try:
            dt = datetime.datetime.strptime(py_string, date_format)
        except ValueError:
            continue
        else:
            return MakeDate(
                MakeDay(Js(dt.year), Js(dt.month - 1), Js(dt.day)),
                MakeTime(
                    Js(dt.hour), Js(dt.minute), Js(dt.second),
                    Js(dt.microsecond // 1000)))

    raise MakeError(
        'TypeError',
        'Could not parse date %s - unsupported date format. Currently only supported formats are RFC3339 utc, ISO Date, Short Date, and Long Date. Sorry!'
        % py_string)


def date_constructor(*args):
    if len(args) >= 2:
        return date_constructor2(*args)
    elif len(args) == 1:
        return date_constructor1(args[0])
    else:
        return date_constructor0()


def date_constructor0():
    return now()


def date_constructor1(value):
    v = value.to_primitive()
    if v._type() == 'String':
{'fraction_non_alphanumeric': 0.12665719696969696, 'fraction_numerical': 0.016571969696969696, 'mean_word_length': 2.1763157894736844, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5872694', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2662, 'n_words': 707}