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