Python pendulum.from_format() Examples
The following are 19
code examples of pendulum.from_format().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pendulum
, or try the search function
.
Example #1
Source File: plugin.py From limnoria-plugins with Do What The F*ck You Want To Public License | 6 votes |
def _parseDateInput(cls, date): """Verify that the given string is a valid date formatted as YYYY-MM-DD. Also, the API seems to go back until 2014-10-04, so we will check that the input is not a date earlier than that. In case of failure, throws a ValueError exception. """ date = date.lower() if date in cls._FUZZY_DAYS: date = cls._EnglishDateToDate(date) elif date.replace("-", "").isdigit(): try: parsed_date = pendulum.from_format(date, "YYYY-MM-DD") except: raise ValueError("Incorrect date format, should be YYYY-MM-DD") # The current API goes back until 2014-10-04. Is it in range? if parsed_date < pendulum.datetime(2014, 10, 4): raise ValueError("I can only go back until 2014-10-04") else: raise ValueError("Date is not valid") return cls._stripDateSeparators(date)
Example #2
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_with_timezone(): d = pendulum.from_format( "1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss", tz=pendulum.timezone("Europe/London"), ) assert_datetime(d, 1975, 5, 21, 22, 32, 11) assert "Europe/London" == d.timezone_name
Example #3
Source File: parser.py From shadowreader with Apache License 2.0 | 5 votes |
def parse_file(file: str, app: str, bucket: str, timeformat: str, regex: str): with open(file) as f: lines = f.readlines() regex = re.compile(regex) lines = [x.strip() for x in lines if x.strip()] lines = [regex.match(x) for x in lines if x] lines = [x.groupdict() for x in lines if x] def parse_time(t): if timeformat: return pendulum.from_format(t, timeformat) else: return pendulum.parse(t) if not lines: click.echo( f"No logs were parsed for {file}. Could the RegEx be wrong or the file empty?" ) return tzinfo = pendulum.tz.local_timezone() if lines: first = lines[0] inst = parse_time(first[timestamp_field]) tzinfo = inst.tzinfo for l in lines: l[timestamp_field] = parse_time(l[timestamp_field]) payload = {app: defaultdict(list)} payload = _batch_lines_by_timestamp(lines, payload, app) click.echo(f"Processing done. Pushing results to S3...") parse_results = _put_payload_on_s3( payload=payload[app], bucket=bucket, elb_name=app, testing={} ) print_stats(parse_results, tzinfo, app)
Example #4
Source File: types.py From DeleteFB with MIT License | 5 votes |
def convert_date(text): """ Tries to parse a date into a DateTime instance Returns `None` if it cannot be parsed """ try: return pendulum.from_format(text, "DD/M/YYYY") except ValueError: try: return (pendulum.from_format(text, "DD MMM") .set(year=pendulum.now().year)) except ValueError: return None # Data type definitions of posts and comments
Example #5
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_error(text, fmt, locale): now = pendulum.datetime(2018, 2, 2) with pendulum.test(now): with pytest.raises(ValueError): pendulum.from_format(text, fmt, locale=locale)
Example #6
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format(text, fmt, expected, now): if now is None: now = pendulum.datetime(2015, 11, 12) else: now = pendulum.parse(now) # Python 2.7 loses precision for x timestamps # so we don't test if fmt == "x" and PY2: return with pendulum.test(now): assert pendulum.from_format(text, fmt).isoformat() == expected
Example #7
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_with_invalid_padded_day(): with pytest.raises(ValueError): pendulum.from_format("Apr 2 12:00:00 2020 GMT", "MMM DD HH:mm:ss YYYY z")
Example #8
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_with_padded_day(): d = pendulum.from_format("Apr 2 12:00:00 2020 GMT", "MMM DD HH:mm:ss YYYY z") assert_datetime(d, 2020, 4, 2, 12)
Example #9
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_with_millis(): d = pendulum.from_format("1975-05-21 22:32:11.123456", "YYYY-MM-DD HH:mm:ss.SSSSSS") assert_datetime(d, 1975, 5, 21, 22, 32, 11, 123456)
Example #10
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_with_escaped_elements_valid_tokens(): d = pendulum.from_format("1975-05-21T22:32:11.123Z", "YYYY-MM-DD[T]HH:mm:ss.SSS[Z]") assert_datetime(d, 1975, 5, 21, 22, 32, 11) assert "UTC" == d.timezone_name
Example #11
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_with_square_bracket_in_timezone(): with pytest.raises(ValueError, match="^String does not match format"): pendulum.from_format( "1975-05-21 22:32:11 Eu[rope/London", "YYYY-MM-DD HH:mm:ss z", )
Example #12
Source File: main.py From performance_tracker with GNU General Public License v3.0 | 5 votes |
def main(command, datetime=None): ctx = Context() if datetime is None: datetime = pendulum.now(ctx.config["TIMEZONE"]) else: try: datetime = pendulum.from_format(datetime, 'YYYY-MM-DD', tz=ctx.config["TIMEZONE"]) ctx.logger(f"Running in date override mode with date {datetime.format('YYYY-MM-DD')}.") ctx.logger("This is an experimental feature and may generate incorrect data.") except: ctx.logger("Failed to parse custom datetime argument") return 1 try: outcome = ACTIONS[command](ctx, datetime) except Exception as exc: ctx.logger(exc) traceback.print_tb(exc.__traceback__) ctx.logger(f"{command} failed") return 1 if outcome is 0: ctx.logger(f"{command} completed successfully") return 0 else: ctx.logger(f"{command} failed") return 1
Example #13
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_with_timezone_string(): d = pendulum.from_format( "1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss", tz="Europe/London" ) assert_datetime(d, 1975, 5, 21, 22, 32, 11) assert "Europe/London" == d.timezone_name
Example #14
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_rejects_extra_text(): with pytest.raises(ValueError): pendulum.from_format("1975-05-21 22:32:11 extra text", "YYYY-MM-DD HH:mm:ss")
Example #15
Source File: test_from_format.py From pendulum with MIT License | 5 votes |
def test_from_format_returns_datetime(): d = pendulum.from_format("1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss") assert_datetime(d, 1975, 5, 21, 22, 32, 11) assert isinstance(d, pendulum.DateTime) assert "UTC" == d.timezone_name
Example #16
Source File: calendar.py From performance_tracker with GNU General Public License v3.0 | 5 votes |
def services_running_on(self, datestring): within_range = self.on_date(datestring) date = pendulum.from_format(datestring, "YYYY-MM-DD") day = date.format("dddd").lower() services = within_range[within_range[day] == 1] if len(services.index) is 0: # This can apply on public holidays # The day flag can be incorrect so we # instead just look for services with a # start and end time exactly on the date services = self.exactly_on_date(datestring) return services
Example #17
Source File: datetimefs.py From performance_tracker with GNU General Public License v3.0 | 5 votes |
def extract_time(path_to_dt, full_path, tz="UTC"): string_to_match = f"(?:{path_to_dt}/)([0-9]+:[0-9]+:[0-9]+)" dateinfo = re.match(string_to_match, full_path).groups() return pendulum.from_format(dateinfo[0], "HH:mm:ss", tz=tz)
Example #18
Source File: datetimefs.py From performance_tracker with GNU General Public License v3.0 | 5 votes |
def extract_date(path_to_dt, full_path, tz="UTC"): string_to_match = f"(?:{path_to_dt}/)([0-9]+-[0-9]+-[0-9]+)" dateinfo = re.match(string_to_match, full_path).groups() return pendulum.from_format(dateinfo[0], "YYYY-MM-DD", tz=tz)
Example #19
Source File: datetimefs.py From performance_tracker with GNU General Public License v3.0 | 5 votes |
def extract_datetime(path_to_dt, full_path, tz="UTC"): string_to_match = f"(?:{path_to_dt}/)([0-9]+-[0-9]+-[0-9]+)/([0-9]+:[0-9]+:[0-9]+)" dateinfo = re.match(string_to_match, full_path).groups() return pendulum.from_format( dateinfo[0] + "T" + dateinfo[1], "YYYY-MM-DDTHH:mm:ss", tz=tz )