Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to convert a vague time specfier into a concrete timestamp

by perlsherpa (Novice)
on Sep 20, 2022 at 19:46 UTC ( [id://11147031]=note: print w/replies, xml ) Need Help??


in reply to How to convert a vague time specfier into a concrete timestamp

Here's a good start using static words that do not require you to treat them as a DSL (domain specific language).
# referred to as 'words2date.pl' below use strict; use warnings; use POSIX qw/strftime/; my $datewords = join q{ }, @ARGV; # Take "vague" words and convert them into a # date/time stamp, e.g.,: "now", "today", "5pm Friday" sub format_epoch { my $epoch = shift; # example, Tuesday, December 12, 1995 return strftime(qq{%A, %B %d, %Y},localtime($epoch)); } my $static_phrases = { now => sub { print format_epoch(time) }, today => sub { print format_epoch(time) }, tomorrow => sub { print format_epoch(time + 86400) }, yesterday => sub { print format_epoch(time - 86400) }, }; if ( defined $static_phrases->{$datewords} ) { $static_phrases->{$datewords}->(); } else { warn qq{[WARNING] I do not understand what you mean by, "$datewords" +\n}; }
Example,
shell> perl words2date.pl now
Tuesday, September 20, 2022

shell> perl words2date.pl today
Tuesday, September 20, 2022

shell> perl words2date.pl tomorrow
Wednesday, September 21, 2022

shell> perl words2date.pl yesterday
Monday, September 19, 2022    
The challenge is defining and implementing DSL, then translating that to the "date math" that will need.
For example, SQL uses something very primative and structured when what you want is not NOW(), e.g.,
SELECT DATE_ADD('2008-01-02', INTERVAL 31 DAY)
So my suggestion is to start with static word or phrases, then focus specifically on the challenge to develop a
DSL (or implement a well established one) that can then be parsed and then used to get the numbers you need to
perform the date math. Hope that helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147031]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-25 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found