A small program of UNIX timestamp conversions

Source: Internet
Author: User

Long-term viewing of monitoring data, the timestamp format of the monitoring data is the standard UNIX timestamp, to see the effort, so he wrote a small program

The code is as follows

#!/bin/env python#coding:utf8#unix timestamp Conversion Import sys, time, os, redef com_judge ():      '     @ to determine if the executing program is followed by parameters      ' '      com_count = len (SYS.ARGV)     if com_count == 2:         content = sys.argv[1]         return content    else:        output  =  "Usage->> \n\tpython %s 1418239565\n\tpython %s timestamp_filename "        print output %  (Sys.argv[0],sys.argv[0])          exit (1) def dispose (content):    if  Os.path.isfile (content):         with open (content)  as  Timefile:            line_start=1             for read in timefile:                 read = read.strip ()                                    #判断文件中的时间戳格式是否正确                  if re.match (r "\d{10,12}",  Read):                     timestamp =int (Read)                                            #利用python  time  modules for timestamp conversion                      timeArray =  Time.localtime (TimeStamp)                      otherstyletime = time.strftime ("%Y-%m-%d %H:%M:%S",  Timearray)                      print read +  " : " + otherstyletime                     line_ start += 1                 else:                     print&nbsP; " line  " + repr (line_start)  + "  ERROR:  " + read                     exit (1)      else:        input_time =  content                # Determine if the input timestamp format is correct         if re.match (r "\d{10,12}",  input_time):             timestamp =int (input_time)              timearray = time.localtime ( TimeStamp)             otherStyleTime =  Time.strftime ("%y-%m-%d %h:%m:%s",  timearray)              print input_time +  " : " + otherstyletime        else :            print  "UNIX TimeStamp  error :  " + input_time             exit (1)  def main ():     user_input = com_judge ()      dispose (user_input) if __name__ ==  ' __main__ ':     main ()

execution, which can be executed directly with the timestamp

You can also put a lump of time stamp into a file

OK, done, I was to put this program directly under the/usr/bin/, gave permission to use more convenient

A small program of UNIX timestamp conversions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.