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
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." } ;
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 duarionts with the " { $link <=> } " word." } ;
13 { timestamp duration } related-words
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 } "." } ;
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." }
23 { $example "USING: calendar prettyprint ;"
24 "2010 12 25 <date> >gmt midnight ."
25 "T{ timestamp { year 2010 } { month 12 } { day 25 } }"
30 { $values { "array" array } }
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." } ;
35 { $values { "n" integer } { "string" string } }
36 { $description "Looks up the month name and returns it as a string. January has an index of 1 instead of zero." } ;
38 HELP: month-abbreviations
39 { $values { "array" 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." } ;
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." } ;
49 { $values { "array" array } }
50 { $description "Returns an array with the English names of the days of the week." } ;
53 { $values { "n" integer } { "string" string } }
54 { $description "Looks up the day name and returns it as a string." } ;
56 HELP: day-abbreviations2
57 { $values { "array" array } }
58 { $description "Returns an array with the abbreviated English names of the days of the week. This abbreviation is two characters long." } ;
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." } ;
64 HELP: day-abbreviations3
65 { $values { "array" array } }
66 { $description "Returns an array with the abbreviated English names of the days of the week. This abbreviation is three characters long." } ;
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." } ;
74 day-abbreviation2 day-abbreviations2
75 day-abbreviation3 day-abbreviations3
79 { $values { "ratio" 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." } ;
83 { $values { "integer" integer } }
84 { $description "Returns the number of months in a year." } ;
87 { $values { "ratio" 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." } ;
91 { $values { "ratio" 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." } ;
94 HELP: minutes-per-year
95 { $values { "ratio" 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." } ;
98 HELP: seconds-per-year
99 { $values { "integer" 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." } ;
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." } ;
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
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 ."
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 ."
130 { >date< >time< } related-words
133 { $values { "duration" duration } }
134 { $description "Pushes a " { $snippet "duration" } " of zero seconds." } ;
137 { $values { "x" number } { "duration" duration } }
138 { $description "Creates a duration object with the specified number of years." } ;
141 { $values { "x" number } { "duration" duration } }
142 { $description "Creates a duration object with the specified number of months." } ;
145 { $values { "x" number } { "duration" duration } }
146 { $description "Creates a duration object with the specified number of days." } ;
149 { $values { "x" number } { "duration" duration } }
150 { $description "Creates a duration object with the specified number of weeks." } ;
153 { $values { "x" number } { "duration" duration } }
154 { $description "Creates a duration object with the specified number of hours." } ;
157 { $values { "x" number } { "duration" duration } }
158 { $description "Creates a duration object with the specified number of minutes." } ;
161 { $values { "x" number } { "duration" duration } }
162 { $description "Creates a duration object with the specified number of seconds." } ;
165 { $values { "x" number } { "duration" duration } }
166 { $description "Creates a duration object with the specified number of milliseconds." } ;
169 { $values { "x" number } { "duration" duration } }
170 { $description "Creates a duration object with the specified number of microseconds." } ;
173 { $values { "x" number } { "duration" duration } }
174 { $description "Creates a duration object with the specified number of nanoseconds." } ;
176 { years months days hours minutes seconds milliseconds microseconds nanoseconds } related-words
179 { $values { "obj" object } { "?" "a boolean" } }
180 { $description "Returns " { $link t } " if the object represents a leap year." }
182 { $example "USING: calendar prettyprint ;"
186 { $example "USING: calendar prettyprint ;"
187 "2010 1 1 <date> leap-year? ."
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." }
196 { $example "USING: calendar math.order prettyprint ;"
197 "10 months 2 months time+ 1 years <=> ."
200 { $example "USING: accessors calendar math.order prettyprint ;"
201 "2010 1 1 <date> 3 days time+ day>> ."
207 { $values { "duration" duration } { "x" number } }
208 { $description "Calculates the length of a duration in years." }
210 { $example "USING: calendar prettyprint ;"
211 "6 months duration>years ."
216 HELP: duration>months
217 { $values { "duration" duration } { "x" number } }
218 { $description "Calculates the length of a duration in months." }
220 { $example "USING: calendar prettyprint ;"
221 "30 days duration>months ."
227 { $values { "duration" duration } { "x" number } }
228 { $description "Calculates the length of a duration in days." }
230 { $example "USING: calendar prettyprint ;"
231 "6 hours duration>days ."
237 { $values { "duration" duration } { "x" number } }
238 { $description "Calculates the length of a duration in hours." }
240 { $example "USING: calendar prettyprint ;"
241 "3/4 days duration>hours ."
245 HELP: duration>minutes
246 { $values { "duration" duration } { "x" number } }
247 { $description "Calculates the length of a duration in minutes." }
249 { $example "USING: calendar prettyprint ;"
250 "6 hours duration>minutes ."
254 HELP: duration>seconds
255 { $values { "duration" duration } { "x" number } }
256 { $description "Calculates the length of a duration in seconds." }
258 { $example "USING: calendar prettyprint ;"
259 "6 minutes duration>seconds ."
264 HELP: duration>milliseconds
265 { $values { "duration" duration } { "x" number } }
266 { $description "Calculates the length of a duration in milliseconds." }
268 { $example "USING: calendar prettyprint ;"
269 "6 seconds duration>milliseconds ."
274 HELP: duration>microseconds
275 { $values { "duration" duration } { "x" number } }
276 { $description "Calculates the length of a duration in microseconds." }
278 { $example "USING: calendar prettyprint ;"
279 "6 seconds duration>microseconds ."
284 HELP: duration>nanoseconds
285 { $values { "duration" duration } { "x" number } }
286 { $description "Calculates the length of a duration in nanoseconds." }
288 { $example "USING: calendar prettyprint ;"
289 "6 seconds duration>nanoseconds ."
294 { duration>years duration>months duration>days duration>hours duration>minutes duration>seconds duration>milliseconds duration>microseconds duration>nanoseconds } related-words
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." }
301 { $example "USING: calendar math.order prettyprint ;"
302 "10 months 2 months time- 8 months <=> ."
305 { $example "USING: accessors calendar math.order prettyprint ;"
306 "2010 1 1 <date> 3 days time- day>> ."
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" } "." }
315 { $example "USING: accessors calendar prettyprint ;"
316 "gmt noon instant -5 >>hour convert-timezone gmt-offset>> hour>> ."
322 { $values { "timestamp" timestamp } { "timestamp" timestamp } }
323 { $description "Converts the " { $snippet "timestamp" } " to the timezone of your computer." }
325 { $example "USING: accessors calendar kernel prettyprint ;"
326 "now gmt >local-time [ gmt-offset>> ] bi@ = ."
332 { $values { "timestamp" timestamp } { "timestamp" timestamp } }
333 { $description "Converts the " { $snippet "timestamp" } " to the GMT timezone." }
335 { $example "USING: accessors calendar kernel prettyprint ;"
336 "now >gmt gmt-offset>> hour>> ."
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
347 { $values { "duration" duration } { "-duration" duration } }
348 { $description "Negates a duration." }
350 { $example "USING: accessors calendar prettyprint ;"
351 "3 hours before now noon time+ hour>> ."
357 { $values { "timestamp" timestamp } }
358 { $description "Outputs a zero timestamp that consists of zeros for every slot. Used to see if timestamps are valid." } ;
360 HELP: valid-timestamp?
361 { $values { "timestamp" timestamp } { "?" "a boolean" } }
362 { $description "Tests if a timestamp is valid or not." } ;
365 { $values { "timestamp" timestamp } }
366 { $description "Outputs the beginning of UNIX time, or midnight, January 1, 1970." } ;
368 HELP: micros>timestamp
369 { $values { "x" number } { "timestamp" timestamp } }
370 { $description "Converts a number of microseconds into a timestamp value in GMT time." }
372 { $example "USING: accessors calendar prettyprint ;"
373 "1000 micros>timestamp year>> ."
379 { $values { "timestamp" timestamp } }
380 { $description "Outputs the time right now, but in the GMT timezone." } ;
382 { gmt now } related-words
385 { $values { "timestamp" timestamp } }
386 { $description "Outputs the time right now in your computer's timezone." }
388 { $unchecked-example "USING: calendar prettyprint ;"
390 "T{ timestamp f 2008 9 1 16 38 24+801/1000 T{ duration f 0 0 0 -5 0 0 } }"
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 } "." }
399 "USING: calendar prettyprint ;"
401 "T{ timestamp f 2008 9 2 2 47 45+943/1000 T{ duration f 0 0 0 -5 0 0 } }"
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 } "." }
410 "USING: calendar prettyprint ;"
412 "T{ timestamp f 2008 8 11 16 49 52+99/500 T{ duration f 0 0 0 -5 0 0 } }"
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." } ;
422 { $values { "obj" "a timestamp or an integer" } { "n" integer } }
423 { $description "Calculates the number of days in a given year." }
425 { $example "USING: calendar prettyprint ;"
426 "2004 days-in-year ."
432 { $values { "timestamp" timestamp } { "n" integer } }
433 { $description "Calculates the number of days in a given month." }
435 { $example "USING: calendar prettyprint ;"
436 "2008 8 24 <date> days-in-month ."
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." }
445 { $example "USING: calendar prettyprint ;"
446 "now sunday day-of-week ."
452 { $values { "timestamp" timestamp } { "n" integer } }
453 { $description "Calculates the day of the year, resulting in a number from 1 to 366 (leap years)." }
455 { $example "USING: calendar prettyprint ;"
456 "2008 1 4 <date> day-of-year ."
462 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
463 { $description "Returns the Sunday from the current week, which starts on a Sunday." } ;
466 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
467 { $description "Returns the Monday from the current week, which starts on a Sunday." } ;
470 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
471 { $description "Returns the Tuesday from the current week, which starts on a Sunday." } ;
474 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
475 { $description "Returns the Wednesday from the current week, which starts on a Sunday." } ;
478 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
479 { $description "Returns the Thursday from the current week, which starts on a Sunday." } ;
482 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
483 { $description "Returns the Friday from the current week, which starts on a Sunday." } ;
486 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
487 { $description "Returns the Saturday from the current week, which starts on a Sunday." } ;
489 { sunday monday tuesday wednesday thursday friday saturday } related-words
492 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
493 { $description "Returns a timestamp that represents today at midnight, or the beginning of the day." } ;
496 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
497 { $description "Returns a timestamp that represents today at noon, or the middle of the day." } ;
499 HELP: beginning-of-month
500 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
501 { $description "Outputs a timestamp with the day set to one." } ;
503 HELP: beginning-of-week
504 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
505 { $description "Outputs a timestamp where the day of the week is Sunday." } ;
507 HELP: beginning-of-year
508 { $values { "timestamp" timestamp } { "new-timestamp" timestamp } }
509 { $description "Outputs a timestamp with the month and day set to one, or January 1 of the input timestamp." } ;
511 HELP: time-since-midnight
512 { $values { "timestamp" timestamp } { "duration" duration } }
513 { $description "Calculates a " { $snippet "duration" } " that represents the elapsed time since midnight of the input " { $snippet "timestamp" } "." } ;
517 { "duration" duration }
518 { "timestamp" timestamp } }
519 { $description "Adds the duration to the beginning of Unix time and returns the result as a timestamp." } ;
521 ARTICLE: "calendar" "Calendar"
522 "The two data types used throughout the calendar library:"
523 { $subsection timestamp }
524 { $subsection duration }
525 "Durations represent spans of time:"
526 { $subsection "using-durations" }
527 "Arithmetic on timestamps and durations:"
528 { $subsection "timestamp-arithmetic" }
529 "Getting the current timestamp:"
532 "Converting between timestamps:"
533 { $subsection >local-time }
535 "Converting between timezones:"
536 { $subsection convert-timezone }
537 "Timestamps relative to each other:"
538 { $subsection "relative-timestamps" }
539 "Operations on units of time:"
540 { $subsection "years" }
541 { $subsection "months" }
542 { $subsection "days" }
543 "Meta-data about the calendar:"
544 { $subsection "calendar-facts" }
547 ARTICLE: "timestamp-arithmetic" "Timestamp arithmetic"
548 "Adding timestamps and durations, or durations and durations:"
549 { $subsection time+ }
551 { $subsection time- }
552 "Element-wise multiplication:"
553 { $subsection time* } ;
555 ARTICLE: "using-durations" "Using durations"
556 "Creating a duration object:"
557 { $subsection years }
558 { $subsection months }
559 { $subsection weeks }
561 { $subsection hours }
562 { $subsection minutes }
563 { $subsection seconds }
564 { $subsection milliseconds }
565 { $subsection microseconds }
566 { $subsection nanoseconds }
567 { $subsection instant }
568 "Converting a duration to a number:"
569 { $subsection duration>years }
570 { $subsection duration>months }
571 { $subsection duration>days }
572 { $subsection duration>hours }
573 { $subsection duration>minutes }
574 { $subsection duration>seconds }
575 { $subsection duration>milliseconds }
576 { $subsection duration>microseconds }
577 { $subsection duration>nanoseconds } ;
579 ARTICLE: "relative-timestamps" "Relative timestamps"
581 { $subsection hence }
585 { $subsection before }
586 "Days of the week relative to " { $link now } ":"
587 { $subsection sunday }
588 { $subsection monday }
589 { $subsection tuesday }
590 { $subsection wednesday }
591 { $subsection thursday }
592 { $subsection friday }
593 { $subsection saturday }
594 "New timestamps relative to calendar events:"
595 { $subsection beginning-of-year }
596 { $subsection beginning-of-month }
597 { $subsection beginning-of-week }
598 { $subsection midnight }
602 ARTICLE: "days" "Day operations"
604 { $subsection day-abbreviation2 }
605 { $subsection day-abbreviations2 }
606 { $subsection day-abbreviation3 }
607 { $subsection day-abbreviations3 }
608 { $subsection day-name }
609 { $subsection day-names }
610 "Calculating a Julian day number:"
611 { $subsection julian-day-number }
612 "Calculate a timestamp:"
613 { $subsection julian-day-number>date }
616 ARTICLE: "calendar-facts" "Calendar facts"
618 { $subsection average-month }
619 { $subsection months-per-year }
620 { $subsection days-per-year }
621 { $subsection hours-per-year }
622 { $subsection minutes-per-year }
623 { $subsection seconds-per-year }
624 { $subsection days-in-month }
625 { $subsection day-of-year }
626 { $subsection day-of-week }
629 ARTICLE: "years" "Year operations"
630 "Leap year predicate:"
631 { $subsection leap-year? }
632 "Find the number of days in a year:"
633 { $subsection days-in-year }
636 ARTICLE: "months" "Month operations"
638 { $subsection month-name }
639 { $subsection month-names }
640 { $subsection month-abbreviation }
641 { $subsection month-abbreviations }