Package: ph-utils
ph-utilsInstallpipinstallph-utilsThe python3 tool classes. Includes the following modules:config_utils: Configure relevant tool classes,load_envdate_utils: The date processing tool,parse、format、set、start_of、end_of、add、sub、diff、timestamp1.config_utilsConfigure relevant tool classes.Usagefromph_utils.config_utilsimportload_env1. load_env(file_dir, env_files): dict:Load the environment variable file, the content behind the list will replace the content in front.file_dir- The environment variable file folder, default:os.cwd();env_files- The environment variable file list, ifNoneor empty list load['.env', '.env.local', '.env.development', '.env.production']2. load_json(file_dir, json_files): dict:Load json file. default:['config.json']3. load_ini(file_dir, ini_files): dict:Loadinifile. default:[config.ini]2.date-utilsThe date processing tool.Usageimportph_utils.date_utilsasdate_utilsfromph_utils.date_utilsimportparse,format1. parse(date_data: Any, fmt=None): datetimeYou can parse various data formats into date objects, including time stamps, strings, and date objects themselves. Returndatetimeobject.parse timestampdate_utils.parse(1691997308)# 2023-08-14 15:15:08parse stringsdate_utils.parse('2023-08-14 15:23:23')# 2023-08-14 15:23:23date_utils.parse('20230814 152323')# 2023-08-14 15:23:23date_utils.parse('2023/08/14 15:23:23','%Y/%m/%d%H:%M:%S')# 2023-08-14 15:23:23parseNoneobjectdate_utils.parse()# 2023-08-14 15:15:23.830691date_utils.parse(None)# 2023-08-14 15:15:23.830691parsedatetimedate_utils.parse(date_utils.parse())# 2023-08-14 15:19:48.3828712. format(ori_date, pattern): strDate formatting is the process of converting a date to a specific format.Parameter description:ori_date:optionalAll parameters that can be supported by theparsefunction.pattern:optionaldefault: %Y-%m-%d, eg:%Y-%m-%d %H:%M:%Sdate_utils.format(None,'%Y-%m-%d%H:%M:%S')date_utils.format(1691997308,'%Y-%m-%d%H:%M:%S')3. start_of(ori_date, unit, __format): datetime | intSet the start of a time at a certain moment.Paramater description:unit:optionaldefault: date,date- start time of a day__format:optionaldefault: None, set the returned data,None- returndatetime,s、msreturn timestampstart_of()# datetime - 2023-08-15 00:00:00start_of(__format='s')# int - 16920288004. end_of(ori_date, unit, __format): datetime | intSet the end of a time at a certain moment.end_of()# datetime - 2023-08-15 23:59:595. timestamp(ori_date, unit): intGet timestamp of a timeunit:s-defaultlength: 10，accurate to second、mslength: 13, accurate to milliseconds.timestamp()6. set(ori_date, values): datetimeSet date values to a given date.Sets time values to date from object . A value is not set if it is undefined or null or doesn't exist in values.values:str | dict，the given date. If isdict, include the follow property.year、month、day、hour、minute、secondset(None,'2023-08-15 14:49:49')set(None,'2023/08/15 14:49:49')set(None,'20230815 144949')set(None,'20230815')set(None,'144949')set(None,{'year':2023,'month':8,'day':15,'hour':14,'minute':49,'second':49})set(None,{'year':2023,'month':8,'day':15})set(None,{'hour':14,'minute':49,'second':49})7. add(ori_date, duration): datetimeAdd the specified years, months, weeks, days, hours, minutes and seconds to the given date.durationthe object with years, months, weeks, days, hours, minutes and seconds to be added.add(duration={'years':2,'months':9,'days':7,'hours':5,'minutes':9,'seconds':30,})8. subtract(ori_date, duration): datetimeSubtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.subtract(duration={'years':2,'months':9,'days':7,'hours':5,'minutes':9,'seconds':30,})9. sub(ori_date, duration): datetimeAlias ofsubtract.10: diff(start, end, result): int | timedeltaGet the number of full day periods between two dates.result: set the returned data. ifresult = days- returnint, difference in days; otherwise returntimedelta. default:daysdiff(parse(),parse())# => 0