LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2017, 12:35 PM   #1
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,318

Rep: Reputation: 126Reputation: 126
perl get time (hours and minutes) from date string?


I don't do a lot of perl programming, so when I have to do something non-trivial, such as date/time processing, I'm completely lost.

What I need to do is get the time in hour and minute from a date string that looks like this: "20170801112217". So, this is the datetime 2017, 08, 01, 11:22:17. I have already parsed the input data into perl variables as follows below. So, I have a datetime string called $datetime. What do I do with that to get the time from that? What I really want is just the two variables: $hour and $minute.

Code:
while (<>) 
{
   chop;        #removing ending character, i.e. carriage return
   s/#015$//;   #sed command removes CR (carriage return) if found
   s/\r//;
   next unless /LOG/; #Only output LOG entries
   s/LOG\|//;    #change  "LOG| " to ""
   s/\|  /\|/;    #change  "|  " to "|"
   s/\| /\|/;     #change "| " to "|"
   s/G//;         #get rid of "G" in sawtooth


#The following tokenizes using "|" as the separator character
   my ($datetime, $valid, $mode, $fSeconds, $pSeconds, $hSeconds, $rawtic, $min, $max,
        $NumSats, $NavSats, $tDOP, $rawphase, $position, $sawtooth,
        $therm, $freqdelta, $DAC, $t1, $t2, $t3, $t4, $t5, $t6) = split(/\|/);

#data processing code starts here, eventually creating a new file from this data that will be further processed
Here are a couple of sample input lines. There may not be a correspondence to every single field in the data. Much of it I really don't care about and the variables $tn ($t1, $t2, etc) are not time fields, but temporary field names.
Code:
LOG|20170730150551|V|P|250|500|00|2548|0545|3274|12|08|0.35|055.2|-244.5|G-00.366| 92.90| 0|80005|10|000|-243.6| 000.0| 60| 00
LOG|20170730150552|V|P|250|500|00|2587|0545|3274|12|08|0.35|057.0|-245.1|G 02.132| 92.82| 0|80005|00|000|-243.8| 000.0| 60| 00
 
Old 08-01-2017, 02:45 PM   #2
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,318

Original Poster
Rep: Reputation: 126Reputation: 126
Hmm, I'm too wrapped up in the idea that this is a date time value. I can probably do this with just a substring function, can't i?
 
Old 08-01-2017, 03:00 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,330

Rep: Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473Reputation: 4473
Quote:
Originally Posted by Quakeboy02 View Post
I don't do a lot of perl programming, so when I have to do something non-trivial, such as date/time processing, I'm completely lost.
What I need to do is get the time in hour and minute from a date string that looks like this: "20170801112217". So, this is the datetime 2017, 08, 01, 11:22:17. I have already parsed the input data into perl variables as follows below. So, I have a datetime string called $datetime. What do I do with that to get the time from that? What I really want is just the two variables: $hour and $minute.
Code:
while (<>) 
{
   chop;        #removing ending character, i.e. carriage return
   s/#015$//;   #sed command removes CR (carriage return) if found
   s/\r//;
   next unless /LOG/; #Only output LOG entries
   s/LOG\|//;    #change  "LOG| " to ""
   s/\|  /\|/;    #change  "|  " to "|"
   s/\| /\|/;     #change "| " to "|"
   s/G//;         #get rid of "G" in sawtooth


#The following tokenizes using "|" as the separator character
   my ($datetime, $valid, $mode, $fSeconds, $pSeconds, $hSeconds, $rawtic, $min, $max,
        $NumSats, $NavSats, $tDOP, $rawphase, $position, $sawtooth,
        $therm, $freqdelta, $DAC, $t1, $t2, $t3, $t4, $t5, $t6) = split(/\|/);

#data processing code starts here, eventually creating a new file from this data that will be further processed
Here are a couple of sample input lines. There may not be a correspondence to every single field in the data. Much of it I really don't care about and the variables $tn ($t1, $t2, etc) are not time fields, but temporary field names.
Code:
LOG|20170730150551|V|P|250|500|00|2548|0545|3274|12|08|0.35|055.2|-244.5|G-00.366| 92.90| 0|80005|10|000|-243.6| 000.0| 60| 00
LOG|20170730150552|V|P|250|500|00|2587|0545|3274|12|08|0.35|057.0|-245.1|G 02.132| 92.82| 0|80005|00|000|-243.8| 000.0| 60| 00
If all your data is consistent in that format, and all you really want is hour and minute, that's simply done. I'd go with something simpler, if ALL you need is hour and minute:
Code:
 open (INFILE, "sample") or die "Can't open Input file!";
while (<INFILE>)
  {
  @vals = split('\|', $_);
  $Timestamp = @vals[1];
  $hour = substr $Timestamp, 8, 2; 
  $minute = substr $Timestamp, 10, 2;
  print "Raw Timestamp = $Timestamp\tHour = $hour\tMinute = $minute...\n";
}
Prints out:
Code:
Raw Timestamp = 20170730150551  Hour = 15       Minute = 05...
Raw Timestamp = 20170730150552  Hour = 15       Minute = 05...
That's only if you want to read a file with lines like you posted. You could also just replace the $Timestamp variable with what you've already got defined as $datetime, and the substr command will give you the hour/minute from that.

substr is handy....https://perldoc.perl.org/functions/substr.html

Last edited by TB0ne; 08-01-2017 at 03:02 PM.
 
3 members found this post helpful.
Old 08-01-2017, 03:05 PM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,708
Blog Entries: 3

Rep: Reputation: 1183Reputation: 1183Reputation: 1183Reputation: 1183Reputation: 1183Reputation: 1183Reputation: 1183Reputation: 1183Reputation: 1183
If you are working from the $datetime variable, it's just a matter of extracting substrings of the appropriate leng. There are many, many ways to do that. Here are two using patterns:

Code:
my ($y, $m, $d, $hour, $minute, $second) =
    ( $datetime =~ m/^
     ([0-9]{4})
     ([0-9]{2})
     ([0-9]{2})
     ([0-9]{2})
     ([0-9]{2})
     ([0-9]{2})
    $/x );

print qq($y, $m, $d, $hour, $minute, $second\n);
The x modifier allows whitespace in the pattern so it can be split up and be made easier to read. See "man perlre"

Alternately, if you want just the hour and minute:

Code:
my ($hour, $minute) =
    ( $datetime =~ m/^.{8}([0-9]{2})([0-9]{2})/ );

print qq($hour, $minute\n);
There's more than one way to do it. (tm)
 
3 members found this post helpful.
Old 08-01-2017, 04:00 PM   #5
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,318

Original Poster
Rep: Reputation: 126Reputation: 126
Thanks guys! I had originally had an mjd string there, and had cobbled some time functions together to undo the mjd and get the whole datetime. I stopped using that a year or two ago and started getting a plain old time stamp. But, I was still in the mjd mindset, so finding a solution was difficult.

Thanks again,

Bob
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
create file with Julian date and current time --- then subtract 5 minutes mfarch99 Linux - General 10 06-18-2014 09:07 AM
search a string in logs of last 15 minutes from current time. saurabhmehan Linux - Newbie 6 08-27-2010 06:33 AM
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 08:37 AM
PHP: formatting current date & time as: 2006-04-12 13:47:36 and substract 15 minutes NaCo Linux - Software 4 11-13-2008 08:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration