Need to remove timezone from current-date() xquery

6k views Asked by At

I am a newbie in xquery and one of the requirement is to generate daily reports by querying xDB.

If I hardcode the date I am able to generate the report from xDB but if I try to read the current Date the from the system I don't get any values in report.

Code Snippet:

let $day := fn:current-date()
let $hours := ( '00', '01', '02', '03', '04', 
        '05', '06', '07', '08', '09', 
        '10', '11', '12', '13', '14', 
        '15', '16', '17', '18', '19', 
        '20', '21', '22', '23' )

let $ddhh := for $i in $hours return concat($day,"T",$i)
let $title := concat("Average Time By Documents in Hour on ", $day)

In report: ("Average Time By Documents in Hour on 2015-06-08Z"..)

The difference between hardcoded value and current-date() function is the timezone"Z". How do I get rid of timezone "Z". I only want say "2015-06-08" in variable $day and not "2015-06-08Z"

Thanks in advance.

2

There are 2 answers

0
Jens Erat On

When casting a dateTime object to a string, the default ISO date format is chosen, always including the timezone. Convert the date manually instead using fn:format-dateTime($dateTime, $picture) with a date picture string $picture.

format-date(current-date(), "[Y0001]-[M01]-[D01]")

You can also add the hours in the desired format:

format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]")

This even enable you to omit the hour-sequence you pregenerated (of course also works in a single line, split up so it can be explained more easily):

let $today := current-date()                      (: today :)
let $today := xs:dateTime($today)                 (: convert to dateTime :)
let $hour := $today + xs:dayTimeDuration("PT3H")  (: add 3 hours :)
return format-dateTime($hour, "[Y0001]-[M01]-[D01]-[H01]")
2
Florent Georges On

If you want to get rid of the timezone component of the date, you can use fn:adjust-date-to-timezone(), using an empty sequence as the second parameter:

fn:adjust-date-to-timezone($day, ())