Get utc date linux

How can I convert a local date-time into UTC date-time?

I am trying to get a bash script working and in order to do so I need to transform a local time in +%Y%m%d%H%M%S format (example: «20150903170731») into UTC time in the same format. I know date -u can give me the current UTC time:

$ date +%Y%m%d%H%M%S -u 20150903161322 
$ date +%Y%m%d%H%M%S 20150903171322 
echo "20150903154607" | xargs date +"%Y%m%d%H%M%S" -u date: extra operand `20150903154607' 

2 Answers 2

To pass a date to use into date , use the -d option. So your command would look something like echo «20150903154607» | xargs date +»%Y%m%d%H%M%S» -u -d .

It doesn’t take exactly the date format you’re supplying, though:

$ date -d 20150903154607 date: invalid date ‘20150903154607’ $ date -d 20150903\ 15:46:07 Thu Sep 3 15:46:07 BST 2015 

So massage it a little first (GNU sed):

$ echo "20150903154607" \ | sed -re 's/^(1)(4)(9)(1)$/\1\\ \2:\3:\4/' \ | xargs date -u -d Thu Sep 3 15:46:07 UTC 2015 

To convert from local to UTC, you need to do a bit more, because -u affects the interpretation of both input and output dates:

$ echo "20150903171734" \ | sed -re 's/^(8)(7)(5)(2)$/\1\\ \2:\3:\4/' \ | xargs date +@%s -d \ | xargs date -u +%Y%m%d%H%M%S -d 20150903161734 

The first invocation of date above converts local time (according to $TZ ) into seconds since the epoch; the second then converts epoch-seconds into UTC date and time.

Источник

Converting UTC Date and Time to Local Time in Linux

When managing a Linux system, you may frequently come across timestamps recorded in Coordinated Universal Time (UTC). Understanding and converting these timestamps to your local time can be essential, particularly when troubleshooting system events or running time-specific commands.

This article will provide you a comprehensive guide to converting UTC date and time to local time in Linux, accompanied by practical examples.

Understanding Timezones in Linux

In Linux, timezones are typically defined in the /etc/localtime file. This file is often a symbolic link to a file located in the /usr/share/zoneinfo directory that corresponds to your timezone.

You can view the timezone set on your Linux system by running date in the terminal. The output will display the current time along with the timezone.

Converting UTC to Local Time

The basic command to convert a UTC date to local time in Linux is `date -d`, followed by the UTC date and time string.

Here’s how it works. Suppose you have the following UTC time: 2023-06-05 12:00:00 UTC. To convert this to your local time, you can use the following command:

date -d '2023-06-05 12:00:00 UTC' 

This will output the date and time in the local timezone set on your system. Note that the input format must be as shown: YYYY-MM-DD HH:MM:SS.

Читайте также:  Displayport to hdmi linux

Converting UTC Date and Time to Local Time in Linux

Setting Your Timezone

If you find that your system’s timezone is not correctly set, you can change it. Here’s how you can do this:

  1. Check available timezones by listing the contents of the /usr/share/zoneinfo directory. You can do this with the command `ls /usr/share/zoneinfo`.
  2. Once you’ve found the correct timezone file, create a symbolic link to /etc/localtime using the `ln -sf` command. Here’s an example of how to set the timezone to Pacific Time:
ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime 

Now, when you run the `date -d` command with a UTC time, it will convert to Pacific Time.

Converting UTC Date and Time to Local Time in Linux

Practical Examples

Let’s put this into practice with some examples.

date -d '2023-06-05 12:00:00 UTC' 
ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime date -d '2023-06-05 12:00:00 UTC' 
TZ="Asia/Kolkata" date -d '2023-06-05 12:00:00 UTC' 
date -d '2023-06-05 12:00:00 UTC' '+%A, %B %d, %Y, %H:%M:%S' 

Источник

date Command in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

When we work in the Linux command-line or write shell scripts, we often need to handle date and time values, such as outputting a date in our required formats, getting a relative date-time, or changing the system date and time.

In this tutorial, we’ll take a closer look at the date utility and learn its common usages.

2. Introduction to the date Command

The date command is preinstalled by default in all Linux distros since it’s a part of the GNU coreutils package.

The syntax of using the date command is:

date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

If we don’t give any options, the date command will print the current system date and time including the day of the week, month, time, year and timezone:

$ date Sat 21 Mar 2020 08:51:23 PM CET

However, if we pass different options, the date command can do more than that.

Some common usages of the date command are:

    • display the current date and time from a given timezone
    • display the date and time in a required format
    • display the date and time from a given string
    • get a relative date and time
    • set the system date and time

    Let’s go through some examples to learn how to handle each of these.

    3. Override the Timezone

    3.1. Display Date and Time in UTC

    The date command will output the date and time in the timezone defined in our system.

    UTC is a special timezone. The date command supports the option -u to display the date and time in UTC:

    $ date -u Sat 21 Mar 2020 08:51:03 PM UTC

    3.2. Display Date and Time in Another Timezone

    The date command will by default use the timezone defined by /etc/localtime. The /etc/localtime file is usually a symbolic link, pointing to a timezone file:

    $ readlink /etc/localtime /usr/share/zoneinfo/Europe/Berlin

    Sometimes, we want to know the date and time in another timezone. We can set the environment variable TZ to override the timezone setting for the date command:

    $ date Sat 21 Mar 2020 10:07:37 PM CET $ TZ="EST" date Sat 21 Mar 2020 04:07:42 PM EST

    We can assign the TZ variable with a timezone abbreviation, such as the EST in the example above, or a name in timezone database:

    $ TZ="Asia/Tokyo" date Sun 22 Mar 2020 06:07:55 AM JST

    The changed environment variable TZ is only for the following date command, the system timezone retains the original value:

    $ date Sat 21 Mar 2020 10:09:10 PM CET

    4. Display Date and Time in Various Formats

    4.1. Display Date and Time in Supported Formats

    The date command supports many display formatting options. We can pass a required format to date:

    Let’s see a couple of examples of changing the date format:

    $ date '+Current Date: %F%nCurrent Time: %H:%M:%S' Current Date: 2020-03-21 Current Time: 22:23:03 $ date '+Today is %A, the %wth day in the week %W.%nUnix timestamp for now: %s' Today is Saturday, the 6th day in the week 11. Unix timestamp for now: 1584826306

    Some commonly used formats are:

    • %A – locale’s full weekday name (for example, Sunday)
    • %D – date; same as %m/%d/%y
    • %F – full date; like %+4Y-%m-%d
    • %s – seconds since 1970-01-01 00:00:00 UTC
    • %y – last two digits of the year (00..99)
    • %Y – year (for example, 2020)
    • %Z – alphabetic time zone abbreviation (for example, EST)
    • %N – nanoseconds

    We can find the full list of supported format sequences in the man page or the output of date –help.

    4.2. Display Date and Time in Milliseconds

    By default, the date command doesn’t support displaying a date and time in milliseconds. However, the date command supports the nanoseconds format %N. For example, we can get the nanoseconds of the current time:

    If we round the nanoseconds to ​the first three digits, we’ll have the milliseconds:

    We can combine the “%3N” format with other formats to display a date and time with milliseconds.

    Let’s get the milliseconds since 1970-01-01 00:00:00 UTC:

    We can also display the current date and time with milliseconds:

    $ date +"%F %T.%3N" 2020-09-30 22:07:30.450

    5. Display Date From a Given String

    We can pass a static date or time string value together with the -d option to the date command and let it parse and print the date and time in our required format.

    For example, we can pass a date string to indicate 18 Nov 1976 18:18:18 to the date command in several ways:

    $ date -d'1976-11-18 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'11/18/1976 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'18 Nov 1976 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'19761118T18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'@217185498' Thu 18 Nov 1976 06:18:18 PM CET

    From the examples above, we see that the date string pattern supported by the date command is pretty flexible. It can be a Unix timestamp following a @, an ISO date format, or even a human-readable date string.

    6. Get Relative Date and Time

    In the previous section, we’ve seen how flexible the date string for the -d option can be. In this section, we’ll continue using -d’dateString’ to let the date command calculate a relative date and time in the past or future.

    In this section, all examples of relative date calculation will be based on the current date and time:

    $ date Sun 22 Mar 2020 11:27:41 PM CET

    6.1. yesterday and tomorrow

    Let’s start with getting one day before and after the current date. It is pretty straightforward, we just pass yesterday and tomorrow with the -d option:

    $ date -d'yesterday' Sat 21 Mar 2020 11:30:32 PM CET $ date -d'tomorrow' Mon 23 Mar 2020 11:30:37 PM CET

    6.2. next, ago and last

    Passing yesterday or tomorrow with the -d option is helpful for us to get one single day before and after the base date.

    But what if we want to get an arbitrary number of days before and after? Or even in other units, such as weeks, months, years and so on?

    The next, ago, and last expressions can help us with that:

    $ date -d'3 days ago' Thu 19 Mar 2020 11:40:14 PM CET $ date -d'next 2 month' Thu 23 May 2020 12:40:23 AM CET $ date -d'last week' Sun 15 Mar 2020 11:40:29 PM CET $ date -d'next wed' Wed 25 Mar 2020 12:00:00 AM CET $ date -d'last Sunday' Sun 15 Mar 2020 12:00:00 AM CET

    6.3. +x and -x

    In addition to those human-readable date strings such as next and last, we can also pass a number with a unit to the -d option. A positive number will get a date-time in the future, while, a negative number indicates a date-time in the past:

    $ date -d'+7 day' Mon 30 Mar 2020 12:54:43 AM CET $ date -d'-3 year' Wed 22 Mar 2017 11:54:51 PM CET $ date -d'10 week' Mon 01 Jun 2020 12:55:08 AM CET

    This way makes it easy to calculate relative date-time programmatically, for example, in a shell script.

    6.4. Get Relative Date and Time Based on a Given Date

    So far, we’ve seen some examples of getting a relative date and time based on the current date-time. They’ll work for a given date string as well.

    Let’s see some examples of getting relative date-time based on 1976-11-18:

    $ date -d'19761118 -3 days' Mon 15 Nov 1976 12:00:00 AM CET $ date -d'19761118 last year' Tue 18 Nov 1975 12:00:00 AM CET $ date -d'19761118 tomorrow' Fri 19 Nov 1976 12:00:00 AM CET $ date -d'19761118 2 month ago' Sat 18 Sep 1976 12:00:00 AM CET

    7. Change the System Time

    We can use the date command with the -s option to change the system clock manually. Since changing the system time will affect current running programs, only the root user is allowed to do that change.

    For example, the next command will set our system time to 1976-11-18 18:18:18 :

    root# date -s "1976-11-18 18:18:18"

    8. Conclusion

    The date command is a very convenient utility to handle date and time values.

    In this article, we’ve focused on and explored its common, practical usages.

    Источник

Оцените статью
Adblock
detector