10 examples of 'datetime to int python' in Python

Every line of 'datetime to int 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
87def time_as_int(date, utc=False):
88 """
89 Converts a date or datetime object to a unixtimestamp. ``utc=True``
90 interprets the input as a UTC based timestamp.
91 """
92 t = date.timetuple()
93 return int(timegm(t) if utc else time.mktime(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

402def datetime_to_timestamp(dt):
403 """Converts a datetime object to UTC timestamp"""
404 return int(utc_mktime(dt.timetuple()))
263def pyCalendarToSQLTimestamp(pydt):
264
265 if pydt.isDateOnly():
266 return date(year=pydt.getYear(), month=pydt.getMonth(), day=pydt.getDay())
267 else:
268 return datetime(
269 year=pydt.getYear(),
270 month=pydt.getMonth(),
271 day=pydt.getDay(),
272 hour=pydt.getHours(),
273 minute=pydt.getMinutes(),
274 second=pydt.getSeconds(),
275 tzinfo=None
276 )
82def str_to_dt(date):
83 ''' Converts Yahoo Date Strings to datetime objects. '''
84 yr = int(date[0:4])
85 mo = int(date[5:7])
86 day = int(date[8:10])
87 return datetime.datetime(yr,mo, day)
94def to_time_int(time):
95 if isinstance(time, str):
96 t = dt.datetime.strptime(time, "%H:%M:%S")
97 time_int = t.hour * 10000 + t.minute * 100 + t.second
98 return time_int
99 elif isinstance(time, (int, np.integer)):
100 return time
101 else:
102 return -1
11def to_datetime(timestamp):
12 """Return datetime object from timestamp."""
13 return dt.fromtimestamp(time.mktime(
14 time.localtime(int(str(timestamp)[:10]))))
54def totimestamp(inputdate, epoch=datetime(1970,1,1)):
55 dt = datetime.strptime(inputdate, '%Y-%m-%d')
56 td = dt - epoch
57 timestamp = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 1e6 # td.total_seconds()
58 return int(timestamp)
98def string_to_datetime(string):
99 """
100 Takes a string formatted as follows yyyymmddhhmmss and returns a datetime.datetime object.
101 """
102 year=int(string[0:4])
103 month=int(string[4:6])
104 day=int(string[6:8])
105 hour=int(string[8:10])
106 min=int(string[10:12])
107 sec=int(string[12:14])
108
109 return datetime.datetime(year,month,day,hour,min,sec)
42def datetime2int(dt):
43 # represents a datetime as microseconds since the epoch (i think this may assume UTC if the datetime doesn't specify?)
44 return (datetime64(dt) - datetime64(0, 'us')).astype(int)
37def time_to_datetime(t):
38 return _combine_date_time(date.today(), t)

Related snippets