What date format, and what timestamp format?
Have you tried using Date::Manip ?
Main Topics
Browse All TopicsHi,
i would like to convert a date format into unix timestamp.
I searched the whole web but did not find anythin yet.
Any sollutions??
tnahx a lot
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
For conversion of a date format into UNIX timestamp in your Perl program, you can use the mktime() function. However, you must include "use POSIX;" at the top of your script.
Many of the parameters that mktime takes are relative(e.g. month is zero relative, year is 1900 relative) and so I would advise you to check out the unix man page for mktime. Attaching info from the man page for mktime too for your reference.
The Perl code snippet I used to test it out is appended below..
#!/usr/local/bin/perl
use POSIX;
#replace the parameters with your own values..
$sec = 1;
$min = 0;
$hour = 0;
$mday = 14;
$mon = 3 - 1;
$year = 2001 - 1900;
$wday = 3;
$timestamp = mktime($sec,$min,$hour,$md
print ($timestamp);
print("\n");
# just to get the current timestamp.
print(time());
##########################
NAME
mktime - converts a tm structure to a calendar time
SYNOPSIS
#include <time.h>
time_t mktime(struct tm *timeptr);
DESCRIPTION
The mktime() function converts the time represented by the
tm structure pointed to by timeptr into a calendar time
(the number of seconds since 00:00:00 UTC, January 1, 1970).
The tm structure contains the following members:
int tm_sec; /* seconds after the minute [0, 61] */
int tm_min; /* minutes after the hour [0, 59] */
int tm_hour; /* hour since midnight [0, 23] */
int tm_mday; /* day of the month [1, 31] */
int tm_mon; /* months since January [0, 11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday [0, 6] */
int tm_yday; /* days since January 1 [0, 365] */
int tm_isdst; /* flag for daylight savings time */
In addition to computing the calendar time, mktime() normal-
izes the supplied tm structure. The original values of the
tm_wday and tm_yday components of the structure are ignored,
and the original values of the other components are not res-
tricted to the ranges indicated in the definition of the
structure. On successful completion, the values of the
tm_wday and tm_yday components are set appropriately, and
the other components are set to represent the specified
calendar time, but with their values forced to be within the
appropriate ranges. The final value of tm_mday is not set
until tm_mon and tm_year are determined.
The tm_year member must be for year 1901 or later. Calendar
times before 20:45:52 UTC, December 31, 1901 or after
03:14:07 UTC, January 19, 2038 cannot be represented. Port-
able applications should not try to create dates before
00:00:00 UTC, January 1, 1970 or after 00:00:00 UTC, January
1, 2038.
The original values of the components may be either greater
than or less than the specified range. For example, a
tm_hour of -1 means 1 hour before midnight, tm_mday of 0
means the day preceding the current month, and tm_mon of -2
means 2 months before January of tm_year.
If tm_isdst is positive, the original values are assumed to
be in the alternate timezone. If it turns out that the
SunOS 5.7 Last change: 17 Sep 1997 1
C Library Functions mktime(3C)
alternate timezone is not valid for the computed calendar
time, then the components are adjusted to the main timezone.
Likewise, if tm_isdst is zero, the original values are
assumed to be in the main timezone and are converted to the
alternate timezone if the main timezone is not valid. If
tm_isdst is negative, mktime() attempts to determine whether
the alternate timezone is in effect for the specified time.
Local timezone information is used as if mktime() had called
tzset(). See ctime(3C).
RETURN VALUES
The mktime() function returns the specified calendar time.
If the calendar time cannot be represented, the function
returns the value (time_t)-1.
USAGE
The mktime() function is MT-Safe in multithreaded applica-
tions, as long as no user-defined function directly modifies
one of the following variables: timezone, altzone, daylight,
and tzname. See ctime(3C).
EXAMPLES
Example 1: Sample code using mktime().
What day of the week is July 4, 2001?
#include <stdio.h>
#include <time.h>
static char *const wday[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "-unknown-"
};
struct tm time_str;
/*...*/
time_str.tm_year = 2001 - 1900;
time_str.tm_mon = 7 - 1;
time_str.tm_mday = 4;
time_str.tm_hour = 0;
time_str.tm_min = 0;
time_str.tm_sec = 1;
time_str.tm_isdst = -1;
if (mktime(&time_str)== -1)
time_str.tm_wday=7;
printf("%s\n", wday[time_str.tm_wday]);
Hi, your problem is simple.
And the complete solution to it is provided here,
with source code in perl and shell script to run it.
The code is:
a) vastly documented, for your ease of understanding.
b) thoroughly tested in Solaris 5.7, should run on any unix m/c
How to Info:
~~~~~~~~~~~~
How to run the scripts:
There are two attachments:
RunScript.sh
&
DateToTimestamp.pl
KEEP BOTH OF THEM IN THE SAME DIRECTORY
The first attachment "RunScript.sh" is a shell script.
It executes the "date" command
(which has your date format)
and
passes its output to DateToTimestamp.pl.
(which generates the Unix timestamp and prints it)
change its mode to executable
(by the command):
$ chmod 755 RunScript.sh
Then just run this script
(by the command):
$ RunScript.sh
And you get what you want.
REFERENCES
~~~~~~~~~~
1. Manuals
$ man date (for the manual on unix date format)
$ man mktime (for the manual on mktime method I used in .pl)
2. The POSIX library.
##########################
DateToTimestamp.pl
#!/usr/local/bin/perl
#
# author: nabarun of www.arzoo.com
#
use POSIX;
my (
$wday,
$year,
$month,
$date,
$time,
$zone,
@wdays,
@months,
);
#
# Days in a week, as appears in the output of unix shell command "date"
#
@wdays = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
#
# Months in a year, as appears in the output of unix shell command "date"
#
@months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
#
# the date commands output is passed as the input here (by the RunScript.sh)
#
$inputDate = <STDIN>;
#
# storing the different words in the output
#
@words = split (/\s+/, $inputDate);
$wday = $words[0];
$month = $words[1];
$date = $words[2];
$time = $words[3];
$zone = $words[4];
$year = $words[5];
print "\ndate commands output:\n"
. "~~~~~~~~~~~~~~~~~~~~~\n\t
for($i = 0; $i <= $#wdays; $i++) {
if ("$wday" eq "$wdays[$i]") {
$wday = $i;
# print "$wday = $wdays[$i]\n";
last;
}
}
for($i = 0; $i <= $#months; $i++) {
if ("$month" eq "$months[$i]") {
$month = $i;
# print "$month = $months[$i]\n";
last;
}
}
@time_components = split (/\:/, $time);
$hour = $time_components[0];
$min = $time_components[1];
$sec = $time_components[2];
$year = $year - 1900;
print "arguments to the mktime() method:\n"
. "~~~~~~~~~~~~~~~~~~~~~~~~~
print "sec = $sec, min = $min, hour = $hour,\ndate = $date, month = $month, "
. "year (this yr. - 1900) = $year, day = $wday \n\n";
$timestamp = mktime($sec, $min, $hour, $date, $month, $year, $wday, 0, -1);
print "*************************
print "CALCULATED TIMESTAMP = ";
print "$timestamp \n";
print "*************************
print "PRESENT TIMESTAMP = ";
print (time());
print "\n";
print "*************************
##########################
RunScript.sh
#!/bin/sh
#
# @ nabarun of www.arzoo.com
#
#
# clear the screen
#
clear;
#
# run the unix command date and pass its input to the output of
# the perl script DateToTimeStamp.pl.
#
date | perl DateToTimestamp.pl;
Business Accounts
Answer for Membership
by: maneshrPosted on 2001-03-13 at 07:18:40ID: 5924812
kalosi,
"..i would like to convert a date format into unix timestamp..."
What is the source of the date format???
Can you post a sample of the date format you have and the output that you want ???
Please explain with more detail your exact requirements.
Also post any sample code that you have already created.
This will help you get a more accurate answer, faster.