
(provide (quote timezone))

(defvar timezone-world-timezones (quote (("EET" . -200) ("MET" . -100) ("GMT" . 0) ("EDT" . 400) ("EST" . 500) ("CDT" . 500) ("CST" . 600) ("MDT" . 600) ("MST" . 700) ("PDT" . 700) ("PST" . 800) ("JST" . 900) ("GMT+1" . 100) ("GMT+2" . 200) ("GMT+3" . 300) ("GMT+4" . 400) ("GMT+5" . 500) ("GMT+6" . 600) ("GMT+7" . 700) ("GMT+8" . 800) ("GMT+9" . 900) ("GMT+10" . 1000) ("GMT+11" . 1100) ("GMT+12" . 1200) ("GMT+13" . 1300) ("GMT-1" . -100) ("GMT-2" . -200) ("GMT-3" . -300) ("GMT-4" . -400) ("GMT-5" . -500) ("GMT-6" . -600) ("GMT-7" . -700) ("GMT-8" . -800) ("GMT-9" . -900) ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))) "\
*Time differentials of timezone from GMT in hour.")

(defvar timezone-months-assoc (quote (("JAN" . 1) ("FEB" . 2) ("MAR" . 3) ("APR" . 4) ("MAY" . 5) ("JUN" . 6) ("JUL" . 7) ("AUG" . 8) ("SEP" . 9) ("OCT" . 10) ("NOV" . 11) ("DEC" . 12))) "\
Alist of first three letters of a month and its numerical representation.")

(defun timezone-make-date-arpa-standard (date &optional local timezone) "\
Convert DATE to an arpanet standard date.
Optional 1st argumetn LOCAL specifies the default local timezone of the DATE.
Optional 2nd argument TIMEZONE specifies a timezone to be represented in." (byte-code "Ì!ÍÎH!ÍÏH!ÍÐH!ÑÒH!ÍÎH!ÍÏH!ÍÐH!ÓH†8 	†A 	Ô	!Ô!Z
Õ	

\\&ÖÎHÏHÐH×ÒHÓHØH#	%.‡" [date year month day time hour minute second local timezone diff new timezone-parse-date string-to-int 0 1 2 timezone-parse-time 3 4 timezone-zone-to-hour timezone-fix-time timezone-make-arpa-date timezone-make-time-string 5] 20))

(defun timezone-make-date-sortable (date &optional local timezone) "\
Convert DATE to a sortable date string.
Optional 1st argumetn LOCAL specifies the default local timezone of the DATE.
Optional 2nd argument TIMEZONE specifies a timezone to be represented in." (byte-code "Ì!ÍÎH!ÍÏH!ÍÐH!ÑÒH!ÍÎH!ÍÏH!ÍÐH!ÓH†8 	†A 	Ô	!Ô!Z
Õ	

\\&ÖÎHÏHÐH×ÒHÓHØH#$.‡" [date year month day time hour minute second local timezone diff new timezone-parse-date string-to-int 0 1 2 timezone-parse-time 3 4 timezone-zone-to-hour timezone-fix-time timezone-make-sortable-date timezone-make-time-string 5] 20))

(defun timezone-make-arpa-date (year month day time &optional timezone) "\
Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
Optional argument TIMEZONE specifies a time zone." (byte-code "ÆÇÈÉ	
\"@!ÊËÌ\"Ì\"Zƒ ÍP‚ Î&‡" [day month timezone-months-assoc year time timezone format "%02d %s %02d %s%s" capitalize rassq * / 100 " " ""] 12))

(defun timezone-make-sortable-date (year month day time) "\
Make sortable date string from YEAR, MONTH, DAY, and TIME." (byte-code "ÄÅÆÇÈ\"È\"Z	
%‡" [year month day time format "%02d%02d%02d%s" * / 100] 8))

(defun timezone-make-time-string (hour minute second) "\
Make time string from HOUR, MINUTE, and SECOND." (byte-code "ÃÄ	
$‡" [hour minute second format "%02d:%02d:%02d"] 5))

(defun timezone-parse-date (date) "\
Parse DATE and return a vector [year month day time timezone].
19 is prepended to year if necessary. Timezone may be NIL if nothing.
Understand the following styles:
 (1) 14 Apr 89 03:20[:12] [GMT]
 (2) Fri, 17 Mar 89 4:01[:33] [GMT]
 (3) Mon Jan 16 16:12[:37] [GMT] 1989" (byte-code "† ÈÂÂÂÂÂÉÊ\"ƒ' ËÌÍÎÂ‰‚f ÉÏ\"ƒ= ËÌÍÎÐ‰‚f ÉÑ\"ƒS ÎÍÌËÂ‰‚f ÉÒ\"…f ÐÍÌËÎ‰ˆ	…­ Ó	!Ô	!O‰ˆ	GÌV…ƒ 	ÕÂO‰ˆÖ×ØÓ!Ô!O!\"A!‰ˆÓ!Ô!O‰ˆÓ!Ô!O‰ˆ…À Ó!Ô!O‰ˆ	ƒÐ Ù	%‚× ÙÚÚÚÚÂ%.‡" [date year nil month day time zone timezone-months-assoc "" string-match "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\'" 3 2 1 4 "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\([-+a-zA-Z0-9]+\\)" 5 "\\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([0-9]+\\)" "\\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([-+a-zA-Z0-9]+\\) \\([0-9]+\\)" match-beginning match-end -2 int-to-string assoc upcase vector "0"] 24))

(defun timezone-parse-time (time) "\
Parse TIME (HH:MM:SS) and return a vector [hour minute second]." (byte-code "† ÅÂÂÂÆÇ\"ƒ ÈÉÊ‰‚+ ÆË\"…+ ÈÉÂ‰ˆÌ	ƒ< Í	!Î	!O‚= ÏƒL Í!Î!O‚M Ïƒ\\ Í!Î!O‚] Ï#,‡" [time hour nil minute second "" string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" 1 2 3 "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" vector match-beginning match-end "0"] 14))

(defun timezone-zone-to-hour (timezone) "\
Translate TIMEZONE (in zonename or integer) to integer hour." (byte-code "ƒ$ ÂÃ!	\"A† ‰ˆ;… Ä!‰ˆÅÆ\"‚% Ç‡" [timezone timezone-world-timezones assoc upcase string-to-int / 100 0] 6))

(defun timezone-fix-time (year month day hour minute second) "\
Fix date and time." (byte-code "ÆXƒ4 ÆZ‰ˆ	T‰ˆÇ
\"	W…1 
T‰ˆÈ‰ˆÉ
W…1 È‰ˆT‰‚e ÊV…e Æ\\‰ˆ	S‰ˆÈ	V…e 
S‰ˆÈ
V…^ É‰ˆS‰ˆÇ
\"‰ˆË
	&‡" [hour day month year minute second 24 timezone-last-day-of-month 1 12 0 vector] 9))

(defun timezone-last-day-of-month (month year) "\
The last day in MONTH during YEAR." (byte-code "ÂU…	 Ã	!ƒ Ä‚ ÅSH‡" [month year 2 timezone-leap-year-p 29 [31 28 31 30 31 30 31 31 30 31 30 31]] 3))

(defun timezone-leap-year-p (year) "\
Returns t if YEAR is a Gregorian leap year." (byte-code "ÁÂÃ\"!… ÁÂÄ\"!?† ÁÂÅ\"!‡" [year zerop mod 4 100 400] 8))
