Search code examples
perlcalendarjulian-date

how to convert from a calendar date to an ordinal date with out any modules


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.


Solution

  • 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);