You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Hank Knight <hk...@gmail.com> on 2014/01/08 15:17:29 UTC

CouchDB: Timestamp inaccurate and seems to be cached

I use this Map Function to get the current Unix timestamp:

function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}

The odd thing is, it returns a correct timestamp the first time I run
a query but it returns the exact same timestamp one minute later even
though 60 seconds have passed!  Is this due to caching or something
else?  What is the best way for me to always get the current Unix
timestamp?

Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Simon Metson <si...@cloudant.com>.
You map function is only run when the data changes and the view needs to be rebuilt, so you’re not recording the time when you access view but the time when the document was processed by the view.  

Can you explain what you’re trying to achieve? Is there a reason you can’t get the current time in the client application?   


On Wednesday, 8 January 2014 at 09:17, Hank Knight wrote:

> I use this Map Function to get the current Unix timestamp:
>  
> function(doc) {emit(Math.round(new Date().getTime()/1000), null );}
>  
> The odd thing is, it returns a correct timestamp the first time I run
> a query but it returns the exact same timestamp one minute later even
> though 60 seconds have passed! Is this due to caching or something
> else? What is the best way for me to always get the current Unix
> timestamp?




Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Filippo Fadda <fi...@programmazione.it>.
I suggest to generate the timestamp when you save the document, not when you index it.

-Filippo

On Jan 8, 2014, at 3:17 PM, Hank Knight wrote:

> I use this Map Function to get the current Unix timestamp:
> 
> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
> 
> The odd thing is, it returns a correct timestamp the first time I run
> a query but it returns the exact same timestamp one minute later even
> though 60 seconds have passed!  Is this due to caching or something
> else?  What is the best way for me to always get the current Unix
> timestamp?


Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Dave Cottlehuber <dc...@jsonified.com>.
It seems this is a common request; instead of computing it expensively in
JS,  perhaps you'd like to open a jira ticket at
https://issues.apache.org/jira/browse/COUCHDB  with some use case details,
and we could generate it using a cheap & efficient erlang function,  and
include its output in e.g. GET / .

A+
Dave



On 8 January 2014 15:42, Jean-Felix Girard <je...@icloud.com> wrote:

> Hi,
> To get server time from the client, I use a update function...  You could
> do something like this:
> {
>    "_id": "_design/js",
>    "_rev": "2-17d3cd5c245d45d1232ee95176d2e792",
>    "language": "javascript",
>    "updates": {
>        "time": "function(doc, req) {  return [null, Math.round(new
> Date().getTime()/1000) + ''];}"
>    }
> }
> and query it like that:
>
> curl localhost:5984/data_test/_design/js/_update/time -X POST
> 1389192069
> ~:$
>
> Jeff
>
> On Jan 8, 2014, at 9:17 AM, Hank Knight <hk...@gmail.com> wrote:
>
> > I use this Map Function to get the current Unix timestamp:
> >
> > function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
> >
> > The odd thing is, it returns a correct timestamp the first time I run
> > a query but it returns the exact same timestamp one minute later even
> > though 60 seconds have passed!  Is this due to caching or something
> > else?  What is the best way for me to always get the current Unix
> > timestamp?
>
>

Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Jean-Felix Girard <je...@icloud.com>.
Hi,
To get server time from the client, I use a update function...  You could do something like this:
{
   "_id": "_design/js",
   "_rev": "2-17d3cd5c245d45d1232ee95176d2e792",
   "language": "javascript",
   "updates": {
       "time": "function(doc, req) {  return [null, Math.round(new Date().getTime()/1000) + ''];}"
   }
}
and query it like that:

curl localhost:5984/data_test/_design/js/_update/time -X POST
1389192069
~:$

Jeff

On Jan 8, 2014, at 9:17 AM, Hank Knight <hk...@gmail.com> wrote:

> I use this Map Function to get the current Unix timestamp:
> 
> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
> 
> The odd thing is, it returns a correct timestamp the first time I run
> a query but it returns the exact same timestamp one minute later even
> though 60 seconds have passed!  Is this due to caching or something
> else?  What is the best way for me to always get the current Unix
> timestamp?


Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Mike Marino <mm...@gmail.com>.
It's not exactly clear what you would like to do.  Do you just want to get
the timestamp as seen by the couchdb server?

