From Facebook Graph API I retrieve events which have the start_time set as string with the following format: "2012-10-12T23:30:00+0200". When location is specified it returns also the event object contains also the timezone (e.g.: "Europe/Rome").
I simply would like to check which events are upcoming (newer than "now").
I wonder if there is a way to do so without using pytz, which is not installed by default in Django. For deployment, the more portable the solution is the better.
You can use the built-in datetime module after a quick parse of the returned string (using "-", "T", and "+" as tokens).
Here is a quick reference to the documentation : http://docs.python.org/library/datetime.html
Note : this module handles timezone (search for tzone in the page).
Related
How can I get the minutes spent from a worklog from an issue using the jira-python library?
Using the jirashell I see that the issue has the attribute issue.fields.worklog, however when I try to access that in my python code I get the error: AttributeError: type object 'PropertyHolder' has no attribute 'worklog'.
If I create a jira client and do jira_client.worklogs(ticket.key) in my python code, it returns a list of Worklogs and their ids but I don't know what to do with that. I see in the documentation there's a worklog() function that takes in the issue id, and the worklog id, but I don't understand what it returns and how I would use that/if it is what I'm looking for.
I found a roundabout way of doing it through the client.
As I iterate through each issue I get the list of worklogs per ticket by doing worklogs = jira_client.worklogs(issue.key) and then i iterate through all of the worklog items in the worklogs list (a nested for loop):
for worklog in worklogs:
totaltime += readtime(worklog.timeSpent)
Using the jirashell I accessed a specific worklog of a specific ticket: wl = jira_client.worklog(<issue key>, <worklog id>) then I typed in wl. and pressed TAB, it listed the following:
wl.author, wl.comment, wl.created, wl.delete, wl.find, wl.id, wl.raw, wl.self, wl.started,
wl.timeSpent, wl.timeSpentSeconds, wl.update, wl.updateAuthor, wl.updated
(Note: you need to include the period at the end of wl before pressing tab)
Running wl.timespent in the jirashell gave me gave me a unicode string with the number and then h or m for hour or minute (for example: u'6h'). Then I new that once I generated the worklog object in my loop above, I could access the time by using the timepsent attribute.
(Note: My readtime function turns the string into an integer and converts hours to minutes, and is not shown here)
The jirashell really helps with trying to find the attributes of the fields, etc. (Note: you need to install jira-python in addition to jira in order to run jirashell. Also if you installed jira-python in your virtualenv you need to run env/bin/jirashell from your command line once you are in your project's directory.)
I'm trying to parse a string to a date object on Google App Engine. The string is formatted locally and I thereby either need to set the locale setting before parsing the string or find a third part module that does international formatting.
In Google App Engine I found that only the "C" locale is supported, which made my initial implementation useless:
datestring = "20. oktober 2013, 18:43"
locale.setlocale(locale.LC_ALL, 'da_DK.UTF-8')
date = datetime.datetime.strptime(datestring, "%d. %B %Y, %H:%M")
Threw error:
Error: locale emulation only supports "C" locale
In my search for a third part module I found Babel. It has formatting features from data objects to string but I can't see that it works the other way around, and I can't find another module that does the trick.
Is there any other solution I haven't thought of, or do I need to go make my own module for this?
Thanks!
Is there a method to retrieve time zone names in another language?
In Python, if I do something like this:
for tz in pytz.common_timezones_set :
print tz
The result is in English, but what if I would like to have it in Spanish or Arabic?
You can use babel package
import pytz
from babel.dates import get_timezone_location
for tz in pytz.common_timezones_set:
print(get_timezone_location(tz, locale='es'))
No, unfortunately there are no translations for the timezone names. The names are part of the Olson timezone database (not part of Python or pytz). New ones are added from time to time, so any potential translation project would have to stay in sync with that database.
pytz.common_timezones_set returns a set of timezone ids in the tz database. They are designed to be human readable but they are not translatable.
PyICU provides access to the localized timezone names:
>>> from datetime import datetime
>>> import icu
>>> df = icu.DateFormat.createDateTimeInstance(icu.DateFormat.SHORT, icu.DateFormat.FULL, icu.Locale.getFrance())
>>> df.format(datetime.now(icu.ICUtzinfo.getDefault()))
'27/01/2015 21:01:01 heure normale d’Europe centrale'
Coming quite late but I ran into a similar issue lately, and ended up creating my own package l18n for that purpose. It is available here : https://pypi.org/project/l18n/
It provides translations files for human-readable places and timezones used in pytz. Most of the translations are automatically fetched from the CLDR database, but there are always a few of them missing. For the moment only translation files for English and French are available. Feel free to contact me (create an issue on the packages's repo https://github.com/tkhyn/l18n) or follow the step-by-step procedure here if you want to add translations for your language.
I want to know how to convert different format dates to expected format in python .
ex : i want to get this format : 2/29/2012
['2012-02-01 // 2012-02-28', '2/15/2012', '2/13/2012', '2/14/2012', '2/23/2012', '2/18/2012', '2/29/2012']
How to check today date in the range '2012-02-01 // 2012-02-28'
Share your suggestions
Use the datetime library in python. You can just compare two different datetime.datetime objects. And you can separate the year, month, date thing to put it in anyform you want.
Check this link for all the library details.
http://docs.python.org/library/datetime.html
Hope that helped.
The dateutil python library parses a wider variety of date formats than the standard datetime module.
In my Django application I get times from a webservice, provided as a string, that I use in my templates:
{{date.string}}
This provides me with a date such as:
2009-06-11 17:02:09+0000
These are obviously a bit ugly, and I'd like to present them in a nice format to my users. Django has a great built in date formatter, which would do exactly what I wanted:
{{ value|date:"D d M Y" }}
However this expects the value to be provided as a date object, and not a string. So I can't format it using this. After searching here on StackOverflow pythons strptime seems to do what I want, but being fairly new to Python I was wondering if anyone could come up with an easier way of getting date formatting using strings, without having to resort to writing a whole new custom strptime template tag?
You're probably better off parsing the string received from the webservice in your view code, and then passing the datetime.date (or string) to the template for display. The spirit of Django templates is that very little coding work should be done there; they are for presentation only, and that's why they go out of their way to prevent you from writing Python code embedded in HTML.
Something like:
from datetime import datetime
from django.shortcuts import render_to_response
def my_view(request):
ws_date_as_string = ... get the webservice date
the_date = datetime.strptime(ws_date, "%Y-%m-%d %H:%M:%S+0000")
return render_to_response('my_template.html', {'date':the_date})
As Matthew points out, this drops the timezone. If you wish to preserve the offset from GMT, try using the excellent third-party dateutils library, which seamlessly handles parsing dates in multiple formats, with timezones, without having to provide a time format template like strptime.
This doesn't deal with the Django tag, but the strptime code is:
d = strptime("2009-06-11 17:02:09+0000", "%Y-%m-%d %H:%M:%S+0000")
Note that you're dropping the time zone info.