0

I need to search for a time range in a log file. The time stamps are in JDE Julian date format and I cannot use modules.

The date format is YYYYJJJHHMMSS where JJJ is the days in Julian.

I need to convert the user input in JDE with out using a module.

Community
  • 1
  • 1

2 Answers2

1

The fact that these number are date/time values makes no difference. They are integers (albeit rather large ones) and they can be compared in exactly the same way as any other integer.

while (<$your_input_filehandle>) {
  # I have no idea of the format of your input data, so I can't
  # begin to implement extract_timestamp()
  my $this_records_timestamp = extract_timestamp($_);

  if ($this_records_timestamp >= $min_timestamp and
      $this_records_timestamp <= $max_timestamp) {
    # timestamp is within the given range
  }
}

Update: To convert YYYYMMDD to YYYYJJ

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Time::Piece;

my $in_format  = '%Y%m%d';
my $out_format = '%Y%j';

my $in_date = shift
  || die "Please pass date in format YYYYMMDD\n";

my $date = Time::Piece->strptime($in_date, $in_format);

say $date->strftime($out_format);
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Thanks for this Dave Cross, i dont have a problem doing the comparison or search. i just need guidence or help converting from YYYYMMDD to YYYYJJJ. example: Input : 20150101 convert to : 2015001 – Manuel de Oca Jul 21 '15 at 16:12
  • thanks again, but i dont have access to any modules, just need to convert it doing simple math. – Manuel de Oca Jul 21 '15 at 16:59
  • 2
    Time:: Piece is a core module. You'll have access to that. – Dave Cross Jul 21 '15 at 17:20
  • 1
    @ManueldeOca "20150101 convert to : 2015001" That's not the J.D. Edwards Julian date format. JDE format for January 1, 2015 would be `1000 * (2015 - 1900) + 1`, or `115001`. Please edit your question to clarify, since you said in a comment that you were using JDE format. – ThisSuitIsBlackNot Jul 21 '15 at 17:39
-1

You should have access at least to core modules as POSIX:

perl -MPOSIX=mktime -pe'@F=/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;$j=sprintf"%03d",1+(localtime(mktime(@F[5,4,3,2],$F[1]-1,$F[0]-1900)))[7];s/.{4}\K.{4}/$j/;'

or with Time::Piece

perl -MTime::Piece -ne'print Time::Piece->strptime($_,"%Y%m%d%H%M%S\n")->strftime("%Y%j%H%M%S\n")'
Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73