What you are doing here is writing a view, the results of which will only
be regenerated if a new document has been inserted between a previous query
and the current query.

On Wed, Jan 8, 2014 at 3:17 PM, Hank Knight <hk...@gmail.com> wrote:

> I use this Map Function to get the current Unix timestamp:
>
> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
>
> The odd thing is, it returns a correct timestamp the first time I run
> a query but it returns the exact same timestamp one minute later even
> though 60 seconds have passed!  Is this due to caching or something
> else?  What is the best way for me to always get the current Unix
> timestamp?
>

Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Aurélien Bénel <au...@utt.fr>.
> I use this Map Function to get the current Unix timestamp:
> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}

Eek! never do that.
A map function should be a function in a mathematical sense.
This means that the return value of map(x) should be ever and ever the same for a given x.


Regards,

Aurélien 



Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Stanley Iriele <si...@gmail.com>.
Maps should not be used for that... Also...why can't show or list functions
be used for that?...
On Jan 8, 2014 10:59 AM, "Hank Knight" <hk...@gmail.com> wrote:

> I want to get the current timestamp according to the CouchDB server
> using a GET request.  When new documents are created, an update
> function is used to add a timestamp to a document.  When I get a
> result set, it is important to know the server's current timestamp so
> the age of the documents in the result set can be correctly
> determined.
>
> On Wed, Jan 8, 2014 at 10:17 AM, Hank Knight <hk...@gmail.com> wrote:
> > I use this Map Function to get the current Unix timestamp:
> >
> > function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
> >
> > The odd thing is, it returns a correct timestamp the first time I run
> > a query but it returns the exact same timestamp one minute later even
> > though 60 seconds have passed!  Is this due to caching or something
> > else?  What is the best way for me to always get the current Unix
> > timestamp?
>

Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Hank Knight <hk...@gmail.com>.
Thanks, you are right!

On Wed, Jan 8, 2014 at 4:58 PM, Stanley Iriele <si...@gmail.com> wrote:
> That's the browser cache... Not the JavaScript call you'll make... Try
> making call...logging it to the console.. Then making that call again a
> second later... It should work just fine.. You can even get the age from
> the show function... With a new date minus created date and just return
> that in a field
> On Jan 8, 2014 12:54 PM, "Jean-Felix Girard" <je...@icloud.com> wrote:
>
>> try to add a random query parameter to your URL to avoid browser cache...
>> if you use jQuery, you can set the ajax cache property to false. (it adds
>> ?_=randomValue).
>>
>> On Jan 8, 2014, at 3:46 PM, Hank Knight <hk...@gmail.com> wrote:
>>
>> > I cannot use the headers because cross-domain issues.  I need the
>> > results to be returned in JSONP format with a callback.
>> >
>> > Using a show function for this seems like a good idea but it does not
>> > work either!
>> >
>> > Here is an example.  Notice how the time does not refresh!
>> >
>> https://zuhqtr5.couchappy.com/test/_design/showtimestamp/_show/serverTime
>> >
>> > Here is my show function:
>> >
>> > {
>> >   "_id": "_design/showtimestamp",
>> >   "shows": {
>> >       "timestamp": "function(doc, req) {return
>> > ''+String(Math.round(new Date().getTime()/1000));}",
>> >       "serverTime": "function(doc, req) {return
>> > 'serverTime({\"timestamp\": '+String(Math.round(new
>> > Date().getTime()/1000))+'});';}"
>> >   }
>> > }
>> >
>> >
>> >
>> > On Wed, Jan 8, 2014 at 3:40 PM, Jean-Felix Girard <je...@icloud.com>
>> wrote:
>> >> I just noticed that Couchdb returns a "Date" response header.
>> >>
>> >> < HTTP/1.1 200 OK
>> >> < Transfer-Encoding: chunked
>> >> < Server: CouchDB/1.5.0 (Erlang OTP/R16B02)
>> >> < ETag: "4E9MYK7J4X9CKM0EUK0V1K7IZ"
>> >> < Date: Wed, 08 Jan 2014 19:34:38 GMT
>> >> < Content-Type: text/plain; charset=utf-8
>> >> < Cache-Control: must-revalidate
>> >>
>> >> You can parse that date (from the GET request on the view) and compare
>> it to the document date to find out theirs age.
>> >>
>> >> Jeff
>> >>
>> >>
>> >> On Jan 8, 2014, at 1:58 PM, Hank Knight <hk...@gmail.com> wrote:
>> >>
>> >>> I want to get the current timestamp according to the CouchDB server
>> >>> using a GET request.  When new documents are created, an update
>> >>> function is used to add a timestamp to a document.  When I get a
>> >>> result set, it is important to know the server's current timestamp so
>> >>> the age of the documents in the result set can be correctly
>> >>> determined.
>> >>>
>> >>> On Wed, Jan 8, 2014 at 10:17 AM, Hank Knight <hk...@gmail.com>
>> wrote:
>> >>>> I use this Map Function to get the current Unix timestamp:
>> >>>>
>> >>>> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
>> >>>>
>> >>>> The odd thing is, it returns a correct timestamp the first time I run
>> >>>> a query but it returns the exact same timestamp one minute later even
>> >>>> though 60 seconds have passed!  Is this due to caching or something
>> >>>> else?  What is the best way for me to always get the current Unix
>> >>>> timestamp?
>> >>
>>
>>

Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Stanley Iriele <si...@gmail.com>.
That's the browser cache... Not the JavaScript call you'll make... Try
making call...logging it to the console.. Then making that call again a
second later... It should work just fine.. You can even get the age from
the show function... With a new date minus created date and just return
that in a field
On Jan 8, 2014 12:54 PM, "Jean-Felix Girard" <je...@icloud.com> wrote:

