Weird Moment-Timezone Behavior | UTC-LT Converstion - node.js

I try to convert UTC Times to Local Times
I have this Script
var moment = require("moment-timezone");
var utcDateStart = moment.utc("2022-03-12").startOf("day");
var utcDateEnd = moment.utc("2022-03-12").endOf("day");
let ltDateStart = utcDateStart.clone().tz("Europe/Berlin").startOf("day").utc();
let ltDateEnd = utcDateEnd.clone().tz("Europe/Berlin").endOf("day").utc();
console.log(ltDateStart, ltDateEnd);
I get this result:
Moment<2022-03-11T23:00:00Z> Moment<2022-03-13T22:59:59Z>
But i expect this:
Moment<2022-03-11T23:00:00Z> Moment<2022-03-12T22:59:59Z>
I dont get it... Is this a bug? How can i achieve this? (without subtracting 1 day)

Related

Reading excel values as seconds and convert to minutes

I am currently writing a lager script to ease my life.
Right now I am reading raw values from cells from an excel.
So far so good.
These numbers need to be interpreted as seconds and then converted into minutes.
I tried my best with datetime but no luck.
Any suggestions?
elif auswahl == '2':
print("Some user friendly-text:")
excel_download = openpyxl.load_workbook(r'/Path/to/excel.xlsx')
sheet = excel_download.active
Grund_1 = sheet['B2'].value
Grund_2 = sheet['B3'].value
Grund_3 = sheet['B4'].value
Grund_4 = sheet['B5'].value
Grund_5 = sheet['B6'].value
Grund_6 = sheet['B7'].value
Zeit_in_Sekunden_1 = sheet['C2'].value
Zeit_in_Sekunden_2 = sheet['C3'].value
Zeit_in_Sekunden_3 = sheet['C4'].value
Zeit_in_Sekunden_4 = sheet['C5'].value
Zeit_in_Sekunden_5 = sheet['C6'].value
Zeit_in_Sekunden_6 = sheet['C7'].value
print("Du warst heute für", Zeit_in_Sekunden_1, Grund_1, "!")
break
My idea:
raw_seconds_from_C2 = sheet['C2'].value
Then somehow convert to minutes from raw_seconds_from_C2
I am really out of ideas as I then need to put the converted minutes into a print().
Divide the value by 60 to obtain minutes from seconds:
c2_minutes = sheet['C2'].value / 60
Thanks to #Alonso's comment on the question.

How do I make a snipe command that shows the timestamp in the footer

I am able to get the time in UTC using the 'pytz' library and 'datetime' library, but I need it in local time of the user. Say you run the snipe command from the USA, you should get your local time, and if I run it from say Italy, I should get Italy's time. I hope I made it clear.
x = message = {}
y = author = {}
z = author_avatar = {}
time = {}
#client.event
async def on_message_delete(msg):
UTC = pytz.utc
datetime_utc = datetime.now(UTC)
if msg.author.bot == False:
x[msg.channel.id] = msg.content
y[msg.channel.id] = msg.author
time[msg.channel.id] = datetime_utc.strftime('%H:%M UTC')
if msg.author == client.user:
x[msg.channel.id] = msg.content
y[msg.channel.id] = msg.author
time[msg.channel.id] = datetime_utc.strftime('%H:%M UTC')
#client.command(name = 'snipe')
async def snipe(ctx):
try:
em = discord.Embed(description = f" {x[ctx.channel.id]}" ,color = random.choice(colors_for_embeds1), timestamp = datetime.now())
em.set_author(name = y[ctx.channel.id] ,icon_url = (y[ctx.channel.id]).author.url)
em.set_footer(text = f"at {time[ctx.channel.id]}")
await ctx.send(embed = em)
except:
await ctx.send("There is nothing to snipe!")
This is how the command works. The deleted message gets added to a dictionary with the channel ID as the key, the author id gets saved in a dictionary with the channel ID.
I hope this answers your question.
UTC time updates for your location, so for you, it would show your time (example: Today at 8:00 AM) then for someone else that is somewhere else in the world would show (Today at 9:00 AM).
I don't know if I answered this well or not, or if you understood it.
But hope answers your question! :D
your bot has no way of knowing the timezone of the people running the command. The timestamp on discord embeds always show the time in the local format for the people who see the embed, so different people will see different times depending on their timezones.
A solution would be to record the user timezone with a different command and save it to a database.
Then on your command parse the time into the footer for the timezone you want.

