Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file/zip: clamp timestamps to supported range #4451

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkgs/racket-doc/file/scribblings/zip.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Timestamps in @exec{zip} archives are precise only to two seconds; by
default, the time is rounded toward the future (like WinZip or PKZIP),
but time is rounded toward the past (like Java) if
@racket[round-timestamps-down?] is true.
The @exec{zip} archive format only supports timestamps between January 1,
1980 and December 31, 2107. Timestamps outside of this range will be
clamped to the closest representable timestamp.

The @racket[sys-type] argument determines the system type recorded in
the archive.
Expand Down
30 changes: 24 additions & 6 deletions racket/collects/file/zip.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,33 @@

;; date->msdos-time : date -> msdos-time
(define (date->msdos-time date round-down?)
(bitwise-ior (arithmetic-shift (+ (if round-down? 0 1) (date-second date)) -1)
(arithmetic-shift (date-minute date) 5)
(arithmetic-shift (date-hour date) 11)))
(define-values (h m s)
;; Zip cannot represent timestamps before 1980-01-01T00:00:00
(cond
[(< (date-year date) 1980)
(values 0 0 0)]
[(> (date-year date) 2107)
(values 23 59 59)]
[else
(values (date-hour date) (date-minute date) (date-second date))]))
(bitwise-ior (arithmetic-shift (+ (if round-down? 0 1) s) -1)
(arithmetic-shift m 5)
(arithmetic-shift h 11)))

;; date->msdos-date : date -> msdos-date
(define (date->msdos-date date)
(bitwise-ior (date-day date)
(arithmetic-shift (date-month date) 5)
(arithmetic-shift (- (date-year date) 1980) 9)))
(define-values (y m d)
;; Zip cannot represent timestamps before 1980-01-01T00:00:00
(cond
[(< (date-year date) 1980)
(values 1980 1 1)]
[(> (date-year date) 2107)
(values 2107 12 31)]
[else
(values (date-year date) (date-month date) (date-day date))]))
(bitwise-ior d
(arithmetic-shift m 5)
(arithmetic-shift (- y 1980) 9)))

;; seekable-port? : port -> boolean
(define (seekable-port? port)
Expand Down