<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Simon Willison's Weblog: datetime</title><link href="http://simonwillison.net/" rel="alternate"/><link href="http://simonwillison.net/tags/datetime.atom" rel="self"/><id>http://simonwillison.net/</id><updated>2025-05-08T22:37:44+00:00</updated><author><name>Simon Willison</name></author><entry><title>SQLite CREATE TABLE: The DEFAULT clause</title><link href="https://simonwillison.net/2025/May/8/sqlite-create-table-default-timestamp/#atom-tag" rel="alternate"/><published>2025-05-08T22:37:44+00:00</published><updated>2025-05-08T22:37:44+00:00</updated><id>https://simonwillison.net/2025/May/8/sqlite-create-table-default-timestamp/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.sqlite.org/lang_createtable.html#the_default_clause"&gt;SQLite CREATE TABLE: The DEFAULT clause&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
If your SQLite create table statement includes a line like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;CREATE TABLE alerts (
    -- ...
    alert_created_at text default current_timestamp
)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;current_timestamp&lt;/code&gt; will be replaced with a UTC timestamp in the format &lt;code&gt;2025-05-08 22:19:33&lt;/code&gt;. You can also use &lt;code&gt;current_time&lt;/code&gt; for &lt;code&gt;HH:MM:SS&lt;/code&gt; and &lt;code&gt;current_date&lt;/code&gt; for &lt;code&gt;YYYY-MM-DD&lt;/code&gt;, again using UTC.&lt;/p&gt;
&lt;p&gt;Posting this here because I hadn't previously noticed that this defaults to UTC, which is a useful detail. It's also a strong vote in favor of &lt;code&gt;YYYY-MM-DD HH:MM:SS&lt;/code&gt; as a string format for use with SQLite, which &lt;a href="https://www.sqlite.org/lang_datefunc.html"&gt;doesn't otherwise provide&lt;/a&gt; a formal datetime type.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/sql"&gt;sql&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/sqlite"&gt;sqlite&lt;/a&gt;&lt;/p&gt;



</summary><category term="datetime"/><category term="sql"/><category term="sqlite"/></entry><entry><title>High-precision date/time in SQLite</title><link href="https://simonwillison.net/2024/Aug/9/high-precision-datetime-in-sqlite/#atom-tag" rel="alternate"/><published>2024-08-09T15:31:40+00:00</published><updated>2024-08-09T15:31:40+00:00</updated><id>https://simonwillison.net/2024/Aug/9/high-precision-datetime-in-sqlite/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://antonz.org/sqlean-time/"&gt;High-precision date/time in SQLite&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Another neat SQLite extension from Anton Zhiyanov. &lt;code&gt;sqlean-time&lt;/code&gt; (&lt;a href="https://github.com/nalgeon/sqlean/tree/main/src/time"&gt;C source code here&lt;/a&gt;) implements high-precision time and date functions for SQLite, modeled after the design used by Go.&lt;/p&gt;
&lt;p&gt;A time is stored as a 64 bit signed integer seconds &lt;code&gt;0001-01-01 00:00:00 UTC&lt;/code&gt; - signed so you can represent dates in the past using a negative number - plus a 32 bit integer of nanoseconds - combined into a a 13 byte internal representation that can be stored in a BLOB column.&lt;/p&gt;
&lt;p&gt;A duration uses a 64-bit number of nanoseconds, representing values up to roughly 290 years.&lt;/p&gt;
&lt;p&gt;Anton includes dozens of functions for parsing, displaying, truncating, extracting fields and converting to and from Unix timestamps.

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="https://lobste.rs/s/6mzz3c/high_precision_date_time_sqlite"&gt;lobste.rs&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/go"&gt;go&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/sqlite"&gt;sqlite&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/anton-zhiyanov"&gt;anton-zhiyanov&lt;/a&gt;&lt;/p&gt;



</summary><category term="datetime"/><category term="go"/><category term="sqlite"/><category term="anton-zhiyanov"/></entry><entry><title>Temporal: getting started with JavaScript’s new date time API</title><link href="https://simonwillison.net/2021/Jul/7/temporal/#atom-tag" rel="alternate"/><published>2021-07-07T22:29:49+00:00</published><updated>2021-07-07T22:29:49+00:00</updated><id>https://simonwillison.net/2021/Jul/7/temporal/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://2ality.com/2021/06/temporal-api.html"&gt;Temporal: getting started with JavaScript’s new date time API&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Axel Rauschmayer explores the new proposed API for handling dates, times and timezones in JavaScript., which is under development by Ecma TC39 at the moment (and made available as a Polyfill which you are recommended not to run in production since the API is still being figured out). This is a notoriously difficult problem so it’s always interesting to see the latest thinking on how to best address it.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/timezones"&gt;timezones&lt;/a&gt;&lt;/p&gt;



