Coverage for /Users/Dave/git_repos/_packages_/python/fundamentals/fundamentals/times.py : 15%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/local/bin/python
2# encoding: utf-8
3"""
4*Some time functions to be used with logging etc*
6:Author:
7 David Young
9:Date Created:
10 February 26, 2016
11"""
12################# GLOBAL IMPORTS ####################
13from builtins import str
14import sys
15import os
16os.environ['TERM'] = 'vt100'
19def get_now_sql_datetime():
20 """
21 *A datetime stamp in MySQL format: ``YYYY-MM-DDTHH:MM:SS``*
23 **Return:**
24 - ``now`` -- current time and date in MySQL format
26 **Usage:**
27 .. code-block:: python
29 from fundamentals import times
30 now = times.get_now_sql_datetime()
31 print now
33 # OUT: 2016-03-18T11:08:23
34 """
35 ## > IMPORTS ##
36 from datetime import datetime, date, time
37 now = datetime.now()
38 now = now.strftime("%Y-%m-%dT%H:%M:%S")
40 return now
43def datetime_relative_to_now(date):
44 """
45 *convert date to a relative datetime (e.g. +15m, +2hr, +1w)*
47 **Key Arguments:**
48 - ``date`` -- absolute date
50 **Return:**
51 - a relative date
53 **Usage:**
54 .. code-block:: python
56 from fundamentals import times
57 relTime = times.datetime_relative_to_now(date)
58 """
59 from datetime import datetime
60 diff = datetime.now() - date
61 s = diff.seconds
62 if diff.days == 1:
63 return ' + 1d'
64 elif diff.days > 1:
65 return ' +{0}d'.format(diff.days)
66 elif s <= 1:
67 return ' just now'
68 elif s < 60:
69 return ' +{0}sec'.format(s)
70 elif s < 120:
71 return ' +1min'
72 elif s < 3600:
73 return ' +{0}min'.format(s / 60)
74 elif s < 7200:
75 return ' +1hr'
76 else:
77 return ' +{0}hr'.format(s / 3600)
80def calculate_time_difference(startDate, endDate):
81 """
82 *Return the time difference between two dates as a string*
84 **Key Arguments:**
85 - ``startDate`` -- the first date in YYYY-MM-DDTHH:MM:SS format
86 - ``endDate`` -- the final date YYYY-MM-DDTHH:MM:SS format
88 **Return:**
89 - ``relTime`` -- the difference between the two dates in Y,M,D,h,m,s (string)
91 **Usage:**
92 .. code-block:: python
94 from fundamentals import times
95 diff = times.calculate_time_difference(startDate="2015-10-13 10:02:12", endDate="2017-11-04 16:47:05")
96 print diff
98 # OUT: 2yrs 22dys 6h 44m 53s
99 """
100 ################ > IMPORTS ################
101 from datetime import datetime
102 from dateutil import relativedelta
104 ################ > VARIABLE SETTINGS ######
106 ################ >ACTION(S) ################
107 if "T" not in startDate:
108 startDate = startDate.strip().replace(" ", "T")
109 if "T" not in endDate:
110 endDate = endDate.strip().replace(" ", "T")
111 startDate = datetime.strptime(startDate, '%Y-%m-%dT%H:%M:%S')
112 endDate = datetime.strptime(endDate, '%Y-%m-%dT%H:%M:%S')
113 d = relativedelta.relativedelta(endDate, startDate)
115 relTime = ""
116 if d.years > 0:
117 relTime += str(d.years) + "yrs "
118 if d.months > 0:
119 relTime += str(d.months) + "mths "
120 if d.days > 0:
121 relTime += str(d.days) + "dys "
122 if d.hours > 0:
123 relTime += str(d.hours) + "h "
124 if d.minutes > 0:
125 relTime += str(d.minutes) + "m "
126 if d.seconds > 0:
127 relTime += str(d.seconds) + "s"
128 ###############################
130 if relTime == "":
131 relTime = "0s"
133 return relTime