lang_datefunc.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: Date And Time Functions</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <h2 align="center">SQL As Understood By SQLite</h2></a><h1>Date And Time Functions</h1>
  47. <p>
  48. SQLite supports five date and time functions as follows:
  49. </p>
  50. <p>
  51. <ol>
  52. <li> <b>date(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li>
  53. <li> <b>time(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li>
  54. <li> <b>datetime(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li>
  55. <li> <b>julianday(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li>
  56. <li> <b>strftime(</b><i>format, timestring, modifier, modifier, ...</i><b>)</b> </li>
  57. </ol>
  58. <p>
  59. All five functions take a time string as an argument. The time string
  60. is followed by zero or more modifiers.
  61. The strftime() function also takes a format string as its first argument.
  62. </p>
  63. <p>
  64. The date and time functions use a subset of
  65. <a href="http://en.wikipedia.org/wiki/ISO_8601">IS0-8601</a> date and time
  66. formats.
  67. The date() function returns the date in this format: YYYY-MM-DD.
  68. The time() function returns the time as HH:MM:SS.
  69. The datetime() function returns "YYYY-MM-DD HH:MM:SS".
  70. The julianday() function returns the
  71. <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> - the
  72. number of days since noon in Greenwich on November 24, 4714 B.C.
  73. (<a href="http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar">Proleptic
  74. Gregorian calendar</a>).
  75. The strftime() routine returns the date formatted according to
  76. the format string specified as the first argument.
  77. The format string supports the most common substitutions found in the
  78. <a href="http://opengroup.org/onlinepubs/007908799/xsh/strftime.html">
  79. strftime() function</a> from
  80. the standard C library plus two new substitutions, %f and %J.
  81. The following is a complete list of valid strftime() substitutions:
  82. </p>
  83. <blockquote>
  84. <table border="0" cellpadding="0" cellspacing="0">
  85. <tr><td><td width="10"><td></tr>
  86. <tr><td> %d <td><td> day of month: 00
  87. <tr><td> %f <td><td> fractional seconds: SS.SSS
  88. <tr><td> %H <td><td> hour: 00-24
  89. <tr><td> %j <td><td> day of year: 001-366
  90. <tr><td> %J <td><td> Julian day number
  91. <tr><td> %m <td><td> month: 01-12
  92. <tr><td> %M <td><td> minute: 00-59
  93. <tr><td> %s <td><td> seconds since 1970-01-01
  94. <tr><td> %S <td><td> seconds: 00-59
  95. <tr><td> %w <td><td> day of week 0-6 with sunday==0
  96. <tr><td> %W <td><td> week of year: 00-53
  97. <tr><td> %Y <td><td> year: 0000-9999
  98. <tr><td> %% <td><td> %
  99. </table>
  100. </blockquote>
  101. <p>
  102. Notice that all other date and time functions can be expressed
  103. in terms of strftime():
  104. </p>
  105. <blockquote>
  106. <table border="0" cellpadding="0" cellspacing="0">
  107. <tr><td><b>Function</b><td width="30"><td><b>Equivalent strftime()</b>
  108. <tr><td> date(...) <td><td> strftime('%Y-%m-%d', ...)
  109. <tr><td> time(...) <td><td> strftime('%H:%M:%S', ...)
  110. <tr><td> datetime(...) <td><td> strftime('%Y-%m-%d %H:%M:%S', ...)
  111. <tr><td> julianday(...) <td><td> strftime('%J', ...)
  112. </table>
  113. </blockquote>
  114. <p>
  115. The only reasons for providing functions other than strftime() is
  116. for convenience and for efficiency.
  117. </p>
  118. <h3>Time Strings</h3>
  119. <p>A time string can be in any of the following formats:</p>
  120. <ol>
  121. <li> <i>YYYY-MM-DD</i>
  122. <li> <i>YYYY-MM-DD HH:MM</i>
  123. <li> <i>YYYY-MM-DD HH:MM:SS</i>
  124. <li> <i>YYYY-MM-DD HH:MM:SS.SSS</i>
  125. <li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM</i>
  126. <li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM:SS</i>
  127. <li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM:SS.SSS</i>
  128. <li> <i>HH:MM</i>
  129. <li> <i>HH:MM:SS</i>
  130. <li> <i>HH:MM:SS.SSS</i>
  131. <li> <b>now</b>
  132. <li> <i>DDDDDDDDDD</i>
  133. </ol>
  134. <p>
  135. In formats 5 through 7, the "T" is a literal character separating
  136. the date and the time, as required by
  137. <a href="http://www.w3c.org/TR/NOTE-datetime">ISO-8601</a>.
  138. Formats 8 through 10 that specify only a time assume a date of
  139. 2000-01-01. Format 11, the string 'now', is converted into the
  140. current date and time as obtained from the xCurrentTime method
  141. of the sqlite3_vfs object in use.
  142. <a href="http://en.wikipedia.org/wiki/Coordinated_Universal_Time">
  143. Universal Coordinated Time (UTC)</a> is used.
  144. Format 12 is the
  145. <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day number</a>
  146. expressed as a floating point value.
  147. </p>
  148. <h3>Modifiers</h3>
  149. <p>The time string can be followed by zero or more modifiers that
  150. alter the date and time string. Each modifier
  151. is a transformation that is applied to the time string to its left.
  152. Modifiers are applied from left to right; order is important.
  153. The available modifiers are as follows.</p>
  154. <ol>
  155. <li> NNN days
  156. <li> NNN hours
  157. <li> NNN minutes
  158. <li> NNN.NNNN seconds
  159. <li> NNN months
  160. <li> NNN years
  161. <li> start of month
  162. <li> start of year
  163. <li> start of day
  164. <li> weekday N
  165. <li> unixepoch
  166. <li> localtime
  167. <li> utc
  168. </ol>
  169. <p>The first six modifiers (1 through 6)
  170. simply add the specified amount of time to the date
  171. specified by the preceding timestring.
  172. Note that "&plusmn;NNN months" works by rendering the original date into
  173. the YYYY-MM-DD format, adding the &plusmn;NNN to the MM month value, then
  174. normalizing the result. Thus, for example, the data 2001-03-31 modified
  175. by '+1 month' initially yields 2001-04-31, but April only has 30 days
  176. so the date is normalized to 2001-05-01. A similar effect occurs when
  177. the original date is February 29 of a leapyear and the modifier is
  178. &plusmn;N years where N is not a multiple of four.</p>
  179. <p>The "start of" modifiers (7 through 9) shift the date backwards
  180. to the beginning of the current month, year or day.</p>
  181. <p>The "weekday" modifier advances the date forward to the next date
  182. where the weekday number is N. Sunday is 0, Monday is 1, and so forth.</p>
  183. <p>The "unixepoch" modifier (11) only works if it immediately follows
  184. a timestring in the DDDDDDDDDD format.
  185. This modifier causes the DDDDDDDDDD to be interpreted not
  186. as a Julian day number as it normally would be, but as
  187. <a href="http://en.wikipedia.org/wiki/Unix_time">Unix Time</a> - the
  188. number of seconds since 1970. If the "unixepoch" modifier does not
  189. follow a timestring of the form DDDDDDDDDD which expresses the number
  190. of seconds since 1970 or if other modifiers
  191. separate the "unixepoch" modifier from prior DDDDDDDDDD then the
  192. behavior is undefined.
  193. Due to precision limitations imposed by the implementations use
  194. of 64-bit integers, the "unixepoch" modifier only works for
  195. dates between 0000-01-01 00:00:00 and 5352-11-01 10:52:47 (unix times
  196. of -62167219200 through 10675199167).</p>
  197. <p>The "localtime" modifier (12) assumes the time string to its left is in
  198. Universal Coordinated Time (UTC) and adjusts the time
  199. string so that it displays localtime. If "localtime"
  200. follows a time that is not UTC, then the behavior is undefined.
  201. The "utc" is the opposite of "localtime". "utc" assumes that the string
  202. to its left is in the local timezone and adjusts that string to be in UTC.
  203. If the prior string is not in localtime, then the result of "utc" is
  204. undefined.</p>
  205. <h3>Examples</h3>
  206. <p>Compute the current date.<p>
  207. <blockquote>SELECT date('now');</blockquote>
  208. <p>Compute the last day of the current month.</p>
  209. <blockquote>SELECT date('now','start of month','+1 month','-1 day');
  210. </blockquote>
  211. <p>Compute the date and time given a unix timestamp 1092941466.</p>
  212. <blockquote>
  213. SELECT datetime(1092941466, 'unixepoch');
  214. </blockquote>
  215. <p>Compute the date and time given a unix timestamp 1092941466, and
  216. compensate for your local timezone.</p>
  217. <blockquote>
  218. SELECT datetime(1092941466, 'unixepoch', 'localtime');
  219. </blockquote>
  220. <p>Compute the current unix timestamp.</p>
  221. <blockquote>
  222. SELECT strftime('%s','now');
  223. </blockquote>
  224. <p>Compute the number of days since the signing of the US Declaration
  225. of Independent.</p>
  226. <blockquote>
  227. SELECT julianday('now') - julianday('1776-07-04');
  228. </blockquote>
  229. <p>Compute the number of seconds since a particular moment in 2004:</p>
  230. <blockquote>
  231. SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');
  232. </blockquote>
  233. <p>
  234. Compute the date of the first Tuesday in October
  235. for the current year.
  236. </p>
  237. <blockquote>
  238. SELECT date('now','start of year','+9 months','weekday 2');
  239. </blockquote>
  240. <p>Compute the time since the unix epoch in seconds
  241. (like strftime('%s','now') except includes fractional part):</p>
  242. <blockquote>
  243. SELECT (julianday('now') - 2440587.5)*86400.0;
  244. </blockquote>
  245. <h3>Caveats And Bugs</h3>
  246. <p>The computation of local time depends heavily on the whim
  247. of politicians and is thus difficult to get correct for
  248. all locales. In this implementation, the standard C library
  249. function localtime_r() is used to assist in the calculation of
  250. local time. The
  251. localtime_r() C function normally only works for years
  252. between 1970 and 2037. For dates outside this range, SQLite
  253. attempts to map the year into an equivalent year within
  254. this range, do the calculation, then map the year back.</p>
  255. <p>These functions only work for dates between 0000-01-01 00:00:00
  256. and 9999-12-31 23:59:59 (julidan day numbers 1721059.5 through 5373484.5).
  257. For dates outside that range, the results of these
  258. functions are undefined.</p>
  259. <p>Non-Vista Windows platforms only support one set of DST rules.
  260. Vista only supports two. Therefore, on these platforms,
  261. historical DST calculations will be incorrect.
  262. For example, in the US, in 2007 the DST rules changed.
  263. Non-Vista Windows platforms apply the new 2007 DST rules
  264. to all previous years as well. Vista does somewhat better
  265. getting results correct back to 1986, when the rules were also changed.</p>
  266. <p>All internal computations assume the
  267. <a href="http://en.wikipedia.org/wiki/Gregorian_calendar">
  268. Gregorian calendar</a> system. It is also assumed that every
  269. day is exactly 86400 seconds in duration.</p>