</summary><category term="datetime"/><category term="javascript"/><category term="timezones"/></entry><entry><title>Tips on using python's datetime module</title><link href="https://simonwillison.net/2009/Jul/6/datetime/#atom-tag" rel="alternate"/><published>2009-07-06T14:20:12+00:00</published><updated>2009-07-06T14:20:12+00:00</updated><id>https://simonwillison.net/2009/Jul/6/datetime/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.enricozini.org/2009/debian/using-python-datetime/"&gt;Tips on using python&amp;#x27;s datetime module&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Wow. I’ve run in to problems with datetime and timezones before, but I had no idea how intrinsic those problems were to the design of the library.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/dates"&gt;dates&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/times"&gt;times&lt;/a&gt;&lt;/p&gt;



</summary><category term="dates"/><category term="datetime"/><category term="python"/><category term="times"/></entry><entry><title>Making the HTML5 time element safe for historians</title><link href="https://simonwillison.net/2009/Apr/6/quirksblog/#atom-tag" rel="alternate"/><published>2009-04-06T14:01:37+00:00</published><updated>2009-04-06T14:01:37+00:00</updated><id>https://simonwillison.net/2009/Apr/6/quirksblog/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.quirksmode.org/blog/archives/2009/04/making_time_saf.html"&gt;Making the HTML5 time element safe for historians&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
PPK presents a detailed history of dates and calendars and points out that the HTML5 time element is ill prepared to faithfully represent the kind of dates historians are interested in.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/calendars"&gt;calendars&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/dates"&gt;dates&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/historians"&gt;historians&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/history"&gt;history&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/html5"&gt;html5&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ppk"&gt;ppk&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/standards"&gt;standards&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/time"&gt;time&lt;/a&gt;&lt;/p&gt;



</summary><category term="calendars"/><category term="dates"/><category term="datetime"/><category term="historians"/><category term="history"/><category term="html5"/><category term="ppk"/><category term="standards"/><category term="time"/></entry><entry><title>Datejs - A JavaScript Date Library</title><link href="https://simonwillison.net/2007/Dec/3/datejs/#atom-tag" rel="alternate"/><published>2007-12-03T21:01:10+00:00</published><updated>2007-12-03T21:01:10+00:00</updated><id>https://simonwillison.net/2007/Dec/3/datejs/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.datejs.com/"&gt;Datejs - A JavaScript Date Library&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Building a date API around chaining—Date.today().next().thursday()—is a neat concept. I’d like to see that adapted for Python’s datetime library.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/chaining"&gt;chaining&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/datejs"&gt;datejs&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;&lt;/p&gt;



</summary><category term="chaining"/><category term="datejs"/><category term="datetime"/><category term="javascript"/><category term="python"/></entry><entry><title>calendar.timegm()</title><link href="https://simonwillison.net/2007/Sep/3/calendar/#atom-tag" rel="alternate"/><published>2007-09-03T01:54:35+00:00</published><updated>2007-09-03T01:54:35+00:00</updated><id>https://simonwillison.net/2007/Sep/3/calendar/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://docs.python.org/lib/module-calendar.html#l2h-719"&gt;calendar.timegm()&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
An “unrelated but handy function” that converts a time.gmtime() in to a corresponding Unix timestamp. I’ve been hand-rolling this one for years; never thought to look in calendar.

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="http://intertwingly.net/blog/2007/09/02/Dealing-With-Dates"&gt;Sam Ruby&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/calendar"&gt;calendar&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/sam-ruby"&gt;sam-ruby&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/time"&gt;time&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/timezones"&gt;timezones&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unixtimestamp"&gt;unixtimestamp&lt;/a&gt;&lt;/p&gt;



</summary><category term="calendar"/><category term="datetime"/><category term="python"/><category term="sam-ruby"/><category term="time"/><category term="timezones"/><category term="unixtimestamp"/></entry><entry><title>Django vs feedparser on dates</title><link href="https://simonwillison.net/2007/Sep/2/django/#atom-tag" rel="alternate"/><published>2007-09-02T10:17:52+00:00</published><updated>2007-09-02T10:17:52+00:00</updated><id>https://simonwillison.net/2007/Sep/2/django/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.deadlybloodyserious.com/2007/09/feedparser-v-django/"&gt;Django vs feedparser on dates&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Some useful tips in the comments. I find Python’s timezone stuff endlessly frustrating: I know it can do what I want, but it always takes me a ridiculously long time to figure out the necessary incantations.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/datetime"&gt;datetime&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/django"&gt;django&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/feedparser"&gt;feedparser&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/timezones"&gt;timezones&lt;/a&gt;&lt;/p&gt;



</summary><category term="datetime"/><category term="django"/><category term="feedparser"/><category term="python"/><category term="timezones"/></entry></feed>