Hot answers tagged

99

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 seconds' If you need portability, you're out of luck. The only time you can format with a POSIX shell command (without ...


92

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 ...


63

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/ ...


59

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 ...


59

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.


47

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 ...


41

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 ...


37

Use date -s: date -s '2014-12-25 12:34:56' Run that as root or under sudo. Changing only one of the year/month/day is more of a challenge and will involve repeating bits of the current date. There are also GUI date tools built in to the major desktop environments, usually accessed through the clock. To change only part of the time, you can use command ...


28

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


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) ...


26

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.


25

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 ' ...


25

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 ...


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

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.


21

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, ...


20

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 ...


19

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.


19

More than likely it is your use of set. That will assign 'today', '=' and the output of the date program to positional parameters (aka command-line arguments). You want to just use C shell (which you are tagging this as "bash", so likely not), you will want to use: today=`date +%Y-%m-%d.%H:%M:%S` # or whatever pattern you desire Notice the lack of ...


19

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


18

date -d @1190000000 Replace 1190000000 with your epoch


17

System time You can use date to set the system date. The GNU implementation of date (as found on most non-embedded Linux-based systems) accepts many different formats to set the time, here a few examples: set only the year: date -s 'next year' date -s 'last year' set only the month: date -s 'last month' date -s 'next month' set only the day: date -s ...


16

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


16

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.


16

The reason is that TZ=UTC-8 is interpreted as a POSIX time zone. In the POSIX timezone format, the 3 letters are the timezone abbreviation (which is arbitrary) and the number is the number of hours the timezone is behind UTC. So UTC-8 means a timezone abbreviated "UTC" that is −8 hours behind the real UTC, or UTC + 8 hours. (It works that way ...


15

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 ...


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

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


14

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 ...



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