Author: wmb Date: 2007-11-07 23:34:41 +0100 (Wed, 07 Nov 2007) New Revision: 719
Added: ofw/fs/unixtime.fth Modified: ofw/fs/ext2fs/dir.fth ofw/fs/ext2fs/methods.fth ofw/fs/jffs2/jffs2.fth Log: Factored out the conversion routines to and from Unix time values (seconds since 1 Jan 1970) so only one copy is needed. This helps with OLPC trac 4707, avoiding the need for a third copy of those routines.
Modified: ofw/fs/ext2fs/dir.fth =================================================================== --- ofw/fs/ext2fs/dir.fth 2007-11-07 07:56:38 UTC (rev 718) +++ ofw/fs/ext2fs/dir.fth 2007-11-07 22:34:41 UTC (rev 719) @@ -47,50 +47,11 @@ \ \ After this point, the code should be independent of the disk format!
-\ time stamps - -\ date&time is number of seconds since 1970 -create days/month -\ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec - 31 c, 28 c, 31 c, 30 c, 31 c, 30 c, 31 c, 31 c, 30 c, 31 c, 30 c, 31 c, - -: >d/m ( day-in-year -- day month ) - 12 0 do - days/month i ca+ c@ 2dup < if - drop 1+ i 1+ leave - then - - - loop -; -: sec>time&date ( seconds -- s m h d m y ) - 60 u/mod 60 u/mod 24 u/mod ( s m h days ) - [ 365 4 * 1+ ] literal /mod >r ( s m h day-in-cycle ) ( r: cycles ) - dup [ 365 365 + 31 + 29 + ] literal - 2dup = if \ exactly leap year Feb 29 - 3drop 2 29 2 ( s m h year-in-cycle d m ) - else - > if 1- then \ after leap year - 365 u/mod ( s m h day-in-year year-in-cycle ) - swap >d/m ( s m h year-in-cycle d m ) - then - rot r> 4 * + 1970 + ( s m h d m y ) -; -: timestamp ( s m h d m y -- seconds ) \ since 1970 - d# 1970 - 4 /mod [ d# 365 4 * 1+ ] literal * ( s m h d m yrs days ) - swap d# 365 * + ( s m h d m days ) - swap 1- 0 ?do i days/month + c@ + loop ( s m h d days ) - + 1- ( s m h days ) - d# 24 * + d# 60 * + d# 60 * + -; - -\ e.g. time&date timestamp -\ timestamp sec>time&date should have no net effect - : init-inode ( mode inode# -- ) inode >r ( mode ) r@ /inode erase ( mode ) r@ short! ( ) - time&date timestamp ( time ) + time&date >unix-seconds ( time ) dup r@ 2 la+ int! ( time ) \ set access time dup r@ 3 la+ int! ( time ) \ set creation time r@ 4 la+ int! ( ) \ set modification time @@ -256,7 +217,7 @@ inode# inode /inode 6 /l* /string erase
\ delete inode, and set its deletion time. - time&date timestamp ( time ) + time&date >unix-seconds ( time ) inode# inode 5 la+ int! update inode# free-inode ; @@ -292,7 +253,7 @@
: file-info ( id -- false | id' s m h d m y len attributes name$ true ) inode# >r file-handle to inode# ( id ) - 1+ file-sec sec>time&date file-size file-attr file-name true + 1+ file-sec unix-seconds> file-size file-attr file-name true r> to inode# ;
Modified: ofw/fs/ext2fs/methods.fth =================================================================== --- ofw/fs/ext2fs/methods.fth 2007-11-07 07:56:38 UTC (rev 718) +++ ofw/fs/ext2fs/methods.fth 2007-11-07 22:34:41 UTC (rev 719) @@ -71,7 +71,7 @@ drop bfbase @ bsize free-mem \ Registered with initbuf modified? if false to modified? - time&date timestamp file-handle inode 4 la+ int! update + time&date >unix-seconds file-handle inode 4 la+ int! update then ;
Modified: ofw/fs/jffs2/jffs2.fth =================================================================== --- ofw/fs/jffs2/jffs2.fth 2007-11-07 07:56:38 UTC (rev 718) +++ ofw/fs/jffs2/jffs2.fth 2007-11-07 22:34:41 UTC (rev 719) @@ -1465,37 +1465,6 @@ file-size ;
-\ This code is copied from ext2fs and could be shared -decimal -\ date&time is number of seconds since 1970 -create days/month -\ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec - 31 c, 28 c, 31 c, 30 c, 31 c, 30 c, 31 c, 31 c, 30 c, 31 c, 30 c, 31 c, - -: >d/m ( day-in-year -- day month ) - 12 0 do - days/month i ca+ c@ 2dup < if - drop 1+ i 1+ leave - then - - - loop -; -: sec>time&date ( seconds -- s m h d m y ) - 60 u/mod 60 u/mod 24 u/mod ( s m h days ) - [ 365 4 * 1+ ] literal /mod >r ( s m h day-in-cycle ) ( r: cycles ) - dup [ 365 365 + 31 + 29 + ] literal - 2dup = if \ exactly leap year Feb 29 - 3drop 2 29 2 ( s m h year-in-cycle d m ) - else - > if 1- then \ after leap year - 365 u/mod ( s m h day-in-year year-in-cycle ) - swap >d/m ( s m h year-in-cycle d m ) - then - rot r> 4 * + 1970 + ( s m h d m y ) -; -hex -\ End of common code - : next-file-info ( id -- false | id' s m h d m y len attributes name$ true ) dup 0= if drop prep-dirents minodes then ( tdirent ) dup next-minode = if drop false exit then ( tdirent ) @@ -1507,7 +1476,7 @@ 0 ( ... attributes r: tdirent ) else ( id' rinode r: tdirent ) >r ( id' r: tdirent rinode ) - r@ rimtime@ sec>time&date ( id' s m h d m y r: tdirent rinode ) + r@ rimtime@ unix-seconds> ( id' s m h d m y r: tdirent rinode ) r@ riisize@ ( id' s m h d m y len r: tdirent rinode ) r> rimode@ ( id' s m h d m y len mode r: tdirent ) then
Added: ofw/fs/unixtime.fth =================================================================== --- ofw/fs/unixtime.fth (rev 0) +++ ofw/fs/unixtime.fth 2007-11-07 22:34:41 UTC (rev 719) @@ -0,0 +1,66 @@ +\ See license at end of file +purpose: Convert Unix seconds to time and date + +decimal +\ date&time is number of seconds since 1970 +create days/month +\ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + 31 c, 28 c, 31 c, 30 c, 31 c, 30 c, 31 c, 31 c, 30 c, 31 c, 30 c, 31 c, + +: >d/m ( day-in-year -- day month ) + 12 0 do + days/month i ca+ c@ 2dup < if + drop 1+ i 1+ leave + then + - + loop +; +: unix-seconds> ( seconds -- s m h d m y ) + 60 u/mod 60 u/mod 24 u/mod ( s m h days ) + [ 365 4 * 1+ ] literal /mod >r ( s m h day-in-cycle ) ( r: cycles ) + dup [ 365 365 + 31 + 29 + ] literal + 2dup = if \ exactly leap year Feb 29 + 3drop 2 29 2 ( s m h year-in-cycle d m ) + else + > if 1- then \ after leap year + 365 u/mod ( s m h day-in-year year-in-cycle ) + swap >d/m ( s m h year-in-cycle d m ) + then + rot r> 4 * + 1970 + ( s m h d m y ) +; +: >unix-seconds ( s m h d m y -- seconds ) \ since 1970 + d# 1970 - 4 /mod [ d# 365 4 * 1+ ] literal * ( s m h d m yrs days ) + swap d# 365 * + ( s m h d m days ) + swap 1- 0 ?do i days/month + c@ + loop ( s m h d days ) + + 1- ( s m h days ) + d# 24 * + d# 60 * + d# 60 * + +; + +\ e.g. time&date >unix-seconds +\ >unix-seconds unix-seconds> should have no net effect + +hex + +\ LICENSE_BEGIN +\ Copyright (c) 2006 FirmWorks +\ +\ Permission is hereby granted, free of charge, to any person obtaining +\ a copy of this software and associated documentation files (the +\ "Software"), to deal in the Software without restriction, including +\ without limitation the rights to use, copy, modify, merge, publish, +\ distribute, sublicense, and/or sell copies of the Software, and to +\ permit persons to whom the Software is furnished to do so, subject to +\ the following conditions: +\ +\ The above copyright notice and this permission notice shall be +\ included in all copies or substantial portions of the Software. +\ +\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +\ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +\ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +\ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +\ +\ LICENSE_END