GREAT
-
New plugin [pimatic-datetime]
-
I renamed the datetime variable to “formatted” and published a new version.
-
Maybe a timezone feature would also make sense. What do you think?
-
@saxnpaule said in New plugin [pimatic-datetime]:
Maybe a timezone feature would also make sense. What do you think?
Yes, as we discussed earlier. In my opinion the timezone setting should be part of the device configuration. If timezone is set to “UTC” (moment.js accepts this as timezone setting) the ISO values for day of week, weeks in year, and such should be used.
- https://momentjs.com/docs/#/get-set/iso-weekday/
- https://momentjs.com/docs/#/get-set/iso-week/
- https://momentjs.com/docs/#/get-set/iso-week-year/
- https://momentjs.com/docs/#/get-set/iso-weeks-in-year/
EDIT: Well, about the latter this should perhaps not be bound to the timezone, but to the locale. Maybe a fake locale “ISO” where the system local is used as if locale is left empty, but ISO values are used where applicable
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
@saxnpaule One more thing:
I am not sure whether or not the following is already supported in your implementation:
- It should be possible to leave the local string empty. In this case the locale set on the system where pimatic is installed should be implied.
- In my opinion, the empty locale string should also be the default value defined in the device config schema.
Please also note, the locale string should be provided in lowercase according to what is written in the doc.
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
Locale and dateformat are optional atm. If not applied, the system default is used.
I already implemented a timezone device parameter that is applied to the loc values (Ho Chi Minh):
The question is: Should the device provide multiple differnet locales/timezone at once or would it be better to have multiple devices if multiple timezones/locales are required?
My suggestion is, that locale and timezone are device specific and all variables depend on them. Then no loc* variables are needed and we would only have per device:
- dayOfWeek
- dayOfMonth
- time
- date
- datetime
- formatted
- unixTimestamp
-
@saxnpaule said in New plugin [pimatic-datetime]:
My suggestion is, that locale and timezone are device specific and all variables depend on them. Then no loc* variables are needed and we would only have per device
+1
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
Got it. I created two devices. One with default settings (Germany) and one with en_US locale, Asia/Ho_Chi_Minh timezone and a specific dateformat:
But why is dayOfWeek = 4 in the german case? And how could the formatted datetime of Vietnam contain 62 seconds?
-
@saxnpaule said in New plugin [pimatic-datetime]:
But why is dayOfWeek = 4 in the german case?
I believe moment.js has quiet a few design flaws. From the documentation my understanding is the
isoWeekday()
function returns a number in the range 1…7 whileweekday()
returns a number in the range 0…6.And how could the formatted datetime of Vietnam contain 62 seconds
Which format string did you use?
EDIT: Test Case for weekday issue:
var moment = require('moment-timezone') var chrono = require('chrono-node') var date = chrono.parseDate('Monday this week') console.log(date) console.log(moment(date).weekday()) console.log(moment(date).isoWeekday())
Output:
Mon Mar 19 2018 12:00:00 GMT+0100 (Mitteleuropäische Zeit) 0 1
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
Nice plugin, maybe it would also be useful to get the dayOfYear and the Weeknumber in a Device.
pimatic-google-calendar | pimatic-wmi | pimatic-snmp | pimatic-wakeonlan |
Like my work? Then consider a donation
Follow me: www.thorstenreichelt.de -
@mwittig The weekday is received by .weekday()
Thats all the magic:
currentDate = new Date() @unixTimestamp = currentDate.getTime() moment = Moment(currentDate) if @timezone? moment.tz(@timezone) if @locale moment.locale(@locale) @dayOfWeek = moment.weekday() @dayOfMonth = moment.date() @time = moment.format('HH:mm') @date = moment.format('L') @datetime = @date + " " + @time if @dateformat? @formatted = moment.format(@dateformat) else @formatted = moment.format()
The format String I used was:
YYYY-MM-dd HH:mm:SS
-
@saxnpaule said in New plugin [pimatic-datetime]:
YYYY-MM-dd HH:mm:SS
You need to use
ss
instead ofSS
. The latter is for a fraction of a second. See third table a bit further down at https://momentjs.com/docs/#/parsing/string-format/"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
@thost96 said in New plugin [pimatic-datetime]:
Nice plugin, maybe it would also be useful to get the dayOfYear and the Weeknumber in a Device.
Done
You need to use
ss
instead ofSS
. The latter is for a fraction of a second. See third table a bit further down at https://momentjs.com/docs/#/parsing/string-format/OMG, I’m so stupid. Thx for the advise. But the weekday is still a mystery, as .weekday() should be locale aware.
-
@saxnpaule said in New plugin [pimatic-datetime]:
But the weekday is still a mystery, as .weekday() should be locale aware.
Which locale string did you use for the “vietnam” device?
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
Vietnam is “en-US” and Germany only "de"
I also tested Germany without locale and with “de-DE” and “de_DE” but its always 4 -
@mwittig For Germany 4 is correct, as Monday is the first day of the week in Germany and
weekday()
starts with index0
instead of1
. See also my earlier post and https://de.wikipedia.org/wiki/WocheFor “en-US” it is
5
, because in the USA the first day of the week is “Sunday”.var moment = require('moment-timezone') moment.locale('de'); console.log(moment().weekday()); console.log(moment().weekday(0).format("dddd")); moment.locale('en-US'); console.log(moment().weekday()); console.log(moment().weekday(0).format("dddd")); moment.locale('vi-VTS'); console.log(moment().weekday()); console.log(moment().weekday(0).format("dddd"));
Result
4 Montag 5 Sunday 4 thứ hai
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
@mwittig said in New plugin [pimatic-datetime]:
I believe moment.js has quiet a few design flaws. From the documentation my understanding is the isoWeekday() function returns a number in the range 1…7 while weekday() returns a number in the range 0…6.
My suggestion is to let
dayOfWeek
bemoment().weekday() + 1
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
-
Okay, I thought starting with 0 is only for IsoWeekDay.
Well, then I will add in all cases 1 because it is easier to understand for the user in my opinion.
-
I would say, the work is done:
released version 0.1.0
-
I got a feature request from a friend. He wants to know if it is weekend. I think I will add this too.