@@ -2,19 +2,19 @@ xquery version "3.0";
22
33(:~
44 : Utility functions for dates ( RFC822/RSS, MJD , elapsedTime)
5- : Import module
5+ : Import module
66 : import module namespace jmmc-dateutil="http://exist.jmmc.fr/jmmc-resources/dateutil" at "/db/apps/jmmc-resources/content/jmmc-dateutil.xql";
7- : or import last version of updated library
8- :
7+ : or import last version of updated library
8+ :
99 : TODO: re-implement following conversion formulae ( see https://fr.wikipedia.org/wiki/Jour_julien#cite_note-Source_Meeus-5 )
1010 : GM trusts xquery date handling LB not ;)
11- :
11+ :
1212 :)
1313module namespace jmmc-dateutil="http://exist.jmmc.fr/jmmc-resources/dateutil" ;
1414
1515(:~
1616 : Return day abbreviation.
17- :
17+ :
1818 : @param $dateTime date to analyse
1919 : @return Sun, Mon, Tue, Wed, Thu, Fri, Sat according given date.
2020 :)
@@ -30,17 +30,31 @@ as xs:string
3030
3131(:~
3232 : Return Month abreviation
33- :
33+ :
3434 : @param $dateTime given date
35- : @return month abreviation of given date
35+ : @return month abreviation of given date or empty sequence if unapplicable
3636 :)
3737declare function jmmc-dateutil:month-abbreviation ($dateTime as xs:dateTime)
38- as xs:string
38+ as xs:string?
3939{
40- ( 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec
41- ' )[ fn:month-from-dateTime ($dateTime)]
40+ let $m := map{ 1 : 'Jan' , 2 : 'Feb' , 3 : 'Mar' , 4 : 'Apr' , 5 : 'May' , 6 : 'Jun' , 7 : 'Jul' , 8 : 'Aug' , 9 : 'Sep' , 10 : 'Oct' , 11 : 'Nov' , 12 : 'Dec' }
41+ return $m( fn:month-from-dateTime ($dateTime))
4242};
4343
44+ (:~
45+ : Return Month index using the first 3 letters
46+ :
47+ : @param $month-name given month value
48+ : @return month index or empty sequence if unapplicable
49+ :)
50+ declare function jmmc-dateutil:month-index ($month-name as xs:string)
51+ as xs:string?
52+ {
53+ let $m := map{'jan' :'01' , 'feb' :'02' , 'mar' :'03' , 'apr' :'04' , 'may' :'05' , 'jun' :'06' , 'jul' :'07' , 'aug' :'08' , 'sep' :'09' , 'oct' :'10' , 'nov' :'11' , 'dec' :'12' }
54+ return $m(lower-case (substring ($month-name, 1 ,3 )))
55+ };
56+
57+
4458declare function jmmc-dateutil:format-timezone ($dateTime as xs:dateTime)
4559as xs:string
4660{
@@ -60,8 +74,22 @@ as xs:string
6074};
6175
6276(:~
63- : Return iso8601 date to RFC822 (used by RSS format)
64- :
77+ : Convert RFC822 date to iso8601 (may come from RSS of HTTP last-modified header)
78+ :
79+ : @param $rfcDate given RFC822 date string
80+ : @return iso8601 date
81+ :)
82+ declare function jmmc-dateutil:RFC822toISO8601 ($rfcDate as xs:string)
83+ as xs:dateTime
84+ {
85+ let $tks := tokenize ($rfcDate)
86+ let $els := ( string-join (($tks[4 ],jmmc-dateutil:month-index ($tks[3 ]),$tks[2 ]), '-' ), 'T' , $tks[5 ] )
87+ return xs:dateTime (string-join ($els))
88+ };
89+
90+ (:~
91+ : Convert iso8601 date to RFC822 (used by RSS format)
92+ :
6593 : @param $dateTime given date
6694 : @return rfc822 date
6795 :)
@@ -76,70 +104,69 @@ as xs:string
76104 jmmc-dateutil:format-number (fn:ceiling (fn:seconds-from-dateTime ($dateTime))), ' ' , '+0200' )
77105};
78106
79-
80107(:~
81108 : Convert a mjd date to iso8601
82- :
109+ :
83110 : @param $mjd Modified Julian Day to convert
84111 : @return the associated datetime
85112 :)
86113declare function jmmc-dateutil:MJDtoISO8601 ($mjd as xs:double)
87114as xs:dateTime
88115{
89116 (: http://tycho.usno.navy.mil/mjd.html
90- $JD0 := xs:dateTime("-4712-01-01T12:00:00")
91- $MJD0 := $JD0 + xs:dayTimeDuration('P1D')* 2400000.5
117+ $JD0 := xs:dateTime("-4712-01-01T12:00:00")
118+ $MJD0 := $JD0 + xs:dayTimeDuration('P1D')* 2400000.5
92119 := 1858-12-25T00:00:00 as computed by exist-db
93- but should be 1858-11-17T00:00:00
120+ but should be 1858-11-17T00:00:00
94121 :)
95122 xs:dateTime ("1858-11-17T00:00:00" ) + $mjd * xs:dayTimeDuration ('P1D' )
96123};
97124
98125(:~
99- : Convert an iso8601 date to mjd
100- :
126+ : Convert an iso8601 date to mjd
127+ :
101128 : @param $dateTime datetime to convert
102129 : @return the associated mjd
103130 :)
104131declare function jmmc-dateutil:ISO8601toMJD ($dateTime as xs:dateTime)
105132as xs:double
106133{
107- ($dateTime - xs:dateTime ("1858-11-17T00:00:00" )) div xs:dayTimeDuration ('P1D' )
134+ ($dateTime - xs:dateTime ("1858-11-17T00:00:00" )) div xs:dayTimeDuration ('P1D' )
108135};
109136
110137
111138(:~
112139 : Convert a Julian Day to iso8601
113- :
140+ :
114141 : @param $jd Julian Day to convert
115142 : @return the associated datetime
116143 :)
117144declare function jmmc-dateutil:JDtoISO8601 ($jd as xs:double)
118145as xs:dateTime
119146{
120147 (: http://aa.usno.navy.mil/cgi-bin/aa_jdconv.pl?form=2&jd=0
121- $JD0 := xs:dateTime("-4713-01-01T12:00:00")
122- but should be -4713-11-24T12:00:00
148+ $JD0 := xs:dateTime("-4713-01-01T12:00:00")
149+ but should be -4713-11-24T12:00:00
123150 :)
124151 xs:dateTime ("-4713-11-24T12:00:00" ) + $jd * xs:dayTimeDuration ('P1D' )
125152};
126153
127154
128155(:~
129156 : Convert an iso8601 to Julian Day
130- :
157+ :
131158 : @param $dateTime datetime to convert
132159 : @return the associated jd
133160 :)
134161declare function jmmc-dateutil:ISO8601toJD ($dateTime as xs:dateTime)
135162as xs:double
136163{
137- ($dateTime - xs:dateTime ("-4714-11-24T12:00:00" )) div xs:dayTimeDuration ('P1D' )
164+ ($dateTime - xs:dateTime ("-4714-11-24T12:00:00" )) div xs:dayTimeDuration ('P1D' )
138165};
139166
140167(:~
141168 : Convert an UT date to iso8601
142- :
169+ :
143170 : @param $ut UT date to convert [s]
144171 : @param $epoch reference epoch of J2000 if empty
145172 : @return the associated datetime
@@ -155,7 +182,7 @@ as xs:dateTime
155182
156183(:~
157184 : Convert an iso8601 to UT date
158- :
185+ :
159186 : @param $dateTime datetime to convert
160187 : @param $epoch reference epoch of J2000 if empty
161188 : @return the associated ut [s]
@@ -170,7 +197,7 @@ as xs:double
170197
171198(:~
172199 : Convert an UT date to MJD
173- :
200+ :
174201 : @param $dateTime datetime to convert
175202 : @param $epoch reference epoch of J2000 if empty
176203 : @return the associated ut [s]
0 commit comments