Formatting Date in Title - reportbuilder3.0

I have figured out how to get a date (from a report parameter) in my title:
[#Start]
Annoying that I cannot do "this" which is what I really want to do
[#Start] to [#End]
But I can deal with that; it just means 3 titles instead of one.
However, what I cannot seem to figure out is how to format the date:
I Get: 11/13/2011 12:00:00 AM
I want: Nov 13th
I could live with 11/13/2011

For the suffix I recommend to use the switch function:
=Format(Parameters!Start.Value, "MMM-dd") +
Switch(Format(Parameters!Start.Value, "dd") >= 11 And Format(Parameters!Start.Value, "dd") <= 13 , "th",
Right(Format(Parameters!Start.Value, "dd"), 1) = "1", "st",
Right(Format(Parameters!Start.Value, "dd"), 1) = "2", "nd",
Right(Format(Parameters!Start.Value, "dd"), 1) = "3", "rd",
1 = 1, "th")
Or you can make a user function to do the same...

It took awhile to figure out that the "expr" field can be very complex.
The following gives me very close to what I need:
=Format(Parameters!Start.Value, "MMM-dd") + " to " + Format(Parameters!End.Value, "MMM-dd")

Related

How to get the minutes from a timespan in KQL

I wanted to extract the time in minutes for a Kusto query I was working on. I have a cloumn where timespan is represented in the following format (HH:MM:SS.MilliSeconds) 01:18:54.0637555. I wanted to extract the number of minutes from this in this case 78 minutes. How can I do that ?
Try dividing the timespan value by 1min, as explained here: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/datetime-timespan-arithmetic
If you just need to print the timespan parts, you can create a small user-defined function to collect each part of the timespan:
let print_timespan = (input: timespan) {
iif(
isempty(input), "",
strcat(
format_timespan(input, 'dd'), "d ",
format_timespan(input, 'hh'), "h ",
format_timespan(input, 'mm'), "m ",
format_timespan(input, 'ss'), "s ")
)
};
let t = time(29.09:00:05.12345);
print print_timespan(t)
---
29d 09h 00m 05s

Extract bibtex entries based on the year

Okay, I got the file.bib file with multiple entries such
#Book{Anley:2007:shellcoders-handbook-2nd-ed,
author = {Chris Anley and John Heasman and Felix Lindner and Gerardo
Richarte},
title = "{The Shellcoder's Handbook}",
publisher = {Wiley},
year = 2007,
edition = 2,
month = aug,
}
there you can find the "year = 2007" line. My task is to filter out the years which are greater than 2020 ($currentyear) or lower than 1900 ($minyear), the result should be a also the output of the month "may", which stands behind a "year" line in this file. (Which is a mistake by the admin). (btw the file is over 4000 lines long).
It is better to use awk for this. Similar to your line, it would read:
awk -v t1="1900" -v t2="$(date "+%Y")" \
'!match($0,/year.*=.*/){next}
{t=substr(RSTART,RLENGTH)
match(t,/[0-9][0-9][0-9][0-9]/)
y=substr(RSTART,RLENGTH)
}
(y > t1) && (y <= t2) { print y }' file

Finding the Last Day of the Month in a Year (VB 6.0)

I'm creating a program that will predict the last day of the month. I've managed to give the months their value, but the year needs to be written as well in order to determine whether it's a leap year or not.
Objects:
2 Textboxes, 1 Command Button, 1 Label
Needs to be Select case unfortunately.
My code:
Private Sub Command1_Click()
Dim month, year As String
month = txtmonth.Text
year = txtyear.Text
Select Case month
Case "Feb", "February"
lblday.Caption = CStr(Day(DateSerial(year, 3, 1) - 1))
Case "Jan", "January", "Mar", "March", "May", "Jul", "July", "Aug", "August", "Oct", "October", Dec, December
lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, 3 + 2, 1) - 1)) & " " & "days"
'year/30/month
Case "April", "Jun", "June", "Sept", "September", "Nov", "November"
lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, 3 + 1, 1) - 1)) & " " & "days"
'year/31/month
End Select
End Sub
What is the user supposed to enter into the textbox, string or a number?
If user input is a string, then you should put months in quotes like this Case "Feb", "February" and month needs to be declared as a String and assigned txtmonth.Text directly without Val
If user input is a number, then you need to remove all the short and long month names from the Case like Feb and March and use a number like this Case 2 ' this is for February
If you wish to optimize your code (assuming you absolutely have to have a Case in there), instead of asking user for a leap year you can calculate that yourself:
Case Feb, February
lblday.Caption = Cstr(day(DateSerial(Year(Now), 3, 1) - 1))
if you didn't have to have a Case in there, all your code could of been optimized to this:
Private Sub Command1_Click()
lblday.Caption = CStr(Day(DateSerial(Year(Now), Month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1))
End Sub
With that code you can enter month number, month full name or month short name and it will work. It will assume the year to be the current year (you didn't specify whether user enters year or not).
EDIT:
I took your code and fixed the following:
rearranged months because some months were in the case for 30-day month and they were 31-day months and the other way around
changed case into lower case. This way you can enter month name in any case you wish. For example, before, only Oct would work. Now anything works: oct, OCT, oCt, OcT ...
changed Sept to sep to be consistant
changed the way you calculate days in a month. For 30 day months, you take the number of days from April with this Day(DateSerial(year, 3 + 2, 1) - 1) and for 31 day months from March with this Day(DateSerial(year, 3 + 1, 1) - 1) . What happens if in the future, we change the number of days in March? the whole logic collapses. I realize this is very unlikely, but still a possibility. There is absolutely no reason why we can't calculate each months on its own like this CStr(Day(DateSerial(year, month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1))
because of the thing I changed above, I had to use function called Month but you already had a badly named variable called month so it was a conflict I had to fix and thus removed variable month altogether. If you have to have it, rename it to myMonth or something.
Some tips to have a well defined code:
- rename year to myYear
use camel style: change all labels and text boxes to follow this naming convention. Example: renamelbldaytolblDayandtxtmonthtotxtMonth
don't use variables if you don't need to. month variable was unnecessary. You could even remove year variable too, since you do not do any calculations on it at all.
Private Sub Command1_Click()
Dim year As String
year = txtyear.Text
Select Case LCase(txtmonth.Text)
Case "feb", "february"
lblday.Caption = CStr(Day(DateSerial(year, 3, 1) - 1))
Case "april", "apr", "jun", "june", "sep", "september", "nov", "november"
lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1)) & " " & "days"
'year/30/month
Case "jan", "january", "mar", "march", "may", "jul", "july", "aug", "august", "oct", "october", "dec", "december"
lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1)) & " " & "days"
'year/31/month
End Select
End Sub
LastDate= DateAdd("m", 1, DateAdd("d", -1 * Day(Date), Date))

