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