> try to add a random query parameter to your URL to avoid browser cache...
> if you use jQuery, you can set the ajax cache property to false. (it adds
> ?_=randomValue).
>
> On Jan 8, 2014, at 3:46 PM, Hank Knight <hk...@gmail.com> wrote:
>
> > I cannot use the headers because cross-domain issues.  I need the
> > results to be returned in JSONP format with a callback.
> >
> > Using a show function for this seems like a good idea but it does not
> > work either!
> >
> > Here is an example.  Notice how the time does not refresh!
> >
> https://zuhqtr5.couchappy.com/test/_design/showtimestamp/_show/serverTime
> >
> > Here is my show function:
> >
> > {
> >   "_id": "_design/showtimestamp",
> >   "shows": {
> >       "timestamp": "function(doc, req) {return
> > ''+String(Math.round(new Date().getTime()/1000));}",
> >       "serverTime": "function(doc, req) {return
> > 'serverTime({\"timestamp\": '+String(Math.round(new
> > Date().getTime()/1000))+'});';}"
> >   }
> > }
> >
> >
> >
> > On Wed, Jan 8, 2014 at 3:40 PM, Jean-Felix Girard <je...@icloud.com>
> wrote:
> >> I just noticed that Couchdb returns a "Date" response header.
> >>
> >> < HTTP/1.1 200 OK
> >> < Transfer-Encoding: chunked
> >> < Server: CouchDB/1.5.0 (Erlang OTP/R16B02)
> >> < ETag: "4E9MYK7J4X9CKM0EUK0V1K7IZ"
> >> < Date: Wed, 08 Jan 2014 19:34:38 GMT
> >> < Content-Type: text/plain; charset=utf-8
> >> < Cache-Control: must-revalidate
> >>
> >> You can parse that date (from the GET request on the view) and compare
> it to the document date to find out theirs age.
> >>
> >> Jeff
> >>
> >>
> >> On Jan 8, 2014, at 1:58 PM, Hank Knight <hk...@gmail.com> wrote:
> >>
> >>> I want to get the current timestamp according to the CouchDB server
> >>> using a GET request.  When new documents are created, an update
> >>> function is used to add a timestamp to a document.  When I get a
> >>> result set, it is important to know the server's current timestamp so
> >>> the age of the documents in the result set can be correctly
> >>> determined.
> >>>
> >>> On Wed, Jan 8, 2014 at 10:17 AM, Hank Knight <hk...@gmail.com>
> wrote:
> >>>> I use this Map Function to get the current Unix timestamp:
> >>>>
> >>>> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
> >>>>
> >>>> The odd thing is, it returns a correct timestamp the first time I run
> >>>> a query but it returns the exact same timestamp one minute later even
> >>>> though 60 seconds have passed!  Is this due to caching or something
> >>>> else?  What is the best way for me to always get the current Unix
> >>>> timestamp?
> >>
>
>

Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Jean-Felix Girard <je...@icloud.com>.
try to add a random query parameter to your URL to avoid browser cache... if you use jQuery, you can set the ajax cache property to false. (it adds ?_=randomValue).  