Node-red output a message to CSV

For the past couple of days I have been trying and reading to get something very specific done in Node-Red: I want to send the (LoRa) message to a CSV.
This CSV should contain the following items:
topic
date
payload
I can insert the date using a function node:
var str1 = Date();
I have been playing around with CSV node, but I can't get it to output comma separated values. All this has probably to do with my lack of javascript programming skills, which is why I turn to you.
Can you help me out?
Edit:
I'm still looking for the answer, which has brought me the following:
Function node:
var res = Date() + "," + msg.topic + "," + msg.payload; return [ { payload: res } ];
Output:
[{"col1":"Mon Oct 17 2016 17:10:20 GMT+0200 (CEST)","col2":"1/test/1","col3":"string1"}]
All I want now is to lose the extra information such as column names and [{}]
The CSV node works only on the msg.payload field so you will have to copy the extra data into the payload object to get it to output what you want.
So to format the data correctly you need to place a function node with the following before the CSV node:
var originalPayload = msg.payload;
var newPayload = {};
newPayload.date = new Date().toString();
newPayload.topic = msg.topic;
newPayload.payload = originalPayload;
msg.payload = newPayload;
return msg;
And configure the CSV node to output columns "date,topic,payload"

Node.js: Running a script as file or interactive produces different result

if I save the lines below and run like ./node saved.js
var that = this;
that.val1 = 109;
var f1 = function(){return this;}
var f2 = function(){return this;}
var that1 = f1();
var that2 = f2();
console.log(that1.val1)//undefined
that1.val2=111;
console.log(that2.val2)//111
I get this result
undefined
111
But if I paste it to already started shell ./node, I get
...
...
> console.log(that1.val1)//undefined
109
undefined
> that1.val2=111;
111
> console.log(that2.val2)//111
111
Why is the output of the first console.log different?
When you run it in the script, this inside of your functions refers to a different object than it does outside your functions. For example, I made this change to the beginning of your script:
var that = this;
console.log(this)
that.val1 = 109;
var f1 = function(){console.log(this); console.log("eq: " + (this === that));return this;}
When that second line executes running it as node test.js, I get an empty object, whereas then the one inside of f1 is executed a few lines deeper, it's a much different object.
When that's run from the Node REPL, I get an object matching the one from inside f1 in the node test.js version in both places. Thus, that.va1 = 109 is acting on different objects in the 2 cases and is why you see the difference.
Edit: And see Jonathan Lonowski's comment on the question for what the 2 different objects are.

Nodejs parameter resolution and printing

I am trying to debug my mobile website which is running on Nodejs.
In some part there are those lines:
var log_line = {accessCount: accessCount, x: x, time: t};
logstream.write(JSON.stringify(log_line));
Which are suppose to log parts of the users page request. The path is defined as app.get('/:x?.:y?.:z?', function(req, res){ ).
For some users this works fine, but for others in the log I only find a line saying "{"accessCount": 1, "time": "10/10/10"}". How is it possible? shouldn't it at least printed the x without anything beside it? what could cause this effect?
If x is undefined, it won't get printed:
> JSON.stringify({x:undefined})
'{}'
If you are looking for a solution to get over it. Change the code as
if (!x) {
x = "NOT_DEFINED" // or your own place holder value
}
var log_line = {accessCount: accessCount, x: x :, time: t};
or more terse would be
var log_line = {accessCount: accessCount, x: x ? x : "NOT_DEFINED", time: t};

Resources