Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: utc2tod.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | id: @(#)utc2tod.fth 1.2 06/03/17 | |
43 | purpose: | |
44 | copyright: Copyright 2006 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | headerless | |
48 | ||
49 | \ ordinary year calendar | |
50 | create year-days | |
51 | d# 31 c, d# 28 c, d# 31 c, d# 30 c, d# 31 c, d# 30 c, | |
52 | d# 31 c, d# 31 c, d# 30 c, d# 31 c, d# 30 c, d# 31 c, | |
53 | ||
54 | \ leap-year calendar (february has 29 days) | |
55 | create leap-days | |
56 | d# 31 c, d# 29 c, d# 31 c, d# 30 c, d# 31 c, d# 30 c, | |
57 | d# 31 c, d# 31 c, d# 30 c, d# 31 c, d# 30 c, d# 31 c, | |
58 | ||
59 | \ Determine time within the day | |
60 | : utc-to-time ( utc -- h m s ) | |
61 | >r ( ) ( r: utc ) | |
62 | r@ d# d# 86400 mod d# 3600 / ( h ) ( r: utc ) | |
63 | r@ d# 3600 mod d# 60 / ( h m ) ( r: utc ) | |
64 | r> d# 60 mod ( h m s ) | |
65 | ; | |
66 | ||
67 | ||
68 | \ Walk through each month decrementing number of days left, leaving month and | |
69 | \ day on stack. This depends on january having longest month, so when we have | |
70 | \ found a date, subsequent calls just return leaving date unaltered. If we | |
71 | \ walk through the entire calendar's worth of days without having run out of | |
72 | \ days, we increment the year number and revert month to january. | |
73 | ||
74 | : calc-month ( year month days-left calendar -- year month days-left ) | |
75 | ||
76 | swap ( year month calendar days-left ) | |
77 | ||
78 | \ Walk through all twelve months, subtracting number of days per month | |
79 | \ until we find a month with more days than we have left - then return. | |
80 | d# 12 0 do | |
81 | over i + c@ 2dup > if ( year month cal days-left #days ) | |
82 | - rot 1+ -rot ( year month+1 calendar days-left ) | |
83 | else | |
84 | drop nip unloop exit ( year month day ) | |
85 | then | |
86 | loop ( year month cal days-left ) | |
87 | ||
88 | \ If we get here, we've decremented an entire year of days. Bump year#. | |
89 | nip -rot drop 1+ 1 rot ( year+1 1 days-left ) | |
90 | ; | |
91 | ||
92 | \ Convert utc (number of seconds since jan 0, 1970) to | |
93 | \ day of week, day of month, month and years-since-1900. | |
94 | ||
95 | : utc-to-date ( utc -- dow d m y ) | |
96 | ||
97 | d# 86400 / ( days-since-1970 ) | |
98 | dup 4 + 7 mod 1+ swap ( dow days-since-1970 ) | |
99 | ||
100 | \ Skip over 4-year groups (three years and one leap year), so we can then | |
101 | \ determine the date within the more complicated pattern of leap years. Each | |
102 | \ four-year group consists of 1461 days (365*3 + 366). Note that we depend | |
103 | \ on the fact that 2000 (unlike 1900 or 2100) was a leap year. This code | |
104 | \ will need to be fixed sometime this century. | |
105 | ||
106 | d# 1461 2dup / dup >r ( dow days-since-1970 quadreniums ) | |
107 | ( r: quadreniums ) | |
108 | * - ( dow days-since-quadrenium ) | |
109 | ( r: quadreniums ) | |
110 | ||
111 | \ Quadreniums * 4 + 70 = years since 1900. | |
112 | 1+ r> 4 * d# 70 + swap 1 swap ( dow year month days-left ) | |
113 | ||
114 | \ Walk through month-by-month within the quadrenium | |
115 | ||
116 | year-days calc-month ( dow year month days-left ) | |
117 | year-days calc-month ( dow year month days-left ) | |
118 | leap-days calc-month ( dow year month days-left ) | |
119 | year-days calc-month ( dow year month days-left ) | |
120 | -rot swap ( dow day month year ) | |
121 | ; | |
122 |