Divide by 6 hours in jinja date format - jinja2

I have time string which represents a time in ISO8601 format.
The formula to format to a date: {{ time.strftime('%Y-%m-%d') }}
Current output:
time = 2018-01-23T08:11:58.000000
output: 2018-01-23
I want to divide it in 6 hours spans:
time = 2018-01-23T08:11:58.000000
output: 2018-01-23_1 // last "1" represents second span of 6 hours in 24 hours day (6-12)
What I need to add to my formula to achieve this?

Just divided hour by 6.
{{ time.strftime('%Y-%m-%d') }}_{{ time.strftime('%H')/6 }}
and got results:
2018-01-23_1 // for hours 6-12
2018-01-23_2 // for hours 12-18
No idea why couldn't think this simple logic earlier.

Related

Inline AmpScript Help Needed

Can the below 2 codes work as one inline ampScript on the html page? For example, the following is the copy I want to turn dynamic: Until 30 March 2023, you can get our boiler cover2 from £7.50 a month for your first 6 months. That’s half the usual price for the first 6 months, to help keep your boiler working safely and efficiently.
My two codes are :
'%%[
Set #field_energy_type = AttributeValue("energy_type")
If Empty(#field_energy_type) Or #field_energy_type = "" Then
Set #value_energy_type = " Until 30 March 2023..."
Else
Set #value_energy_type = ""
EndIf
]%%
%%[
SET #energy_type = "dual_fuel"
IF #energy_type == "electricity_only" THEN
SET #price = "£7.50"
ELSEIF #energy_type == "dual_fuel" THEN
SET #price = "£12"
ELSE
SET #price = "£7.50"
ENDIF
]%%'
And the following Ampscript based rules within the code I want to archive:
If the field energy_type is not empty then show the copy starting 'Until 30 March 2023... all the way until below the 'Sign me up' call to action.'
If the field energy_type = electricity_only then the price in the above paragraph should be £7.50 a month, if the field energy_type = dual_fuel then the price should be £12 per month.
Hope this helps.
Thanks in advance.
And the following Ampscript based rules within the code I want to archive:
If the field energy_type is not empty then show the copy starting 'Until 30 March 2023... all the way until below the 'Sign me up' call to action.'
If the field energy_type = electricity_only then the price in the above paragraph should be £7.50 a month, if the field energy_type = dual_fuel then the price should be £12 per month.
Hope this helps.
Thanks in advance.

Override Date formatting tick hierarchy in lightningchart.js

I'm trying to build a heatmap in LightningChart that covers a fixed 24 hours time span in 15 minutes intervals. For the sake of conveinience, that interval is inclusive, so if I'm reading a midnight-to-midnight interval, I expect to have the data for both midnights, making for an effective range of 24¼ hours over 97 samples.
My problem is that, when using the datetime tick strategy, having an axis interval covering more than exactly 24 hours causes it to use the FormattingDay format, which uses a tick hierarchy of great: ISOWeek, major: DayOfMonth, minor: Hour, which is very inconveinient to read in this sort of situation.
On the left is when the great tick (ISO week, every monday) shows up in the middle of the axis (from Sunday August 21 to Monday August 22), and on the right is when only a major tick is visible (from Monday August 22 to Tuesday August 23). Both of these I find very confusing to read and not at all what I want the axis to look like. The use of months would actually be preferable, as ISO weeks aren't exactly a common layman time representation.
Is there a way to force a specific tick hierarchy to be used to label axes? Either great: Month, major: DayOfMonth, minor: Hour or great: DayOfMonth, major: Hour, minor: Minute would be good, but the default of great: ISOWeek is unusable to me.
That seems very inconvenient indeed.
Maybe in your case it would be actually easier to manually place each tick along the Y axis?
You can create custom ticks like this:
const tick = Axis.addCustomTick(UIElementBuilders.AxisTick)
Here's an example that you can reference for styling the ticks: https://www.arction.com/lightningchart-js-interactive-examples/edit/lcjs-example-0011-customTicksScrolling.html
In any case this is an important form of feedback, unfortunately there are a number of cases where the current DateTime ticks don't deliver.
Not a perfect solution, but a suitable workaround for the time being: I ended up just using the axis range extremums to detect when the value to be formatted is a great tick beyond what the axis should display (ticks pointing to the "start of the current week"/"start of next week" outside the chart have a value of midnight the previous Monday), and clamp it to the axis range, so they can just be formatted as dates.
myChart.getDefaultAxisY().setTickStrategy("DateTime", (ticks) => {
// If the axis range covers more than exactly 24h, LightningCharts uses FormattingDay instead of FormattingHour.
// That means one great tick at the start of every monday (2022-W33) but not the start every day (that's major ticks) or every month (August 1st is a regular major tick)
// This still makes for a great tick at the start of every Monday, but clamps the date to match what's inside the axis,
// and formats it like a date, so the "context" dates at both ends of the axis behave more or less like they do with FormattingHour.
return ticks.setFormattingDay(
(val, range, loc) => {
let adjustedVal;
if (range.getInnerStart() < range.getInnerEnd()) {
adjustedVal = Math.min(Math.max(range.getInnerStart(), val), range.getInnerEnd())
} else {
adjustedVal = Math.min(Math.max(range.getInnerEnd(), val), range.getInnerStart())
}
let formattedDate = new Date(adjustedVal);
return formattedDate.toLocaleDateString(loc);
},
{ month: 'long', day: '2-digit' }, // Midnight every day
{ hour: '2-digit', minute: '2-digit' } // Every hour
);
// return ticks.setFormattingDay(
// { month: 'long', day: '2-digit' }, // Midnight every ISO start of week (Monday)
// { month: 'long', day: '2-digit' }, // Midnight every day
// { hour: '2-digit', minute: '2-digit' }, // Every hour
// );
});

