Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[suggestion] i wanted an option to specify a date #19

Open
luffah opened this issue Feb 17, 2021 · 0 comments
Open

[suggestion] i wanted an option to specify a date #19

luffah opened this issue Feb 17, 2021 · 0 comments

Comments

@luffah
Copy link

luffah commented Feb 17, 2021

A little suggestion to specify a start date (instead of now).
Too, in python, code is clear enough not to comment every lines.

ヽ(´ー`)┌
--- trainline_cli.py	2021-02-17 18:40:51.312990416 +0100
+++ trainline_cli.py	2021-02-17 19:17:46.396430200 +0100
@@ -4,9 +4,16 @@
 """CLI tool for trainline."""
 import click
 import trainline
+import re
 from datetime import datetime, timedelta
 
 # Usage : trainline_cli.py --help
+DATE_FORMAT="%d/%m/%Y %H:%M"
+KNOWN_DATE_FORMATS=list(set([
+    DATE_FORMAT,
+    "%d/%m/%Y", "%d/%m/%Y %H:%M",
+    '%Y-%m-%d', '%Y-%m-%d %H:%M'
+]))
 
 
 @click.command()
@@ -24,9 +31,16 @@
     required=True,
 )
 @click.option(
+    '--start', '-s',
+    type=click.DateTime(formats=KNOWN_DATE_FORMATS),
+    help='start date',
+    default=datetime.now().strftime(DATE_FORMAT),
+    show_default=True,
+)
+@click.option(
     '--next', '-n',
     type=str,
-    help='period of search from now \
+    help='period of search from start date \
 (example : 1day, 2days, 3d, 1hour, 2hours, 3h)',
     default='3hours',
     show_default=True,
@@ -43,21 +57,16 @@
     is_flag=True,
     help='verbose mode',
 )
-def main(departure, arrival, next, transport, verbose):
+def main(departure, arrival, start, next, transport, verbose):
     """ Search trips with Trainline and returns it in csv """
 
-    # Get current datetime > from_date
-    from_date_obj = datetime.now()
-
-    # Decode duration (ex : 1day => timedelta(days=1))
+    from_date = start
     delta = _decode_next_param(next)
-
-    # Calculate the end date > to_date
-    to_date_obj = from_date_obj + delta
+    to_date = from_date + delta
 
     # Convert the datetime objects to strings
-    from_date = from_date_obj.strftime("%d/%m/%Y %H:%M")
-    to_date = to_date_obj.strftime("%d/%m/%Y %H:%M")
+    from_date = from_date.strftime(DATE_FORMAT)
+    to_date = to_date.strftime(DATE_FORMAT)
 
     if transport == "any":
         transport = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant