# Setting timezone in painless source

**URL:** <https://discuss.elastic.co/t/setting-timezone-in-painless-source/203278>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 11, 2019, 6:00pm UTC](https://discuss.elastic.co/t/setting-timezone-in-painless-source/203278 "2019-10-11T18:00:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwallace](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jwallace](https://discuss.elastic.co/u/jwallace)\
**Post date:** [October 11, 2019, 6:00pm UTC](https://discuss.elastic.co/t/setting-timezone-in-painless-source/203278/1 "2019-10-11T18:00:00Z")

</div>

I am having trouble setting timezone in a query. I am attempting to group data into day of the week over a time period of a month (so all mondays, tuesdays, etc would be grouped). I am doing this by doing a query for the past month using range to start. Then I have aggs with a painless script to group by day of week. This all works fine, except that I cannot seem to add timezone into the painless script.

What I am doing I have seen in another post, though it does not work. I've also tried using atZone() which also throws errors.

This code returns data, but the timezone isn't set. It makes sense to me that this doesn't work since the params variable is not used in the script. I got it from another example though, so included it here.  
"total\_energy\_per\_day\_of\_week": {  
"terms": {  
"script": {  
"lang": "painless",  
"source": "doc['timestamp'].value.dayOfWeekEnum",  
"params": {  
"timeZone": "America/Vancouver"  
}  
}  
}

This snippet throws an error.  
"total\_energy\_per\_day\_of\_week": {  
"terms": {  
"script": {  
"lang": "painless",  
"source": "doc['timestamp'].value.atZone(ZoneId.of(params.timeZone)).dayOfWeekEnum",  
"params": {  
"timeZone": "America/Vancouver"  
}  
}  
},

The error thrown is:  
"failures": [  
{  
"shard": 0,  
"index": "index-sensor-state",  
"node": "Z6EqbxK7QPezKdaKc615KQ",  
"reason": {  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"doc['timestamp'].value.atZone(ZoneId.of(params.timeZone)).dayOfWeekEnum",  
" ^---- HERE"  
],  
"script": "doc['timestamp'].value.atZone(ZoneId.of(params.timeZone)).dayOfWeekEnum",  
"lang": "painless",  
"caused\_by": {  
"type": "illegal\_argument\_exception",  
"reason": "dynamic method [org.elasticsearch.script.JodaCompatibleZonedDateTime, atZone/1] not found"  
}  
}

If anyone has any insight into my issue, it would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 12, 2019, 6:47pm UTC](https://discuss.elastic.co/t/setting-timezone-in-painless-source/203278/2 "2019-10-12T18:47:02Z")

</div>

`atZone` is a method on `LocalDateTime`, to return a `ZonedDateTime`. The value returned from doc values for a date field is a `ZonedDateTime` (actually a compatible layer over it that mimics both joda and java date apis). I think you want `withSameLocalDate(ZoneId)`, which will return a `ZonedDateTime` shifted to the given timezone. All date time objects returned from doc values are in UTC timezone.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 9, 2019, 6:47pm UTC](https://discuss.elastic.co/t/setting-timezone-in-painless-source/203278/3 "2019-11-09T18:47:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
