%Calendar.AmbiguousDateTime{possible_date_times calendar Elixir - calendar

I have been doing date time conversions using Calendar everything was working fine for last 4 months but all of sudden am starting some strange behaviour in this,
what am doing is
1..total_days |> Enum.reduce(start_date, fn _i, acc ->
day_of_week = acc |> Calendar.Date.day_of_week_name
rec_head = get_head_tail(schedule[day_of_week])
rec_head |> Enum.each(fn(x) ->
iterate(x, acc, timezone) |> t_download(interval, t_agent)
end)
acc |> Calendar.DateTime.to_erl |> IO.inspect |> Calendar.DateTime.from_erl!(timezone, {123456, 6}) |> IO.inspect |> Calendar.DateTime.add!(86400)
end)
And the
acc |> Calendar.DateTime.to_erl |> IO.inspect |> Calendar.DateTime.from_erl!(timezone, {123456, 6}) |> IO.inspect |> Calendar.DateTime.add!(86400)
give output as
{{2016, 10, 27}, {1, 0, 0}}
%Calendar.DateTime{abbr: "IST", day: 27, hour: 1, min: 0, month: 10, sec: 0,
std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0,
year: 2016}
{{2016, 10, 28}, {1, 0, 0}}
%Calendar.DateTime{abbr: "IST", day: 28, hour: 1, min: 0, month: 10, sec: 0,
std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0,
year: 2016}
{{2016, 10, 29}, {1, 0, 0}}
%Calendar.DateTime{abbr: "IST", day: 29, hour: 1, min: 0, month: 10, sec: 0,
std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0,
year: 2016}
{{2016, 10, 30}, {1, 0, 0}}
But on the last date and time instead of giving me right output as it is giving above, It is giving this
{:ambiguous, %Calendar.AmbiguousDateTime{possible_date_times: [%Calendar.DateTime{abbr: "GMT", day: 30, hour: 1, min: 0, month: 10, sec: 0, std_off: 0, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0, year: 2016}, %Calendar.DateTime{abbr: "IST", day: 30, hour: 1, min: 0, month: 10, sec: 0, std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0, year: 2016}]}}
Am totally unknown to that reason as It was working fine but all of sudden its giving me these errors. any help?
UPDATE: am using https://github.com/lau/calendar Calendar.

According to the docs, this happens when the input time you provide is ambiguous:
AmbiguousDateTime provides a struct which represents an ambiguous time
and date in a certain time zone. These structs will be returned from
the DateTime.from_erl/2 function when the provided time is ambiguous.
AmbiguousDateTime contains two DateTime structs. For instance they can
represent both a DST and non-DST time. If clocks are turned back an
hour at 2:00 when going from summer to winter time then the “wall
time” between 1:00 and 2:00 happens twice. One of them is on DST and
one of them is not.
This seems to be your case. The times output are 1 a.m. Europe/Dublin time, and the 1 a.m. occurs twice on October 30th 2016: the second time is when at 2 a.m. you turn the clock back when Daylight Savings Time ends.
It seems like you can use the disamb/2 function to disambiguate the result you're getting.

Related

Rechart draw lines for mutiple sets of data

Say, I have two arrays of data, A and B.
A = [
{name:'Mon Oct 24 2022 17:00:00', value: 1},
{name:'Mon Oct 24 2022 18:00:00', value: 2},
{name:'Mon Oct 24 2022 19:00:00', value: 3},
{name:'Mon Oct 24 2022 20:00:00', value: 4}
]
and
B = [
{name:'Mon Oct 24 2022 17:30:00', value: 5},
{name:'Mon Oct 24 2022 18:30:00', value: 4},
{name:'Mon Oct 24 2022 19:30:00', value: 3},
]
Now I want to draw two lines with different colours for these data in the same line chart with the correct x-axis, e.g. B[0] is in the middle of A[0] and A[1]. But I couldn't make it work, it's always the case that the line for B comes after the line for A the line.
It would be better if the x-axis uses [0, 'auto'] as the domain property.
Ah! Solve it. Conver time string to stamp, and make Axis type be number.

Get all days from a month

I wish to have all the days from the current month in an array. For example this month (April 2022) has 30 days so I wish to have an array of integers like so:
const monthDays = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 , 30 ]
My attempt :
Array.from(Array(moment('2022-04').daysInMonth()).keys())
And the output is :
//  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
I have and idea how moment works and 0 is always the first day or the first month , but how can i achieve the result that i want from the example above
So basically moment will generate automatically this array if I fetch the current month. How can we achieve that?
Create moment object
Set the month to the desired month
Use daysInMonth() to get the number of days
Create an array from 1 to the result of step 3
const mom = new moment();
mom.set('month', 3); // 0-indexed, so 3 --> 4 --> April
const daysInApril = mom.daysInMonth();
const aprilDays = Array.from({length: daysInApril}, (_, i) => i + 1);
console.log(aprilDays);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Want to concatenate elements of array into single element

I have an array of arrays that looks like
time
array([array([ 0, 1, 0, 10, 12, 2011], dtype=int16),
array([ 0, 1, 0, 10, 12, 2011], dtype=int16),
array([ 0, 1, 0, 10, 12, 2011], dtype=int16), ...,
array([ 0, 59, 23, 10, 12, 2011], dtype=int16),
array([ 0, 59, 23, 10, 12, 2011], dtype=int16),
array([ 0, 59, 23, 10, 12, 2011], dtype=int16)],
dtype=object)
and I would like to transform this into something like
time
array([0:1:0 10-12-2011,
etc
0:59:23 10-12-2011])
I feel like I should be able to do this for the whole structure without having to loop through each individual row/column.
I would say you cannot avoid loops, but you can get a pretty decent result by looping through the outer array and converting your data into datetime objects. Let's say a is your array:
from datetime import datetime
results = array([datetime(*row[::-1]) for row in a])

