Python and module - date - python

If I don't have access to the Python time module, how would I determine the number of days that have passed since I was born? That is, within the code, how many days old I am?
The code I am trying to understand better is this:
import time
start = raw_input(“Enter the time stamp in seconds: “)
start = float(start)
end = time.time()
elapsed = end - start
st_elapsed = time.gmtime(elapsed)
print "\n"
hours_mins_secs = time.strftime("%H:%M:%S", st_elapsed)
print "Elapsed time in HH:MM:SS ->", hours_mins_secs, "\n"
Now, I looked to the site https://docs.python.org/2/library/time.html
but I didn't find the alternative related to time, without using module time.
My goal is understand better this code.

This sounds like a homework question. You should give us what you've done so far and we will help you. SO users are not your personal coders.
(No criticism intended)

Related

Python: How to print date format with using the help function?

I would like to print the date format so I dont need to search in the navigator every time I want to print a date like in the following code:
time = now.strftime("%Y-%m-%d %H:%M:%S")
print("time:", time)
I been searching and didn't find any thing about this.
When you run help(the_date.strftime), it doesn't show the possible parameters.
You can see the format codes here - Basic date and time types
The below sample reference:
import time
print("time:{}".format(time.strftime('%Y-%m-%d %H:%M:%S')))

How to get the Date and Time of Current Location in Python [duplicate]

This question already has answers here:
How do I get the current time?
(54 answers)
Closed 3 years ago.
I want to get the Date and Time of The Current Location. If somebody runs the code in a different country thought it will show the date and and time for them, and not for me. If you know any commands for this please put the down bellow. I prefer If I don't need a library so that people who get the code don't need to download the library.
I think you can not get the date or time of a location.
This code gets your device's date and time:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%D")
print("Current Time: ", current_time)
print("Current Date: ", current_date)
A standard Python module called datetime can probably help you with retrieving the current system date and time from where the program is run
Since it's a standard module, there would be no need to install it rather just import it in the program itself.
Example :
import datetime
x = datetime.datetime.now()
print(x)
Would give you the output as :
2019-07-20 00:52:27.158746
The above output contains the current year, month, day, hour, minute, second, and microsecond.

Getting microseconds past the hour

I am working on a quick program to generate DIS (Distributed Interactive Simulation) packets to stress test a gateway we have. I'm all set and rearing to go, except for one small issue. I'm having trouble pulling the current microseconds past the top of the hour correctly.
Currently I'm doing it like this:
now = dt.now()
minutes = int(now.strftime("%M"))
seconds = int(now.strftime("%S")) + minutes*60
microseconds = int(now.strftime("%f"))+seconds*(10**6)
However when I run this multiple times in a row, I'll get results all over the place, with numbers that cannot physically be right. Can someone sanity check my process??
Thanks very much
You can eliminate all that formatting and just do the following:
now = dt.now()
microseconds_past_the_hour = now.microsecond + 1000000*(now.minute*60 + now.second)
Keep in mind that running this multiple times in a row will continually produce different results, as the current time keeps advancing.

How to compare a google datastore datetime to python datetime?

I am trying to write a function in python that deletes entries in my datastore that are more than five minutes old. I'm making a kitten picture database for a class, so my code looks something like this:
class KittenImg(db.Model):
"""Models a Gallery entry with kitten_name, image, and date."""
kitten_name = db.StringProperty(multiline=True)
image = db.BlobProperty()
date = db.DateTimeProperty(auto_now_add=True)
A user uploads a KittenImg and it loads into the datastore and returns just fine, but I don't think I understand really what format a kitten.date value would return and how I can compare it to datetime.now() using Python. I have tried a few different options in the python datetime module documentation, but I just really don't think I have a good enough understanding of what I'm getting when I call datetime.now() and when I ask for a kitten.date.
I feel like after looking at the documentation for about three hours, I still have no idea how to even begin getting the solution.
I've been trying things like:
now = datetime.now()
then = kitten.date
tdelta = now - then
And:
now = total_seconds(datetime.now())
then = total_seconds(kitten.date)
tdelta = now - then
But in each case, it gives me an unauthorized operator for the - sign.
It seems like datetime.timedelta() should have something to do with it, but I have absolutely no idea how to use that function even after staring at it for hours.
Can someone please help me either:
1. Understand what's going on with the datetime module better or
2. Give me another way to approach my problem?
Thanks
Sorry, I answered my own question. I must have been doing something different last night, but I got it to work tonight. This is what I did:
for kitten in kittens:
then = kitten.date
now = datetime.datetime.now()
tdelta = now - then
if tdelta.total_seconds() > 300:
kitten.delete()
Should probably have put a static of FIVE_MIN instead of using the magic number 300, so forgive me for that, but it worked.

Python get micro time from certain date

I was browsing the Python guide and some search machines for a few hours now, but I can't really find an answer to my question.
I am writing a switch where only certain files are chosen to be in the a file_list (list[]) when they are modified after a given date.
In my loop I do the following code to get its micro time:
file_time = os.path.getmtime(path + file_name)
This returns me a nice micro time, like this: 1342715246.0
Now I want to compare if that time is after a certain date-time I give up. So for testing purposes, I used 1790:01:01 00:00:00.
# Preset (outside the class/object)
start_time = '1970-01-01 00:00:00'
start_time = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
start_time = start_time.strftime("%Y-%m-%d %H:%M:%S")
# In my Method this check makes sure I only append files to the list
# that are modified after the given date
if file_time > self.start_time:
file_list.append(file_name)
Of course this does not work, haha :P. What I'm aiming for is to make a micro time format from a custom date. I can only find ClassMethods online that make micro time formats from the current date.
Take a look at Python datetime to microtime. You need the following snippet:
def microtime(dt):
time.mktime(dt.timetuple()) + dt.microsecond / 1000000.0
You can compare datetime.datetime objects by themselves. If you keep start_time as a datetime.datetime object and make file_time a datetime.datetime object, you can successfully do file_time > self.start_time
Take a look at the supported operators in the documentation

Categories