InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Converting Dates and Times to Epoch Seconds in bash
Bash

Converting Dates and Times to Epoch Seconds in bash

InfinityCoder February 20, 2017

You want to convert a date and time to Epoch seconds to make it easier to do date and time arithmetic.

Use the GNU date command with the nonstandard -d option and a standard %s format:

1
2
3
4
5
6
# "Now" is easy
$ date '+%s'
1131172934
# Some other time needs the non-standard -d
$ date -d '2005-11-05 12:00:00 +0000' '+%s'
1131192000

If you do not have the GNU date command available, this is a harder problem to solve.

Our advice is to obtain and use the GNU date command if at all possible.

If that is not possible you might be able to use Perl.

Here are three ways to print the time right now in Epoch seconds:

1
2
3
4
5
6
7
8
9
$ perl -e 'print time, qq(\n);'
1154158997
 
# Same as above
$ perl -e 'use Time::Local; print timelocal(localtime( )) . qq(\n);'
1154158997
 
$ perl -e 'use POSIX qw(strftime); print strftime("%s", localtime( )) . qq(\n);'
1154159097

Using Perl to convert a specific day and time instead of right now is even harder due to Perl’s date/time data structure.

Years start at 1900 and months (but not days) start at zero instead of one.

The format of the command is: timelocal(sec, min, hour, day, month-1, year-1900).

So to convert 2005-11-05 06:59:49 to Epoch seconds:

1
2
3
4
5
6
7
8
9
# The given time is in local time
$ perl -e 'use Time::Local; print timelocal("49", "59", "06", "05", "10", "105") .
qq(\n);'
1131191989
 
# The given time is in UTC time
$ perl -e 'use Time::Local; print timegm("49", "59", "06", "05", "10", "105") . qq(\
n);'
1131173989

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Setting a POSIX $PATH in bash
You are on a machine that provides older or proprietary …

Setting a POSIX $PATH in bash

Using chroot Jails in bash
You have to use a script or application that you …

Using chroot Jails in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap