Extract each year, month, day, year from getctime , getmtime in Python - python

I want to extract the year month day hours min eachly from below value.
import os, time, os.path, datetime
date_of_created = time.ctime(os.path.getctime(folderName))
date_of_modi = time.ctime(os.path.getmtime(folderName))
Now I only can get like below
'Thu Dec 26 19:21:37 2019'
but I want to get the the value separtly
2019 // Dec(Could i get this as int??) // 26
each
I want to extract each year month day each time min value from date_of_created and date_of_modi
Could i get it? in python?

You can convert the string to a datetime object:
from datetime import datetime
date_of_created = datetime.strptime(time.ctime(os.path.getctime(folderName)), "%a %b %d %H:%M:%S %Y") # Convert string to date format
print("Date created year: {} , month: {} , day: {}".format(str(date_of_created.year),str(date_of_created.month),str(date_of_created.day)))

The time.ctime function returns the local time in string form. You might want to use the time.localtime function, which returns a struct_time object which contains the information you are looking for. As example,
import os, time
date_created_string = time.ctime(os.path.getctime('/home/b-fg/Downloads'))
date_created_obj = time.localtime(os.path.getctime('/home/b-fg/Downloads'))
print(date_created_string) # Mon Feb 10 09:41:03 2020
print('Year: {:4d}'.format(date_created_obj.tm_year)) # Year: 2020
print('Month: {:2d}'.format(date_created_obj.tm_mon)) # Month: 2
print('Day: {:2d}'.format(date_created_obj.tm_mday)) # Day: 10
Note that these are integer values, as requested.

time.ctime([secs])
Convert a time expressed in seconds since the epoch to a string of a form: 'Sun Jun 20 23:21:05 1993' representing local time.
If that's not what you want... use something else? time.getmtime will return a struct_time which should have the relevant fields, or for a more modern interface use datetime.datetime.fromtimestamp which... returns a datetime object from a UNIX timestamp.
Furthermore, using stat would probably more efficient as it ctime and mtime will probably perform a stat call each internally.

You can use the datetime module, more specifically the fromtimestamp() function from the datetime module to get what you expect.
import os, time, os.path, datetime
date_of_created = datetime.datetime.fromtimestamp(os.path.getctime(my_repo))
date_of_modi = datetime.datetime.fromtimestamp(os.path.getmtime(my_repo))
print(date_of_created.strftime("%Y"))
Output will be 2020 for a repo created in 2020.
All formats are available at this link

Related

How to show a notification when the previously entered date arrives

I'm trying to make a reminder app, but I can't find out how to show if the entered date is the current date. To give an example:
I added a reminder on December 19, 2021 at 17:45, and I want to know if this date is before today's date.
You can use datetime.datetime.strptime() to convert string to datetime object. Then you can calculate time delta with to check how much time is left.
import datetime
reminder = datetime.datetime.strptime('12/19/2021, 19:11:14',
"%m/%d/%Y, %H:%M:%S")
print(datetime.datetime.now()) # 2021-12-12 17:24:01.573420
time_delta = reminder - datetime.datetime.now()
print(time_delta.seconds) # 6432
print(time_delta.days) # 7
If the current date is after the reminder date, days will be negative.
You can use the python's builtin datetime module.
You can import it like this:
from datetime import datetime
To convert the reminder string to a date time object, you can use the strptime() function.
reminder_date = "December 19, 2021 at 17:45"
reminder = datetime.strptime(reminder_date, "%B %d, %Y at %H:%M")
Next, get the current datetime object.
current = datetime.now()
Finally, you can compare them using python's builtin comparison operators as follows.
if reminder < current:
print("passed")
else:
print("future")

Parse Date string to datetime with timezone

