Hello.
I can use any particular (stupid or not) format when using bash date command.
Example :
or
or
But I can't verify if the input date is in a particular format.
I use this piece of code to make verification : Example 1 : standard format and good date time ( Relative to date format template )
Good result for the test
Example 2 : standard format and and bad date time ( Relative to date format template )
Good result for the test.
But this is a coincidence.
In any data tested, the test will fail because the time format 'HH-MM-SS' is refused by the date command.
So the test will always failed.
Example 3 : personnal format and bad date time ( Relative to date format template )
Good result for the test
The date is accepted by the date command.
But the test fail because the time is not in the template format 'yyyy-mm-dd HH-MM-SS'.
Example 4 : personnal format and good date time ( Relative to date format template )
Bad result for the test.
The test will always failed because the time format 'HH-MM-SS' is refused by the date command.
So the test failed.
Is it possible to test the relative to the format used if the format used is not standard.
Any help is welcome
The test will always failed because the time format 'HH-MM-SS' is refused by the date command.
So the test failed.
Is it possible to test the relative to the format used if the format used is not standard.
Any help is welcome
There are several different aspects to your question, so bear with me. Perhaps i come from a different background of scripting as i mostly write scripts which have to run on a multitude of different UNIX systems and Linux systems too. Portability is of the utmost importance for me.
GNU is not a given
I often see the -d/--date option of the date command used. Note that standard-conforming date-commands do NOT have that, so you are limiting your scripts to systems with GNU-date (and, IIRC, FreeBSD-date) installed. If this is OK for you, then so be it, but you should be aware that it is a design decision. If you want to avoid this dependency you might want to look at perderabos datecalc script, here is an example of how to use it, along with a C program you may find useful too.
internal and external date representation
The way UNIX/Linux systems represent the date information is like this: there is an "external" representation, which is what you are trying to work with. But this is only a representation of what is used internally to measure time and this is the "UNIX time" or "epoch". It is a 32-bit-unsigned integer counting the seconds since 0:00, Jan 1st, 1970. Notice that it will overflow in somewhere in 2038.
If you are planning to work with dates and do arithmetic with dates my sugestion is to do it like perderabo: create a layer of scripts to convert everything to/from a common representation (ideally this should be the epoch time because it lends itself well to numeric manipulation/calculation) and then calculate with the resulting integers.
interpreting arbitrary formats
First off: arbitrary formats are exactly that: arbitrary. Their (correct) interpretation is somewhat of a guessing game and every rule you can come up with can be circumvented (or "made not to work") by some outlandish format. I have once tried to create a "time format canonifier" you might want to use as a starting point for your own development. It is not exactly what you want but maybe you can get some ideas from it:
I hope this helps.
bakunin
These 3 Users Gave Thanks to bakunin For This Post:
Where I ran into real world problems is with free form entry on date (as text) fields. So. The mix provided by the OP was not realistic based on my experience.
I had to correct several hundred million rows of almost unbelievable garbage dates in a transaction table. The data was entered by users in several countries.
Example: "Apr 4". Okay. What year? I had to use the table's unique sequence and search nearby to find temporally close rows until I found a year. So for each
bizarre date problem we ran a separate script with a subsequent validation script. Took several weeks to fix the mess.
Overall the problems got better run times as more neighbors were "fixed" - in this one kind of example problem.
This User Gave Thanks to jim mcnamara For This Post:
I think the way to tackle this problem is to start by converting the format string to a RE with named groups e.g %m-%d-%y would become (?<d>\d\d)-(?<m>\d\d)-(?<Y>\d{4})
There are still locale issues as %a would become (?<a>Mon|Tue|Wed|Thu|Fri|Sat|Sun) in English locals but (?<a>Lun|Mar|Mer|Jeu|Ven|Sam|Dim) in French locals.
Once this is done if a string matches the RE you can then pull out the named group values and verify they go together correctly e.g that a=Tue is correct for d=4 m=6 Y=2019.
How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid.
echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }'
ex:
./script.ksh 12:34:21 = okay
./script.ksh... (10 Replies)
Hi, I am extracting a date string from the source file like this :
06/05/2014 16:04:00
I want to change it to 05-JUN-14 04.05.00.000000000 PM
I basically store the date in a variable. I got solutions to change date in dd-mmm-yyyy format using tr but I guess it works only with the "date"... (8 Replies)
Hello,
I have date format as shown in red color below
Fri, Sep 12, 2012 08:38:05 PM
Can anyone help me with command to change this format to yyyy-mm-dd and HH:MM:SS
Note: Time should be in 24 hours format
I really appreciate your help!!
Thanks,
Elavarasan (4 Replies)
Hello All,
I have a requirement to convert a 12 hour format to 24 hour time format and the sample input /out put is below
Input Time format : Nov 2 2011 12:16AM
Out Put Format : Nov 2 2011 0:16
Input : Nov 2 2011 4:16PM
Out Put: Nov 2 2011 16:16
I have done this using a... (6 Replies)
Hello,
I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Frens,
I have a logfile which has got manier things in it and somewer in that file i have a date in mm/dd/yyyy hh:mm format....
I just need to get the date and time nothing else....
i have searched in this forum for it but got only date in mm-dd-yyyy format..
but i need to search for... (10 Replies)
I want to append current date and time to a file name like filename_090920091210. If I use filename_`date +%d%m%Y%T`, the ouput is filename_0909200912:10:33.
How to format this as filename_090920091210.
Please let me know.
Thanks in advance. (3 Replies)
I have a file named "suspected" with series of line like these :
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent... (3 Replies)
1) i am trying to print the timeframe of a log file using the start/end date/time being extracted from the file itself. my problem is how to convert the following numeric date format to the one that i prefer:
Start time: 0204161129
End time : 0204171431
into:
Start time: Apr 16 2002... (6 Replies)