@@ -285,6 +285,41 @@ def parse_to_shortdate(date_str):
285285 except :
286286 return ""
287287
288+
289+ class MyTime :
290+ def __init__ (self , t = None ):
291+ import pytz
292+
293+ self .tz = pytz .timezone ('Asia/Tokyo' )
294+
295+ if t is None :
296+ t = datetime .now ()
297+
298+ # Convert now to local timezone and then to JST
299+ self .naive_time = t .replace (tzinfo = None )
300+ self .local_time = t .astimezone ()
301+ self .jst_time = self .local_time .astimezone (self .tz )
302+
303+ def _format_time (self , dt , format_type ):
304+ if format_type == 'obj' :
305+ return dt
306+ elif format_type == "short" :
307+ return dt .strftime ('%y%m%d_%H%M%S' )
308+ elif format_type == "pretty" :
309+ return dt .strftime ('%Y-%m-%d (%a) %H:%M:%S' )
310+ else :
311+ raise ValueError ("Invalid format_type. Choose from 'obj', 'short', 'pretty'." )
312+
313+ def local (self , format_type = "obj" ):
314+ return self ._format_time (self .local_time , format_type )
315+
316+ def jst (self , format_type = "obj" ):
317+ return self ._format_time (self .jst_time , format_type )
318+
319+ def naive (self , format_type = "obj" ):
320+ return self ._format_time (self .naive_time , format_type )
321+
322+ # deprecated, will be removed in the future
288323def get_current_time (now = None ):
289324 # pip install pytz
290325 import pytz
@@ -481,13 +516,13 @@ def requests_retry_session(
481516 session .mount ('https://' , adapter )
482517 return session
483518
484- def get (url , headers = None , cookies = None , encoding = None , session = None , parser = 'lxml' ):
519+ def get (url , headers = None , cookies = None , encoding = None , session = None , parser = 'lxml' , timeout = None ):
485520 # pip install requests, lxml, beautifulsoup4
486521 from bs4 import BeautifulSoup
487522
488523 if not session :
489524 session = requests_retry_session ()
490- r = session .get (url , cookies = cookies , headers = headers )
525+ r = session .get (url , cookies = cookies , headers = headers , timeout = timeout )
491526 if encoding :
492527 return BeautifulSoup (r .content , parser , from_encoding = encoding )
493528 else :
0 commit comments