LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   date command is not accepting (https://www.linuxquestions.org/questions/linux-general-1/date-command-is-not-accepting-593371/)

monu 10-20-2007 07:47 PM

date command is not accepting
 
Hi,

date command is not accepting year 0001 for example: date -d "00010101" +%Y%m%A

error invalid date..

but when i use cal command it doesn't show me any errors. I want to use date command not cal command.is there any possible to use date command?

thanks

reverse 10-21-2007 01:26 AM

Code:

      %A    locale’s full weekday name (e.g., Sunday)

PAix 10-21-2007 07:18 PM

The Unix/Linux date command will accept no date before 01 Jan 1970 or after 2038.
So the date command is not appropriate for your purpose. There are other ways - i'm sure that someone will advise you appropriately.
put in a file called myfile 19700101 and run this code
Code:

echo $(date -f myfile)
Change the date to before 1970 to check what I say. then try after 2038 (not sure what month day)
Good luck.

I think Reverse's previous comment was pointing out that you were probably looking for a format +%Y%m%d YYYYMMDD, but instead of the day you put %A which is the weekday. I only explain that because it took me more than a few moments. Sorry Reverse, I'm a bit slow tonight!

archtoad6 10-24-2007 01:58 PM

While +%Y%m%A will work, +"%Y %m %d %A" or +'%Y %m %d %A' is better practice -- it allows putting spaces in your format string:
Code:

$ date -d "19700101" +'%Y %m %d %A'
1970 01 01 Thursday


PAix 10-25-2007 08:38 AM

A nicely spaced date format is not to be sniffed at and is exceedingly useful in human terms, but often a compacted YYYYMMDD is a very convenient sortable date for use in logs to simplify further searching, sorting and processing; a personal preference and possibly the reason for Monu's original choice of format. Notwithstanding that, a very useful pointer Archtoad6 that introduces a bit of readability.
I think Monu's greater problem is in using a date before 01 January 1970. I believe that there is a :) Perl Date-Calc module :) (unsure of the capitalisation) that will handle the significantly greater range which is of interest. No reason why that should get in the way of useful information though.

Thanks PAix

matthewg42 10-25-2007 09:03 AM

YYYYMMDD format is the best. Sort a list of these dates in dictionary order, and you get chronological sort order for FREE! :)

The command line date command only works for dates between ~1902 and ~2038. If you want to do a lot of date manipulation, you're better off using a proper date processing library and using a more sophisticated language than shellscript. As PAix mentioned, the Date::Calc module with Perl is very usable. IIRC it handles dates in the conventional western calendar back to 1 A.D.

jschiwal 10-25-2007 09:18 AM

Remember that different countries changed their calenders at different times in history. The US didn't adopt the Gregorian calendar until 1752, Alaska in 1867. Greece did in 1923.

colucix 10-25-2007 09:20 AM

Indeed, the range of dates we can span with the date command depends on the machine architecture, 32 or 64-bit. On a 32-bit machine the maximum representable signed integer is (2^32 -1)/2 that is plus or minus 2147483648. Since the date command uses seconds since epoch to do calculations and the epoch is 01-Jan-1970 01:00, the range of dates on a 32-bit machine is
Code:

> date -d "13-Dec-1901 21:45:52" +%s
-2147483648
>
> date -d date -d "19-Jan-2038 04:14:07" +%s
2147483647

Instead, on a 64-bit machine you can easily span from year 0001 to 9999 since the integer representation is beyond these limits.


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