Debian Codegraph @

www/lang_datefunc.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Date And Time Functions</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Date And Time Functions
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#time_values">2. Time Values</a></div>
130 <div class="fancy-toc1"><a href="#modifiers">3. Modifiers</a></div>
131 <div class="fancy-toc1"><a href="#examples">4. Examples</a></div>
132 <div class="fancy-toc1"><a href="#caveats_and_bugs">5. Caveats And Bugs</a></div>
133 </div>
134 </div>
135 <script>
136 function toggle_toc(){
137 var sub = document.getElementById("toc_sub")
138 var mk = document.getElementById("toc_mk")
139 if( sub.style.display!="block" ){
140 sub.style.display = "block";
141 mk.innerHTML = "&#x25bc;";
142 } else {
143 sub.style.display = "none";
144 mk.innerHTML = "&#x25ba;";
145 }
146 }
147 </script>
148 </div>
149
150
151
152
153 <h1 id="overview"><span>1. </span>Overview</h1>
154
155 <p>
156 SQLite supports seven <a href="lang_corefunc.html">scalar</a> date and time functions as follows:
157 </p>
158
159 <p>
160 </p><ol>
161 <li> <b>date(</b><i>time-value, modifier, modifier, ...</i><b>)</b> </li>
162 <li> <b>time(</b><i>time-value, modifier, modifier, ...</i><b>)</b> </li>
163 <li> <b>datetime(</b><i>time-value, modifier, modifier, ...</i><b>)</b> </li>
164 <li> <b>julianday(</b><i>time-value, modifier, modifier, ...</i><b>)</b> </li>
165 <li> <b>unixepoch(</b><i>time-value, modifier, modifier, ...</i><b>)</b> </li>
166 <li> <b>strftime(</b><i>format, time-value, modifier, modifier, ...</i><b>)</b> </li>
167 <li> <b>timediff(</b><i>time-value, time-value</i><b>)</b> </li>
168 </ol>
169
170 <p>
171 The first six date and time functions take an optional <a href="lang_datefunc.html#tmval">time-value</a> as an argument, followed
172 by zero or more <a href="lang_datefunc.html#dtmods">modifiers</a>.
173 The strftime() function also takes a format string as its first argument.
174 The timediff() function takes exactly two arguments which are both <a href="lang_datefunc.html#tmval">time-values</a>.
175 </p>
176
177 <p>
178 SQLite does not have a dedicated date/time datatype. Instead,
179 date and time values can stored as any of the following:
180
181 </p><p></p><blockquote>
182 <table border="0" cellspacing="0" cellpadding="0">
183 <tr>
184 <td><a href="http://en.wikipedia.org/wiki/ISO_8601">ISO-8601</a>
185 </td><td>A text string that is an ISO 8601 date/time value.
186 Example: <tt>'2025-05-29 14:16:00'</tt>
187 </td></tr><tr>
188 <td><a href="http://en.wikipedia.org/wiki/Julian_day">Julian day number</a>
189 </td><td>The number of days including fractional days since -4713-11-24 12:00:00
190 Example: <tt>2460825.09444444</tt>
191 </td></tr><tr>
192 <td><a href="https://en.wikipedia.org/wiki/Unix_time">Unix timestamp</a>
193 </td><td>The number of seconds including fractional seconds since 1970-01-01 00:00:00
194 Example: <tt>1748528160</tt>
195 </td></tr></table>
196 </blockquote>
197
198 <p>
199 These three formats are collectively known as <a href="lang_datefunc.html#tmval">time-values</a>.
200 All of the date time functions accept time-values as either ISO-8601 text or
201 as Julian day numbers. They can also be made to accept unix timestamps by
202 adding optional modifiers arguments <a href="lang_datefunc.html#automod">'auto'</a> or
203 <a href="lang_datefunc.html#jdmod">'unixepoch'</a>.
204 Since the timediff() function does not accept modifiers,
205 it can only use ISO-8601 and julian day number time-values.
206
207 <a name="dttm"></a>
208
209 </p><p>
210 The <b>date()</b> function returns the date as text in this format: YYYY-MM-DD.
211
212 <a name="dttm"></a>
213
214 </p><p>
215 The <b>time()</b> function returns the time as text in formatted as HH:MM:SS or as HH:MM:SS.SSS if
216 the <a href="lang_datefunc.html#subsec">subsec modifier</a> is used.
217
218 <a name="dttm"></a>
219
220 </p><p>
221 The <b>datetime()</b> function returns the date and time formatted as YYYY-MM-DD HH:MM:SS or
222 as YYYY-MM-DD HH:MM:SS.SSS if the <a href="lang_datefunc.html#subsec">subsec modifier</a> is used.
223
224 <a name="jlndy"></a>
225
226 </p><p>
227 The <b>julianday()</b> function returns the
228 <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> - the
229 fractional number of days since noon in Greenwich on November 24, 4714 B.C.
230 (<a href="http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar">Proleptic Gregorian calendar</a>).
231
232 <a name="uepch"></a>
233
234 </p><p>
235 The <b>unixepoch()</b> function returns a unix timestamp - the number of seconds
236 since 1970-01-01 00:00:00 UTC. The unixepoch() function normally returns
237 an integer number of seconds, but with the optional <a href="lang_datefunc.html#subsec">subsec modifier</a> it
238 will return a floating point number which is the fractional number of seconds.
239
240 <a name="strftm"></a>
241
242 </p><p>
243 The <b>strftime()</b> function returns the date formatted according to
244 the format string specified as the first argument.
245 The format string supports the most common substitutions found in the
246 <a href="http://opengroup.org/onlinepubs/007908799/xsh/strftime.html">strftime() function</a>
247 from the standard C library plus two new substitutions, %f and %J.
248 The following is a complete list of valid strftime() substitutions
249 as of version 3.46.0 (2024-05-23). Earlier versions of SQLite
250 might not support all substitutions. If an undefined or unsupported
251 substitution is seen, the result is NULL.
252 </p>
253
254 <blockquote>
255 <table border="0" cellpadding="0" cellspacing="0">
256 <tr><td></td><td width="10"></td><td></td></tr>
257
258 <tr><td> %d </td><td></td><td> day of month: 01-31
259 </td></tr><tr><td> %e </td><td></td><td> day of month without leading zero: 1-31
260 </td></tr><tr><td> %f </td><td></td><td> fractional seconds: SS.SSS
261 </td></tr><tr><td> %F </td><td></td><td> ISO 8601 date: YYYY-MM-DD
262 </td></tr><tr><td> %G </td><td></td><td> ISO 8601 year corresponding to %V
263 </td></tr><tr><td> %g </td><td></td><td> 2-digit ISO 8601 year corresponding to %V
264 </td></tr><tr><td> %H </td><td></td><td> hour: 00-24
265 </td></tr><tr><td> %I </td><td></td><td> hour for 12-hour clock: 01-12
266 </td></tr><tr><td> %j </td><td></td><td> day of year: 001-366
267 </td></tr><tr><td> %J </td><td></td><td> Julian day number (fractional)
268 </td></tr><tr><td> %k </td><td></td><td> hour without leading zero: 0-24
269 </td></tr><tr><td> %l </td><td></td><td> %I without leading zero: 1-12
270 </td></tr><tr><td> %m </td><td></td><td> month: 01-12
271 </td></tr><tr><td> %M </td><td></td><td> minute: 00-59
272 </td></tr><tr><td> %p </td><td></td><td> "AM" or "PM" depending on the hour
273 </td></tr><tr><td> %P </td><td></td><td> "am" or "pm" depending on the hour
274 </td></tr><tr><td> %R </td><td></td><td> ISO 8601 time: HH:MM
275 </td></tr><tr><td> %s </td><td></td><td> seconds since 1970-01-01
276 </td></tr><tr><td> %S </td><td></td><td> seconds: 00-59
277 </td></tr><tr><td> %T </td><td></td><td> ISO 8601 time: HH:MM:SS
278 </td></tr><tr><td> %U </td><td></td><td> week of year (00-53) - week 01 starts on the first Sunday
279 </td></tr><tr><td> %u </td><td></td><td> day of week 1-7 with Monday==1
280 </td></tr><tr><td> %V </td><td></td><td> ISO 8601 week of year
281 </td></tr><tr><td> %w </td><td></td><td> day of week 0-6 with Sunday==0
282 </td></tr><tr><td> %W </td><td></td><td> week of year (00-53) - week 01 starts on the first Monday
283 </td></tr><tr><td> %Y </td><td></td><td> year: 0000-9999
284 </td></tr><tr><td> %% </td><td></td><td> %
285 </td></tr></table>
286 </blockquote>
287
288 <p>
289 Other date and time functions can be expressed
290 in terms of strftime():
291 </p>
292
293 <blockquote>
294 <table border="0" cellpadding="0" cellspacing="0">
295 <tr><td><b>Function</b></td><td width="30"></td><td><b>Equivalent strftime()</b>
296 </td></tr><tr><td> date(...) </td><td></td><td> strftime('%F', ...)
297 </td></tr><tr><td> time(...) </td><td></td><td> strftime('%T', ...)
298 </td></tr><tr><td> datetime(...) </td><td></td><td> strftime('%F %T', ...)
299 </td></tr><tr><td> julianday(...) </td><td></td><td> <nobr>CAST(strftime('%J', ...) as REAL)</nobr>
300 </td></tr><tr><td> unixepoch(...) </td><td></td><td> <nobr>CAST(strftime('%s', ...) as INT)</nobr>
301 </td></tr></table>
302 </blockquote>
303
304 <p>
305 The date(), time(), and datetime() functions all return text, and so their
306 strftime() equivalents are exact. However, the julianday()
307 and unixepoch() functions return numeric values. Their strftime() equivalents
308 return a string that is the text representation of the corresponding number.
309 </p>
310
311 <p>
312 The main reasons for providing functions other than strftime() are
313 for convenience and for efficiency. The julianday() and unixepoch()
314 functions return real and integer values respectively, and do not
315 incur the format conversion costs or inexactitude resulting from use
316 of the '%J' or '%s' format specifiers with the strftime() function.
317 </p>
318
319 <a name="tmdif"></a>
320
321 <p>
322 The <b>timediff(A,B)</b> function returns a string that describes the amount
323 of time that must be added to B in order to reach time A. The format of
324 the timediff() result is designed to be human-readable. The format is:
325 </p><blockquote>
326 (+|-)YYYY-MM-DD HH:MM:SS.SSS
327 </blockquote>
328 <p>
329 This time difference string is also an allowed modifier for the other
330 date/time functions. The following invariant holds for time-values A
331 and B:
332 </p><blockquote>
333 datetime(A) = datetime(B, timediff(A,B))
334 </blockquote>
335
336 <p>
337 The length of months and years vary. February is shorter than March.
338 Leap years are longer than non-leap years. The output from timediff()
339 takes this all into account. The timediff() function is intended to provide
340 a human-friendly description of the time span. If you want to know the
341 number of days or seconds between two dates, A and B, then you can always do
342 one of these:
343 </p><blockquote>
344 SELECT julianday(B) - julianday(A);<br>
345 SELECT unixepoch(B) - unixepoch(A);
346 </blockquote>
347
348 <p>
349 The timediff(A,B) might return the same result even for values A and B
350 that span a different number of days - depending on the starting date.
351 For example, both of the following two timediff() calls return the
352 same result ("-0000-01-00 00:00:00.000") even though the first timespan
353 is 28 days and the seconds is 31 days:
354 </p><blockquote>
355 SELECT timediff('2023-02-15','2023-03-15');<br>
356 SELECT timediff('2023-03-15','2023-04-15');
357 </blockquote>
358 <p>
359 Summary: If you want a human-friendly time span, use timediff(). If you
360 what a precise time difference (in days or seconds) use the difference
361 between two julianday() or unixepoch() calls.
362 </p>
363
364
365 <a name="tmval"></a>
366
367 <h1 id="time_values"><span>2. </span>Time Values</h1>
368
369 <p>A time-value can be in any of the following formats shown below.
370 The value is usually a string, though it can be an integer or floating
371 point number in the case of format 12.
372
373 </p><ol>
374 <li> <i>YYYY-MM-DD</i>
375 </li><li> <i>YYYY-MM-DD HH:MM</i>
376 </li><li> <i>YYYY-MM-DD HH:MM:SS</i>
377 </li><li> <i>YYYY-MM-DD HH:MM:SS.SSS</i>
378 </li><li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM</i>
379 </li><li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM:SS</i>
380 </li><li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM:SS.SSS</i>
381 </li><li> <i>HH:MM</i>
382 </li><li> <i>HH:MM:SS</i>
383 </li><li> <i>HH:MM:SS.SSS</i>
384 </li><li> <b>now</b>
385 </li><li> <i>DDDDDDDDDD</i>
386 </li></ol>
387
388 <p>
389 In formats 5 through 7, the "T" is a literal character separating
390 the date and the time, as required by
391 <a href="http://www.w3c.org/TR/NOTE-datetime">ISO-8601</a>.
392 Formats 8 through 10 that specify only a time assume a date of
393 2000-01-01. Format 11, the string 'now', is converted into the
394 current date and time as obtained from the xCurrentTime method
395 of the <a href="c3ref/vfs.html">sqlite3_vfs</a> object in use.
396 The 'now' argument to date and time functions always returns exactly the
397 same value for multiple invocations within the same <a href="c3ref/step.html">sqlite3_step()</a> call.
398 <a href="http://en.wikipedia.org/wiki/Coordinated_Universal_Time">Universal Coordinated Time (UTC)</a> is used.
399 Format 12 is the
400 <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day number</a>
401 expressed as an integer or floating point value.
402 Format 12 might also be interpreted as a unix timestamp if it is immediately followed
403 either the <a href="lang_datefunc.html#automod">'auto'</a> or <a href="lang_datefunc.html#jdmod">'unixepoch'</a> modifier.
404 </p>
405
406 <p>
407 Formats 2 through 10 may be optionally followed by a timezone indicator of the form
408 "<i>&#91;+-&#93;HH:MM</i>" or just "<i>Z</i>". The date and time functions use UTC or "zulu"
409 time internally, and so the "Z" suffix is a no-op. Any non-zero "HH:MM" suffix is
410 subtracted from the indicated date and time in order to compute zulu time.
411 For example, all of the following time-values are equivalent:
412 </p>
413
414 <blockquote>
415 2013-10-07 08:23:19.120<br>
416 2013-10-07T08:23:19.120Z<br>
417 2013-10-07 04:23:19.120-04:00<br>
418 2456572.84952685
419 </blockquote>
420
421 <p>
422 In formats 4, 7, and 10, the fractional seconds value SS.SSS can have
423 one or more digits following the decimal point. Exactly three digits are
424 shown in the examples because only the first three digits are significant
425 to the result, but the input string can have fewer or more than three digits
426 and the date/time functions will still operate correctly.
427 Similarly, format 12 is shown with 10 significant digits, but the date/time
428 functions will really accept as many or as few digits as are necessary to
429 represent the Julian day number.
430 </p>
431
432 <p>
433 In all functions other than timediff(),
434 the time-value (and all modifiers) may be omitted, in which case a time
435 value of 'now' is assumed.
436
437 <a name="dtmods"></a>
438
439 </p><h1 id="modifiers"><span>3. </span>Modifiers</h1>
440
441 <p>For all date/time functions other than timediff(),
442 the time-value argument can be followed by zero or more modifiers that
443 alter date and/or time. Each modifier
444 is a transformation that is applied to the time-value to its left.
445 Modifiers are applied from left to right; order is important.
446 The available modifiers are as follows.</p>
447
448 <ol>
449 <li value="1"> NNN days
450 </li><li value="2"> NNN hours
451 </li><li value="3"> NNN minutes
452 </li><li value="4"> NNN seconds
453 </li><li value="5"> NNN months
454 </li><li value="6"> NNN years
455 </li><li value="7"> &plusmn;HH:MM
456 </li><li value="8"> &plusmn;HH:MM:SS
457 </li><li value="9"> &plusmn;HH:MM:SS.SSS
458 </li><li value="10"> &plusmn;YYYY-MM-DD
459 </li><li value="11"> &plusmn;YYYY-MM-DD HH:MM
460 </li><li value="12"> &plusmn;YYYY-MM-DD HH:MM:SS
461 </li><li value="13"> &plusmn;YYYY-MM-DD HH:MM:SS.SSS
462 </li><li value="14"> ceiling
463 </li><li value="15"> floor
464 </li><li value="16"> start of month
465 </li><li value="17"> start of year
466 </li><li value="18"> start of day
467 </li><li value="19"> weekday N
468 </li><li value="20"> unixepoch
469 </li><li value="21"> julianday
470 </li><li value="22"> auto
471 </li><li value="23"> localtime
472 </li><li value="24"> utc
473 </li><li value="25"> subsec
474 </li><li value="26"> subsecond
475 </li></ol>
476
477 <p>The first thirteen modifiers (1 through 13)
478 add the specified amount of time to the date and time
479 specified by the arguments to its left.
480 The 's' character at the end of the modifier names in 1 through 6 is optional.
481 The NNN value can be any floating point number, with an optional '+' or '-' prefix.
482
483 <a name="tmshf"></a>
484
485 </p><p>The <b>time shift modifiers</b> (7 through 13) move the time-value by the
486 number of years, months, days, hours, minutes, and/or seconds specified.
487 An initial "+" or "-" is required for formats 10 through 13 but is optional
488 for formats 7, 8, and 9. The changes are applies from left to right.
489 First the year is shifted by YYYY, then the month by MM, and then day
490 by DD, and so forth. The
491 timediff(A,B) function returns a time shift in format 13 that shifts
492 the time-value B into A.</p>
493
494 <a name="dtambg"></a>
495
496 <p>Because the length of a month or year changes from one month or year
497 to the next, ambiguities can arise when shifting a date by months and/or years.
498 For example, what is the date one year after 2024-02-29? Is it 2025-02-28
499 or 2025-03-01? Or what is the date that is two months after 2023-12-31?
500 Is it 2024-02-29 or 2024-03-02? There is no consensus on how to resolve
501 this ambiguity, so the "<b>ceiling</b>" and "<b>floor</b>" modifiers
502 (14 and 15) are available to
503 let the programmer decide. If the next modifier after a time shift is
504 "ceiling", then any ambiguity in the date is resolved by choosing the
505 later date. The "floor" modifier resolves ambiguities
506 by resolving to the last day of the previous month. The default
507 behavior is "ceiling".
508
509 </p><p>The "<b>start of</b>" modifiers (16 through 18) shift the date backwards
510 to the beginning of the subject month, year or day.</p>
511
512 <p>The "<b>weekday</b>" modifier advances the date forward, if necessary,
513 to the next date where the weekday number is N. Sunday is 0, Monday is 1,
514 and so forth.
515 If the date is already on the desired weekday, the "weekday" modifier
516 leaves the date unchanged. </p>
517
518 <a name="jdmod"></a>
519
520 <p>The "<b>unixepoch</b>" modifier (20) only works if it immediately follows
521 a time-value in the DDDDDDDDDD format.
522 This modifier causes the DDDDDDDDDD to be interpreted not
523 as a Julian day number as it normally would be, but as
524 <a href="http://en.wikipedia.org/wiki/Unix_time">Unix Time</a> - the
525 number of seconds since 1970. If the "unixepoch" modifier does not
526 follow a time-value of the form DDDDDDDDDD which expresses the number
527 of seconds since 1970 or if other modifiers
528 separate the "unixepoch" modifier from prior DDDDDDDDDD then the
529 behavior is undefined.
530
531 <a name="jdmod"></a>
532
533 </p><p>The "<b>julianday</b>" modifier must immediately follow the initial
534 time-value which must be of the form DDDDDDDDD. Any other use of
535 the 'julianday' modifier is an error and causes the function to return NULL.
536 The 'julianday' modifier forces the time-value number to be interpreted
537 as a julian-day number. As this is the default behavior, the 'julianday'
538 modifier is scarcely more than a no-op. The only difference is that
539 adding 'julianday' forces the DDDDDDDDD time-value format, and causes
540 a NULL to be returned if any other time-value format is used.
541
542 <a name="automod"></a>
543
544 </p><p>The "<b>auto</b>" modifier must immediately follow the initial time-value.
545 If the time-value is numeric (the DDDDDDDDDD format) then the 'auto'
546 modifier causes the time-value to interpreted as either a julian day
547 number or a unix timestamp, depending on its magnitude. If the value
548 is between 0.0 and 5373484.499999, then it is interpreted as a julian
549 day number (corresponding to dates between
550 -4713-11-24 12:00:00 and 9999-12-31 23:59:59, inclusive). For numeric
551 values outside of the range of valid julian day numbers, but within
552 the range of -210866760000 to 253402300799, the 'auto' modifier causes
553 the value to be interpreted as a unix timestamp. Other numeric values
554 are out of range and cause a NULL return. The 'auto' modifier is a no-op
555 for ISO 8601 text time-values.
556 The "auto" modifier is designed to work with time-values even in
557 cases where it is not known which time-value format
558 is stored in the database file, or in cases where the same column
559 stores time-values in different formats on different rows.
560 The 'auto' modifier will automatically select the
561 appropriate format. However, there is some ambiguity. Unix
562 timestamps for the first 63 days of 1970 will be interpreted as julian
563 day numbers. The 'auto' modifier is very useful when the dataset is
564 guaranteed to contain no dates within that range, but should be
565 avoided for applications that might make use of dates in the opening
566 months of 1970.
567
568 <a name="localtime"></a>
569
570 </p><p>The "<b>localtime</b>" modifier assumes the time-value to its left is in
571 Universal Coordinated Time (UTC) and adjusts that time
572 value so that it is in localtime. If "localtime"
573 follows a time that is not UTC, then the behavior is undefined.
574 The "<b>utc</b>" modifier is the opposite of "localtime".
575 "utc" assumes that the time-value
576 to its left is in the local timezone and adjusts that time-value to be in UTC.
577 If the time to the left is not in localtime, then the result of "utc" is
578 undefined.</p>
579
580 <a name="subsec"></a>
581
582 <p>
583 The "<b>subsecond</b>" modifier (which may be abbreviated as just
584 "<b>subsec</b>") increases the resolution of the output for
585 <a href="lang_datefunc.html#dttm">datetime()</a>, <a href="lang_datefunc.html#dttm">time()</a>, and <a href="lang_datefunc.html#uepch">unixepoch()</a>, and for the "%s"
586 format string in <a href="lang_datefunc.html#strftm">strftime()</a>. The "subsecond"
587 modifier has no effect on other date/time functions.
588 The current implemention increases the resolution from seconds
589 to milliseconds, but this might increase to a higher resolution
590 in future releases of SQLite. When "subsec" is used with
591 <a href="lang_datefunc.html#dttm">datetime()</a> or <a href="lang_datefunc.html#dttm">time()</a>, the seconds field at the end is
592 followed by a decimal point and one or more digits to show
593 fractional seconds. When "subsec" is used with <a href="lang_datefunc.html#uepch">unixepoch()</a>,
594 the result is a floating point value which is the number of
595 seconds and fractional seconds since 1970-01-01.
596 The "subsecond" and "subsec" modifiers have the special property
597 that they can occur as the first argument to date/time functions
598 (or as the first argument after the format string for strftime()).
599 When this happens, the time-value that is normally in the first
600 argument is understood to be "now". For example, a short cut to
601 get the current time in seconds since 1970 with millisecond
602 precision is to say:
603
604 </p><blockquote>
605 SELECT unixepoch('subsec');
606 </blockquote>
607
608 <h1 id="examples"><span>4. </span>Examples</h1>
609
610 <p>Compute the current date.</p><p>
611
612 </p><blockquote>SELECT date();</blockquote>
613
614 <p>Compute the last day of the current month.</p>
615
616 <blockquote>SELECT date('now','start of month','+1 month','-1 day');
617 </blockquote>
618
619 <p>Compute the date and time given a unix timestamp 1092941466.</p>
620
621 <blockquote>
622 SELECT datetime(1092941466, 'unixepoch');<br>
623 SELECT datetime(1092941466, 'auto'); -- Does not work for early 1970!
624 </blockquote>
625
626 <p>Compute the date and time given a unix timestamp 1092941466, and
627 compensate for your local timezone.</p>
628
629 <blockquote>
630 SELECT datetime(1092941466, 'unixepoch', 'localtime');
631 </blockquote>
632
633 <p>Compute the current unix timestamp.</p>
634
635 <blockquote>
636 SELECT unixepoch();<br>
637 SELECT strftime('%s');
638 </blockquote>
639
640 <p>Compute the number of days since the signing of the US Declaration
641 of Independence.</p>
642
643 <blockquote>
644 SELECT julianday('now') - julianday('1776-07-04');
645 </blockquote>
646
647 <p>Compute the number of seconds since a particular moment in 2004:</p>
648
649 <blockquote>
650 SELECT unixepoch() - unixepoch('2004-01-01 02:34:56');
651 </blockquote>
652
653 <p>
654 Compute the date of the first Tuesday in October
655 for the current year.
656 </p>
657
658 <blockquote>
659 SELECT date('now','start of year','+9 months','weekday 2');
660 </blockquote>
661
662 <p>Compute the time since the unix epoch in seconds with
663 millisecond precision:</p>
664
665 <blockquote>
666 SELECT (julianday('now') - 2440587.5)*86400.0;<br>
667 SELECT unixepoch('now','subsec');
668 </blockquote>
669
670 <p>Compute how old Abraham Lincoln would be if he were still alive today:</p>
671
672 <blockquote>
673 SELECT timediff('now','1809-02-12');
674 </blockquote>
675
676 <h1 id="caveats_and_bugs"><span>5. </span>Caveats And Bugs</h1>
677
678 <p>The computation of local time depends heavily on the whim
679 of politicians and is thus difficult to get correct for
680 all locales. In this implementation, the standard C library
681 function localtime_r() is used to assist in the calculation of
682 local time. The
683 localtime_r() C function normally only works for years
684 between 1970 and 2037. For dates outside this range, SQLite
685 attempts to map the year into an equivalent year within
686 this range, do the calculation, then map the year back.</p>
687
688 <p>These functions only work for dates between 0000-01-01 00:00:00
689 and 9999-12-31 23:59:59 (julian day numbers 1721059.5 through 5373484.5).
690 For dates outside that range, the results of these
691 functions are undefined.</p>
692
693 <p>Non-Vista Windows platforms only support one set of DST rules.
694 Vista only supports two. Therefore, on these platforms,
695 historical DST calculations will be incorrect.
696 For example, in the US, in 2007 the DST rules changed.
697 Non-Vista Windows platforms apply the new 2007 DST rules
698 to all previous years as well. Vista does somewhat better
699 getting results correct back to 1986, when the rules were also changed.</p>
700
701 <p>All internal computations assume the
702 <a href="http://en.wikipedia.org/wiki/Gregorian_calendar">Gregorian calendar</a>
703 system. They also assume that every
704 day is exactly 86400 seconds in duration; no leap seconds are incorporated.</p>
705 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/lang_datefunc.in?m=91b979fe1e">2024-04-16 16:29:07</a> UTC </small></i></p>
706