Tag Info

Hot answers tagged

76

This is a matter of portability. In early Unices, some versions of cron accepted 0 as Sunday, and some accepted 7 as Sunday -- this format is an attempt to be portable with both. From man 5 crontab in vixie-cron (emphasis my own): When specifying day of week, both day 0 and day 7 will be considered Sunday. BSD and AT&T seem to disagree about ...


61

On *BSD: date -r 1234567890 On Linux (specifically, with GNU coreutils ≥5.3): date -d @1234567890 With older versions of GNU date, you can calculate the relative difference to the UTC epoch: date -d '1970-01-01 UTC + 1234567890 s' If you need portability, you're out of luck. The only time you can format with a POSIX shell command (without doing the ...


43

You should be able to set a timezone for the duration of the query, thusly: TZ=America/New_York date Note the whitespace between the TZ setting and the date command. This sets the TZ variable only for the command line.


38

This depends on exactly what you mean by "opened", but in general, yes. There are three timestamps normally recorded: mtime — updated when the file contents change. This is the "default" file time in most cases. ctime — updated when the file or its metadata (owner, permissions) change atime — updated when the file is read So, generally, what you want to ...


34

The "n weeks after a date" is easy with GNU date(1): $ date -d 'now + 3 weeks' Tue Dec 6 23:58:04 EST 2011 $ date -d 'Aug 4 + 3 weeks' Thu Aug 25 00:00:00 EST 2011 $ date -d 'Jan 1 1982 + 11 weeks' Fri Mar 19 00:00:00 EST 1982 I don't know of a simple way to calculate the difference between two dates, but you can wrap a little logic around date(1) with a ...


30

Take a look at this blog post titled: How To: 2 Methods To Change TimeZone in Linux. Red Hat distros If you're using a distribution such as Red Hat then your approach of copying the file would be mostly acceptable. $ ls /usr/share/zoneinfo/ Africa/ CET Etc/ Hongkong Kwajalein Pacific/ ROK zone.tab America/ ...


27

You can just use the -d switch and provide a date to be calculated date Sun Sep 23 08:19:56 BST 2012 NEW_expration_DATE=$(date -d "+10 days") echo $NEW_expration_DATE Wed Oct 3 08:12:33 BST 2012 -d, --date=STRING display time described by STRING, not ‘now’ This is quite a powerful tool as you can do things like date -d "Sun Sep 11 ...


27

Sometimes you don't even need the source code. Use strace. $ strace touch -t 201212121212 foobar execve("/usr/bin/touch", ["touch", "-t", "201212121212", "foobar"], [/* 61 vars */]) = 0 [...] lots of noise [...] open("foobar", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 dup2(3, 0) = 0 close(3) ...


25

In addition to @ChrisDown's excellent answer, this might also be a nifty solution to a localisation issue: "According to the Hebrew calendars and traditional Christian calendars, Sunday is the first day of the week." Having it both ways, which is really easy programmatically, makes it easy to use for either group.


22

touch calls the utimes system call to set the file's modification time and its access time. On some systems, instead of utimes, it opens the file and then sets the file times through the descriptor, e.g. with utimensat under Linux. You can see how touch works on your system by looking at the system calls it makes. Under Linux, use strace, e.g. strace touch ...


22

With GNU, FreeBSD or OS/X date (or date implementations that use the system's libc's strftime() where that is the GNU libc), adding hyphen - after % prevents numeric fields from being padded with zeroes: $ date +'%Y%-m%d' 2015120 From man date on a GNU system: By default, date pads numeric fields with zeroes. The following optional flags may ...


21

You can use man ls and here you can find --time-style parameter. Or you can use: ls --full-time.


19

