]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar-docs.factor
74a06717f1638657bd5c469b6b8cdcb79998b726
[factor.git] / basis / calendar / calendar-docs.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math strings help.markup help.syntax
4 math.order ;
5 IN: calendar
6
7 HELP: duration
8 { $description "A duration is a period of time years, months, days, hours, minutes, and seconds. All duration slots can store " { $link real } " numbers. Compare two durations with the " { $link <=> } " word." } ;
9
10 HELP: timestamp
11 { $description "A timestamp is a date and a time with a timezone offset. Timestamp slots must store integers except for " { $snippet "seconds" } ", which stores reals, and " { $snippet "gmt-offset" } ", which stores a " { $link duration } ". Compare two durations with the " { $link <=> } " word." } ;
12
13 { timestamp duration } related-words
14
15 HELP: gmt-offset-duration
16 { $values { "duration" duration } }
17 { $description "Returns a " { $link duration } " object with the GMT offset returned by " { $link gmt-offset } "." } ;
18
19 HELP: <date>
20 { $values { "year" integer } { "month" integer } { "day" integer } { "timestamp" timestamp } }
21 { $description "Returns a timestamp object representing the start of the specified day in your current timezone." }
22 { $examples
23     { $example "USING: accessors calendar prettyprint ;"
24                "2010 12 25 <date> instant >>gmt-offset ."
25                "T{ timestamp { year 2010 } { month 12 } { day 25 } }"
26     }
27 } ;
28
29 HELP: average-month
30 { $values { "value" ratio } }
31 { $description "The length of an average month averaged over 400 years. Used internally for adding an arbitrary real number of months to a timestamp." } ;
32
33 HELP: months-per-year
34 { $values { "value" integer } }
35 { $description "Returns the number of months in a year." } ;
36
37 HELP: days-per-year
38 { $values { "value" ratio } }
39 { $description "Returns the number of days in a year averaged over 400 years. Used internally for adding an arbitrary real number of days to a timestamp." } ;
40
41 HELP: hours-per-year
42 { $values { "value" ratio } }
43 { $description "Returns the number of hours in a year averaged over 400 years. Used internally for adding an arbitrary real number of hours to a timestamp." } ;
44
45 HELP: minutes-per-year
46 { $values { "value" ratio } }
47 { $description "Returns the number of minutes in a year averaged over 400 years. Used internally for adding an arbitrary real number of minutes to a timestamp." } ;
48
49 HELP: seconds-per-year
50 { $values { "value" integer } }
51 { $description "Returns the number of seconds in a year averaged over 400 years. Used internally for adding an arbitrary real number of seconds to a timestamp." } ;
52
53 HELP: julian-day-number
54 { $values { "year" integer } { "month" integer } { "day" integer } { "n" integer } }
55 { $description "Calculates the Julian day number from a year, month, and day. The difference between two Julian day numbers is the number of days that have elapsed between the two corresponding dates." }
56 { $warning "Not valid before year -4800 BCE." } ;
57
58 HELP: julian-day-number>date
59 { $values { "n" integer } { "year" integer } { "month" integer } { "day" integer } }
60 { $description "Converts from a Julian day number back to a year, month, and day." } ;
61 { julian-day-number julian-day-number>date } related-words
62
63 HELP: >date<
64 { $values { "timestamp" timestamp } { "year" integer } { "month" integer } { "day" integer } }
65 { $description "Explodes a " { $snippet "timestamp" } " into its year, month, and day components." }
66 { $examples { $example "USING: arrays calendar prettyprint ;"
67                        "2010 8 24 <date> >date< 3array ."
68                        "{ 2010 8 24 }"
69                        }
70 } ;
71
72 HELP: >time<
73 { $values { "timestamp" timestamp } { "hour" integer } { "minute" integer } { "second" integer } }
74 { $description "Explodes a " { $snippet "timestamp" } " into its hour, minute, and second components." }
75 { $examples { $example "USING: arrays calendar prettyprint ;"
76                        "now noon >time< 3array ."
77                        "{ 12 0 0 }"
78                        }
79 } ;
80
81 { >date< >time< } related-words
82
83 HELP: instant
84 { $values { "duration" duration } }
85 { $description "Pushes a " { $snippet "duration" } " of zero seconds." } ;
86
87 HELP: years
88 { $values { "x" number } { "duration" duration } }
89 { $description "Creates a duration object with the specified number of years." } ;
90
91 HELP: months
92 { $values { "x" number } { "duration" duration } }
93 { $description "Creates a duration object with the specified number of months." } ;
94
95 HELP: days
96 { $values { "x" number } { "duration" duration } }
97 { $description "Creates a duration object with the specified number of days." } ;
98
99 HELP: weeks
100 { $values { "x" number } { "duration" duration } }
101 { $description "Creates a duration object with the specified number of weeks." } ;
102
103 HELP: hours
104 { $values { "x" number } { "duration" duration } }
105 { $description "Creates a duration object with the specified number of hours." } ;
106
107 HELP: minutes
108 { $values { "x" number } { "duration" duration } }
109 { $description "Creates a duration object with the specified number of minutes." } ;
110
111 HELP: seconds
112 { $values { "x" number } { "duration" duration } }
113 { $description "Creates a duration object with the specified number of seconds." } ;
114
115 HELP: milliseconds
116 { $values { "x" number } { "duration" duration } }
117 { $description "Creates a duration object with the specified number of milliseconds." } ;
118
119 HELP: microseconds
120 { $values { "x" number } { "duration" duration } }
121 { $description "Creates a duration object with the specified number of microseconds." } ;
122
123 HELP: nanoseconds
124 { $values { "x" number } { "duration" duration } }
125 { $description "Creates a duration object with the specified number of nanoseconds." } ;
126
127 { years months days hours minutes seconds milliseconds microseconds nanoseconds } related-words
128
129 HELP: leap-year?
130 { $values { "obj" object } { "?" boolean } }
131 { $description "Returns " { $link t } " if the object represents a leap year." }
132 { $examples
133     { $example "USING: calendar prettyprint ;"
134                "2008 leap-year? ."
135                "t"
136     }
137     { $example "USING: calendar prettyprint ;"
138                "2010 1 1 <date> leap-year? ."
139                "f"
140     }
141 } ;
142
143 HELP: time+
144 { $values { "time1" { $or timestamp duration } } { "time2" { $or timestamp duration } } { "time3" { $or timestamp duration } } }
145 { $description "Adds two durations to produce a duration or adds a timestamp and a duration to produce a timestamp. The calculation takes timezones into account." }
146 { $examples
147     { $example "USING: calendar math.order prettyprint ;"
148                "10 months 2 months time+ 1 years <=> ."
149                "+eq+"
150     }
151     { $example "USING: accessors calendar math.order prettyprint ;"
152                "2010 1 1 <date> 3 days time+ day>> ."
153                "4"
154     }
155 } ;
156
157 { time+ time- } related-words
158
159 HELP: duration>years
160 { $values { "duration" duration } { "x" number } }
161 { $description "Calculates the length of a duration in years." }
162 { $examples
163     { $example "USING: calendar prettyprint ;"
164                "6 months duration>years ."
165                "1/2"
166     }
167 } ;
168
169 HELP: duration>months
170 { $values { "duration" duration } { "x" number } }
171 { $description "Calculates the length of a duration in months." }
172 { $examples
173     { $example "USING: calendar prettyprint ;"
174                "30 days duration>months ."
175                "16000/16233"
176     }
177 } ;
178
179 HELP: duration>days
180 { $values { "duration" duration } { "x" number } }
181 { $description "Calculates the length of a duration in days." }
182 { $examples
183     { $example "USING: calendar prettyprint ;"
184                "6 hours duration>days ."
185                "1/4"
186     }
187 } ;
188
189 HELP: duration>hours
190 { $values { "duration" duration } { "x" number } }
191 { $description "Calculates the length of a duration in hours." }
192 { $examples
193     { $example "USING: calendar prettyprint ;"
194                "3/4 days duration>hours ."
195                "18"
196     }
197 } ;
198 HELP: duration>minutes
199 { $values { "duration" duration } { "x" number } }
200 { $description "Calculates the length of a duration in minutes." }
201 { $examples
202     { $example "USING: calendar prettyprint ;"
203                "6 hours duration>minutes ."
204                "360"
205     }
206 } ;
207 HELP: duration>seconds
208 { $values { "duration" duration } { "x" number } }
209 { $description "Calculates the length of a duration in seconds." }
210 { $examples
211     { $example "USING: calendar prettyprint ;"
212                "6 minutes duration>seconds ."
213                "360"
214     }
215 } ;
216
217 HELP: duration>milliseconds
218 { $values { "duration" duration } { "x" number } }
219 { $description "Calculates the length of a duration in milliseconds." }
220 { $examples
221     { $example "USING: calendar prettyprint ;"
222                "6 seconds duration>milliseconds ."
223                "6000"
224     }
225 } ;
226
227 HELP: duration>microseconds
228 { $values { "duration" duration } { "x" number } }
229 { $description "Calculates the length of a duration in microseconds." }
230 { $examples
231     { $example "USING: calendar prettyprint ;"
232                "6 seconds duration>microseconds ."
233                "6000000"
234     }
235 } ;
236
237 HELP: duration>nanoseconds
238 { $values { "duration" duration } { "x" number } }
239 { $description "Calculates the length of a duration in nanoseconds." }
240 { $examples
241     { $example "USING: calendar prettyprint ;"
242                "6 seconds duration>nanoseconds ."
243                "6000000000"
244     }
245 } ;
246
247 { duration>years duration>months duration>days duration>hours duration>minutes duration>seconds duration>milliseconds duration>microseconds duration>nanoseconds } related-words
248
249
250 HELP: time-
251 { $values { "time1" { $or timestamp duration } } { "time2" { $or timestamp duration } } { "time3" { $or timestamp duration } } }
252 { $description "Subtracts two durations to produce a duration or subtracts a duration from a timestamp to produce a timestamp. The calculation takes timezones into account." }
253 { $examples
254     { $example "USING: calendar math.order prettyprint ;"
255                "10 months 2 months time- 8 months <=> ."
256                "+eq+"
257     }
258     { $example "USING: accessors calendar math.order prettyprint ;"
259                "2010 1 1 <date> 3 days time- day>> ."
260                "29"
261     }
262 } ;
263
264 HELP: convert-timezone
265 { $values { "timestamp" timestamp } { "duration" duration } }
266 { $description "Converts the " { $snippet "timestamp" } "'s " { $snippet "gmt-offset" } " to the GMT offset represented by the " { $snippet "duration" } "." }
267 { $examples
268     { $example "USING: accessors calendar prettyprint ;"
269                "now >gmt noon -5 hours convert-timezone gmt-offset>> hour>> ."
270                "-5"
271     }
272 } ;
273
274 HELP: >local-time
275 { $values { "timestamp" timestamp } }
276 { $description "Converts the " { $snippet "timestamp" } " to the timezone of your computer." }
277 { $examples
278     { $example "USING: accessors calendar kernel prettyprint ;"
279                "now dup clone >gmt >local-time [ gmt-offset>> ] same? ."
280                "t"
281     }
282 } ;
283
284 HELP: >gmt
285 { $values { "timestamp" timestamp } }
286 { $description "Converts the " { $snippet "timestamp" } " to the GMT timezone." }
287 { $examples
288     { $example "USING: accessors calendar kernel prettyprint ;"
289                "now >gmt gmt-offset>> hour>> ."
290                "0"
291     }
292 } ;
293
294 HELP: local-time
295 { $values { "timestamp" timestamp } }
296 { $description "Set the time zone to the computer's local timezone." }
297 { $notes "The time is not converted, if you want that then call " { $link >local-time } "." } ;
298
299 HELP: gmt
300 { $values { "timestamp" timestamp } }
301 { $description "Set the time zone to GMT." }
302 { $notes "The time is not converted, if you want that then call " { $link >gmt } "." } ;
303
304 { local-time >local-time gmt >gmt convert-timezone } related-words
305
306 HELP: duration*
307 { $values { "obj1" object } { "obj2" object } { "obj3" object } }
308 { $description "Multiplies each time slot of a duration by a number and make a new duration from the result. Used in the implementation of " { $link before } "." } ;
309
310 HELP: before
311 { $values { "duration" duration } { "-duration" duration } }
312 { $description "Negates a duration." }
313 { $examples
314     { $example "USING: accessors calendar prettyprint ;"
315                "3 hours before now noon time+ hour>> ."
316                "9"
317     }
318 } ;
319
320 HELP: unix-1970
321 { $values { "timestamp" timestamp } }
322 { $description "Returns the beginning of UNIX time, or midnight, January 1, 1970." } ;
323
324 HELP: micros>timestamp
325 { $values { "x" number } { "timestamp" timestamp } }
326 { $description "Converts a number of microseconds into a timestamp value in GMT time." }
327 { $examples
328     { $example "USING: accessors calendar prettyprint ;"
329                "1000 micros>timestamp year>> ."
330                "1970"
331     }
332 } ;
333
334 HELP: now
335 { $values { "timestamp" timestamp } }
336 { $description "Returns the time right now in your computer's timezone." }
337 { $examples
338     { $unchecked-example "USING: calendar prettyprint ;"
339         "now ."
340          "T{ timestamp f 2008 9 1 16 38 24+801/1000 T{ duration f 0 0 0 -5 0 0 } }"
341     }
342 } ;
343
344 HELP: hence
345 { $values { "duration" duration } { "timestamp" timestamp } }
346 { $description "Computes a time in the future that is the " { $snippet "duration" } " added to the result of " { $link now } "." }
347 { $examples
348     { $unchecked-example
349        "USING: calendar prettyprint ;"
350        "10 hours hence ."
351        "T{ timestamp f 2008 9 2 2 47 45+943/1000 T{ duration f 0 0 0 -5 0 0 } }"
352     }
353 } ;
354
355 HELP: ago
356 { $values { "duration" duration } { "timestamp" timestamp } }
357 { $description "Computes a time in the past that is the " { $snippet "duration" } " subtracted from the result of " { $link now } "." }
358 { $examples
359     { $unchecked-example
360        "USING: calendar prettyprint ;"
361        "3 weeks ago ."
362        "T{ timestamp f 2008 8 11 16 49 52+99/500 T{ duration f 0 0 0 -5 0 0 } }"
363     }
364 } ;
365
366 HELP: (day-of-week)
367 { $values { "year" integer } { "month" integer } { "day" integer } { "n" integer } }
368 { $description "An implementation of the Zeller's congruence algorithm that computes the day of the week given a date. Days are indexed starting from Sunday, which is index 0." }
369 { $notes "User code should use the " { $link day-of-week } " word, which takes a " { $snippet "timestamp" } " instead of integers." } ;
370
371 HELP: days-in-year
372 { $values { "obj" { $or timestamp integer } } { "n" integer } }
373 { $description "Calculates the number of days in a given year." }
374 { $examples
375     { $example "USING: calendar prettyprint ;"
376                "2004 days-in-year ."
377                "366"
378     }
379 } ;
380
381 HELP: days-in-month
382 { $values { "timestamp" timestamp } { "n" integer } }
383 { $description "Calculates the number of days in a given month." }
384 { $examples
385     { $example "USING: calendar prettyprint ;"
386                "2008 8 24 <date> days-in-month ."
387                "31"
388     }
389 } ;
390
391 HELP: day-of-week
392 { $values { "timestamp" timestamp } { "n" integer } }
393 { $description "Calculates the index of the day of the week. Sunday will result in an index of 0." }
394 { $examples
395     { $example "USING: calendar prettyprint ;"
396                "now sunday day-of-week ."
397                "0"
398     }
399 } ;
400
401 HELP: day-of-year
402 { $values { "timestamp" timestamp } { "n" integer } }
403 { $description "Calculates the day of the year, resulting in a number from 1 to 366 (leap years)." }
404 { $examples
405     { $example "USING: calendar prettyprint ;"
406                "2008 1 4 <date> day-of-year ."
407                "4"
408     }
409 } ;
410
411 HELP: week-number
412 { $values { "timestamp" timestamp } { "[1,53]" integer } }
413 { $description "Calculates the ISO 8601 week number from 1 to 53 (leap years). See " { $snippet "https://en.wikipedia.org/wiki/ISO_week_date" } }
414 { $examples
415     "Last day of 2018 is already in the first week of 2019."
416     { $example "USING: calendar prettyprint ;"
417                "2018 12 31 <date> week-number ."
418                "1"
419     }
420     "2020 is a leap year with 53 weeks, and January 1st, 2021 is still in week 53 of 2020."
421     { $example "USING: calendar prettyprint ;"
422                "2021 1 1 <date> week-number ."
423                "53"
424     }
425 } ;
426
427 HELP: sunday
428 { $values { "timestamp" timestamp } }
429 { $description "Returns the Sunday from the current week, which starts on a Sunday." } ;
430
431 HELP: monday
432 { $values { "timestamp" timestamp } }
433 { $description "Returns the Monday from the current week, which starts on a Sunday." } ;
434
435 HELP: tuesday
436 { $values { "timestamp" timestamp } }
437 { $description "Returns the Tuesday from the current week, which starts on a Sunday." } ;
438
439 HELP: wednesday
440 { $values { "timestamp" timestamp } }
441 { $description "Returns the Wednesday from the current week, which starts on a Sunday." } ;
442
443 HELP: thursday
444 { $values { "timestamp" timestamp } }
445 { $description "Returns the Thursday from the current week, which starts on a Sunday." } ;
446
447 HELP: friday
448 { $values { "timestamp" timestamp } }
449 { $description "Returns the Friday from the current week, which starts on a Sunday." } ;
450
451 HELP: saturday
452 { $values { "timestamp" timestamp } }
453 { $description "Returns the Saturday from the current week, which starts on a Sunday." } ;
454
455 { sunday monday tuesday wednesday thursday friday saturday } related-words
456
457 HELP: midnight
458 { $values { "timestamp" timestamp } }
459 { $description "Sets the timestamp to represent the day at midnight, or the beginning of the day." } ;
460
461 HELP: noon
462 { $values { "timestamp" timestamp } }
463 { $description "Sets the timestamp to represent the day at noon, or the middle of the day." } ;
464
465 HELP: today
466 { $values { "timestamp" timestamp } }
467 { $description "Sets the timestamp to represents today at midnight." } ;
468
469 HELP: start-of-month
470 { $values { "timestamp" timestamp } }
471 { $description "Sets the timestamp with the day set to one." } ;
472
473 HELP: start-of-week
474 { $values { "timestamp" timestamp } }
475 { $description "Sets the timestamp with the day of the week set to Sunday." } ;
476
477 HELP: start-of-year
478 { $values { "object" object } { "timestamp" timestamp } }
479 { $description "Sets the timestamp with the month and day set to one, or January 1 of the input timestamp, given a year or a timestamp." } ;
480
481 HELP: time-since-midnight
482 { $values { "timestamp" timestamp } { "duration" duration } }
483 { $description "Calculates a " { $snippet "duration" } " that represents the elapsed time since midnight of the input " { $snippet "timestamp" } "." } ;
484
485 HELP: since-1970
486 { $values
487      { "duration" duration }
488      { "timestamp" timestamp } }
489 { $description "Adds the duration to the beginning of Unix time and returns the result as a timestamp." } ;
490
491 ARTICLE: "calendar" "Calendar"
492 "The " { $vocab-link "calendar" } " vocabulary defines two data types and a set of operations on them:"
493 { $subsections
494     timestamp
495     duration
496 }
497 "Durations represent spans of time:"
498 { $subsections "using-durations" }
499 "Arithmetic on timestamps and durations:"
500 { $subsections "timestamp-arithmetic" }
501 "Getting the current timestamp:"
502 { $subsections
503     now
504     gmt
505 }
506 "Time zones:"
507 { $subsections
508     >local-time
509     >gmt
510     convert-timezone
511 }
512 "Timestamps relative to each other:"
513 { $subsections "relative-timestamps" }
514 "Operations on units of time:"
515 { $subsections
516     "years"
517     "months"
518     "days"
519 }
520 "Both " { $link timestamp } "s and " { $link duration } "s implement the " { $link "math.order" } "."
521 $nl
522 "Meta-data about the calendar:"
523 { $subsections "calendar-facts" } ;
524
525 ARTICLE: "timestamp-arithmetic" "Timestamp arithmetic"
526 "Adding timestamps and durations, or durations and durations:"
527 { $subsections time+ }
528 "Subtracting:"
529 { $subsections time- }
530 "Multiplying durations:"
531 { $subsections duration* } ;
532
533 ARTICLE: "using-durations" "Using durations"
534 "Creating a duration object:"
535 { $subsections
536     years
537     months
538     weeks
539     days
540     hours
541     minutes
542     seconds
543     milliseconds
544     microseconds
545     nanoseconds
546     instant
547 }
548 "Converting a duration to a number:"
549 { $subsections
550     duration>years
551     duration>months
552     duration>days
553     duration>hours
554     duration>minutes
555     duration>seconds
556     duration>milliseconds
557     duration>microseconds
558     duration>nanoseconds
559 } ;
560
561 ARTICLE: "relative-timestamps" "Relative timestamps"
562 "In the future:"
563 { $subsections hence }
564 "In the past:"
565 { $subsections ago }
566 "Invert a duration:"
567 { $subsections before }
568 "Days of the week relative to " { $link now } ":"
569 { $subsections
570     sunday
571     monday
572     tuesday
573     wednesday
574     thursday
575     friday
576     saturday
577 }
578 "New timestamps relative to calendar events:"
579 { $subsections
580     start-of-year
581     start-of-month
582     start-of-week
583     midnight
584     noon
585 } ;
586
587 ARTICLE: "calendar-facts" "Calendar facts"
588 "Calendar facts:"
589 { $subsections
590     average-month
591     months-per-year
592     days-per-year
593     hours-per-year
594     minutes-per-year
595     seconds-per-year
596     days-in-month
597     day-of-year
598     day-of-week
599     week-number
600 }
601 "Calculating a Julian day number:"
602 { $subsections julian-day-number }
603 "Calculate a timestamp:"
604 { $subsections julian-day-number>date } ;
605
606 ARTICLE: "years" "Year operations"
607 "Leap year predicate:"
608 { $subsections leap-year? }
609 "Find the number of days in a year:"
610 { $subsections days-in-year } ;
611
612 ABOUT: "calendar"