Comments on Linux Date Command Tutorial for Beginners (8 Examples)
While working on the Linux command line, you might find yourself in situations where-in you need to display (or even change) the current system time. Not only that, if you work in a team with members in different timezones, you may want to keep yourself updated with time-related information for zones in which other members are sitting. If you're looking for a tool that lets you do all this (and much more), you will be glad to know there exists a command - dubbed date - that does all this.
3 Comment(s)
Comments
When I need to make a file with a YYYY-MM-DD portion in it:
date "+%F"
Makes sorting trivial.
Here a small bash function that I wrote to display the time in different timezones It makes use of the date command.
Simply drop it in your .bashrc
Don't know how to make a proper code section in that comment so formating will probably be crap. Sorry
# tz ## Display the specified time in multiple timezones (default is "now").## Use tzselect to find the proper timezone names (see ZONES below). ## Timezones are sorted by their UTC offsets.## Timezones with the same UTC offset than the current timezone# are marked with '*'## However, they will be marked with '?' instead if the UTC offset # of the current time (now) and of the specified time are not equal # This usually indicates a summer/winter time change. ## Examples: (see 'info date' for more)## tz : for current time (or "now") # tz 14:00 : for 14:00 today in the current timezone# tz 14:00+4 : for 14:00 today in UTC+4 # tz tomorrow 2pm PST : for 14:00 tomorrow in Pacific Standard Time # tz 2pm July 3 CEST : for Friday July 3 14:00 2015 in Central European Summer Time ## Remark: Not all symbolic timezone names work as expected.# First because some of them are only valid during parts of the year (e.g CET# vs CEST in Central Europe) and also because some names are ambiguous.# For instance, CST may refer to Central Standard Time in the US and # to China Standard Time in China. # Using UTC or GMT relative timezones is usually safer.## http://www.timeanddate.com/time/map/#tz () { local A D T tz loc M UTC0 UTC1 local -A LOCATIONS D="$*" [ -n "$D" ] || D=now T=$(date -d "$D" +%s) || return # Add here your preferred locations and their timezone. # See in tzselect or in /usr/share/zoneinfo/ for the values LOCATIONS["Californie"]="US/Pacific" # PST/PDT LOCATIONS["Texas"]="US/Central" # PST/PDT LOCATIONS["New York"]="US/Eastern" # EST/EDT LOCATIONS["UK"]="Europe/London" # GMT/BST LOCATIONS["France"]="Europe/Paris" # CET/CEST LOCATIONS["Moscou"]="Europe/Moscow" # MSK LOCATIONS["Inde"]="Asia/Calcutta" # IST (India Standard Time ) LOCATIONS["Japan"]="Asia/Tokio" # JST # Get current timezone now (in $UTC0) and at requested time (in $UTC1) # They may be different because of summer/winter time changes. UTC0=$(LC_ALL=C date -d "now" +"%z") UTC1=$(LC_ALL=C date -d "@$T" +"%z") for loc in "${!LOCATIONS[@]}" ; do tz=${LOCATIONS[$loc]} # echo "@ $loc tz" # UTC2 is the UTC timezone of $tz at the given time UTC2=$(LC_ALL=C TZ="$tz" date -d "@$T" +"%z") if [ "$UTC1" == "$UTC2" ] ; then if [ "$UTC0" == "$UTC1" ] ; then M="*" # This is consistent with current timezone. else M="?" # Also but probably with a summer/winter time change. fi else M=" " fi A=$(LC_ALL=C TZ="$tz" date -d "@$T" +"%a %b %d %H:%M (UTC%:z) %4Z $tz") # Prefix each output line with its numeric timezone. # That prefix will we removed after sorting. printf "%s %c%-17s %s\n" "$UTC2" "$M" "$loc" "$A" done | sort -n | cut -c 7-}
What a mess :-)
For those that are interested, I dropped the file in http://www.chauveau-central.net/pub/tz.bash
This is not a shell script. You will need to source it or to copy it into your .bashrc file before you can use the tz function.