Convert data types within a custom function

I have a Google Spreadsheets I have been using to keep track of my hours worked at my job. I am trying to create a custom function to calculate my total hours for the week. Say I work 6 hours of overtime, but then take Friday off. My regular hours would be 32, and I would have 6 hours or overtime. In the event that I don't work 40 hours, I would like to adjust my total hours by taking from any overtime hours and adding to my regular hours.
I have come up with the following function, but I have not yet been able to make it work. I believe I am running into a problem with data types (the inputs are Durations), but I'm not sure how to resolve it. I am dividing by 24 because that seems to convert the values from Duration to Number, but I still can't get it to return the correct answer.
function calcAdjRegHours(regHours, otHours) {
if(regHours<(40/24));
{
if(otHours>(0/24));
{
if((regHours + otHours)>(40/24));
{
var diff = (40/24) - regHours;
regHours += diff;
return regHours;
} elseif; {
return "regHours + otHours is less than 40";
}
} elseif; {
return "there are no otHours";
}
} elseif; {
return "regHours is greater than 40";
}
}
What am I overlooking, or am I making this overly complicated?
Edit: When I call this function with inputs of 40:00, and 2:00, I get the value:
Sun Dec 31 1899 17:00:00 GMT-0700 (MST)2208988800001.6665.
If I run this function:
function calcAdjRegHours(regHours, otHours) {
return ((regHours*24 + otHours*24)/24);
}
I get: -4418114400000.
If I use "return (regHours + otHours);", I get:
Sun Dec 31 1899 17:00:00 GMT-0700 (MST)Sat Dec 30 1899 03:00:00 GMT-0700 (MST).
Something is going wrong when I try to add the variables. They are formatted as Duration, and from my research I can/need to convert them to do arithmetic. I did that by multiplying the variables by 24, adding, and then dividing by 24 again to get it back to a duration.
I ended up using: =if(F14<(40/24), (F14+G14), if(F14=(40/24), F14, F14-(40/24))). That did the trick, although I would still like to come up with a custom function that would do it a little more nicely.

Convert UK date to US date in Flex

Hi there seems to be plenty out there to convert a US formatted date (MM/DD/YYYY) to a UK date (DD/MM/YYYY), but I'm trying to do the opposite: i receive a load of UK dates (DD-MM-YYYY) from the server which I'm trying to format as DD-MMM-YYYY (eg: 11 Jan 2013 ), but Flex thinks it's trying to convert American days and so when it gets 17-02-2013, it returns an empty string because there is no month 17.
How do i tell it I'm giving it UK dates and not US dates?
Thanks in advance.
Short answer, you can't.
Longer answer, there's not enough information to determine the date format. As you saw, in some cases you CAN determine that the date is not valid for the expected format (17-02-2013 - since 17 isn't a valid month, it must be DD-MM-YYYY rather than MM-DD-YYYY), but for just under half of the dates you just can't tell (is 01-02-2013 supposed to be Jan 2 or Feb 1?)
If you DO know that the dates are supposed to be in a particular format, you can use DateField.stringToDate to parse the string:
var value:Date = DateField.stringToDate(dateString, 'DD-MM-YYYY');
OK, got round it in the end with this ugly hack: ended up converting the string into an array and re-organising it so that it would fit the american format before using the DateFormatter.format on the result. There must be a more elegant way...
<Script>
public convertDateStringToUS( date : String ) : String
{
var dateArray : Array = date.split( "-" );
var day = dateArray[0];
var month = dateArray[1];
var year = dateArray[2];
date = month + "-" + day + "-" + year;
return date
}
</Script>
<declarations>
<mx:DateFormatter id="dateFormatter"
formatString="DD-MMM-YYYY" />
</declarations>
<s:Label id="myDateLabel"
text =" { dateFormatter.format( convertDateStringToUS( date ) ) } "/>

rails email sending code needs days + 2 for tomorrow, why is that?

I have a loop to select 'LibrarySwaps' for tomorrow.
This works, but not when I set days_ahead default to 1 (it returns records with todays date).
Why do I need to add 2 to the date to get a day than is only 1 day in the future?
I am doing this 11am EST so this is not a time zone issue with that and UTC both being the same day... I thought maybe 'cos one side has a time component and the other doesn't but nope, I'm using date() for the sql and Date + 1.days for the ruby. I may switch to (one date minus the other date) and look at the result.
Thanks!
Returns Tomorrows (uses 2):
def self.find_future_swaps(days_ahead=2)
#upcoming_swaps = LibrarySwap.all(:conditions => ['date(suggested_date) = ?',Date.today + days_ahead.day ])
end
Returns Todays (uses 1):
def self.find_future_swaps(days_ahead=1)
#upcoming_swaps = LibrarySwap.all(:conditions => ['date(suggested_date) = ?',Date.today + days_ahead.day ])
end
MySQL is likely storing your suggested_date field in UTC. So an entry from 10pm on 4/29 would actually be stored as 3am on 4/30 (assuming you're in the Eastern timezone).
You can do this to add the offset to the times you're searching for:
#upcoming_swaps = LibrarySwap.all(:conditions => ['date(convert_tz(suggested_date,'+00:00','-05:00')) = ?',Date.today + days_ahead.day ])