I'm creating using node.js and mongodb an application that stores all temperature values by hour in a day.
Temperature values should be shown in a dashboard according the timezone where the sensor is located.
I created a data model following the recommendation for time series data but I don't know how to deal with timezone because mongodb stores dates in UTC so in my data model the object "hours" has static fields for every hour of the day.
// Temperatures by hour in a day.
{
dateStart: ISODate("2016-08-06T00:00:00.000Z"), // This is the start of the day
timeZone: "Europe/Madrid", // We could store the time zone of the sensor.
hours: { // I'm not sure how to deal with these values. Should be UTC hours too?
0: 20
1: 21,
2: 24,
.
.
.
23: 16
}
}
The question is: how can I deal with timezone?
In my local time the start of a day (2016-08-06T00:00:00) is converted to UTC to 2016-08-05T22:00:00.000+02:00.
A first approach could be:
Sensor time zone is 'Europe/Madrid' (CEST, 02:00 diff from UTC)
Get the start of day using 'Europe/Madrid time zone, convert it to UTC and store it in database. Field startDate will be "2016-08-05T22:00:00.000Z"
In order to store a 25 C temperature for an hour: Get local time ('Europe/Madrid'), for example 18h, then convert to UTC. Result is 16h. So hours.16=25.
In this case we have UTC times in star date but the object of hours is not UTC. It is 'Europe/Madrid and I'm not very convinced about this decision.
Any ideas for improving this design?
Your design should work, however your example in 3. should be
{
...
16: 25
...
}
The hours is the offset respect to dateStart. So when displaying you should add 16 hours to the start date which you can display in the timezone that you want.
Related
I'm working with a data set in Athena where all the timestamps are UTC, but I need to adjust to British Summer Time, i.e. add an hour for all timestamps between 1:00am on the last Sunday in March and 1:00am on the last Sunday in October. Any help would be much appreciated. Thanks!
List of supported time zones does not contain BST but has Europe/London which AFAIK should match it. To convert time zone use AT TIME ZONE:
-- sample data
WITH dataset (time) AS (
VALUES (timestamp '2022-02-01 10:00:00'),
(now())
)
-- query
select time, time AT TIME ZONE 'Europe/London'
from dataset
Output:
time
_col1
2022-02-01 10:00:00.000 UTC
2022-02-01 10:00:00.000 Europe/London
2022-04-13 12:13:06.610 UTC
2022-04-13 13:13:06.610 Europe/London
My end goal is to check subscription.cancel_at_period_end for false and store subscription end date as a moment date object.
const subscriptionEndDate = moment(subscription.current_period_end);
This is the result field coming from stripe in test mode.
current_period_end: 1649650039
But even using new Date(subscription.current_period_end) is coming back as 1970-01-20T02:14:10.039Z
Is this not the field that is suppose to show when the next billing date is ?
Any thoughts? What am I missing ?
UPDATE:
I was just doing some testing and figured out if I multiply that value by 1000 it comes out to be 1649650039000 which equates to
Mon 11 April 2022 00:07:19
Is there a reason for this? Is this a safe method moving forward?
UPDATE:
I accepted answer below and am providing momentjs that converts directly to unix timestamp.
const date = moment(new Date()).unix();
Issue
Stripe reports date fields as Unix timestamps. These represent a date/time as the number of seconds since January 1, 1970 (kinda...leap seconds are weird).
The Javascript Date object attempts to convert the number of milliseconds since January 1, 1970 as that is an increment of time that is more relevant to front-end web coding.
Solution
You have already discovered an adequate solution, that is multiply the timestamp by 1000 and thereby convert the value in seconds to a value in milliseconds. This appears to be a common work around 1, 2, 3
I was trying to convert a timezone from CST to UTC inside a data flow. A quick google gave me that CST is in UTC-6:00, so it should add 6 hours.
The following derived column function for some reason only added 5 hours:
toUTC(localDatetime, 'CST')
Moreover, CurrentUTC('CST') Gave me the correct 6 hours difference, so I had to rewrite this function like this:
LocalDatetime + hours(toInteger((currentUTC('CST') - currentUTC()) / 1000 / 60 / 60))
// Minus between timestamps gives difference in milliseconds
Is this intended behavior? I am worried that when the timezone "shifts" this code will break, so using toUTC would be the best solution, but for some reason it gives incorrect results now
toUTC() converts a datetime to UTC time zone based on the Daylight-Saving Time effectiveness.
When you are converting from CST to UCT, if your date falls when Daylight saving is effective, then it adds +5 to your date else adds +6 to your date.
Example:
Derived column expression: toUTC(toTimestamp(Date_UTC), 'CST')
During my scripting in Vugen (Web/HTTP-HTML), I have captured the time in date format (Thu 31 Oct 2019 10:08:29) in parameter file using date-time format (%a %d %b %Y %H:%M:%S). The value I have to pass in the Body section of web_custom_request, it accepts only milliseconds (1572516509000). How we can convert this date format to milliseconds in LR.
Can anyone suggest to me here?
Thanks,
Soumen
Help to understand the use case. Do you need to have the time in milliseconds at the time of submission? At some time in the past for a given date and time? At some point in the future for a given date and time?
The reason why I ask is that there are built in functions for generating a UNIX styled timestamp for the current time. You could potentially plus or minus some number of milliseconds off of this for dates in the past or future depending upon the nature of the request: 86,400,000 milliseconds in a day
I will get time input from the user for scheduling a task. The options are as follows,
Day of the week, hour and minute (or)
Day of the month, hour and minute (or)
Month of the year, hour and minute (or)
User can be in any timezone. I have modeled my database table to store the user input as a configuration.
In my table I will calculate the next_run_at and populate it, so that poller can find the jobs to run based on it and execute.
To be timezone agnostic, my next_run_at should be in UTC.
Is there a way to convert the above mentioned configuration(Day, hour, minute) alone in UTC and store it?
TL;DR I know we can convert a date to specific timezone. Is there a way to convert the combination of just day, hour and minute alone to UTC?
Construct a date from the day, hours, and minutes you have. Convert it to UTC and get the day, hours, and months values back.
But I think there will be edge cases as to which month the day corresponds to.
23:00 28th Feb PST will be 06:00 1st Mar UTC;
23:00 28th Mar PST will be 06:00 29th Mar UTC
You asked:
Is there a way to convert the combination of just day, hour and minute alone to UTC?
No, there is not a general way to do that. You must have the full date and time, including year, month, day, hour and minute. Otherwise you cannot be certain if daylight saving time is in effect, or if the standard time has changed.
You will have to assume some of the data you don't have. For example, you could make an assumption that the data is relative to "now". Just understand you will get different results depending on when you run the code.
Of course, this doesn't apply if there's only a single fixed offset for the time zone in question, such as Arizona being fixed to UTC-7. Just you can't assume this in the general case of any time zone of the world.