Have any of these attempts to get GPS time into a mavlink been successful?

I'm trying to get GPS time out of a pixhawk. I've found a bunch of discussions about such but none that appear to have been resolved. Is there any update?
This guy and this guy were both told to just use system time (which is not UTC)
I also tried to get GLOBAL_POSITION_INT_COV but found it was not available.
Here is a long dev discussion about such that was never resolved (circa 2013)
Another dev discussion that references a bunch of pull requests for such - but doesn't look like any of them ever made it in or am I wrong?
Much thanks!
As noted in your second link, ArduPilot sends unix time in the SYSTEM_TIME message. You didn't mention what language you are using, but in Python, unix time can easily be converted to UTC using the datetime module.
#vehicle.on_message('SYSTEM_TIME')
def listener(self, name, message):
unix_time = (int) (message.time_unix_usec/1000000)
print(datetime.datetime.fromtimestamp( unix_time ))
GPS times reported in MAVLink messages are in two parts: The GPS week number, and the number of milliseconds since the start of that week. Also, need to know when GPS weeks started (Jan 6, 1980).
Here's a bit of javascript code that seems to work well for me (could easily be ported to other languages):
// no. of milliseconds in a date since midnight of
// January 1, 1970, according to UTC time;
// months are zero-based, but days are 1-based
var GPS_EPOCH_MILLIS = (new Date(1980, 0, 6, 0, 0, 0, 0)).getTime();
// all the leap seconds that have been defined so far;
// must keep this list current!
var leapSecondsMillis = [
(new Date(1981, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1982, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1983, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1985, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1988, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1990, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1991, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1992, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1993, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1994, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1996, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1997, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1999, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(2006, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(2009, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(2012, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(2015, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(2017, 0, 1, 0, 0, 0, 0)).getTime()
];
// convert GPS time to a date & time object
var gpsTimeToDate = function(gpsWeek, millisIntoGPSWeek) {
return new Date(gpsTimeToMillis(gpsWeek, millisIntoGPSWeek));
};
// 604,800 seconds in a week (60 x 60 x 24 x 7)
var gpsTimeToMillis = function(gpsWeek, millisIntoGPSWeek) {
var millis = GPS_EPOCH_MILLIS + (gpsWeek * 604800000) + millisIntoGPSWeek;
// add leap seconds as appropriate
for (var leap in leapSecondsMillis)
if (millis >= leap)
millis -= 1000;
return millis;
};

How convert raw data to useful angles?

I use Gyro L3GD20 and STM32F4 microcontroller. I get this data from gyro sensor.
0, -1, -1, -2, -3, -4, -5, -6, -6, -5, -3, -4, -4, -5, -6, -5, -3, -3, -3, -3, -3, -4, -4, -4, -3, -3, -3, -3, -5, -5, -5,
-4, -4, -4, -5, -5, -6, -6, -5, -5, -6, -6, -8, -9, -10, -10, -11, -12, -14, -16, -17, -16, -14, -12, -11, -10, -8, -7, -8,
-8, -8, -8, -6, -5, -4, -4, -2, -2, -3, -2, -2, -2, -2, -1, -2, -2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3,
4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 9, 9, 8, 9, 10, 12, 15, 19, 17, 13, 9, 9, 14, 19, 8, 15, 15, 17, 17, 18, 17, 14, 14, 15, 15,
11, 7, 5, 5, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
This relates to the angular-velocity along the x-axis.
I would like to extract angle from raw data.
Piece of code
while(1)
{
if(measure)
{
///20 ms///.
L3GD20_Read(&L3GD20_Data);
int16_t x = L3GD20_Data.X;
USART_putint(USART2, _x, 10);
USART_SendData(USART2, ',');
measure = 0;
}
}
Thank You in advance.
A MEMS gyroscope is an angular velocity sensor where the output proportional to degrees-per-second. To obtain relatve orientation from angular-velocity, you must integrate over time, which will get you the change in angle over that time. Essentially the change in angle is proportional to the sum of all angular-velocity samples.
The L3GD20 is a three-axis sensor so can provide output for yaw, pitch and roll. It has an I2C/SPI digital interface and performs the sampling and timing for you, and places the data in a FIFO, so you probably should not second guess that timing by only reading at 20ms intervals; rather you should read all available data in response to the data-ready interrupt. (The mimimum output data rate of the part is 95 samples per second, so you are loosing data when reading at 50sps). How you do that depends on the API you are using. It has programmable sensitivity of 250, 500 or 2000 degrees per second; you should use the lowest value practical to get the highest resolution.
#define GYRO_FS_DPS 250
#define GYRO_ABS_SAMPLE_MAX 0x7fff
long x_integrator = 0 ;
while(1)
{
while( /* L3GD20 data available */ )
{
L3GD20_Read( &L3GD20_Data ) ;
x_integrator += L3GD20_Data.X ;
}
...
}
Then relative orientation in degrees is determined at any time by:
orientation = ((x_integrator * GYRO_FS_DPS) / GYRO_ABS_SAMPLE_MAX) % 360 ;
Ultimately you need to clearly understand the part's datsheet, it is a very flexible and configurable part and exactly how you process its data will depend a great deal on how you have set it up.

Resources