Login

Tag "time"

Snippet List

Translate datetime format strings from Python to PHP

A simple Python function that converts a Python datetime formatting string to its nearest PHP equivalent. Python and PHP use different format string conventions when specifying datetime formats: * Python: [https://docs.python.org/2/library/time.html#time.strftime](https://docs.python.org/2/library/time.html#time.strftime) * PHP: [http://php.net/manual/en/function.date.php](http://php.net/manual/en/function.date.php) Working with Django the Date, Time and DateTime widgets all use Python format strings as stored in: * django.conf.global_settings.DATE_INPUT_FORMATS * django.conf.global_settings.TIME_INPUT_FORMATS * django.conf.global_settings.DATETIME_INPUT_FORMATS but if you want to use a datetime widget like the datetimepicker here: [http://xdsoft.net/jqplugins/datetimepicker/](http://xdsoft.net/jqplugins/datetimepicker/) then you'll find it uses PHP format specifiers. If you want Django and the datetimepicker to populate a field in the same way, you need to ensure they use the same format. So I add to the Django from context the default format as follows: `context["default_datetime_input_format"] = datetime_format_python_to_PHP(DATETIME_INPUT_FORMATS[0])` and in the template Javascript on my form for the datetimepicker i give it: `"format": {{default_datetime_input_format}}` and the datetimepicker now populates the the datetime field in the same format as Django.

  • datetime
  • date
  • format
  • time
Read More

Creating custom time entries in Django Date widget

jQuery code for making custom list on Admin page in DateTime widget. Create new js file in your static folder with this code. To use add custom js to Admin page like this: class NiceAdmin(admin.ModelAdmin): class Media: js = ('js/adminNice.js',) This code will change **all** DateTime widgets on selected page.

  • time
  • widget
  • django-admin
Read More

another request logging middleware with request time and extra info

Simple logging middleware that captures the following: * remote address (whether proxied or direct) * if authenticated, then user email address * request method (GET/POST etc) * request full path * response status code (200, 404 etc) * content length * request process time * If DEBUG=True, also logs SQL query information - number of queries and how long they took

  • middleware
  • request-path
  • time
  • request
  • logging
  • time-logging
Read More

datetime.time/datetime.datetime to Unix Epoch (with microsecond support)

This is useful when you need to convert a datetime.datetime.now() or datetime.date.today() into a unix epoch seconds, with microsecond precision (precision only applies to datetime.datetime, as datetime.date won't have any microseconds). I have found this is necessary for when storing the DateTime in the models as a FloatField, in order to keep the usec/microsecond precision. At some point, I will probably create a custom model field called DateTimeWithUSec or something like that, but for now, this will do :)

  • datetime
  • date
  • time
  • epoch
  • convert
  • unix
  • usec
  • precision
  • microsecond
Read More

Template filter to convert timecodes into links

This template filter, "jumptime" will find any timecodes in a chunk of text and convert them to links that can be used to jump a video player to that point. E.g., If there is the string "3:05", it will be converted into a link that can be used to jump to that point. This is similar to what youtube does. For information on how to implement, see Django's [custom template tag information](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags). You'd use this with some javascript like this: `jQuery(document).ready(function(){ jQuery('a.jumpToTime').bind('click',function(){ player.sendEvent('PLAY'); player.sendEvent('SEEK', jQuery(this).attr('value')); }); });`

  • regex
  • time
  • re
  • timecode
Read More

Time toggle on mouseover template filter

Shows the timesince to the user and then on mouseover it will display the exact time for allowing the user to see the exact time. Example: shows "about 1 day, 3 hours ago" and then changes on mouseover to read "at 3:05pm on Wednesday 22nd April 2009"

  • date
  • time
  • usability
  • time-toggle
Read More

HTML/JS template filter to show localized dates/times

While working on my website projects today I had the idea to use HTML/JS instead of a IP database to localize the dates and times shown on the websites. Here are the snippets to use the JS snippets as filters for Django running on Google App Engine. You can use those filters on datetime objects.

  • template
  • filter
  • django
  • datetime
  • date
  • time
  • appengine
  • local
Read More

timeto template filter

This is a more compact version of django's [timeuntil](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#timeuntil) filter that only shows hours & minutes. If used like `{{ dt|timeto }}`, will produce output like "1hr 30min". If you know for sure that the server has the same timezone as the [datetime](http://docs.python.org/library/datetime.html#datetime-objects) value, then you don't need [datetutil.tz](http://labix.org/python-dateutil#head-587bd3efc48f897f55c179abc520a34330ee0a62) for utc time conversion.

  • datetime
  • time
  • humanize
Read More

Create breakpoints to time code at

Include in your code like this: t=Timer() Then use it like this: t.tick('Some optional description') It will output the time spent between the tick and the previous tick (or inception) and the total time spent since it began tracking time. Can be placed multiple times in a long segment of code. Can be used to break out the amount of time being spent on various parts of your code so you can focus on optimizing those sections.

  • time
  • high-performance
  • profiling
  • timer
  • optimize
Read More

TodayDateTimeField

Automatically sets date to today if only time part was entered. If today is 01/01/2008, then both DateTimeField and TodayDateTimeField clean '2008-01-01 15:05' to datetime(2008,01,01,15,5,0), while '15:05' is cleaned as datetime(1900,01,01,15,5,0) for DateTimeField but datetime(2008,01,01,15,5,0) for TodayDateTimeField.

  • date
  • time
  • field
  • today
Read More

Time ranges like 7-9 p.m.

Template filter to format a start and end time in to a range. Uses Django's ["P" format](http://www.djangoproject.com/documentation/templates/#now) and assumes start and end time are on the same day or night before/morning after. `{{ start_time|time_range:end_time }}` Examples: 7-8 p.m. 8 p.m. - midnight noon - 4 p.m. 9:45 a.m. - 5:15 p.m. 10:30 p.m. - 1:30 a.m.

  • format
  • time
  • range
Read More
Author: sgb
  • 3
  • 4

Date/time util template filters

**Explanations:** - the series "is_*_of" was created 'cos it's easier write: `{% if 10|is_day_of:date and user %}` than write: `{% ifequal date.day 10 %}{% if user %}...` - the series "inc/dec" is not complete, but can the extended to day, hour, minute, etc as you needs. It's util to inc 10 months since 05/31/2006 by example and get a 2007's date :) **Setup:** Insert the snippet into an_app/templatetags/datetimeutils.py. **Use in template:** `{% load datetimeutils %}` and use filters as following: - `{{ 30|is_day_of:a_date_time_variable }}` - `{{ 11|is_month_of:a_date_time_variable }}` - `{{ 2006|is_year_of:a_date_time_variable }}` - `{{ 58|is_minute_of:a_date_time_variable }}` - `{{ 23|is_hour_of:a_date_time_variable }}` - `{{ a_date_time_variable|dec_year:2 }}` - `{{ a_date_time_variable|dec_month:2 }}` - `{{ a_date_time_variable|inc_year:2 }}` - `{{ a_date_time_variable|inc_month:2 }}`

  • date
  • time
  • template-filters
Read More

24 snippets posted so far.