I have a string:
r = 'Thu Dec 17 08:56:41 CST 2020'
Here CST represent China central time('Asia/Shanghai'). I wanted to parse it to datetime...I am doing something like
from dateparser import parse
r1 = parse(r)
Which is giving me r1 as:
2020-12-17 08:56:41-06:00
And I am also doing this
r2 = r1.replace(tzinfo=pytz.timezone("Asia/Shanghai"))
And this is giving me r2 as:
2020-12-17 08:50:41+08:00
There is 6 min lag in r2 can someone tell me why is that? And how to correctly transfer my raw string r1 to desired r2 which is:
2020-12-17 08:56:41 in Asia/Shanghai timezone
Thanks
Using dateutil.parser you can directly parse your date correctly.
Note that CST is an ambiguous timezone, so you need to specify which one you mean. You can either do this directly in the tzinfos parameter of the parse() call or you can define a dictionary that has mappings for timezones and pass this. In this dict, you can either specify the offset, e.g.
timezone_info = {
"CDT": -5 * 3600,
"CEST": 2 * 3600,
"CST": 8 * 3600
}
parser.parse(r, tzinfos=timezone_info)
or (using gettz) directly specify a timezone:
timezone_info = {
"CDT": gettz("America/Chicago"),
"CEST": gettz("Europe/Berlin"),
"CST": gettz("Asia/Shanghai")
}
parser.parse(r, tzinfos=timezone_info)
See also the dateutil.parser documentation and the answers to this SO question.
Be aware that the latter approach is tricky if you have a location with daylight saving time! Depending on the date you apply it to, gettz("America/Chicago") will have UTC-5 or UTC-6 as a result (as Chicago switches between Central Standard Time and Central Daylight Time). So depending on your input data, the second example may actually not really be correct and yield the wrong outcome! Currently, China observes China Standard Time (CST) all year, so for your use case it makes no difference (may depend on your date range though).
Overall:
from dateutil import parser
from dateutil.tz import gettz
timezone_info = {"CST": gettz("Asia/Shanghai")}
r = 'Thu Dec 17 08:56:41 CST 2020'
d = parser.parse(r, tzinfos=timezone_info)
print(d)
print(d.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
gets you
2020-12-17 08:56:41+08:00
2020-12-17 08:56:41 CST+0800
EDIT: Printing the human readable timezone name instead of the abbreviated one name is just a little more complicated with this approach, as dateutil.tz.gettz() gets you a tzfile that has no attribute which has just the name. However, you can obtain it via the protected _filename using split():
print(d.strftime('%Y-%m-%d %H:%M:%S') + " in " + "/".join(d.tzinfo._filename.split('/')[-2:]))
yields
2020-12-17 08:56:41+08:00 in Asia/Shanghai
This of course only works if you used gettz() to set the timezone in the first place.
EDIT 2: If you know that all your dates are in CST anyway, you can also ignore the timezone when parsing. This gets you naive (or unanware) datetimes which you can then later add a human readable timezone to. You can do this using replace() and specify the timezone either as shown above using gettz() or using timezone(() from the pytz module:
from dateutil import parser
from dateutil.tz import gettz
import pytz
r = 'Thu Dec 17 08:56:41 CST 2020'
d = parser.parse(r, ignoretz=True)
d_dateutil = d.replace(tzinfo=gettz('Asia/Shanghai'))
d_pytz = d.replace(tzinfo=pytz.timezone('Asia/Shanghai'))
Note that depending on which module you use to add the timezone information, the class of tzinfo differs. For the pytz object, there is a more direct way of accessing the timezone in human readable form:
print(type(d_dateutil.tzinfo))
print("/".join(d_dateutil.tzinfo._filename.split('/')[-2:]))
print(type(d_pytz.tzinfo))
print(d_pytz.tzinfo.zone)
produces
<class 'dateutil.tz.tz.tzfile'>
Asia/Shanghai
<class 'pytz.tzfile.Asia/Shanghai'>
Asia/Shanghai
from datetime import datetime
import pytz
# The datetime string you have
r = "Thu Dec 17 08:56:41 CST 2020"
# The time-zone string you want to use
offset_string = 'Asia/Shanghai'
# convert the time zone string into offset from UTC
# a. datetime.now(pytz.timezone(offset_string)).utcoffset().total_seconds() --- returns seconds offset from UTC
# b. convert seconds into hours (decimal) --- divide by 60 twice
# c. remove the decimal point, we want the structure as: +0800
offset_num_repr = '+{:05.2f}'.format(datetime.now(pytz.timezone(offset_string)).utcoffset().total_seconds()/60/60).replace('.', '')
print('Numeric representation of the offset: ', offset_num_repr)
# replace the CST 2020 with numeric timezone offset
# a. replace it with the offset computed above
updated_datetime = str(r).replace('CST', offset_num_repr)
print('\t Modified datetime string: ', updated_datetime)
# Now parse your string into datetime object
r = datetime.strptime(updated_datetime, "%a %b %d %H:%M:%S %z %Y")
print('\tFinal parsed datetime object: ', r)
Should produce:
Numeric representation of the offset: +0800
Modified datetime string: Thu Dec 17 08:56:41 +0800 2020
Final parsed datetime object: 2020-12-17 08:56:41+08:00

How to format a timestamp in python to a readable format?

I have a slack bot that gets schedule information and prints the start and end times of the user's schedule. I am trying to format the timestamp so that it is a more readable format. Is there a way to format this response?
Here is an example of the output:
co = get_co(command.split(' ')[1])
start = get_schedule(co['group'])['schedule']['schedule_layers'][0]['start']
end = get_schedule(co['group'])['schedule']['schedule_layers'][0]['end']
response = 'Start: {} \n End: {}'.format(start,end)
The current time format is 2019-06-28T15:12:49-04:00, but I want it to be something more readable like Fri Jun 28 15:12:49 2019
You can use dateparser to parse the date time string easily.
import dateparser
date = dateparser.parse('2019-06-28T15:12:49-04:00') # parses the date time string
print(date.strftime('%a %b %d %H:%m:%M %Y'))
# Fri Jun 28 15:06:12 2019
See this in action here
To convert timestamps of any format you can use the datetime module as described here:
https://stackabuse.com/converting-strings-to-datetime-in-python/

Python 3.6 Weekday

I have a combination date/time string 20180104 06:09:36.234 from which I want to find the weekday in python, expecting to get the result for for the date (which is 04 January 2018) as "4" (ie Thursday) . Have searched far and wide to no avail.Can anyone help please?
Thanks CJ
See explanation of datetime.strptime
from datetime import datetime
date_string = '20180104 06:09:36.234'
date = datetime.strptime(date_string, '%Y%m%d %H:%M:%S.%f')
print(date.isoweekday()) # 4
Finally worked it out by various combinations of functions:
Identify year, month, date,
Convert to integer,
Use datetime module.
import datetime
DateTime = '20180104 06:09:36.234'
Weekday = datetime.date(int(DateTime[0:4]), int(DateTime[5:6]),
int(DateTime[7:8])).weekday()
print("Weekday = " , Weekday)
Weekday = 3
Is there a better way??

Restricting RSS elements by date with feedparser. [Python]

I iterate a RSS feed like so where _file is the feed
d = feedparser.parse(_file)
for element in d.entries:
print repr(element.date)
The date output comes out like so
u'Thu, 16 Jul 2009 15:18:22 EDT'
I cant seem to understand how to actually quantify the above date output so I can use it to limit feed elements. I So what I am asking is how can I get a actual time out of this, so I can say if greater then 7 days old, skip this element.
feedparser is supposed to give you a struct_time object from Python's time module. I'm guessing it doesn't recognize that date format and so is giving you the raw string.
See here on how to add support for parsing malformed timestamps:
http://pythonhosted.org/feedparser/date-parsing.html
If you manage to get it to give you the struct_time, you can read more about that here:
http://docs.python.org/library/time.html#time.struct_time
struct_time objects have everything you need. They have these members:
time.struct_time(tm_year=2010, tm_mon=2, tm_mday=4, tm_hour=23, tm_min=44, tm_sec=19, tm_wday=3, tm_yday=35, tm_isdst=0)
I generally convert the structs to seconds, like this:
import time
import calendar
struct = time.localtime()
seconds = calendar.timegm(struct)
Then you can just do regular math to see how many seconds have elapsed, or use the datetime module to do timedeltas.
one way
>>> import time
>>> t=time.strptime("Thu, 16 Jul 2009 15:18:22 EDT","%a, %d %b %Y %H:%M:%S %Z")
>>> sevendays=86400*7
>>> current=time.strftime ("%s",time.localtime())
>>> if int(current) - time.mktime(t) > sevendays:
print "more than 7 days"
you can also see the datetime module and make use of timedelta() for date calculations.
If you install the dateutil module:
import dateutil.parser as dp
import dateutil.tz as dtz
import datetime
date_string=u'Thu, 16 Jul 2009 15:18:22 EDT'
adatetime=dp.parse(date_string)
print(adatetime)
# 2009-07-16 15:18:22-04:00
now=datetime.datetime.now(dtz.tzlocal())
print(now)
# 2010-02-04 23:35:52.428766-05:00
aweekago=now-datetime.timedelta(days=7)
print(aweekago)
# 2010-01-28 23:35:52.428766-05:00
if adatetime<aweekago:
print('old news')
If you are using Ubuntu, dateutil is provided by the python-dateutil package.

Categories