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)
Related
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 need to convert a peculiar format of textual dates into dates in Julia. I found out about the MONTHTOVALUE dictionary in the Dates documentation, but this object is missing from the Dates namespace, gives no results in the help, and only appears in the Github source code in the documentation. I used Date.format() with formatting "U" to define my own MONTHTOVALUE:
# Build dictionary from month names to integers
MONTHTOVALUE = Dict{String, Integer}()
for i in 1:12
month = Dates.format(Date(1990, i, 1), "U")
MONTHTOVALUE[month] = i
end;
# Regular expression for date in the format [Month Year(Quarter)]
date_regex = r"(^[A-Z][a-z]*) ?(\d{4}) ?\((I*V?)\)";
function string_to_date(date_string)
month = MONTHTOVALUE[replace(date_string, date_regex, s"\1")]
year = parse(Int, replace(date_string, date_regex, s"\2"))
return Dates.Date(year, month, 1)
end;
#assert Dates.Date(1860, 10, 1) == string_to_date("October 1860(III)")
Does the MONTHTOVALUE dictionary exist already, or do you suggest a cleaner way?
If I understand the question correctly, you wish to access the dictionary inside the Dates module that maps month names to their number ("March"=>3, etc.), is that correct?
If so, Dates.LOCALES["english"].month_value seems to be the one you're looking for:
julia> Dates.LOCALES["english"].month_value
Dict{String,Int64} with 24 entries:
"January" => 1
"august" => 8
"May" => 5
"may" => 5
"December" => 12
"january" => 1
"August" => 8
"november" => 11
"december" => 12
"September" => 9
"july" => 7
"september" => 9
"October" => 10
"june" => 6
"November" => 11
"April" => 4
"February" => 2
"october" => 10
"March" => 3
"June" => 6
"april" => 4
"march" => 3
"february" => 2
"July" => 7
(There's also a Dates.LOCALES["english"].month_abbr_value if you need it.)
I'm guessing that part of the documentation is outdated, and MONTHTOVALUE used to be month_value dict's older name.
There's also the function Dates.monthname_to_value, used like Dates.monthname_to_value("September",Dates.LOCALES["english"]), providing an interface into the above dict.
I've not heard of Julia, but passage seems relevant:
Support for text-form month parsing is also supported through the u
and U characters, for abbreviated and full-length month names,
respectively. By default, only English month names are supported, so u
corresponds to "Jan", "Feb", "Mar", etc. And U corresponds to
"January", "February", "March", etc. Similar to other name=>value
mapping functions dayname() and monthname(), custom locales can be
loaded by passing in the locale=>Dict{String,Int} mapping to the
MONTHTOVALUEABBR and MONTHTOVALUE dicts for abbreviated and full-name
month names, respectively.
https://docs.julialang.org/en/v0.6.2/manual/dates/
Edit: I think you can make the dictionary like this:
monthtovalue = Dict{UTF8String, Int}()
for (value, name) in VALUETOMONTH[locale::AbstractString="english"]
monthtovalue[lowercase(name)] = value
end
I want to know about how to represent the 'datetime' format such as "yyyy-MM-dd hh:mm" using EBNF.
One possible way of formulating this using EBNF is shown below. The expression parses only legal years, months and timestamps. However, it allows any month to have up to 31 days.
Timestamp = [ "-" ] Year "-" Month "-" Day " " Time ;
Year = Digit Digit Digit Digit ;
Month = "0" Digit | "1" "0".."2" ;
Day = "0".."2" Digit | "3" "0".."1" ;
Time = Hour ":" Minute ;
Hour = "0".."1" Digit | "2" "0".."3" ;
Minute = "0".."5" Digit ;
Digit = "0".."9" ;
I have pattern namely QUARTERDATE and FILENAME inside the file.
Both will have some value as in below eg.
My requirement is, I should rename the file like FILENAME_QUARTERDATE.
My file(myfile.txt) will be as below:
QUARTERDATE: 03/31/14 - 06/29/14
FILENAME : LEAD
field1 field2
34567
20.0 5,678
20.0 5,678
20.0 5,678
20.0 5,678
20.0 5,678
I want the the file name to be as LEAD_201402.txt
Date range in the file is for Quarter 2, so i given as 201402.
Thanks in advance for the replies.
newname=$(awk '/QUARTERDATE/ { split($4, d, "/");
quarter=sprintf("%04d%02d", 2000+d[3], int((d[1]-1)/3)+1); }
/FILENAME/ { fn = $3; print fn "_" quarter; exit; }' "$file")
mv "$file" "$newname"
How is a quarter defined?
As noted in comments to the main question, the problem is as yet ill-defined.
What data would appear in the previous quarter's QUARTERDATE line? Could Q1 ever start with a date in December of the previous year? Could the end date of Q2 ever be in July (or Q1 in April, or Q3 in October, or Q4 in January)? Since the first date of Q2 is in March, these alternatives need to be understood. Could a quarter ever start early and end late simultaneously (a 14 week quarter)?
To which the response was:
QUARTERDATE of Q2 will start as 1st Monday of April and end as last Sunday of June.
Which triggered a counter-response:
2014-03-31 is a Monday, but hardly a Monday in April. What this mainly means is that your definition of a quarter is, as yet, not clear. For example, next year, 2015-03-30 is a Monday, but 'the first Monday in April' is 2015-04-06. The last Sunday in March 2015 is 2015-03-29. So which quarter does the week (Mon) 2015-03-30 to (Sun) 2015-04-05 belong to, and why? If you don't know (both how and why), we can't help you reliably.
Plausible working hypothesis
The lessons of Y2K have been forgotten already (why else are two digits used for the year, dammit!).
Quarters run for an integral number of weeks.
Quarters start on a Monday and end on a Sunday.
Quarters remain aligned with the calendar quarters, rather than drifting around the year. (There are 13 weeks in 91 days, and 4 such quarters in a year, but there's a single extra day in an ordinary year and two extra in a leap year, which mean that occasionally you will get a 14-week quarter, to ensure things stay aligned.)
The date for the first date in a quarter will be near 1st January, 1st April, 1st July or 1st October, but the month might be December, March (as in the question), June or September.
The date for the last date in a quarter will be near 31st March, 30th June, 30th September, 31st December, but the month might be April, July, October or January.
By adding 1 modulo 12 (values in the range 1..12, not 0..11) to the start month, you should end up with a month firmly in the calendar quarter.
By subtracting 1 modulo 12 (values in the range 1..12 again) to the end month, you should end up with a month firmly in calendar quarter.
If the data is valid, the 'start + 1' and 'end - 1' months should be in the same quarter.
The early year might be off-by-one if the start date is in December (but that indicates Q1 of the next year).
The end year might be off-by-one if the end date is in January (but that indicates Q4 of the prior year).
More resilient code
Despite the description above, it is possible to write code that detects the quarter despite any or all of the idiosyncrasies of the quarter start and end dates. This code borrows a little from Barmar's answer, but the algorithm is more resilient to the vagaries of the calendar and the quarter start and end dates.
#!/bin/sh
awk '/QUARTERDATE/ {
split($2, b, "/")
split($4, e, "/")
if (b[1] == 12) { q = 1; y = e[3] }
else if (e[1] == 1) { q = 4; y = b[3] }
else
{
if (b[3] != e[3]) {
print "Year mismatch (" $2 " vs " $4 ") in file " FILENAME
exit 1
}
m = int((b[1] + e[1]) / 2)
q = int((m - 1) / 3) + 1
y = e[3]
}
quarter = sprintf("%.4d%.2d", y + 2000, q)
}
/FILENAME/ {
print $3 "_" quarter
# exit
}' "$#"
The calculation for m adds the start month plus one to the end month minus one and then does integer division by two. With the extreme cases already taken care of, this always yields a month number that is in the correct quarter.
The comment in front of the exit associated with FILENAME allows testing more easily. When processing each file separately, as in Barmar's example, that exit is an important optimization. Note that the error message gives an empty file name if the input comes from standard input. (Offhand, I'm not sure how to print the error message to standard error rather than standard output, other than by a platform-specific technique such as print "message" > "/dev/stderr" or print "message" > "/dev/fd/2".)
Given this sample input data (semi-plausible start and end dates for 6 quarters from 2014Q1 through 2015Q2):
QUARTERDATE: 12/30/13 - 03/30/14
FILENAME : LEAD
QUARTERDATE: 03/31/14 - 06/29/14
FILENAME : LEAD
QUARTERDATE: 06/30/14 - 09/28/14
FILENAME : LEAD
QUARTERDATE: 09/29/14 - 12/28/14
FILENAME : LEAD
QUARTERDATE: 12/29/14 - 03/29/15
FILENAME : LEAD
QUARTERDATE: 03/30/15 - 06/29/15
FILENAME : LEAD
The output from this script is:
LEAD_201401
LEAD_201402
LEAD_201403
LEAD_201404
LEAD_201501
LEAD_201502
You can juggle the start and end dates of the quarters within reason and you should still get the required output. But always be wary of calendrical calculations; they are almost invariably harder than you expect.
Noob here,
I'm stuck at trying to present user input in military time into standard time. The code works so far, but I need to subtract 12 hours from the end time to display in standard time. How do I do this using datetime.time? Also, do I need to convert the original user input to an integer to perform datetime.timedelta calculations? Previous questions don't seem to answer my coding questions.
My code is:
def timeconvert():
print "Hello and welcome to Python Payroll 1.0."
print ""
# User input for start time. Variable stored.
start = raw_input("Enter your check-in time in military format (0900): ")
# User input for end time. Variable stored.
end = raw_input("Enter your check-out time in military format (1700): ")
print ""
# ---------------------------------------------------------------------------
# Present user input in standard time format hhmm = hh:mm
# ---------------------------------------------------------------------------
import datetime, time
convert_start = datetime.time(hour=int(start[0:2]), minute=int(start[2:4]))
# need to find a way to subtract 12 from the hour to present end time in standard time
convert_end = datetime.time(hour=int(end[0:2]), minute=int(end[2:4]))
print 'You started at', convert_start.strftime("%H:%M"),'am', 'and ended at', convert_end.strftime("%H:%M"), 'pm'
# ---------------------------------------------------------------------------
# Use timedelta to caculate time worked.
# ---------------------------------------------------------------------------
# print datetime.timedelta
timeconvert()
raw_input("Press ENTER to exit program") # Closes program.
Thanks.
You can use strftime("%I:%M %p") to get standard 12 hour formatting with "AM" or "PM" at the end. See the Python documentation for more details on datetime string formatting.
Also, while it is not natively supported, you can simply use the two datetime.time instances to do your calculation as part of the timedelata constructor.
The below code should suffice, though proper error checking should definitely be used. ;)
--ap
start = raw_input("Enter your check-in time in military format (0900): ")
end = raw_input("Enter your check-out time in military format (1700): ")
# convert user input to datetime instances
start_t = datetime.time(hour=int(start[0:2]), minute=int(start[2:4]))
end_t = datetime.time(hour=int(end[0:2]), minute=int(end[2:4]))
delta_t = datetime.timedelta(
hours = (end_t.hour - start_t.hour),
minutes = (end_t.minute - start_t.minute)
)
# datetime format
fmt = "%I:%M %p"
print 'You started at %s and ended at %s' % (start_t.strftime(fmt), end_t.strftime(fmt))
print 'You worked for %s' % (delta_t)
def time12hr(string):
hours = string[:2]
minutes = string[2:]
x = " "
if int(hours) == 12:
x = "p.m."
hours = "12"
elif int(hours) == 00:
x = "a.m."
hours = "12"
elif int(hours) > 12:
x = "p.m."
hours = str(int(hours) - 12)
else:
x = "a.m."
return "%s:%s %s"%(hours ,minutes,x)
print time12hr('1202')
print time12hr('1200')
print time12hr('0059')
print time12hr('1301')
print time12hr('0000')