python - Difference between two unix timestamps - python-2.7

I have two timestamps in miliseconds and i want to compute the difference between the two in minutes:
d1 = 1502053449617
current_time_utc = int(round(time.time() * 1000))
The values for d1 are dynamically generated by a third party API and are in UTC . I am trying to get the difference between the current time in UTC and d1.
fmt = '%Y-%m-%d %H:%M:%S'
time1 = datetime.strptime(d1, fmt)
time2 = datetime.strptime(current_time_utc, fmt)
I want to be able to find the difference between the two (time1 - time2) . If i do the below , i get an error saying "string expected, long given"
print( time1-time2)
I want the difference between the two in minutes . Please help

You don't need to format the string, you just need to convert the timestamp directly, by first dividing it by 1000. Then its just a matter of printing out the differences (and calculating it in minutes):
from __future__ import division
import datetime
d1 = 1502053449617
converted_d1 = datetime.datetime.fromtimestamp(round(d1 / 1000))
current_time_utc = datetime.datetime.utcnow()
print((current_time_utc - converted_d1))
print((current_time_utc - converted_d1).total_seconds() / 60)
The above prints:
3 days, 5:08:14.087515
4628.234791916667

I had to calculate the difference between two unix timestamps - but in days, as follows:
create two unix timestamps:
import datetime
timestamp1 = datetime.datetime(2017, 12, 1).strftime('%s')
timestamp2 = datetime.datetime(2017, 11, 14).strftime('%s')
print(timestamp1)
print(timestamp2)
1512079200
1510610400
calculate the day difference:
print((float(timestamp1)-float(timestamp2))/(60*60*24))
output:
17.0

Related

Subtracting two datetimes from each other and returning in HH:MM:SS in python 2.7

I have two date-time strings that I wish to subtract from each other and want to return a answer in
the HH:MM:SS format using python 2.7. For Example I have "2019-01-22 10:46:34" and "2019-01-22 10:30:34" and want it to return something like this 00:16:00. I have tried converting the times to integers but can't seem to convert back. I have also tried the datetime module. Below is a rudimentary example of something I tried that I wish to convert into a function.
from datetime import datetime
a = "2019-01-22 10:46:34"
b = "2019-01-22 10:30:25"
c = a-b
print(datetime.time(c, '%Y-%m-%d %H:%M:%S')
I have been working on this problem for a few days so any help would be much appreciated.
Take a look at the timedelta Object.
With that you can get the difference of two datetime Objects in Seconds and then you can calc the Minutes Hours etc.
Example you have two datetime objects a and b, c will be the timedelta object:
import datetime
# datetime object (year, month, day, hour, minute, second)
a = datetime.datetime.strptime("2019-01-22 10:46:34", "%Y-%m-%d %H:%M:%S")
b = datetime.datetime.strptime("2019-01-22 10:30:25", "%Y-%m-%d %H:%M:%S")
# returns timedelta object
c = a-b
print('Difference: ', c)
# return (minutes, seconds)
minutes = divmod(c.seconds, 60)
print('Difference in minutes: ', minutes[0], 'minutes',
minutes[1], 'seconds')
EDIT: divmod() is used to get batter result in terms of minutes and seconds, but you could also just write
minutes = c.seconds / 60
print('Difference in minutes: ', minutes)
Output: Difference in minutes: 16 minutes 9 seconds

datetime strptime method for format HH:MM:SS.MICROSECOND

I'm trying to investigate the Python time striptime method to decompose a time represented as 11:49:57.74. The standard %H, %M, %S are able to decompose the hour , minute , second. However, since the data is a string ( which is taken in python pandas column as datatype object, the Milliseconds after the decimal second is left uninterpreted. Hence, I get an error. Could someone please advise how to parse the example so that the seconds and microseconds are correctly interpreted from the time string ?
I would then use them to find the time delta between two time stamps.
I don't know if I had correctly understood your question.
So, to convert that string time to datetime and calculate the timedelta between two times you need to do as follow:
timedelta = str() #declare an empty string where save the timedelta
my_string = '11:49:57.74' # first example time
another_example_time = '13:49:57.74' #second example time, invented by me for the example
first_time = datetime.strptime(my_string, "%H:%M:%S.%f") # extract the first time
second_time = datetime.strptime(another_example_time , "%H:%M:%S.%f") # extract the second time
#calculate the time delta
if(first_time > second_time):
timedelta = first_time - second_time
else:
timedelta = second_time - first_time
print "The timedelta between %s and %s is: %s" % (first_time, second_time, timedelta)
Here obviusly you don't have any date, so the datetime library as default use 1900-01-01 as you can see in the result of the print:
The timedelta between 1900-01-01 11:49:57.740000 and 1900-01-01 13:49:57.740000 is: 2:00:00
I hope this solution is what you need. Next time provide a little bit more information please, or share an example with the code that you have tried to write.

Python - Subtracting a specific date-time from current GMT date-time

I'm trying to do some time math, but I'm not sure how I could do this. I'd like to subtract a specific date & time I have in a string (e.g.: 15:54:00 2017-5-20) from current GMT time (e.g: 20:06:27 2017-12-22).
Any thoughts on how I could do this?
# import data into Python
with open(output_file2) as f:
reader = csv.reader(f, delimiter="\t")
d = list(reader)
# here, objects [0][4] and [0][5] would be, for instance: 15:54:00 and 2017-5-20
# , respectively
# UTC Time
os.system("date -u \"+%H:%M:%S %Y-%m-%d\" | gawk '{print \" UTC Date & Time: \", $1, \"\", $2}'")
# eg.: 20:06:27 2017-12-22
Any thoughts would be great! Thanks =)
Update: I've tried so far:
UTC_time = datetime.datetime.utcnow().strftime("%H:%M:%S %Y-%m-%d")
print ' UTC Date & Time: ', UTC_time
time1 = d[0][4]
date1 = d[0][5]
mytime = time1, date1
time_difference = datetime.datetime.utcnow() - mytime
print "HELLO", time_difference
but I keep getting an error:
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'tuple'
Not sure what I am doing wrong...
Your mytime variable is not a datetime.datetime object and cannot be used to operate against one. Instead of mytime = time1, date1 you are looking for something more along the lines of
my_date_str = "{} {}".format(time1, date1)
mytime = datetime.datetime.strptime(my_date_str, "%H:%M:%S %Y-%m-%d")
Do realize that in spite of using utcnow() the resulting datetime.datetime object is NOT timezone aware, and neither is mytime. So any math between the two is pure clock math, no timezones taken into account.
If you require timezone support, look into pytz or Pendulum or perhaps find another if they don't suit you.