Timezones are listed in /usr/share/zoneinfo. If you wanted the current time in Singapore, for example, you could pass that to date: TZ=Asia/Singapore date Sun Jun 14 17:17:49 SGT 2015 To simplify this procedure, if you need to frequently establish the local time in different timezones, you could add a couple of functions to your shell rc file (eg, ...


17

My shortest method uses zsh: print -rl **/*(.Om) If you have GNU find, make it print the file modification times and sort by that. I assume there are no newlines in file names. find . -type f -printf '%T@ %p\n' | sort -k 1 -n | sed 's/^[^ ]* //' If you have Perl (again, assuming no newlines in file names): find . -type f -print | perl -l -ne ' ...


16

I have encountered this problem in an embedded Linux system that needed to handle dates past 2038 in some long-term cryptographic certificates, so I'd say the likehood of this depends on your application domain. While most systems should be ready well before 2038, if you find yourself today calculating dates far into the future, you may have a problem.


15

date -d @1190000000 Replace 1190000000 with your epoch


15

You want the find tool. find folder -depth -type f -atime +7 -delete (This will delete all files (only regular ones, no pipes, special devices, directories, symbolic links) in the given folder and all subdirectories (recursively) where the last access time is longer than 7 days ago.)


15

One solution is: find -type f -mtime 90 That finds files that was last modified exactly 90 days ago. find -type f -mtime -90 finds files that were modified in last 90 days.


14

In your case, you can simply disable zero padding by append - after % in the format string of date: %-H By default, date pads numeric fields with zeroes. The following optional flags may follow '%': - (hyphen) do not pad the field _ (underscore) pad with spaces 0 (zero) pad with zeros ^ use upper case if possible # use opposite case if ...


13

Good question. The documentation says it should be allowed. info date 'Date input formats' 'Calendar date items' For numeric months, the ISO 8601 format `YEAR-MONTH-DAY' is allowed, where YEAR is any positive number, ... A leading zero must be present if a number is less than ten. If YEAR is 68 or smaller, then 2000 is added to it; ...


13

This isn't working because the command date returns a string with spaces in it. $ date Wed Oct 16 19:20:51 EDT 2013 If you truly want filenames like that you'll need to wrap that string in quotes. $ touch "foo.backup.$(date)" $ ll foo* -rw-rw-r-- 1 saml saml 0 Oct 16 19:22 foo.backup.Wed Oct 16 19:22:29 EDT 2013 You're probably thinking of a different ...


13

Using the libfaketime software could be a solution sudo apt-get install faketime faketime '2006-09-20' wine Example.exe


12

I think this is going to be a significant problem, much more pernicious than the Y2K issues of 1999/2000 because the affected code is generally lower-level (it's CTIME) and so it's harder to spot places where time is being stored that way. To complicate matters further, the fact that Y2K was perceived to be a damp squib will make it harder to draw attention ...


12

I was going to suggest hacking e2fsck to disable the specific checks for a last mount time or last write times in the future. These are defined in problem.c / problem.h, and used in super.c. But in looking, I discovered that E2fsprogs 1.41.10 adds a new option to /etc/e2fsck.conf called broken_system_clock. This seems to be exactly what you need, and since ...


12

A python example for calculating the number of days I've walked the planet: $ python >>> from datetime import date as D >>> print (D.today() - D(1980, 6, 14)).days 11476


12

As per the GNU date manpage: By default, date pads numeric fields with zeroes. The following optional flags may follow '%': - (hyphen) do not pad the field Therefore you can do alias date = date +"%Y.%-m.%-d.%-H.%-M.%-S" and receive 2013.6.14.3.19.31


12

Try running this code snippet: if [[ 5 < 20 ]] then echo "5 < 20, as expected" else echo "5 is not less than 20, but why?" fi And the output would be 5 is not less than 20, but why?. The answer is that you're using the < conditional expression operator, which is documented as doing: string1 < string2 True if ...


11

You can get the current unix timestamp with date "+%s", find the current quarter-hour with some simple math (my example is in bash) and print it back with a better format with date : curdate=`date "+%s"` curquarter=$(($curdate - ($curdate % (15 * 60)))) date -d"@$curquarter" The @ syntax to set the current date and time from a timestamp is a GNU extention ...


11

You could use some evil trickery: date -d "Feb 12 10:02:10" +%s will return the number of seconds since the epoch for that date. Combining this with the same command (for the other date) and running through bc: echo "`date -d 'Feb 12 10:53:15' +%s` - `date -d 'Feb 12 10:02:10' +%s`" | bc Will give you 3065 the number of seconds between the times. ...


11

You can do this by manipulating the TZ environment variable. The following will give you the local time for US/Eastern, which will also be smart enough to handle DST when that rolls around: # all on one line TZ=":US/Eastern" date +%Y%m%d The zone name comes from the files and directories inside /usr/share/zoneinfo.



Only top voted, non community-wiki answers of a minimum length are eligible