aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/doc/src/calendar.xml
blob: d8193a9ec2122c091a480203141444987f1eafdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1996</year><year>2013</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.
    
      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.
    
    </legalnotice>

    <title>calendar</title>
    <prepared>Peter H&ouml;gfeldt</prepared>
    <docno></docno>
    <date>1996-11-05</date>
    <rev>B</rev>
  </header>
  <module>calendar</module>
  <modulesummary>Local and universal time, day-of-the-week, date and time conversions</modulesummary>
  <description>
    <p>This module provides computation of local and universal time,
      day-of-the-week, and several time conversion functions.</p>
    <p>Time is local when it is adjusted in accordance with the current
      time zone and daylight saving. Time is universal when it reflects
      the time at longitude zero, without any adjustment for daylight
      saving. Universal Coordinated Time (UTC) time is also called
      Greenwich Mean Time (GMT).</p>
    <p>The time functions <c>local_time/0</c> and
      <c>universal_time/0</c> provided in this module both return date
      and time. The reason for this is that separate functions for date
      and time may result in a date/time combination which is displaced
      by 24 hours. This happens if one of the functions is called
      before midnight, and the other after midnight. This problem also
      applies to the Erlang BIFs <c>date/0</c> and <c>time/0</c>, and
      their use is strongly discouraged if a reliable date/time stamp
      is required.</p>
    <p>All dates conform to the Gregorian calendar. This calendar was
      introduced by Pope Gregory XIII in 1582 and was used in all
      Catholic countries from this year. Protestant parts of Germany
      and the Netherlands adopted it in 1698, England followed in 1752,
      and Russia in 1918 (the October revolution of 1917 took place in
      November according to the Gregorian calendar).</p>
    <p>The Gregorian calendar in this module is extended back to year 0.
      For a given date, the <em>gregorian days</em> is the number of
      days up to and including the date specified. Similarly,
      the <em>gregorian seconds</em> for a given date and time, is
      the the number of seconds up to and including the specified date
      and time.</p>
    <p>For computing differences between epochs in time, use
      the functions counting gregorian days or seconds. If epochs are
      given as local time, they must be converted to universal time, in
      order to get the correct value of the elapsed time between epochs.
      Use of the function <c>time_difference/2</c> is discouraged.</p>
    <p>There exists different definitions for the week of the year.
      The calendar module contains a week of the year implementation
      which conforms to the ISO 8601 standard. Since the week number for
      a given date can fall on the previous, the current or on the next
      year it is important to provide the information which year is it
      together with the week number. The function <c>iso_week_number/0</c>
      and <c>iso_week_number/1</c> returns a tuple of the year and the
      week number.</p>
  </description>

  <datatypes>
    <datatype>
      <name name="datetime"/>
    </datatype>
    <datatype>
      <name name="datetime1970"/>
    </datatype>
    <datatype>
      <name name="date"/>
    </datatype>
    <datatype>
      <name name="year"/>
      <desc><p>Year cannot be abbreviated. Example: 93 denotes year
        93, not 1993. Valid range depends on the underlying OS. The
        date tuple must denote a valid date.</p>
      </desc>
    </datatype>
    <datatype>
      <name name="year1970"/>
    </datatype>
    <datatype>
      <name name="month"/>
    </datatype>
    <datatype>
      <name name="day"/>
    </datatype>
    <datatype>
      <name name="time"/>
    </datatype>
    <datatype>
      <name name="hour"/>
    </datatype>
    <datatype>
      <name name="minute"/>
    </datatype>
    <datatype>
      <name name="second"/>
    </datatype>
    <datatype>
      <name name="daynum"/>
    </datatype>
    <datatype>
      <name name="ldom"/>
    </datatype>
    <datatype>
      <name name="yearweeknum"/>
    </datatype>
    <datatype>
      <name name="weeknum"/>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="date_to_gregorian_days" arity="1"/>
      <name name="date_to_gregorian_days" arity="3"/>
      <type variable="Date" name_i="1"/>
      <type variable="Year"/>
      <type variable="Month"/>
      <type variable="Day"/>
      <fsummary>Compute the number of days from year 0 up to the given date</fsummary>
      <desc>
        <p>This function computes the number of gregorian days starting
          with year 0 and ending at the given date.</p>
      </desc>
    </func>
    <func>
      <name name="datetime_to_gregorian_seconds" arity="1"/>
      <fsummary>Compute the number of seconds from year 0 up to the given date and time</fsummary>
      <desc>
        <p>This function computes the number of gregorian seconds
          starting with year 0 and ending at the given date and time.</p>
      </desc>
    </func>
    <func>
      <name name="day_of_the_week" arity="1"/>
      <name name="day_of_the_week" arity="3"/>
      <fsummary>Compute the day of the week</fsummary>
      <type variable="Date" name_i="1"/>
      <type variable="Year"/>
      <type variable="Month"/>
      <type variable="Day"/>
      <desc>
        <p>This function computes the day of the week given <c><anno>Year</anno></c>,
          <c><anno>Month</anno></c> and <c><anno>Day</anno></c>. The return value denotes the day
          of the week as <c>1</c>: Monday, <c>2</c>: Tuesday, and so on.</p>
      </desc>
    </func>
    <func>
      <name name="gregorian_days_to_date" arity="1"/>
      <fsummary>Compute the date given the number of gregorian days</fsummary>
      <desc>
        <p>This function computes the date given the number of
          gregorian days.</p>
      </desc>
    </func>
    <func>
      <name name="gregorian_seconds_to_datetime" arity="1"/>
      <fsummary>Compute the date given the number of gregorian days</fsummary>
      <desc>
        <p>This function computes the date and time from the given
          number of gregorian seconds.</p>
      </desc>
    </func>
    <func>
      <name name="is_leap_year" arity="1"/>
      <fsummary>Check if a year is a leap year</fsummary>
      <desc>
        <p>This function checks if a year is a leap year.</p>
      </desc>
    </func>
    <func>
      <name name="iso_week_number" arity="0"/>
      <fsummary>Compute the iso week number for the actual date</fsummary>
      <desc>
        <p>This function returns the tuple {Year, WeekNum} representing
          the iso week number for the actual date. For determining the
          actual date, the function <c>local_time/0</c> is used.</p>
      </desc>
    </func>
    <func>
      <name name="iso_week_number" arity="1"/>
      <fsummary>Compute the iso week number for the given date</fsummary>
      <desc>
        <p>This function returns the tuple {Year, WeekNum} representing
          the iso week number for the given date.</p>
      </desc>
    </func>
    <func>
      <name name="last_day_of_the_month" arity="2"/>
      <fsummary>Compute the number of days in a month</fsummary>
      <desc>
        <p>This function computes the number of days in a month.</p>
      </desc>
    </func>
    <func>
      <name name="local_time" arity="0"/>
      <fsummary>Compute local time</fsummary>
      <desc>
        <p>This function returns the local time reported by
          the underlying operating system.</p>
      </desc>
    </func>
    <func>
      <name name="local_time_to_universal_time" arity="1"/>
      <fsummary>Convert from local time to universal time (deprecated)</fsummary>
      <desc>
        <p>This function converts from local time to Universal 
          Coordinated Time (UTC). <c><anno>DateTime1</anno></c> must refer to a local
          date after Jan 1, 1970.</p>
        <warning>
          <p>This function is deprecated. Use
            <c>local_time_to_universal_time_dst/1</c> instead, as it
            gives a more correct and complete result. Especially for
            the period that does not exist since it gets skipped during
            the switch <em>to</em> daylight saving time, this function
            still returns a result.</p>
        </warning>
      </desc>
    </func>
    <func>
      <name name="local_time_to_universal_time_dst" arity="1"/>
      <fsummary>Convert from local time to universal time(s)</fsummary>
      <desc>
        <p>This function converts from local time to Universal
          Coordinated Time (UTC). <c><anno>DateTime1</anno></c> must refer to a local
          date after Jan 1, 1970.</p>
        <p>The return value is a list of 0, 1 or 2 possible UTC times:</p>
        <taglist>
          <tag><c>[]</c></tag>
          <item>
            <p>For a local <c>{Date1, Time1}</c> during the period that
              is skipped when switching <em>to</em> daylight saving
              time, there is no corresponding UTC since the local time
              is illegal - it has never happened.</p>
          </item>
          <tag><c>[DstDateTimeUTC, DateTimeUTC]</c></tag>
          <item>
            <p>For a local <c>{Date1, Time1}</c> during the period that
              is repeated when switching <em>from</em> daylight saving
              time, there are two corresponding UTCs. One for the first
              instance of the period when daylight saving time is still
              active, and one for the second instance.</p>
          </item>
          <tag><c>[DateTimeUTC]</c></tag>
          <item>
            <p>For all other local times there is only one
              corresponding UTC.</p>
          </item>
        </taglist>
      </desc>
    </func>
    <func>
      <name name="now_to_local_time" arity="1"/>
      <fsummary>Convert now to local date and time</fsummary>
      <desc>
        <p>This function returns local date and time converted from
        the return value from
	<seealso marker="erts:erlang#timestamp/0"><c>erlang:timestamp/0</c></seealso>.</p>
      </desc>
    </func>
    <func>
      <name name="now_to_universal_time" arity="1"/>
      <name name="now_to_datetime" arity="1"/>
      <fsummary>Convert now to date and time</fsummary>
      <desc>
        <p>This function returns Universal Coordinated Time (UTC)
        converted from the return value from
	<seealso marker="erts:erlang#timestamp/0"><c>erlang:timestamp/0</c></seealso>.</p>
      </desc>
    </func>
    <func>
      <name name="seconds_to_daystime" arity="1"/>
      <fsummary>Compute days and time from seconds</fsummary>
      <desc>
        <p>This function transforms a given number of seconds into days,
          hours, minutes, and seconds. The <c><anno>Time</anno></c> part is always
          non-negative, but <c><anno>Days</anno></c> is negative if the argument
          <c><anno>Seconds</anno></c> is.</p>
      </desc>
    </func>
    <func>
      <name name="seconds_to_time" arity="1"/>
      <fsummary>Compute time from seconds</fsummary>
      <type name="secs_per_day"/>
      <desc>
        <p>This function computes the time from the given number of
          seconds. <c><anno>Seconds</anno></c> must be less than the number of
          seconds per day (86400).</p>
      </desc>
    </func>
    <func>
      <name name="time_difference" arity="2"/>
      <fsummary>Compute the difference between two times (deprecated)</fsummary>
      <desc>
        <p>This function returns the difference between two <c>{Date, Time}</c> tuples. <c><anno>T2</anno></c> should refer to an epoch later
          than <c><anno>T1</anno></c>.</p>
        <warning>
          <p>This function is obsolete. Use the conversion functions for
            gregorian days and seconds instead.</p>
        </warning>
      </desc>
    </func>
    <func>
      <name name="time_to_seconds" arity="1"/>
      <fsummary>Compute the number of seconds since midnight up to  the given time</fsummary>
      <type name="secs_per_day"/>
      <desc>
        <p>This function computes the number of seconds since midnight
          up to the specified time.</p>
      </desc>
    </func>
    <func>
      <name name="universal_time" arity="0"/>
      <fsummary>Compute universal time</fsummary>
      <desc>
        <p>This function returns the Universal Coordinated Time (UTC)
          reported by the underlying operating system. Local time is
          returned if universal time is not available.</p>
      </desc>
    </func>
    <func>
      <name name="universal_time_to_local_time" arity="1"/>
      <fsummary>Convert from universal time to local time</fsummary>
      <desc>
        <p>This function converts from Universal Coordinated Time (UTC)
          to local time. <c><anno>DateTime</anno></c> must refer to a date after Jan 1,
          1970.</p>
      </desc>
    </func>
    <func>
      <name name="valid_date" arity="1"/>
      <name name="valid_date" arity="3"/>
      <type variable="Date" name_i="1"/>
      <type variable="Year"/>
      <type variable="Month"/>
      <type variable="Day"/>
      <fsummary>Check if a date is valid</fsummary>
      <desc>
        <p>This function checks if a date is a valid.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>Leap Years</title>
    <p>The notion that every fourth year is a leap year is not
      completely true. By the Gregorian rule, a year Y is a leap year if
      either of the following rules is valid:</p>
    <list type="bulleted">
      <item>
        <p>Y is divisible by 4, but not by 100; or</p>
      </item>
      <item>
        <p>Y is divisible by 400.</p>
      </item>
    </list>
    <p>Accordingly, 1996 is a leap year, 1900 is not, but 2000 is.</p>
  </section>

  <section>
    <title>Date and Time Source</title>
    <p>Local time is obtained from the Erlang BIF <c>localtime/0</c>.
      Universal time is computed from the BIF <c>universaltime/0</c>.</p>
    <p>The following facts apply:</p>
    <list type="bulleted">
      <item>there are 86400 seconds in a day</item>
      <item>there are 365 days in an ordinary  year</item>
      <item>there are 366 days in a leap year</item>
      <item>there are 1461 days in a 4 year period</item>
      <item>there are 36524 days in a 100 year period</item>
      <item>there are 146097 days in a 400 year period</item>
      <item>there are 719528 days between Jan 1, 0 and Jan 1, 1970.</item>
    </list>
  </section>
</erlref>