时间转换

来自刘东华的维基
跳转至: 导航搜索

概述

工作学习过程中经常遇到不同开发语言或脚本需要转换时间,整理出来便于查阅。

Shell

awk

strftime("%Y-%m-%d %H:%M:%S" [,timestamp]);
 
strftime is guaranteed by the 1999 ISO C standard(37) to support the following date format specifications:
 
%a
The locale's abbreviated weekday name.
%A
The locale's full weekday name.
%b
The locale's abbreviated month name.
%B
The locale's full month name.
%c
The locale's "appropriate" date and time representation. (This is `%A %B %d %T %Y' in the "C" locale.)
%C
The century. This is the year divided by 100 and truncated to the next lower integer.
%d
The day of the month as a decimal number (01--31).
%D
Equivalent to specifying `%m/%d/%y'.
%e
The day of the month, padded with a space if it is only one digit.
%F
Equivalent to specifying `%Y-%m-%d'. This is the ISO 8601 date format.
%g
The year modulo 100 of the ISO week number, as a decimal number (00--99). 
For example, January 1, 1993 is in week 53 of 1992. Thus, the year of its 
ISO week number is 1992, even though its year is 1993.
 Similarly, December 31, 1973 is in week 1 of 1974. Thus, the year of its 
ISO week number is 1974, even though its year is 1973.
%G
The full year of the ISO week number, as a decimal number.
%h
Equivalent to `%b'.
%H
The hour (24-hour clock) as a decimal number (00--23).
%I
The hour (12-hour clock) as a decimal number (01--12).
%j
The day of the year as a decimal number (001--366).
%m
The month as a decimal number (01--12).
%M
The minute as a decimal number (00--59).
%n
A newline character (ASCII LF).
%p
The locale's equivalent of the AM/PM designations associated with a 12-hour clock.
%r
The locale's 12-hour clock time. (This is `%I:%M:%S %p' in the "C" locale.)
%R
Equivalent to specifying `%H:%M'.
%S
The second as a decimal number (00--60).
%t
A TAB character.
%T
Equivalent to specifying `%H:%M:%S'.
%u
The weekday as a decimal number (1--7). Monday is day one.
%U
The week number of the year (the first Sunday as the first day of week one) as a decimal number (00--53).
%V
The week number of the year (the first Monday as the first day of week one) as a decimal number (01--53). 
The method for determining the week number is as specified by ISO 8601. (To wit: if the week containing 
January 1 has four or more days in the new year, then it is week one; otherwise it is week 53 of the 
previous year and the next week is week one.)
%w
The weekday as a decimal number (0--6). Sunday is day zero.
%W
The week number of the year (the first Monday as the first day of week one) as a decimal number (00--53).
%x
The locale's "appropriate" date representation. (This is `%A %B %d %Y' in the "C" locale.)
%X
The locale's "appropriate" time representation. (This is `%T' in the "C" locale.)
%y
The year modulo 100 as a decimal number (00--99).
%Y
The full year as a decimal number (e.g., 1995).
%z
The timezone offset in a +HHMM format (e.g., the format necessary to produce RFC 822/RFC 1036 date headers).
%Z
The time zone name or abbreviation; no characters if no time zone is determinable.
%Ec %EC %Ex %EX %Ey %EY %Od %Oe %OH
%OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy
"Alternate representations" for the specifications that use only the second letter (`%c', `%C', and so on).(38) (These facilitate compliance with the POSIX date utility.)
%%
A literal `%'.

Python

import datetime
 
print(
    datetime.datetime.fromtimestamp(
        int("1284101485")
    ).strftime('%Y-%m-%d %H:%M:%S')
)

C/C++