TimeZone on nodeJs using oAuth - node.js

I am working with the tumbler API and I am using the express framework for node.js, but I have a problem using the tumblr oAuth. The problem is the difference in timezones.
How I can change my current timezone?

You can set the TZ environment variable before calling any date functions.
> process.env.TZ = 'Europe/Amsterdam'
'Europe/Amsterdam'
> d = new Date()
Wed Mar 06 2013 23:30:42 GMT+0100 (CET)
> d.toString()
'Wed Mar 06 2013 23:30:42 GMT+0100 (CET)'
> d.toUTCString()
'Wed, 06 Mar 2013 22:30:42 GMT'

Related

Why Sorting the timestamp using sort_values is not working?

I have a column of timestamp converted to human readable form.
I have tried to sort it from epochtime as well as after converting. It's giving me
Fri, 08 Feb 2019 17:24:16 IST
Mon, 11 Feb 2019 02:19:40 IST
Sat, 09 Feb 2019 00:22:43 IST
which is not sorted.
I have used sort_values()
each_tracker_df = each_tracker_df.sort_values(["timestamp"],ascending=True)
why it isn't working??
Since all the time is in IST. Replace the string IST with NULL.
>>import datetime
>>times=['Fri, 10 Feb 2010 17:24:16','Fri, 11 Feb 2010 17:24:16','Fri, 11 Feb 2019 17:24:16']
>>change_format=[]
>> for time in times:
change_format.append(datetime.datetime.strptime(time, '%a, %d %b %Y %H:%M:%S'))
>>change_format.sort()

How to fire once each time on changing directory content with fs.watch?

Is there a way to fire only once on changing directory content with Node.js fs.watch? By changing directory content I mean adding/deleting bunch of files from it ?
In other words I expect fs.watch to fire on the first event of the first file and ignore any other events for particular changing dir content cycle.
I was able to do this only with fs.watchFile But node guys recommend to use fs.watch if possible.
Note: fs.watch() is more efficient than fs.watchFile and fs.unwatchFile. fs.watch should be used instead of fs.watchFile and fs.unwatchFile when possible.
https://nodejs.org/docs/latest/api/fs.html#fs_fs_watchfile_filename_options_listener
Yury Tarabanko patricianly answered this question earlier, but shortly after
deleted his answer:
You simply stop watching on the first call
const watcher = fs.watch(somePath, (ev, file) => {
watcher.close()
// logic goes here
console.log(`${ev} ${file}`)
})
With resurrecting watcher after closing it, we will have 1 shot recurrent behavior as asked in the question:
const fs = require('fs')
let dir = './watch1'
function watchOnce()
{
const watcher = fs.watch(dir, (evt, file) => {
watcher.close()
// logic goes here
console.log(Date(), evt, file)
// resurrecting watcher after 1 sec
setTimeout(watchOnce, 1000)
})
}
watchOnce()
Testing
Creating and deleting 10,000 files 5 times in a raw with 1 seconds delay in between:
$ for i in {1..5}; do touch {1..10000}; sleep 1; rm {1..10000}; sleep 1; done
Verifying that watcher fired exactly 10 times (5 for creating 10,000 files and 5 for deleting 10,000 files):
$ node watchers.js
Wed Jun 07 2017 12:01:44 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:46 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:47 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:49 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:50 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:52 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:53 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:55 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:56 GMT+0300 (EEST) rename 1
Wed Jun 07 2017 12:01:58 GMT+0300 (EEST) rename 1
I will not marking my answer as accepted, since credits mostly goes to Yury for his watcher.close() idea. And maybe someone else will write more neat solution, since I am not big fun using delay in the function.

toLocaleString() not returning the right date format in node.js

In chrome's console:
(new Date(Date.now())).toLocaleString() returns "9/5/2014 9:43:08 PM"
new Date(Date.now()) returns 'Fri Sep 05 2014 21:43:08 GMT+0530 (IST)'
In node.js REPL/app:
(new Date(Date.now())).toLocaleString() returns 'Fri Sep 05 2014 21:43:18 GMT+0530 (IST)' which is the same as running new Date(Date.now())
What is the change I need to do to get the same output format as chrome.

How to Increment a day in Date using Node.js

I have date object like this "Tue Sep 02 2014 13:34:17 GMT+0500 (Pakistan Standard Time)"
I want increment a day in the date object using node.js.
I.E.
var myDate = "Tue Sep 02 2014 13:34:17 GMT+0500 (Pakistan Standard Time)";
please mention how to increment a day in above myDate.
Thanks
Try this
var myDate = new Date("Tue Sep 02 2014 13:34:17 GMT+0500 (Pakistan Standard Time)");
myDate.setDate(myDate.getDate() + 1);
alert(myDate);

NodeJS add two hours to date?

I've got a date string as such:
Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)
How can I add two hours to this?
So I get:
Tue Jul 29 2014 01:44:06 GMT+0000 (UTC)
Here's one solution:
var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
date += (2 * 60 * 60 * 1000);
console.log(new Date(date).toUTCString());
// displays: Wed, 30 Jul 2014 01:44:06 GMT
Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.
Using MomentJS:
var moment = require('moment');
var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");
//sets an internal flag on the moment object.
date1.utc();
console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));
//adds 2 hours
date1.add(2, 'h');
console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));
Prints out the following:
Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)
Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)
This works well:
const epoch = new Date('01-01-2000')
const notBeforeDate = new Date(epoch).setSeconds(notBefore)
const notAfterDate = new Date(epoch).setSeconds(notAfter)
NOTE: the setSeconds() call actually adds seconds to the current Date value, it does not reset the Date to some absolute number of seconds. This detail is poorly addressed in the documentation and causes a lot of heartache when first trying to work with Dates in JavaScript.

Resources