Calculating julian date in python

I'm trying to create a julian date in python and having major struggles. Is there nothing out there as simple as:
jul = juliandate(year,month,day,hour,minute,second)
where jul would be something like 2457152.0 (the decimal changing with the time)?
I've tried jdcal, but can't figure out how to add the time component (jdcal.gcal2jd() only accepts year, month and day).
Not a pure Python solution but you can use the SQLite in memory db which has a julianday() function:
import sqlite3
con = sqlite3.connect(":memory:")
list(con.execute("select julianday('2017-01-01')"))[0][0]
which returns: 2457754.5
Here you go - a pure python solution with datetime and math library.
This is based on the the Navy's Astronomical Equation found here and verified with their own calculator: http://aa.usno.navy.mil/faq/docs/JD_Formula.php
import datetime
import math
def get_julian_datetime(date):
"""
Convert a datetime object into julian float.
Args:
date: datetime-object of date in question
Returns: float - Julian calculated datetime.
Raises:
TypeError : Incorrect parameter type
ValueError: Date out of range of equation
"""
# Ensure correct format
if not isinstance(date, datetime.datetime):
raise TypeError('Invalid type for parameter "date" - expecting datetime')
elif date.year < 1801 or date.year > 2099:
raise ValueError('Datetime must be between year 1801 and 2099')
# Perform the calculation
julian_datetime = 367 * date.year - int((7 * (date.year + int((date.month + 9) / 12.0))) / 4.0) + int(
(275 * date.month) / 9.0) + date.day + 1721013.5 + (
date.hour + date.minute / 60.0 + date.second / math.pow(60,
2)) / 24.0 - 0.5 * math.copysign(
1, 100 * date.year + date.month - 190002.5) + 0.5
return julian_datetime
Usage Example:
# Set the same example as the Naval site.
example_datetime = datetime.datetime(1877, 8, 11, 7, 30, 0)
print get_julian_datetime(example_datetime)
Answer one from Extract day of year and Julian day from a string date in python. No libraries required.
Answer two is a library from https://pypi.python.org/pypi/jdcal
The easiest: df['Julian_Dates']= df.index.to_julian_date(). you need to set your date time column to index (Use Pandas).
There is a way with using Astropy. First, change your time to a list (t). Second, change that list to astropy time (Time). Finally, compute your JD or MJD (t.jd t.mjd).
https://docs.astropy.org/en/stable/time/
For df:
t = Time(DF.JulianDates,format='jd',scale='utc')
A simple fudge the numbers script via wiki don't know either. Please note that this was written using Python 3.6 so I'm not sure it would work on Python 2.7 but this is also an old question.
def julian_day(now):
"""
1. Get current values for year, month, and day
2. Same for time and make it a day fraction
3. Calculate the julian day number via https://en.wikipedia.org/wiki/Julian_day
4. Add the day fraction to the julian day number
"""
year = now.year
month = now.month
day = now.day
day_fraction = now.hour + now.minute / 60.0 + now.second / 3600.0 / 24.0
# The value 'march_on' will be 1 for January and February, and 0 for other months.
march_on = math.floor((14 - month) / 12)
year = year + 4800 - march_on
# And 'month' will be 0 for March and 11 for February. 0 - 11 months
month = month + 12 * march_on - 3
y_quarter = math.floor(year / 4)
jdn = day + math.floor((month * 153 + 2) / 5) + 365 * year + y_quarter
julian = year < 1582 or year == (1582 and month < 10) or (month == 10 and day < 15)
if julian:
reform = 32083 # might need adjusting so needs a test
else:
reform = math.floor(year / 100) + math.floor(year / 400) + 32030.1875 # fudged this
return jdn - reform + day_fraction
Generally this was just to try for myself as the most common algorithm was giving me trouble. That works and if you search around for it and write your script using it as it comes in many languages. But this one has steps in the docs to try to keep it simple. The biggest decision is how often are you going to look for dates that are before Gregorian reform. That is why I never tested that yet but go ahead and play with it as it needs a lot of massaging. :-D At least I think is conforms to PEP8 even if it isn't up to best practices. Go ahead and pylint it.
You could just use source packages like PyEphem or whatever but you still would like to know what's going on with it so you could write your own tests. I'll link that PyEphem for you but there are lots of ready made packages that have Julian Day calculations.
Your best bet if you are doing lots of work with these types of numbers is to get a list of the constant ones such as J2000.
datetime.datetime(2000, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)
datetime.datetime.toordinal() + 1721425 - 0.5 # not tested
# or even
datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
It's not so hard to figure these out if you get familiar with what datetime library does. Just for fun did you notice the PyEphem logo? I suspect it comes from something like this
One post that I saw seems to work but has no tests is jiffyclub
Now here is the more common way to calculate two values using a datetime object.
def jdn(dto):
"""
Given datetime object returns Julian Day Number
"""
year = dto.year
month = dto.month
day = dto.day
not_march = month < 3
if not_march:
year -= 1
month += 12
fr_y = math.floor(year / 100)
reform = 2 - fr_y + math.floor(fr_y / 4)
jjs = day + (
math.floor(365.25 * (year + 4716)) + math.floor(30.6001 * (month + 1)) + reform - 1524)
if jjs < ITALY:
jjs -= reform
return jjs
# end jdn
def ajd(dto):
"""
Given datetime object returns Astronomical Julian Day.
Day is from midnight 00:00:00+00:00 with day fractional
value added.
"""
jdd = jdn(dto)
day_fraction = dto.hour / 24.0 + dto.minute / 1440.0 + dto.second / 86400.0
return jdd + day_fraction - 0.5
# end ajd
It may not be the best practice in Python but you did ask how to calculate it not just get it or extract it although if that is what you want those questions have been answered as of late.
Try https://www.egenix.com/products/python/mxBase/mxDateTime/
First construct a DateTime object via the syntax
DateTime(year,month=1,day=1,hour=0,minute=0,second=0.0)
Then you can use '.jdn' object method to get the value you are looking for.

