]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/calendar-docs.factor
calendar: cleanup some of the API by making words mutate timestamps.
[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 HELP: duration>years
158 { $values { "duration" duration } { "x" number } }
159 { $description "Calculates the length of a duration in years." }
160 { $examples
161     { $example "USING: calendar prettyprint ;"
162                "6 months duration>years ."
163                "1/2"
164     }
165 } ;
166
167 HELP: duration>months
168 { $values { "duration" duration } { "x" number } }
169 { $description "Calculates the length of a duration in months." }
170 { $examples
171     { $example "USING: calendar prettyprint ;"
172                "30 days duration>months ."
173                "16000/16233"
174     }
175 } ;
176
177 HELP: duration>days
178 { $values { "duration" duration } { "x" number } }
179 { $description "Calculates the length of a duration in days." }
180 { $examples
181     { $example "USING: calendar prettyprint ;"
182                "6 hours duration>days ."
183                "1/4"
184     }
185 } ;
186
187 HELP: duration>hours
188 { $values { "duration" duration } { "x" number } }
189 { $description "Calculates the length of a duration in hours." }
190 { $examples
191     { $example "USING: calendar prettyprint ;"
192                "3/4 days duration>hours ."
193                "18"
194     }
195 } ;
196 HELP: duration>minutes
197 { $values { "duration" duration } { "x" number } }
198 { $description "Calculates the length of a duration in minutes." }
199 { $examples
200     { $example "USING: calendar prettyprint ;"
201                "6 hours duration>minutes ."
202                "360"
203     }
204 } ;
205 HELP: duration>seconds
206 { $values { "duration" duration } { "x" number } }
207 { $description "Calculates the length of a duration in seconds." }
208 { $examples
209     { $example "USING: calendar prettyprint ;"
210                "6 minutes duration>seconds ."
211                "360"
212     }
213 } ;
214
215 HELP: duration>milliseconds
216 { $values { "duration" duration } { "x" number } }
217 { $description "Calculates the length of a duration in milliseconds." }
218 { $examples
219     { $example "USING: calendar prettyprint ;"
220                "6 seconds duration>milliseconds ."
221                "6000"
222     }
223 } ;
224
225 HELP: duration>microseconds
226 { $values { "duration" duration } { "x" number } }
227 { $description "Calculates the length of a duration in microseconds." }
228 { $examples
229     { $example "USING: calendar prettyprint ;"
230                "6 seconds duration>microseconds ."
231                "6000000"
232     }
233 } ;
234
235 HELP: duration>nanoseconds
236 { $values { "duration" duration } { "x" number } }
237 { $description "Calculates the length of a duration in nanoseconds." }
238 { $examples
239     { $example "USING: calendar prettyprint ;"
240                "6 seconds duration>nanoseconds ."
241                "6000000000"
242     }
243 } ;
244
245 { duration>years duration>months duration>days duration>hours duration>minutes duration>seconds duration>milliseconds duration>microseconds duration>nanoseconds } related-words
246
247
248 HELP: time-
249 { $values { "time1" { $or timestamp duration } } { "time2" { $or timestamp duration } } { "time3" { $or timestamp duration } } }
250 { $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." }
251 { $examples
252     { $example "USING: calendar math.order prettyprint ;"
253                "10 months 2 months time- 8 months <=> ."
254                "+eq+"
255     }
256     { $example "USING: accessors calendar math.order prettyprint ;"
257                "2010 1 1 <date> 3 days time- day>> ."
258                "29"
259     }
260 } ;
261
262 HELP: convert-timezone
263 { $values { "timestamp" timestamp } { "duration" duration } }
264 { $description "Converts the " { $snippet "timestamp" } "'s " { $snippet "gmt-offset" } " to the GMT offset represented by the " { $snippet "duration" } "." }
265 { $examples
266     { $example "USING: accessors calendar prettyprint ;"
267                "now-gmt noon instant -5 >>hour convert-timezone gmt-offset>> hour>> ."
268                "-5"
269     }
270 } ;
271
272 HELP: >local-time
273 { $values { "timestamp" timestamp } }
274 { $description "Converts the " { $snippet "timestamp" } " to the timezone of your computer." }
275 { $examples
276     { $example "USING: accessors calendar kernel prettyprint ;"
277                "now now-gmt >local-time [ gmt-offset>> ] same? ."
278                "t"
279     }
280 } ;
281
282 HELP: >gmt
283 { $values { "timestamp" timestamp } }
284 { $description "Converts the " { $snippet "timestamp" } " to the GMT timezone." }
285 { $examples
286     { $example "USING: accessors calendar kernel prettyprint ;"
287                "now >gmt gmt-offset>> hour>> ."
288                "0"
289     }
290 } ;
291
292 HELP: local-time
293 { $values { "timestamp" timestamp } }
294 { $description "Set the time zone to the computer's local timezone." }
295 { $notes "The time is not converted, if you want that then call " { $link >local-time } "." } ;
296
297 HELP: gmt
298 { $values { "timestamp" timestamp } }
299 { $description "Set the time zone to GMT." }
300 { $notes "The time is not converted, if you want that then call " { $link >gmt } "." } ;
301
302 { local-time >local-time gmt >gmt convert-timezone } related-words
303
304 HELP: time*
305 { $values { "obj1" object } { "obj2" object } { "obj3" object } }
306 { $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 } "." } ;
307 { time+ time- time* } related-words
308
309 HELP: before
310 { $values { "duration" duration } { "-duration" duration } }
311 { $description "Negates a duration." }
312 { $examples
313     { $example "USING: accessors calendar prettyprint ;"
314                "3 hours before now noon time+ hour>> ."
315                "9"
316     }
317 } ;
318
319 HELP: unix-1970
320 { $values { "timestamp" timestamp } }
321 { $description "Returns the beginning of UNIX time, or midnight, January 1, 1970." } ;
322
323 HELP: micros>timestamp
324 { $values { "x" number } { "timestamp" timestamp } }
325 { $description "Converts a number of microseconds into a timestamp value in GMT time." }
326 { $examples
327     { $example "USING: accessors calendar prettyprint ;"
328                "1000 micros>timestamp year>> ."
329                "1970"
330     }
331 } ;
332
333 HELP: now-gmt
334 { $values { "timestamp" timestamp } }
335 { $description "Returns the time right now, but in the GMT timezone." } ;
336
337 { now now-gmt } related-words
338
339 HELP: now
340 { $values { "timestamp" timestamp } }
341 { $description "Returns the time right now in your computer's timezone." }
342 { $examples
343     { $unchecked-example "USING: calendar prettyprint ;"
344         "now ."
345          "T{ timestamp f 2008 9 1 16 38 24+801/1000 T{ duration f 0 0 0 -5 0 0 } }"
346     }
347 } ;
348
349 HELP: hence
350 { $values { "duration" duration } { "timestamp" timestamp } }
351 { $description "Computes a time in the future that is the " { $snippet "duration" } " added to the result of " { $link now } "." }
352 { $examples
353     { $unchecked-example
354        "USING: calendar prettyprint ;"
355        "10 hours hence ."
356        "T{ timestamp f 2008 9 2 2 47 45+943/1000 T{ duration f 0 0 0 -5 0 0 } }"
357     }
358 } ;
359
360 HELP: ago
361 { $values { "duration" duration } { "timestamp" timestamp } }
362 { $description "Computes a time in the past that is the " { $snippet "duration" } " subtracted from the result of " { $link now } "." }
363 { $examples
364     { $unchecked-example
365        "USING: calendar prettyprint ;"
366        "3 weeks ago ."
367        "T{ timestamp f 2008 8 11 16 49 52+99/500 T{ duration f 0 0 0 -5 0 0 } }"
368     }
369 } ;
370
371 HELP: (day-of-week)
372 { $values { "year" integer } { "month" integer } { "day" integer } { "n" integer } }
373 { $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." }
374 { $notes "User code should use the " { $link day-of-week } " word, which takes a " { $snippet "timestamp" } " instead of integers." } ;
375
376 HELP: days-in-year
377 { $values { "obj" { $or timestamp integer } } { "n" integer } }
378 { $description "Calculates the number of days in a given year." }
379 { $examples
380     { $example "USING: calendar prettyprint ;"
381                "2004 days-in-year ."
382                "366"
383     }
384 } ;
385
386 HELP: days-in-month
387 { $values { "timestamp" timestamp } { "n" integer } }
388 { $description "Calculates the number of days in a given month." }
389 { $examples
390     { $example "USING: calendar prettyprint ;"
391                "2008 8 24 <date> days-in-month ."
392                "31"
393     }
394 } ;
395
396 HELP: day-of-week
397 { $values { "timestamp" timestamp } { "n" integer } }
398 { $description "Calculates the index of the day of the week. Sunday will result in an index of 0." }
399 { $examples
400     { $example "USING: calendar prettyprint ;"
401                "now sunday day-of-week ."
402                "0"
403     }
404 } ;
405
406 HELP: day-of-year
407 { $values { "timestamp" timestamp } { "n" integer } }
408 { $description "Calculates the day of the year, resulting in a number from 1 to 366 (leap years)." }
409 { $examples
410     { $example "USING: calendar prettyprint ;"
411                "2008 1 4 <date> day-of-year ."
412                "4"
413     }
414 } ;
415
416 HELP: week-number
417 { $values { "timestamp" timestamp } { "[1,53]" integer } }
418 { $description "Calculates the ISO 8601 week number from 1 to 53 (leap years). See " { $snippet "https://en.wikipedia.org/wiki/ISO_week_date" } }
419 { $examples
420     "Last day of 2018 is already in the first week of 2019."
421     { $example "USING: calendar prettyprint ;"
422                "2018 12 31 <date> week-number ."
423                "1"
424     }
425     "2020 is a leap year with 53 weeks, and January 1st, 2021 is still in week 53 of 2020."
426     { $example "USING: calendar prettyprint ;"
427                "2021 1 1 <date> week-number ."
428                "53"
429     }
430 } ;
431
432 HELP: sunday
433 { $values { "timestamp" timestamp } }
434 { $description "Returns the Sunday from the current week, which starts on a Sunday." } ;
435
436 HELP: monday
437 { $values { "timestamp" timestamp } }
438 { $description "Returns the Monday from the current week, which starts on a Sunday." } ;
439
440 HELP: tuesday
441 { $values { "timestamp" timestamp } }
442 { $description "Returns the Tuesday from the current week, which starts on a Sunday." } ;
443
444 HELP: wednesday
445 { $values { "timestamp" timestamp } }
446 { $description "Returns the Wednesday from the current week, which starts on a Sunday." } ;
447
448 HELP: thursday
449 { $values { "timestamp" timestamp } }
450 { $description "Returns the Thursday from the current week, which starts on a Sunday." } ;
451
452 HELP: friday
453 { $values { "timestamp" timestamp } }
454 { $description "Returns the Friday from the current week, which starts on a Sunday." } ;
455
456 HELP: saturday
457 { $values { "timestamp" timestamp } }
458 { $description "Returns the Saturday from the current week, which starts on a Sunday." } ;
459
460 { sunday monday tuesday wednesday thursday friday saturday } related-words
461
462 HELP: midnight
463 { $values { "timestamp" timestamp } }
464 { $description "Sets the timestamp to represent the day at midnight, or the beginning of the day." } ;
465
466 HELP: noon
467 { $values { "timestamp" timestamp } }
468 { $description "Sets the timestamp to represent the day at noon, or the middle of the day." } ;
469
470 HELP: today
471 { $values { "timestamp" timestamp } }
472 { $description "Sets the timestamp to represents today at midnight." } ;
473
474 HELP: start-of-month
475 { $values { "timestamp" timestamp } }
476 { $description "Sets the timestamp with the day set to one." } ;
477
478 HELP: start-of-week
479 { $values { "timestamp" timestamp } }
480 { $description "Sets the timestamp with the day of the week set to Sunday." } ;
481
482 HELP: start-of-year
483 { $values { "object" object } { "timestamp" timestamp } }
484 { $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." } ;
485
486 HELP: time-since-midnight
487 { $values { "timestamp" timestamp } { "duration" duration } }
488 { $description "Calculates a " { $snippet "duration" } " that represents the elapsed time since midnight of the input " { $snippet "timestamp" } "." } ;
489
490 HELP: since-1970
491 { $values
492      { "duration" duration }
493      { "timestamp" timestamp } }
494 { $description "Adds the duration to the beginning of Unix time and returns the result as a timestamp." } ;
495
496 ARTICLE: "calendar" "Calendar"
497 "The " { $vocab-link "calendar" } " vocabulary defines two data types and a set of operations on them:"
498 { $subsections
499     timestamp
500     duration
501 }
502 "Durations represent spans of time:"
503 { $subsections "using-durations" }
504 "Arithmetic on timestamps and durations:"
505 { $subsections "timestamp-arithmetic" }
506 "Getting the current timestamp:"
507 { $subsections
508     now
509     gmt
510 }
511 "Time zones:"
512 { $subsections
513     >local-time
514     >gmt
515     convert-timezone
516 }
517 "Timestamps relative to each other:"
518 { $subsections "relative-timestamps" }
519 "Operations on units of time:"
520 { $subsections
521     "years"
522     "months"
523     "days"
524 }
525 "Both " { $link timestamp } "s and " { $link duration } "s implement the " { $link "math.order" } "."
526 $nl
527 "Meta-data about the calendar:"
528 { $subsections "calendar-facts" } ;
529
530 ARTICLE: "timestamp-arithmetic" "Timestamp arithmetic"
531 "Adding timestamps and durations, or durations and durations:"
532 { $subsections time+ }
533 "Subtracting:"
534 { $subsections time- }
535 "Element-wise multiplication:"
536 { $subsections time* } ;
537
538 ARTICLE: "using-durations" "Using durations"
539 "Creating a duration object:"
540 { $subsections
541     years
542     months
543     weeks
544     days
545     hours
546     minutes
547     seconds
548     milliseconds
549     microseconds
550     nanoseconds
551     instant
552 }
553 "Converting a duration to a number:"
554 { $subsections
555     duration>years
556     duration>months
557     duration>days
558     duration>hours
559     duration>minutes
560     duration>seconds
561     duration>milliseconds
562     duration>microseconds
563     duration>nanoseconds
564 } ;
565
566 ARTICLE: "relative-timestamps" "Relative timestamps"
567 "In the future:"
568 { $subsections hence }
569 "In the past:"
570 { $subsections ago }
571 "Invert a duration:"
572 { $subsections before }
573 "Days of the week relative to " { $link now } ":"
574 { $subsections
575     sunday
576     monday
577     tuesday
578     wednesday
579     thursday
580     friday
581     saturday
582 }
583 "New timestamps relative to calendar events:"
584 { $subsections
585     start-of-year
586     start-of-month
587     start-of-week
588     midnight
589     noon
590 } ;
591
592 ARTICLE: "calendar-facts" "Calendar facts"
593 "Calendar facts:"
594 { $subsections
595     average-month
596     months-per-year
597     days-per-year
598     hours-per-year
599     minutes-per-year
600     seconds-per-year
601     days-in-month
602     day-of-year
603     day-of-week
604     week-number
605 }
606 "Calculating a Julian day number:"
607 { $subsections julian-day-number }
608 "Calculate a timestamp:"
609 { $subsections julian-day-number>date } ;
610
611 ARTICLE: "years" "Year operations"
612 "Leap year predicate:"
613 { $subsections leap-year? }
614 "Find the number of days in a year:"
615 { $subsections days-in-year } ;
616
617 ABOUT: "calendar"