Python time conversion implementation code between various languages, python code

Source: Internet
Author: User

Python time conversion implementation code between various languages, python code

Basic knowledge

Millisecond
Microsecond microseconds
Nanosecond nanoseconds
1 second = 1000 milliseconds 1 millisecond = 1000 microseconds 1 microsecond = 1000 nanoseconds

2 perl

In perl, time or localtime can be used to obtain the time. time returns the number of seconds from, January 1, January 1, 1970. localtime returns the string representation of the current time, or tuple representation of the year, month, and day.

#!/usr/bin/perluse strict;use warnings;use POSIX qw(strftime);# seconds from 1970.01.01 00:00:00my $ti = time();print $ti;print "\n";print strftime("%Y-%m-%d %H:%M:%S\n", localtime($ti));#1310623469#2011-07-14 14:03:58my $t = localtime();print $t;print "\n";#Thu Jul 14 12:25:16 2011my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime();print $year;print "\n";#111print strftime("%Y-%m-%d %H:%M:%S\n", localtime());#2011-07-14 12:26:01

C #
1 tick= 100 nanosecond

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyTest{  class Program  {    static void DateTimeTest()    {      DateTime dt2 = DateTime.Now;      Console.WriteLine(dt2.Ticks);      Console.WriteLine(dt2.ToString("MM/dd/yyyy hh:mm:ss"));    }    static DateTime? ConvertPerlTimeToDateTime(string perltime)    {      DateTime? dt = null;      //perl time variable : seconds from 1970.01.01 00:00:00       string sdt = perltime;      long ldt = 0;      if (long.TryParse(sdt, out ldt))      {        long ldt2 = new DateTime(1970, 1, 1).Ticks + ldt * 1000 * 1000 * 10;         dt = new DateTime(ldt2, DateTimeKind.Local);        Console.WriteLine(dt.Value.ToString("MM/dd/yyyy hh:mm:ss"));      }      return dt;    }    static void Main(string[] args)    {      DateTimeTest();      ConvertPerlTimeToDateTime("1309423883");      //634462479788396720      //07/14/2011 01:46:18      //06/30/2011 08:51:23    }  }}

4. python

Similar to perl in python, time is also the number of seconds since January 1, January 1, 1970.

import timeISOTIMEFORMAT='%Y-%m-%d %X'# seconds from 1970.01.01 00:00:00t = time.time()print (t)print time.strftime(ISOTIMEFORMAT,time.localtime(t))#1310623143.12#2011-07-14 13:59:03(year,mon,day,hour,min,sec,wday,yday,isdst) = time.localtime()print (year)print (time.strftime(ISOTIMEFORMAT, time.localtime()))#2011#2011-07-14 13:59:03

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.