I am trying to pull data (some transaction related data) from DB. To pull the data, I am passing start date and end date as an argument to the query.
Here I need to pull the data of last 2 months. i.e., Start time would be Jun 01, 2022 and End time would be Aug 01, 2022.
Below is the script:
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use Date::Format;
use Date::Parse;
my $nowdate = DateTime->now(time_zone => 'local');
my ($month, $year) = ($nowdate->month, $nowdate->year);
my $date = DateTime->new(
year => $year,
month => $month,
day => 1,
);
my $end_time = str2time($date);
print "END:$end_time\n";
print time2str("%d-%m-%Y %T", $end_time)."\n"; #printing just to see in human readable format
my $start_time = $date->clone;
$start_time->add( months => 1 )->subtract( days => 92 );
$start_time = str2time($start_time);
print "START:$start_time\n";
print time2str("%d-%m-%Y %T", $start_time)."\n"; #printing just to see in human readable format
I have two issues over here:
I am using DateTime object two times to get end time. Can it be done in one shot?
$start_time->add( months => 1 )->subtract( days => 92 ); In this line of code, I have to explicitly mention subtract 92 days, which wouldn't be right always. Since some months have 30 days or 31 days or even 29 days. How can I get 2 month's beginning day date?
Another example: Lets assume if we are in September 2022, then Start time and End time would be Jul 01, 2022 and Sep 01, 2022 respectively.
Note: Perl version is 5.16.3
It would be good If I can do it with Core modules which comes with 5.16.3
You could simplify it by using truncate(to => 'month') to get to the first in the current month:
my $end = DateTime->now(time_zone => 'local')
->truncate(to => 'month');
This may however fail on a day without a midnight so this may be an option:
my $end = DateTime->now(time_zone => 'local')
->set_time_zone('floating')
->truncate(to => 'month');
Then subtract the number of months to get the start date:
my $start = $end->clone->subtract( months => 2 );
Then:
my $start_time = str2time($start);
my $end_time = str2time($end);
print "START:$start_time\n";
print time2str("%d-%m-%Y %T", $start_time)."\n";
print "END:$end_time\n";
print time2str("%d-%m-%Y %T", $end_time)."\n";
Possible output:
START:1654034400
01-06-2022 00:00:00
END:1659304800
01-08-2022 00:00:00
I have a variable that holds a string representing a date.
$d = "March 17,2019"
Actually, my code doesn't set d's value like that, but for arguments sake, lets assume that d holds a string date in the format shown.
Is there an easy way to change that d$ string to be in the following format instead: mm/dd/yy format?
Thanks
One more Basic code for your reference
$d1 = "March 17,2019"
$year=StringRight($d1,2) ; if you want like 2019 use StringRight($d1,4)
$rightstr = StringLeft($d1,(StringLen($d1)-5))
$test = StringSplit($rightstr, " ")
$mon = $test[1]
$day = $test[2]
Local $mon1
Local $aMMM[12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
for $i =0 to 11
if $mon = $aMMM[$i] Then
$mon1 = $i+1
EndIf
Next
$mon1= StringFormat("%02d", $mon1)
$finaldate = $day&"/"&$mon1&"/"&$year
MsgBox(1,"",$finaldate)
$d = "March 17,2019"
$sFormattedDate = _MyDate($d)
If Not #error Then
MsgBox(0, #ScriptName, $sFormattedDate)
EndIf
Func _MyDate($sDate, $iYearLen = 4)
; Get month, day and year from a string (3 = return array of global matches).
$aDate = StringRegExp($sDate, '(\w+)\s+(\d{1,2}),(\d{4})', 3)
If UBound($aDate) = 3 Then
; Create an array of months.
$aMonths = StringSplit('January|February|March|April|May|June|July|August|September|October|November|December', '|')
; Match month and return in mm/dd/yy format.
For $i1 = 1 To UBound($aMonths) -1
If $aDate[0] = $aMonths[$i1] Then
If $iYearLen <> 4 Then
$aDate[2] = StringRight($aDate[2], $iYearLen)
EndIf
Return StringFormat('%02d/%02d/%d', $i1, $aDate[1], $aDate[2])
EndIf
Next
EndIf
; Return error 1 if month is not matched.
Return SetError(1, 0, '')
EndFunc
Uses a regular expression to get month, day and year from the date string.
If the month is matched from an array of months, then the array index of
the month is used in StringFormat instead.
This will return 03/17/2019 from March 17,2019 in the example code.
If _MyDate() fails, #error is set to the value of 1.
StringFormat uses %02d/%02d/%d on each date segment which forces a
zero padding of 2 digits for month and day. If the zero padding is not
needed then remove the 02 between % and d.
If you want the year to be only 2 digits, then use 2 as the 2nd
parameter of _MyDate().
E.g.
$sFormattedDate = _MyDate($d, 2)
The pattern in StringRegExp uses:
\w to match a word character.
\d to match a digit.
\s to match a space.
Parentheses are used to get the 3 segments from the date string.
If you want to keep the month as is, and just replace the space and
the comma with a /.
$d = "March 17,2019"
$sFormattedDate = StringRegExpReplace($d, '[\s,]', '/')
MsgBox(0, #ScriptName, $sFormattedDate)
I'm trying to format a DateTime object to a String by using the Dates.format() function.
I'd like to get the week number for a specific time, however I can't find right formatting option in the docs.
I know I could get this by running Dates.week but I would really like to know if it's possible via format to have cleaner code (and crack a code golfing challenge...).
Here's some actual code:
julia> my_time = now()
2018-03-22T08:16:15.601
julia> Dates.week(my_time)
12
julia> Dates.format(my_time, "Y m d H:M:S")
"2018 3 22 8:16:15"
In R, I could do "%V" to get the weeks formatted. Is there a similar way in Julia?
No, there isn't a format specifier like %V:
julia> Base.Dates.CONVERSION_SPECIFIERS
Dict{Char,Type} with 12 entries:
'm' => Base.Dates.Month
'd' => Base.Dates.Day
'M' => Base.Dates.Minute
'Y' => Base.Dates.Year
'e' => Base.Dates.DayOfWeekToken
's' => Base.Dates.Millisecond
'U' => Base.Dates.Month
'E' => Base.Dates.DayOfWeekToken
'H' => Base.Dates.Hour
'y' => Base.Dates.Year
'u' => Base.Dates.Month
'S' => Base.Dates.Second
The in the code for these specifiers there's a note that there should be a way for packages to add more.
As stated in the title, I am attempting to get association rules for a dataset I am working with.
Unfortunately, the output is 388 rules likes this:
# lhs rhs support confidence lift
#[1] {} => {2760500500} 0.0005037281 0.0005037281 1
#[2] {} => {2638361901} 0.0005037281 0.0005037281 1
#[3] {} => {2810128001} 0.0005037281 0.0005037281 1
#[4] {} => {2217240500} 0.0005084802 0.0005084802 1
Is this a problem with the code or possibly the dataset I am using? Thanks.
EDIT: in the read.transactions function I used the parameter = list(minlen=2) to prevent this for future reference.
I'm building a basic line chart in Kendo UI Dataviz and I'm having a problem with the aggregation method. The goal is to group my sales data by month and aggregate the Sum(Amount) which seems to work in my chart but I'm experiencing a bug that causes data from the first day of October to be included in the sum for September. Oct 2 shows in October but Oct 1 is included in September's total.
The data is:
10/1/2013 12:00:00 AM, 22964.5000
10/2/2013 12:00:00 AM, 6762.9400
Html.Kendo().Chart(Model)
.Name("revenue")
.Title("Revenue Activity by Month")
.Legend(legend => legend
.Position(ChartLegendPosition.Bottom)
)
.Series(series =>
{
series.Area(s => s.TotalRevenue, categoryExpression: model => model.Date).Aggregate(ChartSeriesAggregate.Sum).Name("Total Revenue").Color("#73c100");
series.Line(s => s.RevenueSubscriber, categoryExpression: model => model.Date).Aggregate(ChartSeriesAggregate.Sum).Name("Subscriber Revenue");
series.Line(s => s.RevenueNonSubscriber, categoryExpression: model => model.Date).Aggregate(ChartSeriesAggregate.Sum).Name("Non-Subscriber Revenue");
})
.CategoryAxis(axis => axis.Date()
.BaseUnit(ChartAxisBaseUnit.Months)
)
.ValueAxis(axis => axis
.Numeric("revenue")
.Labels(labels => labels.Format("{0:C}"))
.Line(line => line.Visible(false))
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Format("{0:C}")
)
What I receive is a chart with two points on the X Axis. The first point is Sept 2013 and includes $22,964. The second point is Oct 2013 and includes $6,762.