I want regular expression for HH:MM:SS AM/PM here HH must be 1-12 only, MM must 60 min, SS as usual (60 sec.) I any have it properly ...?
(0[1-9]|1[0-2]):([0-5][0-9]:[0-5][0-9]|(59|44|29):60) (AM|am|PM|pm)
/^([1-9]|10|11|12):[0-5][0-9]:[0-5][0-9] [AP]M$/
If you want to be able to have the zero padding as optional as well as leap seconds:
/^((0?[1-9])|(1[0-2])):[0-5]\d:(([0-5]\d)|(60)) [AP]M$/
The breakdown:
(0?[1-9]|1[0-2]) 1-9 (with optional leading zero) or 10-12
[0-5][0-9] 00-59 (0-5 for first digit, 0-9 for second)
([0-5][0-9])|(60) Leap seconds
[AP]M AM/PM
Related
I have a requirement where I need to kick off certain processes on end of every month. I am getting the date as an Input from previous process and date is in Julian format (for e.g. 2020105 CCYYDDD). I need to check if the date passed is month end. I'm thinking of 2 options:
Directly check if Julian date is month end date.
Convert Julian date to Gregorian or Regular date and then check if it is month end.
In either cases, please guide me to some materials I can refer.
I'm aware I can get the current date using echo $(date '+%Y-%m-%d') and then check if its the last day of the month. But I need to strongly consider the Julian date that is coming as the input to my process as it is being sent by the Business.
Thanks in advance for the help.
If you have Gnu date, you can use the following function:
next_day_of_month () {
date -d "${1:0:4}-01-01 $((10#${1:4:3})) days" +%-d
}
which takes a Julian date in the format you specify, and computes the day of the month of the following day.
If you want a pure bash solution (which, despite the fact that it calls no external utilities, is seriously slow), you can use the following.
There is a not-very-complicated formula which can be used to compute the month corresponding to the day number, but it requires the count of days starting with the previous March 1, rather than with January 1. Correcting the day number in that way requires knowing whether the year is a leap year or not, which makes things just a tad more complicated.
Anyway, here it is. [Note 1]
# Compute the month number (1-12) corresponding to a day in
# in "Julian" format YYYYJJJ (001 == Jan. 1; 365/6 == Dec. 31)
# Yes, I know it's full of magic numbers.
month_for_julian_day () {
local -i y=${1:0:4} j=10#${1:4:3} march1=60
if (( y % 400 == 0 )) || ! (( y % 100 == 0 )) && (( y % 4 == 0)); then
march1=61
fi
if (( j >= march1 )); then j=j-march1; else j=j+305; fi
local -i m=(j*5+461)/153
if (( m > 12 )); then m=m-12; fi
echo $m
}
To make use of this, it would be handy to have
# Advance a Julian date to the next day.
# Note: this one doesn't wrap around. Because of the way it's used
# here, that doesn't matter.
julian_next() { printf "%s%03d" ${1:0:4} $((10#${1:4:3})); }
Then you can define
# Succeeds if the Julian date is the last day of a month
is_last_day_of_month() {
(( $(month_for_julian_day $j) != $(month_for_julian_day $(next_julian $j)) ))
}
Notes:
After I pasted this, I noticed that the syntax highlighter being used here thinks that # starts a comment. That's not true. # only starts a comment if it's the first character in a word. In this case, the assignment j=10#${1:4:3} forces the last three digits of the supplied date to be interpreted in base 10.
I use the type xsd:dateTime for a long time und faced today a node of this type with this value
-292269055-12-02T17:47:04.192+01:00
, which I thought it was not a valid value, but when I use XML Spy 2017 to validate the value, it telle me, that it is valid.
Can someone explain me, why this value is valid or if it is a wrong implementation of XSD validation in XML Spy?
Thanks!
Dingjun
It looks strange, but it seems legal: quoting from https://www.w3.org/TR/xmlschema-2/#dateTime 3.2.7.1
The ·lexical space· of dateTime consists of finite-length sequences of characters of the form: '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?, where
'-'? yyyy is a four-or-more digit optionally negative-signed numeral that represents the year; if more than four digits, leading zeros are prohibited, and '0000' is prohibited (see the Note above (§3.2.7); also note that a plus sign is not permitted);
So yes, a year can be negative and can be longer than 4 digits.
This question already has an answer here:
take date from file in unix
(1 answer)
Closed 6 years ago.
I want to take two dates as argument from the user ( ) with
$./tool.sh --born-since <dateA> --born-until <dateB>
and from a file print the lines that are between those two dates.For example:
933|Mahinda|Perera|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Carmen|Lepland|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Hồ ChÃ|Do|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
So , i use awk command like this :
awk -F'|' '{print $4} [ file ... ]
to take the dates .. how can i use awk to make the dates from the txt to seconds form ?
if the date variables are in the same format, you can convert everything to numbers and use comparison.
awk -F'|' -v from=$dateA -v to=$dateB '{gsub("-","",$5);
gsub("-","",from); gsub("-","",to)}
from <= $5 && $5 <= to' file
Note, it's the fifth field in your file.
You can either call the /bin/date +"%s" --date="DATESTRING" through system() if the DATESTRING matches a format "/bin/date" understands, or you use the internal mktime() function. But then you need to split your date according to awk(1):
mktime(datespec)
Turn datespec into a time stamp of the same form as returned by systime(), and return the result. The datespec is a string of
the form YYYY MM DD HH MM SS[ DST]. The contents of the string are six or seven numbers representing respectively the full year
including century, the month from 1 to 12, the day of the month from 1 to 31, the hour of the day from 0 to 23, the minute from 0
to 59, the second from 0 to 60, and an optional daylight saving flag. The values of these numbers need not be within the ranges
specified; for example, an hour of -1 means 1 hour before midnight. The origin-zero Gregorian calendar is assumed, with year 0
preceding year 1 and year -1 preceding year 0. The time is assumed to be in the local timezone. If the daylight saving flag is
positive, the time is assumed to be daylight saving time; if zero, the time is assumed to be standard time; and if negative (the
default), mktime() attempts to determine whether daylight saving time is in effect for the specified time. If datespec does not
contain enough elements or if the resulting time is out of range, mktime() returns -1.
So you need to prepare your date fields to use the form given in the documentation.
split($5, D, "-");
DS = sprintf("%4d %2d %2d 00 00 00", D[1], D[2], D[3]);
T = mktime(DS);
should do the job.
I have a bunch of strings with opening hours in this format:
Mon-Fri: AM7:00-PM8:00\nSat-Sun: AM8:00-PM6:00
I can deal with the "AM" part by just removing it, but I'd like to convert the PM by
Removing "PM"
Adding 12 to the number before the ":"
Taking care of the fact that PM is sometimes double-digits (e.g. PM11:00)
There can be zero or more PM times in the string.
I'm not sure how to manipulate the time as a number. I've gotten this far:
opening_hours.sub! /PM([\d]?[\d]):/, "***\1***"
Which outputs things like this:
AM7:15-***\u0001***00
The '\u0001` may be due to Japanese characters in the string.
You can take advantage of the fact that String#gsub accepts a block. Something like this will do for you?
s = "Mon-Fri: AM7:00-PM8:00\nSat-Sun: AM8:00-PM11:00"
s2 = s.gsub('AM', '').gsub(/PM(\d+)/) do |match|
(match.gsub('PM', '').to_i + 12).to_s
end
s2 # => "Mon-Fri: 7:00-20:00\nSat-Sun: 8:00-23:00"
Have a look at this question, ruby has a class called datatime.
Convert 12 hr time to 24 hr format in Ruby
I have a string:
A -DDD HH:MM:SS
and currently trying to write a function that will take in this string and also the format to convert it to. For example, say I wanted to display just the HH:MM ss (Hours with leading Zeros + colon + Minutes with leading Zeros + no colons + seconds without leading Zeros.
I understand for VB6 you'd probably use a string function like Mid(str, int, int) to get the time portion. But if I create a custom format of
HH:MM ss
How would you approach formating this?
J
Chop off the strictly formatted time part and use the format function;
s = "A -??? 12:34:56"
t = right$(s,8)
?format$(t, "HH:NN ss")
12:34 56
?format$(t, "HH:NN ss AM/PM")
12:34 56 PM
?format$(t, "H, N, S AM/PM")
12, 34, 56 PM
(N is minute here)