How to calculate a formula with undefined variable

I am trying to eval a string with undefined variable. For example: Formula = 2 * 3 + a The result should return a string of 6 + a. Can the eval method do something like that? Or, can you give me some ideas on how to do this?
Update: Thank you for all the inputs. I guess this is not as simple as i thought it would be. Let's say if I don't need to simplify the formula and all i need to do is to replace the variable with value in string?
Example:
a = { "Bob" => 82,
"Jim" => 94,
"Billy" => 58, ........ and more}
How do I convert this string
"2 * 3 + a["Bob"] * b"
to this: "2 * 3 + 82 * b"
Thanks again for your help.
What you are trying to do is complicated, and I don't think it is worth doing it.
You can use some gem to parse the string into a tree of tokens. Then, look for any node under which there is no undefined variable, and replace the node with the calculated value. After doing that, put the tree back to a string.
What about using something like Parslet and making a parsing expression grammar to simplify your expressions? Look at the get started page where you are walked through a simple example in which Parslet reduces integer expressions.
Here is a naïve solution:
str = "+ 1 * 2 - 3 / 2 + b - a"
terms = str.gsub(/^[^+-]+|[+-][^+-]+/).to_a
=> ["+ 1 * 2 ",
"- 3 ",
"+ b ",
"- a"]
numbers, variables = terms.partition { |exp| eval(exp.to_s) rescue false }
=> [["+ 1 * 2 ", "- 3 "],
["+ b ", "- a"]]
numbers.map! { |exp| exp.gsub(/\d+/, &:to_f) }
=> ["+ 1.0 * 2.0 ",
"- 3.0 / 2.0 "]
numbers.map! { |exp| eval(exp) }
=> [2.0, -1.5]
sum = numbers.reduce(:+)
=> "0.5"
result = "#{sum} #{variables.join}"
=> "0.5 + b - a"

Simplest way to display each hour of the day in Ruby

I have a calendar screen where I want to display the hours of the day like this:
12:00am
1:00am
2:00am
..
4:00pm
5:00pm
etc.
Being a total Ruby noob, I was wondering if anyone could help me figure out the simplest way to display this.
#!/usr/bin/env ruby
# without using actual `Date` objects ...
p ["12:00am"] + (1..11).map {|h| "#{h}:00am"}.to_a +
["12:00pm"] + (1..11).map {|h| "#{h}:00pm"}.to_a
["12:00am", "1:00am", "2:00am", "3:00am", "4:00am", "5:00am", "6:00am",
"7:00am", "8:00am", "9:00am", "10:00am", "11:00am", "12:00pm", "1:00pm",
"2:00pm", "3:00pm", "4:00pm", "5:00pm", "6:00pm", "7:00pm", "8:00pm",
"9:00pm", "10:00pm", "11:00pm"]
Or using actual DateTime objects and %I:%M%p as format:
#!/usr/bin/env ruby
require "Date"
for hour in 0..23 do
d = DateTime.new(2010, 1, 1, hour, 0, 0)
p d.strftime("%I:%M%p")
end
Which would print:
"12:00AM"
"01:00AM"
"02:00AM"
"03:00AM"
"04:00AM"
"05:00AM"
"06:00AM"
"07:00AM"
"08:00AM"
"09:00AM"
"10:00AM"
"11:00AM"
"12:00PM"
"01:00PM"
"02:00PM"
"03:00PM"
"04:00PM"
"05:00PM"
"06:00PM"
"07:00PM"
"08:00PM"
"09:00PM"
"10:00PM"
"11:00PM"
You could generate these like this:
array = ['12:00am'] + (1..11).map {|h| "#{h}:00am"} + ['12:00pm'] + (1..11).map {|h| "#{h}:00pm"}
or simply write out the array (this is more efficient):
array = ["12:00am", "1:00am", "2:00am", "3:00am", "4:00am", "5:00am", "6:00am", "7:00am", "8:00am", "9:00am", "10:00am", "11:00am", "12:00pm", "1:00pm", "2:00pm", "3:00pm", "4:00pm", "5:00pm", "6:00pm", "7:00pm", "8:00pm", "9:00pm", "10:00pm", "11:00pm"]
You can then print these however you want, eg.
array.each do |el|
puts el
end

Resources