473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem in checking time format

34
Hi all

I have a time in this format
12:00p,1:20A .

In linux i am using following regexp to check it.
egrep '[0-9]|[0-2]:(0[0-5]|1[0-9])[paPA]' $time.
I am trying to achive this in perl.

Please help me regarding this Issue....

Regards
Naveen
May 30 '07 #1
8 2235
prn
254 Expert 100+
Hi Naveen,

Just what are you trying to accomplish? What do you mean "check"? Are you trying to determine whether some string that purports to be a time is a potentially valid time? How did these strings get there? Why do you need to check their format? What kind of "answer" are you looking for? Are you actually trying to convert these strings to useful times or are you just trying to ensure a consistent format? What do you want to do to the ones that don't "check"?

As usual, there's more than one way to do it. Whatever "it" may be. But unless we have a somewhat better idea of what "it" is, I, at least, have no clue about what would help you.

Best Regards,
Paul
May 30 '07 #2
pnsreee
34
Hi Paul,
I am checking the string contain the valid time format or not as given above.
These strings are inputs to my program . If these string is in valid formate I go further like conversions to GMT,EST,CST.

Regards
Naveen.P
May 30 '07 #3
prn
254 Expert 100+
Hi Naveen,

So it looks like you are just asking how to tell if a string matches a regular expression. Right?
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3. my $input = '12:45A';
  4. my $timepattern = /[0-9]|[0-2]:(0[0-5]|1[0-9])[paPA]/;
  5.  
  6. if ( $input =~ $timepattern ) {
  7.   print "$input looks like a time.\n";
  8. }
  9. else {
  10.   print "$input does not seem to be a time.\n";
  11. }
On the other hand, if what you really want is a good way to handle strings that (may or may not) contain times in all sorts of formats, then there are good ways to do that. For example, Parsing Dates and Times from Strings is one and from a reasonably well-known source. Another possibilty for a versatile time parser is HTTP::Date, which also looks pretty versatile.

You will often find that it's better to use (or at least study) well-written packages rather than trying to roll your own. Too often people try to do things "the easy way" and fail to take all the cases into account. (I have to admit that I have often been guilty myself.) In fact, using good packages is usually even easier and generally gives you much better coverage for the unexpected cases.

Best Regards,
Paul
May 30 '07 #4
KevinADC
4,059 Expert 2GB
Paul,

You seem to be one of the "good-guys" so I wish to bring this to your attention. The link you posted: "Parsing Dates and Times from Strings" was a link to pirated material. The material seems to have been deleted but that site posts lots of pirated material.
May 30 '07 #5
prn
254 Expert 100+
Paul,

You seem to be one of the "good-guys" so I wish to bring this to your attention. The link you posted: "Parsing Dates and Times from Strings" was a link to pirated material. The material seems to have been deleted but that site posts lots of pirated material.
Ouch! Thanks, Kevin! I'll remember that. Now that you mention it, the URL does look suspicious. You're right. I don't want to encourage that sort of thing.

Paul
May 31 '07 #6
miller
1,089 Expert 1GB
Also, I just wanted to point out that the regex that you were using would not actually validate the most basic of times. Here's one that does what I believe you intended:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. my $input = '12:45A';
  6.  
  7. my $timepattern = /^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
  8.  
  9. if ( $input =~ $timepattern ) {
  10.     print "$input looks like a time.\n";
  11.  
  12. } else {
  13.     print "$input does not seem to be a time.\n";
  14. }
  15.  
- Miller
May 31 '07 #7
KevinADC
4,059 Expert 2GB
I think you need to use "qr" to properly quote the regular expression otherwise it will not work.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3. my $input = '12:45A';
  4. my $timepattern = qr/^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
  5. if ( $input =~ $timepattern ) {
  6.    print "$input looks like a time.\n";
  7. } else {
  8.    print "$input does not seem to be a time.\n";
  9. }
example:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3. my $input = '999991:45A';
  4. my $timepattern = /^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
  5. if ( $input =~ $timepattern ) {
  6.    print "$input looks like a time.\n";
  7. } else {
  8.    print "$input does not seem to be a time.\n";
  9. }

output:

999991:45A looks like a time


Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3. my $input = '999991:45A';
  4. my $timepattern = qr/^(?:[1-9]|1[012]):[0-5][0-9][paPA]$/;
  5. if ( $input =~ $timepattern ) {
  6.    print "$input looks like a time.\n";
  7. } else {
  8.    print "$input does not seem to be a time.\n";
  9. }
output:

999991:45A does not seem to be a time.
Jun 1 '07 #8
miller
1,089 Expert 1GB
I think you need to use "qr" to properly quote the regular expression otherwise it will not work.
Nod. I was just testing you.... You passed! :)

- M
Jun 1 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dave Harrison | last post by:
Ok before I start, please dont mail me telling me to use a more recent version of Python, I _would_ use 2.2.x but due to an existing framework all based on using 2.1.1 I have been specifically told...
1
by: Lentdave67t | last post by:
Thank you in advance for any help you can provide. I am writing a C# program that checks to see if the URLs of favorites/bookmarks are still good. The problem I am having is that while the...
2
by: Lentdave67t | last post by:
Thank you in advance for any help you can provide. I am writing a C# program that checks to see if the URLs of favorites/bookmarks are still good. The problem I am having is that while the...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
1
by: Mustufa Baig | last post by:
I have an ASP.NET website where I am showing off crystal reports to users by exporting them to pdf format. Following is the code: ---------------- 1 Private Sub ExportReport() 2 Dim oStream...
26
by: Tom Becker | last post by:
Is there a way, from Access, to programmatically click the Send and Receive button in Outlook?
35
by: strictly_mk | last post by:
Hi all, forgive me if there is a simple solution for this. I am going through the following piece of code which simply calculates factorials out of a book, but when i run it I get the answer 0 for...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
1
by: halcyon943 | last post by:
have 4 folders that I watch and need to move files from to another location. Three constraints: -Finish time. Make sure the program stops transferring files at a specific time -Number of...
49
by: Bert | last post by:
Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1 Why doesn't this work? /* ID: albert.4 LANG: C TASK: gift1 */
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.