10 examples of 'get time from datetime python' in Python

Every line of 'get time from datetime python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
37def time_to_datetime(t):
38 return _combine_date_time(date.today(), t)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

50def to_datetime(timestring):
51 """Convert one of the bitbucket API's timestamps to a datetime object."""
52 format = '%Y-%m-%d %H:%M:%S'
53 return datetime.datetime(*time.strptime(timestring, format)[:7])
5def parse_datetime(datetime_str):
6 if datetime_str:
7 return datetime.strptime(datetime_str, '%Y-%m-%d %H:%M')
15def parse_datetime(time_string):
16 # Set locale for date parsing
17 utc_offset_str = time_string[-6:].strip()
18 sign = 1
19
20 if utc_offset_str[0] == '-':
21 sign = -1
22 utc_offset_str = utc_offset_str[1:5]
23
24 utcoffset = sign * timedelta(hours=int(utc_offset_str[0:2]),
25 minutes=int(utc_offset_str[2:4]))
26
27 return datetime.strptime(time_string[:-6], '%a, %d %b %Y %H:%M:%S') - utcoffset
117def get_time(data=None):
118 """Receive a dictionary or a string and return a datatime instance.
119
120 data = {"year": 2006,
121 "month": 11,
122 "day": 21,
123 "hour": 16,
124 "minute": 30 ,
125 "second": 00}
126
127 or
128
129 data = "21/11/06 16:30:00"
130
131 2018-04-17T17:13:50Z
132
133 Args:
134 data (str, dict): python dict or string to be converted to datetime
135
136 Returns:
137 datetime: datetime instance.
138
139 """
140 if isinstance(data, str):
141 date = datetime.strptime(data, "%Y-%m-%dT%H:%M:%S")
142 elif isinstance(data, dict):
143 date = datetime(**data)
144 else:
145 return None
146 return date.replace(tzinfo=timezone.utc)
36def datetime_to_timestamp(from_datetime):
37 return time.mktime(from_datetime.timetuple())
66def fromutc(self, dt):
67 assert dt.tzinfo is self
68 stamp = (dt - datetime(1970, 1, 1, tzinfo=self)) // SECOND
69 args = time.localtime(stamp)[:6]
70 dst_diff = DSTDIFF // SECOND
71 # Detect fold
72 fold = (args == time.localtime(stamp - dst_diff))
73 return datetime(*args, microsecond=dt.microsecond,
74 tzinfo=self, fold=fold)
8def convert_to_datetime(datetime_str):
9 tweet_datetime = datetime.strptime(datetime_str,'%a %b %d %H:%M:%S %z %Y')
10
11 return(tweet_datetime)
255def getTimeTodayForLocal(hour=0, minute=0, second=0, microsecond=0):
256 return getTimeForLocal(getCurrentTimestamp(), hour, minute, second, microsecond)
103def get_utc_time(self,rawtime,fmt='datenum'):
104 utc = time.strptime(rawtime,'%m/%d/%Y (%H:%M)')
105 if fmt == 'datenum':
106 utc = calendar.timegm(utc)
107 return utc

Related snippets