Would DateTimeField() work if I have time in this format 1/7/11 9:15 ? If not what would?

I am importing data from a JSON file and it has the date in the following format 1/7/11 9:15
What would be the best variable type/format to define in order to accept this date as it is? If not what would be the most efficient way to accomplish this task?
Thanks.
"What would be the best variable type/format to define in order to accept this date as it is?"
The DateTimeField.
"If not what would be the most efficient way to accomplish this task?"
You should use the datetime.strptime method from Python's builtin datetime library:
>>> from datetime import datetime
>>> import json
>>> json_datetime = "1/7/11 9:15" # still encoded as JSON
>>> py_datetime = json.loads(json_datetime) # now decoded to a Python string
>>> datetime.strptime(py_datetime, "%m/%d/%y %I:%M") # coerced into a datetime object
datetime.datetime(2011, 1, 7, 9, 15)
# Now you can save this object to a DateTimeField in a Django model.
If you take a look at https://docs.djangoproject.com/en/dev/ref/models/fields/#datetimefield, it says that django uses the python datetime library which is docomented at http://docs.python.org/2/library/datetime.html.
Here is a working example (with many debug prints and step-by-step instructions:
from datetime import datetime
json_datetime = "1/7/11 9:15"
json_date, json_time = json_datetime.split(" ")
print json_date
print json_time
day, month, year = map(int, json_date.split("/")) #maps each string in stringlist resulting from split to an int
year = 2000 + year #be ceareful here! 2 digits for a year may cause trouble!!! (could be 1911 as well)
hours, minutes = map(int, json_time.split(":"))
print day
print month
print year
my_datetime = datetime(year, month, day, hours, minutes)
print my_datetime
#Generate a json date:
new_json_style = "{0}/{1}/{2} {3}:{4}".format(my_datetime.day, my_datetime.month, my_datetime.year, my_datetime.hour, my_datetime.minute)
print new_json_style