On Jan 8, 2014, at 3:46 PM, Hank Knight <hk...@gmail.com> wrote:

> I cannot use the headers because cross-domain issues.  I need the
> results to be returned in JSONP format with a callback.
> 
> Using a show function for this seems like a good idea but it does not
> work either!
> 
> Here is an example.  Notice how the time does not refresh!
> https://zuhqtr5.couchappy.com/test/_design/showtimestamp/_show/serverTime
> 
> Here is my show function:
> 
> {
>   "_id": "_design/showtimestamp",
>   "shows": {
>       "timestamp": "function(doc, req) {return
> ''+String(Math.round(new Date().getTime()/1000));}",
>       "serverTime": "function(doc, req) {return
> 'serverTime({\"timestamp\": '+String(Math.round(new
> Date().getTime()/1000))+'});';}"
>   }
> }
> 
> 
> 
> On Wed, Jan 8, 2014 at 3:40 PM, Jean-Felix Girard <je...@icloud.com> wrote:
>> I just noticed that Couchdb returns a "Date" response header.
>> 
>> < HTTP/1.1 200 OK
>> < Transfer-Encoding: chunked
>> < Server: CouchDB/1.5.0 (Erlang OTP/R16B02)
>> < ETag: "4E9MYK7J4X9CKM0EUK0V1K7IZ"
>> < Date: Wed, 08 Jan 2014 19:34:38 GMT
>> < Content-Type: text/plain; charset=utf-8
>> < Cache-Control: must-revalidate
>> 
>> You can parse that date (from the GET request on the view) and compare it to the document date to find out theirs age.
>> 
>> Jeff
>> 
>> 
>> On Jan 8, 2014, at 1:58 PM, Hank Knight <hk...@gmail.com> wrote:
>> 
>>> I want to get the current timestamp according to the CouchDB server
>>> using a GET request.  When new documents are created, an update
>>> function is used to add a timestamp to a document.  When I get a
>>> result set, it is important to know the server's current timestamp so
>>> the age of the documents in the result set can be correctly
>>> determined.
>>> 
>>> On Wed, Jan 8, 2014 at 10:17 AM, Hank Knight <hk...@gmail.com> wrote:
>>>> I use this Map Function to get the current Unix timestamp:
>>>> 
>>>> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
>>>> 
>>>> The odd thing is, it returns a correct timestamp the first time I run
>>>> a query but it returns the exact same timestamp one minute later even
>>>> though 60 seconds have passed!  Is this due to caching or something
>>>> else?  What is the best way for me to always get the current Unix
>>>> timestamp?
>> 


Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Hank Knight <hk...@gmail.com>.
I cannot use the headers because cross-domain issues.  I need the
results to be returned in JSONP format with a callback.

Using a show function for this seems like a good idea but it does not
work either!

Here is an example.  Notice how the time does not refresh!
https://zuhqtr5.couchappy.com/test/_design/showtimestamp/_show/serverTime

Here is my show function:

{
   "_id": "_design/showtimestamp",
   "shows": {
       "timestamp": "function(doc, req) {return
''+String(Math.round(new Date().getTime()/1000));}",
       "serverTime": "function(doc, req) {return
'serverTime({\"timestamp\": '+String(Math.round(new
Date().getTime()/1000))+'});';}"
   }
}



On Wed, Jan 8, 2014 at 3:40 PM, Jean-Felix Girard <je...@icloud.com> wrote:
> I just noticed that Couchdb returns a "Date" response header.
>
> < HTTP/1.1 200 OK
> < Transfer-Encoding: chunked
> < Server: CouchDB/1.5.0 (Erlang OTP/R16B02)
> < ETag: "4E9MYK7J4X9CKM0EUK0V1K7IZ"
> < Date: Wed, 08 Jan 2014 19:34:38 GMT
> < Content-Type: text/plain; charset=utf-8
> < Cache-Control: must-revalidate
>
> You can parse that date (from the GET request on the view) and compare it to the document date to find out theirs age.
>
> Jeff
>
>
> On Jan 8, 2014, at 1:58 PM, Hank Knight <hk...@gmail.com> wrote:
>
>> I want to get the current timestamp according to the CouchDB server
>> using a GET request.  When new documents are created, an update
>> function is used to add a timestamp to a document.  When I get a
>> result set, it is important to know the server's current timestamp so
>> the age of the documents in the result set can be correctly
>> determined.
>>
>> On Wed, Jan 8, 2014 at 10:17 AM, Hank Knight <hk...@gmail.com> wrote:
>>> I use this Map Function to get the current Unix timestamp:
>>>
>>> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
>>>
>>> The odd thing is, it returns a correct timestamp the first time I run
>>> a query but it returns the exact same timestamp one minute later even
>>> though 60 seconds have passed!  Is this due to caching or something
>>> else?  What is the best way for me to always get the current Unix
>>> timestamp?
>

Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Jean-Felix Girard <je...@icloud.com>.
I just noticed that Couchdb returns a "Date" response header.

< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Server: CouchDB/1.5.0 (Erlang OTP/R16B02)
< ETag: "4E9MYK7J4X9CKM0EUK0V1K7IZ"
< Date: Wed, 08 Jan 2014 19:34:38 GMT
< Content-Type: text/plain; charset=utf-8
< Cache-Control: must-revalidate

You can parse that date (from the GET request on the view) and compare it to the document date to find out theirs age.

Jeff


On Jan 8, 2014, at 1:58 PM, Hank Knight <hk...@gmail.com> wrote:

> I want to get the current timestamp according to the CouchDB server
> using a GET request.  When new documents are created, an update
> function is used to add a timestamp to a document.  When I get a
> result set, it is important to know the server's current timestamp so
> the age of the documents in the result set can be correctly
> determined.
> 
> On Wed, Jan 8, 2014 at 10:17 AM, Hank Knight <hk...@gmail.com> wrote:
>> I use this Map Function to get the current Unix timestamp:
>> 
>> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
>> 
>> The odd thing is, it returns a correct timestamp the first time I run
>> a query but it returns the exact same timestamp one minute later even
>> though 60 seconds have passed!  Is this due to caching or something
>> else?  What is the best way for me to always get the current Unix
>> timestamp?


Re: CouchDB: Timestamp inaccurate and seems to be cached

Posted by Hank Knight <hk...@gmail.com>.
I want to get the current timestamp according to the CouchDB server
using a GET request.  When new documents are created, an update
function is used to add a timestamp to a document.  When I get a
result set, it is important to know the server's current timestamp so
the age of the documents in the result set can be correctly
determined.

On Wed, Jan 8, 2014 at 10:17 AM, Hank Knight <hk...@gmail.com> wrote:
> I use this Map Function to get the current Unix timestamp:
>
> function(doc) {emit(Math.round(new Date().getTime()/1000), null  );}
>
> The odd thing is, it returns a correct timestamp the first time I run
> a query but it returns the exact same timestamp one minute later even
> though 60 seconds have passed!  Is this due to caching or something
> else?  What is the best way for me to always get the current Unix
> timestamp?